543 lines
20 KiB
C++
543 lines
20 KiB
C++
// SPDX-License-Identifier: GPL-3.0-only
|
|
// Setup and manager selection are drafts. The running presentation source is
|
|
// replaced only after the final affirmative confirmation succeeds.
|
|
bool App::entry_used_club(const std::string &id) const {
|
|
return std::any_of(managers_draft_.begin(), managers_draft_.end(),
|
|
[&](const auto &m) { return m.value("club", "") == id; });
|
|
}
|
|
bool App::entry_used_portrait(int id) const {
|
|
return std::any_of(
|
|
managers_draft_.begin(), managers_draft_.end(),
|
|
[&](const auto &m) { return m.value("portrait", 0) == id; });
|
|
}
|
|
bool App::entry_name_valid() const {
|
|
const auto i = selections_.find("manager-name");
|
|
return i != selections_.end() &&
|
|
normalized_manager_name(i->second).has_value();
|
|
}
|
|
void App::entry_sync_club() {
|
|
const auto catalog = entry_world();
|
|
if (!catalog)
|
|
return;
|
|
const auto leagues = catalog->leagues(country_);
|
|
if (std::none_of(leagues.begin(), leagues.end(),
|
|
[&](auto *l) { return l->value("id", "") == league_; }))
|
|
league_ = leagues.empty() ? "" : leagues.front()->value("id", "");
|
|
const auto *current = catalog->find("clubs", selected_club_);
|
|
if (current && current->value("competition", "") == league_ &&
|
|
catalog->manageable(selected_club_) && !entry_used_club(selected_club_))
|
|
return;
|
|
selected_club_.clear();
|
|
for (const auto *c : catalog->clubs(league_)) {
|
|
const auto id = c->value("id", "");
|
|
if (catalog->manageable(id) && !entry_used_club(id)) {
|
|
selected_club_ = id;
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
void App::entry_choose_defaults() {
|
|
const auto catalog = entry_world();
|
|
selections_["manager-name"] = std::array<const char *, 4>{
|
|
"Alex Berger", "Chris Winter", "Robin Meier",
|
|
"Jamie Sommer"}[size_t(std::min(manager_index_, 3))];
|
|
for (int p = 0; p < 12; ++p)
|
|
if (!entry_used_portrait(p)) {
|
|
selections_["portrait"] = std::to_string(p);
|
|
break;
|
|
}
|
|
// Prefer the selected league, then the first available German club.
|
|
entry_sync_club();
|
|
if (selected_club_.empty() && catalog)
|
|
for (auto *c : catalog->clubs()) {
|
|
const auto id = c->value("id", "");
|
|
if (catalog->manageable(id) && !entry_used_club(id)) {
|
|
selected_club_ = id;
|
|
country_ = "nation:de";
|
|
league_ = c->value("competition", "");
|
|
break;
|
|
}
|
|
}
|
|
manager_picker_ = manager_confirm_ = manager_success_ = false;
|
|
club_search_.clear();
|
|
}
|
|
void App::entry_begin() {
|
|
auto next = world_;
|
|
if (auto *m = mod(active_mod_); m && m->type == "Modern")
|
|
next = Catalog::open(m->file);
|
|
if (!next) {
|
|
message("Bitte eine Modern-Mod aktivieren.");
|
|
return;
|
|
}
|
|
const auto clubs = next->clubs();
|
|
if (std::none_of(clubs.begin(), clubs.end(), [&](auto *c) {
|
|
return next->manageable(c->value("id", ""));
|
|
})) {
|
|
message("Diese Mod enthält keinen spielbaren deutschen Verein.");
|
|
return;
|
|
}
|
|
entry_return_scene_ =
|
|
scene_ == "desktop" || (scene_ == "manager" && adding_manager_)
|
|
? "desktop"
|
|
: "main";
|
|
entry_catalog_ = std::move(next);
|
|
bind_world(entry_catalog_);
|
|
start_options_ = {{"duration", 5}, {"difficulty", 3}, {"players", 1},
|
|
{"mode", 3}, {"league", true}, {"world", true}};
|
|
if(source_ && world_ && entry_catalog_->manifest().at("id")==world_->manifest().at("id"))start_options_=snap().state.at("edits").at("setup");
|
|
start_options_["mode"]=3;
|
|
adding_manager_ = false;
|
|
managers_draft_ = Json::array();
|
|
manager_index_ = 0;
|
|
manager_picker_ = manager_confirm_ = manager_success_ = false;
|
|
country_ = "nation:de";
|
|
league_.clear();
|
|
entry_choose_defaults();
|
|
scene_ = "newgame";
|
|
menu_.clear();
|
|
focus_after_render_ = "window-close";
|
|
refresh();
|
|
}
|
|
void App::entry_add() {
|
|
if (!source_)
|
|
return;
|
|
entry_catalog_.reset();
|
|
bind_world(world_);
|
|
entry_source_ = source_.get();
|
|
entry_revision_ = snap().revision;
|
|
managers_draft_ = snap().state.at("managers");
|
|
manager_index_ = int(managers_draft_.size());
|
|
adding_manager_ = true;
|
|
country_ = "nation:de";
|
|
entry_choose_defaults();
|
|
if (entry_full()) {
|
|
const auto &last = managers_draft_.back();
|
|
selections_["manager-name"] = last.at("name").get<std::string>();
|
|
selections_["portrait"] = std::to_string(last.value("portrait", 0));
|
|
selections_["portrait-style"] = last.value("portrait_style", "photo");
|
|
selections_["manager-strength"] =
|
|
std::to_string(last.value("strength_choice", 1));
|
|
selected_club_ = last.at("club").get<std::string>();
|
|
league_ = world_->find("clubs", selected_club_)->value("competition", "");
|
|
}
|
|
scene_ = "manager";
|
|
menu_.clear();
|
|
focus_after_render_ = entry_full() ? "window-close" : "manager-name";
|
|
refresh();
|
|
}
|
|
void App::entry_cancel() {
|
|
entry_catalog_.reset();
|
|
bind_world(world_);
|
|
manager_confirm_ = manager_success_ = manager_picker_ = false;
|
|
club_search_.clear();
|
|
scene_ = adding_manager_ ? "desktop" : entry_return_scene_;
|
|
adding_manager_ = false;
|
|
managers_draft_ = Json::array();
|
|
manager_index_ = 0;
|
|
if (source_) {
|
|
selected_club_ = manager(snap()).value("club", "");
|
|
const auto *c = world_->find("clubs", selected_club_);
|
|
if (c)
|
|
league_ = c->value("competition", "");
|
|
country_ = "nation:de";
|
|
}
|
|
focus_after_render_ =
|
|
scene_ == "desktop" ? "object-app-newplayer" : "front-new";
|
|
refresh();
|
|
}
|
|
void App::entry_confirm() {
|
|
if (!manager_confirm_ || entry_full())
|
|
return;
|
|
const auto name = normalized_manager_name(selections_["manager-name"]);
|
|
const auto catalog = entry_world();
|
|
const int portrait =
|
|
selections_["portrait"].empty() ? 0 : std::stoi(selections_["portrait"]);
|
|
if (!name || !catalog || !catalog->manageable(selected_club_) ||
|
|
entry_used_club(selected_club_) || portrait < 0 || portrait >= 12 ||
|
|
entry_used_portrait(portrait))
|
|
return;
|
|
auto candidate = managers_draft_;
|
|
candidate.push_back(
|
|
{{"name", *name},
|
|
{"club", selected_club_},
|
|
{"portrait", portrait},
|
|
{"portrait_style",
|
|
selections_["portrait-style"] == "painted" ? "painted" : "photo"},
|
|
{"strength_choice", selections_["manager-strength"].empty()
|
|
? 1
|
|
: std::stoi(selections_["manager-strength"])}});
|
|
if (!adding_manager_ &&
|
|
int(candidate.size()) < start_options_.value("players", 1)) {
|
|
managers_draft_ = std::move(candidate);
|
|
manager_index_ = int(managers_draft_.size());
|
|
entry_choose_defaults();
|
|
focus_after_render_ = "manager-name";
|
|
refresh();
|
|
return;
|
|
}
|
|
if (adding_manager_) {
|
|
if (source_.get() != entry_source_) {
|
|
message(
|
|
"Die Partie hat sich geändert. Bitte die Managerauswahl neu öffnen.");
|
|
return;
|
|
}
|
|
const auto added = source_->apply({entry_revision_, "managers", candidate});
|
|
if (!added.ok) {
|
|
message("Der Mitspieler konnte nicht hinzugefügt werden: " + added.error);
|
|
return;
|
|
}
|
|
managers_draft_ = std::move(candidate);
|
|
manager_confirm_ = false;
|
|
manager_success_ = true;
|
|
focus_after_render_ = "manager-success-close";
|
|
} else {
|
|
// Construct and validate before releasing any part of the current session.
|
|
auto demo = std::make_unique<DemoSource>(catalog);
|
|
const auto result =
|
|
demo->apply({demo->snapshot().revision,
|
|
"start",
|
|
{{"setup", start_options_}, {"managers", candidate}}});
|
|
if (!result.ok) {
|
|
message("Die neue Partie konnte nicht übernommen werden: " +
|
|
result.error);
|
|
return;
|
|
}
|
|
opening_write(*catalog, demo->snapshot().state, valid_desktop(desktop_) ? desktop_ : default_desktop());
|
|
publish_world(catalog, std::move(demo));
|
|
saved_desktop_ = Json::object();
|
|
undo_.clear();
|
|
undo_themes_.clear();
|
|
managers_draft_ = std::move(candidate);
|
|
manager_confirm_ = false;
|
|
entry_catalog_.reset();
|
|
scene_ = start_options_.value("league", true) ? "opening" : "desktop";
|
|
opening_due_ = 0;
|
|
opening_io_blocked_ = false;
|
|
focus_after_render_.clear();
|
|
}
|
|
refresh();
|
|
}
|
|
bool App::entry_action(const std::string &action, Rml::Element *target) {
|
|
if ((scene_ != "newgame" && scene_ != "manager") || !popup_.empty() || menu_ == "quit")
|
|
return false;
|
|
const auto id =
|
|
target ? target->GetAttribute<Rml::String>("data-id", "") : std::string();
|
|
if (manager_confirm_) {
|
|
if (action == "manager-confirm-yes")
|
|
entry_confirm();
|
|
else if (action == "manager-confirm-no") {
|
|
manager_confirm_ = false;
|
|
focus_after_render_ = "manager-next";
|
|
refresh();
|
|
}
|
|
return true;
|
|
}
|
|
if (manager_success_) {
|
|
if (action == "manager-success-close" || action == "close")
|
|
entry_cancel();
|
|
return true;
|
|
}
|
|
if (action == "close") {
|
|
entry_cancel();
|
|
return true;
|
|
}
|
|
if (action == "setup-step") {
|
|
start_options_["mode"]=3;
|
|
if(id=="mode")return true;
|
|
const auto step = target->GetAttribute<int>("data-step", 0);
|
|
const int limit = id == "duration" ? 5
|
|
: id == "difficulty" ? 10
|
|
: id == "players"
|
|
? (start_options_.value("league", true) ? 4 : 1)
|
|
: 3;
|
|
if (id == "duration" || id == "difficulty" || id == "players")
|
|
start_options_[id] =
|
|
std::clamp(start_options_.value(id, 1) + step, 1, limit);
|
|
const int current = start_options_.value(id, 1);
|
|
focus_after_render_ = "setup-" + id +
|
|
((current == limit && step > 0) ? "-minus"
|
|
: (current == 1 && step < 0) ? "-plus"
|
|
: step < 0 ? "-minus"
|
|
: "-plus");
|
|
} else if (action == "setup-competition") {
|
|
if (id == "league" || id == "world") {
|
|
auto next = start_options_;
|
|
next[id] = !next.value(id, true);
|
|
if ((next.value("league", true) || next.value("world", true)) &&
|
|
(next.value("league", true) || next.value("players", 1) == 1))
|
|
start_options_ = std::move(next);
|
|
}
|
|
const bool locked = id == "league"
|
|
? start_options_.value("league", true) &&
|
|
(start_options_.value("players", 1) > 1 ||
|
|
!start_options_.value("world", true))
|
|
: !start_options_.value("league", true);
|
|
focus_after_render_ = locked ? "setup-next" : target->GetId();
|
|
} else if (action == "setup-next") {
|
|
adding_manager_ = false;
|
|
managers_draft_ = Json::array();
|
|
manager_index_ = 0;
|
|
entry_choose_defaults();
|
|
scene_ = "manager";
|
|
focus_after_render_ = "manager-name";
|
|
} else if (action == "manager-newgame") {
|
|
entry_begin();
|
|
return true;
|
|
} else if (action == "manager-back") {
|
|
if (adding_manager_)
|
|
entry_cancel();
|
|
else {
|
|
scene_ = "newgame";
|
|
manager_confirm_ = manager_picker_ = false;
|
|
focus_after_render_ = "setup-next";
|
|
refresh();
|
|
}
|
|
return true;
|
|
} else if (action == "manager-clubs") {
|
|
if(career_national_entry()||entry_full())return true;
|
|
selections_["manager-name"] = field("manager-name");
|
|
entry_picker_country_ = country_;
|
|
entry_picker_league_ = league_;
|
|
manager_picker_ = true;
|
|
club_search_.clear();
|
|
focus_after_render_ = "manager-club-search";
|
|
} else if (action == "manager-clubs-back") {
|
|
country_ = entry_picker_country_;
|
|
league_ = entry_picker_league_;
|
|
manager_picker_ = false;
|
|
focus_after_render_ = "manager-clubs";
|
|
} else if (action == "portrait-style") {
|
|
selections_["manager-name"] = field("manager-name");
|
|
if (id == "photo" || id == "painted")
|
|
selections_["portrait-style"] = id;
|
|
focus_after_render_ = target->GetId();
|
|
} else if (action == "portrait") {
|
|
const int portrait = std::stoi(id);
|
|
if (entry_full() || portrait < 0 || portrait >= 12 ||
|
|
entry_used_portrait(portrait))
|
|
return true;
|
|
selections_["manager-name"] = field("manager-name");
|
|
selections_["portrait"] = id;
|
|
focus_after_render_ = "manager-portrait-" + id;
|
|
} else if (action == "manager-club") {
|
|
if (entry_full() || !entry_world()->manageable(id) || entry_used_club(id))
|
|
return true;
|
|
selected_club_ = id;
|
|
country_ = "nation:de";
|
|
league_ = entry_world()->find("clubs", id)->value("competition", league_);
|
|
manager_picker_ = false;
|
|
focus_after_render_ = "manager-clubs";
|
|
} else if (action == "manager-next") {
|
|
if (entry_full())
|
|
return true;
|
|
selections_["manager-name"] = field("manager-name");
|
|
if (!entry_name_valid() || !entry_world()->manageable(selected_club_) ||
|
|
entry_used_club(selected_club_))
|
|
return true;
|
|
selections_["manager-name"] =
|
|
*normalized_manager_name(selections_["manager-name"]);
|
|
manager_confirm_ = true;
|
|
focus_after_render_ = "manager-confirm-no";
|
|
} else
|
|
return false;
|
|
refresh();
|
|
return true;
|
|
}
|
|
bool App::entry_change(Rml::Element *target, bool user_reselected) {
|
|
if (scene_ != "manager" || manager_confirm_ || manager_success_)
|
|
return false;
|
|
const auto kind = target->GetAttribute<Rml::String>("data-change", "");
|
|
if (kind != "manager-country" && kind != "manager-league" &&
|
|
kind != "manager-strength")
|
|
return false;
|
|
if (entry_full())
|
|
return true;
|
|
const auto value =
|
|
dynamic_cast<Rml::ElementFormControl *>(target)->GetValue();
|
|
if (kind == "manager-strength") {
|
|
selections_[kind] = value;
|
|
return true;
|
|
}
|
|
if (!manager_picker_)
|
|
selections_["manager-name"] = field("manager-name");
|
|
if (kind == "manager-country") {
|
|
if (value == country_ && !user_reselected)
|
|
return true;
|
|
country_ = value;
|
|
league_.clear();
|
|
} else {
|
|
if (value == league_ && !user_reselected)
|
|
return true;
|
|
league_ = value;
|
|
}
|
|
club_search_.clear();
|
|
if (!manager_picker_ && user_reselected) selected_club_.clear();
|
|
if (manager_picker_) {
|
|
const auto leagues = entry_world()->leagues(country_);
|
|
if (std::none_of(leagues.begin(), leagues.end(),
|
|
[&](auto *l) { return l->value("id", "") == league_; }))
|
|
league_ = leagues.empty() ? "" : leagues.front()->value("id", "");
|
|
} else
|
|
entry_sync_club();
|
|
focus_after_render_ = target->GetId();
|
|
refresh();
|
|
return true;
|
|
}
|
|
bool App::entry_input(Rml::Element *target) {
|
|
if (scene_ != "manager" || manager_confirm_ || manager_success_)
|
|
return false;
|
|
const auto kind = target->GetAttribute<Rml::String>("data-input", "");
|
|
if (kind != "manager-name" && kind != "manager-search")
|
|
return false;
|
|
if (entry_full())
|
|
return true;
|
|
const auto value =
|
|
dynamic_cast<Rml::ElementFormControl *>(target)->GetValue();
|
|
if (kind == "manager-name") {
|
|
selections_["manager-name"] = value;
|
|
if (auto *next = element("manager-next")) {
|
|
if (entry_name_valid() && entry_world()->manageable(selected_club_) &&
|
|
!entry_used_club(selected_club_))
|
|
next->RemoveAttribute("disabled");
|
|
else
|
|
next->SetAttribute("disabled", "");
|
|
}
|
|
dirty_ = true;
|
|
} else {
|
|
club_search_ = value;
|
|
focus_after_render_ = "manager-club-search";
|
|
refresh();
|
|
}
|
|
return true;
|
|
}
|
|
bool App::entry_event(Rml::Event &event) {
|
|
if ((scene_ != "newgame" && scene_ != "manager") || !popup_.empty() || menu_ == "quit")
|
|
return false;
|
|
const auto type = event.GetType();
|
|
auto *target = event.GetTargetElement();
|
|
if (type == "keydown") {
|
|
const auto key =
|
|
Rml::Input::KeyIdentifier(event.GetParameter<int>("key_identifier", 0));
|
|
if (manager_confirm_ || manager_success_) {
|
|
const auto root = element(manager_confirm_ ? "entry-confirm-layer"
|
|
: "entry-success-layer");
|
|
if (key == Rml::Input::KI_ESCAPE) {
|
|
if (manager_confirm_)
|
|
entry_action("manager-confirm-no", nullptr);
|
|
else
|
|
entry_action("manager-success-close", nullptr);
|
|
} else if (key == Rml::Input::KI_TAB && root) {
|
|
Rml::ElementList controls;
|
|
root->QuerySelectorAll(controls, "button");
|
|
if (!controls.empty()) {
|
|
auto i = std::find(controls.begin(), controls.end(),
|
|
context_->GetFocusElement());
|
|
const int old = i == controls.end() ? -1 : int(i - controls.begin());
|
|
const int step = event.GetParameter<int>("shift_key", 0) ? -1 : 1;
|
|
controls[size_t((old + step + int(controls.size())) %
|
|
int(controls.size()))]
|
|
->Focus(true);
|
|
}
|
|
} else if (key == Rml::Input::KI_RETURN || key == Rml::Input::KI_SPACE) {
|
|
for (auto *n = target; n && n != root; n = n->GetParentNode())
|
|
if (n->GetTagName() == "button") {
|
|
n->DispatchEvent("click", {});
|
|
break;
|
|
}
|
|
}
|
|
event.StopImmediatePropagation();
|
|
return true;
|
|
}
|
|
// Leave native selects to the shared select controller, including Escape.
|
|
for (auto *n = target; n && n != doc_; n = n->GetParentNode())
|
|
if (auto *select = dynamic_cast<Rml::ElementFormControlSelect *>(n)) {
|
|
if (key != Rml::Input::KI_ESCAPE || select->IsSelectBoxVisible())
|
|
return false;
|
|
break;
|
|
}
|
|
if (key == Rml::Input::KI_ESCAPE) {
|
|
if (scene_ == "manager" && manager_picker_)
|
|
entry_action("manager-clubs-back", nullptr);
|
|
else if (scene_ == "manager")
|
|
entry_action("manager-back", nullptr);
|
|
else
|
|
entry_cancel();
|
|
event.StopImmediatePropagation();
|
|
return true;
|
|
}
|
|
if (scene_ == "newgame" && key == Rml::Input::KI_RETURN) {
|
|
bool button = false;
|
|
for (auto *n = target; n && n != doc_; n = n->GetParentNode())
|
|
if (n->GetTagName() == "button")
|
|
button = true;
|
|
if (!button) {
|
|
entry_action("setup-next", nullptr);
|
|
event.StopImmediatePropagation();
|
|
return true;
|
|
}
|
|
}
|
|
if (target->GetId() == "manager-name" && key == Rml::Input::KI_RETURN) {
|
|
entry_action("manager-next", nullptr);
|
|
event.StopImmediatePropagation();
|
|
return true;
|
|
}
|
|
if (target->GetAttribute<Rml::String>("role", "") == "radio" &&
|
|
(key == Rml::Input::KI_LEFT || key == Rml::Input::KI_RIGHT ||
|
|
key == Rml::Input::KI_UP || key == Rml::Input::KI_DOWN ||
|
|
key == Rml::Input::KI_HOME || key == Rml::Input::KI_END)) {
|
|
int selected = std::stoi(
|
|
selections_["portrait"].empty() ? "0" : selections_["portrait"]);
|
|
int next = key == Rml::Input::KI_HOME ? 0
|
|
: key == Rml::Input::KI_END ? 11
|
|
: selected;
|
|
const int step = key == Rml::Input::KI_LEFT || key == Rml::Input::KI_UP ||
|
|
key == Rml::Input::KI_END
|
|
? -1
|
|
: 1;
|
|
if (key != Rml::Input::KI_HOME && key != Rml::Input::KI_END)
|
|
next = (next +
|
|
((key == Rml::Input::KI_UP || key == Rml::Input::KI_DOWN)
|
|
? step * 6
|
|
: step) +
|
|
12) %
|
|
12;
|
|
for (int tries = 0; tries < 12 && entry_used_portrait(next); ++tries)
|
|
next = (next + step + 12) % 12;
|
|
if (auto *node = element("manager-portrait-" + std::to_string(next)))
|
|
entry_action("portrait", node);
|
|
event.StopImmediatePropagation();
|
|
return true;
|
|
}
|
|
}
|
|
if (type == "mouseup" && event.GetParameter<int>("button", 0) == 1 &&
|
|
scene_ == "newgame") {
|
|
entry_action("setup-next", nullptr);
|
|
event.StopImmediatePropagation();
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
Json App::entry_snapshot() const {
|
|
return {{"setup", start_options_},
|
|
{"managers", managers_draft_},
|
|
{"adding", adding_manager_},
|
|
{"index", manager_index_},
|
|
{"picker", manager_picker_},
|
|
{"query", club_search_},
|
|
{"confirm", manager_confirm_},
|
|
{"success", manager_success_},
|
|
{"selected_club", selected_club_},
|
|
{"country", country_},
|
|
{"league", league_},
|
|
{"name_valid", entry_name_valid()},
|
|
{"return_scene", entry_return_scene_},
|
|
{"name", selections_.contains("manager-name")
|
|
? selections_.at("manager-name")
|
|
: ""},
|
|
{"portrait",
|
|
selections_.contains("portrait") ? selections_.at("portrait") : "0"},
|
|
{"draft_world", entry_catalog_
|
|
? Json(entry_catalog_->manifest().value("id", ""))
|
|
: Json()}};
|
|
}
|