Advertisement
Guest User

Untitled

a guest
Mar 8th, 2020
178
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.78 KB | None | 0 0
  1.  
  2. void Manager::CompanyData::update_goals() {
  3.     if (this->is_processing)
  4.         return;
  5.     auto self(shared_from_this());
  6.     if (this->new_goals.size() > this->goals.size()) {
  7.         std::string new_text = this->new_goals[this->goals.size()];
  8.         this->is_processing = goal::Create(this->company, new_text)
  9.             .with_callback([self, new_text] (bool res) {
  10.                 self->is_processing = false;
  11.                 if (res) {
  12.                     self->goals.push_back(std::make_shared<GoalData>(_new_goal_id, new_text));
  13.                     self->update_goals();
  14.                 } else {
  15.                     ConsoleError("Goal creation failed (company_id={}, text={})", GetOwner(self->company), new_text);
  16.                 }
  17.             })
  18.             .queue();
  19.         return;
  20.     }
  21.  
  22.     std::sort(
  23.         this->goals.begin(), this->goals.end(),
  24.         [](const auto a, const auto b) -> bool {
  25.              return a->id < b->id;
  26.         }
  27.     );
  28.  
  29.     Commands commands;
  30.     for (size_t i = 0; i < this->goals.size(); i++) {
  31.         auto goal = this->goals[i];
  32.         if (i < this->new_goals.size()) {
  33.             auto new_text = this->new_goals[i];
  34.             if (new_text == goal->text)
  35.                 continue;
  36.             commands.push_back(
  37.                 cmd::goal::SetText(goal->id, new_text)
  38.                 .with_callback([goal, new_text] (bool res) {
  39.                     if (res) {
  40.                         goal->text = new_text;
  41.                     } else {
  42.                         ConsoleError("Goal text change failed (goal_id={}, new_text='{}')",
  43.                               goal->id, new_text);
  44.                     }
  45.                 })
  46.             );
  47.         } else {
  48.             commands.push_back(
  49.                 cmd::goal::Remove(goal->id)
  50.                 .with_callback([self, goal_id=goal->id] (bool res) {
  51.                     if (res) {
  52.                         self->goals.erase(
  53.                             std::remove_if(
  54.                                 self->goals.begin(), self->goals.end(),
  55.                                 [goal_id] (const auto goal) -> bool {
  56.                                     return goal->id == goal_id;
  57.                                 }
  58.                             ),
  59.                             self->goals.end()
  60.                         );
  61.                     } else {
  62.                         ConsoleError("Goal removal failed (goal_id={})", goal_id);
  63.                     }
  64.                 })
  65.             );
  66.         }
  67.     }
  68.  
  69.     if (!commands.empty()) {
  70.         this->is_processing = cmd::Queue(commands, [self] (bool res) {
  71.             self->is_processing = false;
  72.             if (res)
  73.                 self->update_goals();
  74.         });
  75.     }
  76. }
  77.  
  78.  
  79. void Manager::CompanyData::remake_goals() {
  80.     if (this->is_processing || this->new_goals.empty())
  81.         return;
  82.  
  83.     auto self(shared_from_this());
  84.     Commands commands;
  85.     for (auto goal: this->goals) {
  86.         commands.push_back(
  87.             cmd::goal::Remove(goal->id)
  88.             .with_callback([self, goal_id=goal->id] (bool res) {
  89.                 if (res) {
  90.                     self->goals.erase(
  91.                         std::remove_if(
  92.                             self->goals.begin(), self->goals.end(),
  93.                             [goal_id] (const auto goal) -> bool {
  94.                                 return goal->id == goal_id;
  95.                             }
  96.                         ),
  97.                         self->goals.end()
  98.                     );
  99.                 } else {
  100.                     ConsoleError("Goal removal failed (goal_id={})", goal_id);
  101.                 }
  102.             })
  103.         );
  104.     }
  105.  
  106.     for (auto &text: this->new_goals) {
  107.         commands.push_back(
  108.             goal::Create(this->company, text)
  109.             .with_callback([self, text] (bool res) {
  110.                 if (res) {
  111.                     self->goals.push_back(std::make_shared<GoalData>(_new_goal_id, text));
  112.                 } else {
  113.                     ConsoleError("Goal creation failed (company_id={} text={})", GetOwner(self->company), text);
  114.                 }
  115.             })
  116.         );
  117.     }
  118.  
  119.     if (!commands.empty()) {
  120.         this->new_goals.clear();
  121.         this->is_processing = cmd::Queue(commands, [self] (bool res) {
  122.             self->is_processing = false;
  123.             if (res)
  124.                 self->remake_goals();
  125.         });
  126.     }
  127. }
  128.  
  129. void Manager::set_goals(const Company *company, const std::vector<std::string> &goals) {
  130.     auto it = this->goals.find(company);
  131.     if (it == this->goals.end()) {
  132.         it = this->goals.insert(it, std::make_pair(company, std::make_shared<Manager::CompanyData>()));
  133.         it->second->company = company;
  134.     }
  135.     it->second->new_goals = goals;
  136.     it->second->update_goals();
  137. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement