Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- void Manager::CompanyData::update_goals() {
- if (this->is_processing)
- return;
- auto self(shared_from_this());
- if (this->new_goals.size() > this->goals.size()) {
- std::string new_text = this->new_goals[this->goals.size()];
- this->is_processing = goal::Create(this->company, new_text)
- .with_callback([self, new_text] (bool res) {
- self->is_processing = false;
- if (res) {
- self->goals.push_back(std::make_shared<GoalData>(_new_goal_id, new_text));
- self->update_goals();
- } else {
- ConsoleError("Goal creation failed (company_id={}, text={})", GetOwner(self->company), new_text);
- }
- })
- .queue();
- return;
- }
- std::sort(
- this->goals.begin(), this->goals.end(),
- [](const auto a, const auto b) -> bool {
- return a->id < b->id;
- }
- );
- Commands commands;
- for (size_t i = 0; i < this->goals.size(); i++) {
- auto goal = this->goals[i];
- if (i < this->new_goals.size()) {
- auto new_text = this->new_goals[i];
- if (new_text == goal->text)
- continue;
- commands.push_back(
- cmd::goal::SetText(goal->id, new_text)
- .with_callback([goal, new_text] (bool res) {
- if (res) {
- goal->text = new_text;
- } else {
- ConsoleError("Goal text change failed (goal_id={}, new_text='{}')",
- goal->id, new_text);
- }
- })
- );
- } else {
- commands.push_back(
- cmd::goal::Remove(goal->id)
- .with_callback([self, goal_id=goal->id] (bool res) {
- if (res) {
- self->goals.erase(
- std::remove_if(
- self->goals.begin(), self->goals.end(),
- [goal_id] (const auto goal) -> bool {
- return goal->id == goal_id;
- }
- ),
- self->goals.end()
- );
- } else {
- ConsoleError("Goal removal failed (goal_id={})", goal_id);
- }
- })
- );
- }
- }
- if (!commands.empty()) {
- this->is_processing = cmd::Queue(commands, [self] (bool res) {
- self->is_processing = false;
- if (res)
- self->update_goals();
- });
- }
- }
- void Manager::CompanyData::remake_goals() {
- if (this->is_processing || this->new_goals.empty())
- return;
- auto self(shared_from_this());
- Commands commands;
- for (auto goal: this->goals) {
- commands.push_back(
- cmd::goal::Remove(goal->id)
- .with_callback([self, goal_id=goal->id] (bool res) {
- if (res) {
- self->goals.erase(
- std::remove_if(
- self->goals.begin(), self->goals.end(),
- [goal_id] (const auto goal) -> bool {
- return goal->id == goal_id;
- }
- ),
- self->goals.end()
- );
- } else {
- ConsoleError("Goal removal failed (goal_id={})", goal_id);
- }
- })
- );
- }
- for (auto &text: this->new_goals) {
- commands.push_back(
- goal::Create(this->company, text)
- .with_callback([self, text] (bool res) {
- if (res) {
- self->goals.push_back(std::make_shared<GoalData>(_new_goal_id, text));
- } else {
- ConsoleError("Goal creation failed (company_id={} text={})", GetOwner(self->company), text);
- }
- })
- );
- }
- if (!commands.empty()) {
- this->new_goals.clear();
- this->is_processing = cmd::Queue(commands, [self] (bool res) {
- self->is_processing = false;
- if (res)
- self->remake_goals();
- });
- }
- }
- void Manager::set_goals(const Company *company, const std::vector<std::string> &goals) {
- auto it = this->goals.find(company);
- if (it == this->goals.end()) {
- it = this->goals.insert(it, std::make_pair(company, std::make_shared<Manager::CompanyData>()));
- it->second->company = company;
- }
- it->second->new_goals = goals;
- it->second->update_goals();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement