diff --git a/docs/new_engine/ui-anbindung.md b/docs/new_engine/ui-anbindung.md index 7ec7c65..366f18e 100644 --- a/docs/new_engine/ui-anbindung.md +++ b/docs/new_engine/ui-anbindung.md @@ -104,7 +104,7 @@ Die UI ruft diese über `DataSource::read_almanac` und `read_statistics` direkt Test: `modern_ui_contract_tests` (Umschlag, IDs, Geld, Fehler, Reads ändern den Spielstand nicht; Commands: veraltete Revision, fremder Manager, Wiederholung, Konflikt, Ablehnung ohne Änderung, je ein angenommener und ein abgelehnter Befehl für Lager, Aufstellung, Taktik und Training; braucht Spieldaten). -Noch nicht gefüllt: `nationId`, `crestAssetId` der Teams, `employments` (Amtszeiten), `strengthChoice`, `allowedCommands`. +Aus der Welt: `nationId` und `crestAssetId` der Teams (`TeamRef`) und `photoAssetId` der Spieler im Kader (Asset-Schlüssel wie `asset:club//crest`, die UI zeichnet sie); ohne Welt `null`. `employments` des Managers aus dem Almanach: je betreutem Team `teamId`, `teamName`, `role` (club/national), `from`, `until` (`null` solange es läuft). `allowedCommands` im `meta.scope`: die Befehle, die der Schreibtisch annimmt (am Bundestrainer-Schreibtisch nur `session.advance`, `lineup.commit`, `tactics.commit`, `friendly.book`, `manager.resign.commit`; nach Karriereende keine; ein laufender Live-Anstoß ist dort nicht abgebildet, dann gelten nur `live.*`). Noch nicht gefüllt: `strengthChoice`. ## Reihenfolge diff --git a/src/modern/content/statistics.cpp b/src/modern/content/statistics.cpp index 7b4d5f1..265d199 100644 --- a/src/modern/content/statistics.cpp +++ b/src/modern/content/statistics.cpp @@ -1101,3 +1101,42 @@ cli::Json query_news(const ModernState& g, const std::string& team, const std::s } } // namespace bmh::modern + +namespace bmh::modern { + +cli::Json manager_employments(const ModernState& g, int manager) { + // the teams a human manager led, from the recorded matches: a new employment begins when + // the team of his matches changes (a club and the national team run side by side) + struct Stint { std::string team, name, kind; uint32_t from = 0, until = 0; int display_year = 0; }; + std::vector stints; + for (const AlmanacSeason& s : seasons_of(g)) + for (const AlmanacMatch& m : s.matches) + for (int side = 0; side < 2; ++side) { + const int8_t mi = side ? m.away_manager : m.home_manager; + if (mi < 0 || size_t(mi) >= s.managers.size() || s.managers[size_t(mi)].index != manager) continue; + const AlmanacClub& c = s.clubs[side ? m.away : m.home]; + // national: a world's nt: id, or without a world a runtime number from no_team() on + bool national = c.id.starts_with("nt:"); + if (c.id.starts_with("team:")) try { national = std::stoi(c.id.substr(5)) >= no_team(); } catch (const std::exception&) {} + const std::string kind = national ? "national" : "club"; + // the same team: by its world id, without a world by its name (runtime numbers change) + const auto same = [&](const Stint& st) { return c.id.starts_with("team:") ? st.name == c.name : st.team == c.id; }; + Stint* open = nullptr; + for (auto it = stints.rbegin(); it != stints.rend() && !open; ++it) + if (it->kind == kind) open = &*it; + if (open && same(*open)) { open->until = m.date, open->team = c.id; continue; } // the id as last seen + stints.push_back({c.id, c.name, kind, m.date, m.date, s.display_start_year}); + } + A out; + for (size_t i = 0; i < stints.size(); ++i) { + const Stint& st = stints[i]; + bool current = true; // the last stint of its kind is still running + for (size_t k = i + 1; k < stints.size(); ++k) current &= stints[k].kind != st.kind; + out.push_back(O{{"teamId", st.team}, {"teamName", st.name}, {"role", st.kind}, + {"from", iso_date(st.from, st.display_year)}, + {"until", current ? J(nullptr) : J(iso_date(st.until, st.display_year))}}); + } + return out; +} + +} // namespace bmh::modern diff --git a/src/modern/content/statistics.hpp b/src/modern/content/statistics.hpp index 93d7bd6..10a80ab 100644 --- a/src/modern/content/statistics.hpp +++ b/src/modern/content/statistics.hpp @@ -53,6 +53,10 @@ cli::Json live_fixture(const ModernState& g, uint32_t after_date, uint32_t until // {for, against} from the team's side, venue, trailed, minute of the deciding goal). cli::Json query_news(const ModernState& g, const std::string& team, const std::string& from, const std::string& until); +// The teams a human manager (slot) led, from the almanac: [{teamId, teamName, role club or +// national, from, until (null while running)}] in order. Before the first recorded match: empty. +cli::Json manager_employments(const ModernState& g, int manager); + // A game of a matchday under way: runtime teams and goals so far. struct LiveGame { int home = 0, away = 0, home_goals = 0, away_goals = 0; }; // The live table of a league during matchday `round` (1-based): the table before it plus the diff --git a/src/modern/content/ui_contract.cpp b/src/modern/content/ui_contract.cpp index e812b20..e5294e6 100644 --- a/src/modern/content/ui_contract.cpp +++ b/src/modern/content/ui_contract.cpp @@ -98,8 +98,13 @@ J team_ref(const ModernState& g, int team, const std::string& as_of) { if (name.empty()) name = almanac_utf8(g.club_name(team)); if (short_name.empty()) short_name = name; const bool national = team >= no_team(); + // nation and crest as the world names them (the asset key is passed through, the UI draws it) + const World* world = current_world(); + const WorldClub* club = world ? world->club(team_id(g, team)) : nullptr; + const auto text_or_null = [](const std::string* s) { return s && !s->empty() ? J(*s) : J(nullptr); }; return O{{"id", team_id(g, team)}, {"kind", national ? "national" : "club"}, {"name", name}, - {"shortName", short_name}, {"nationId", nullptr}, {"crestAssetId", nullptr}, {"nameAsOf", as_of}}; + {"shortName", short_name}, {"nationId", text_or_null(club ? &club->nation : nullptr)}, + {"crestAssetId", text_or_null(club ? &club->crest : nullptr)}, {"nameAsOf", as_of}}; } std::string manager_id(const ModernState& g, int manager) { @@ -115,7 +120,25 @@ J manager_json(const ModernState& g, int manager, bool at_desk) { {"control", "human"}, {"status", at_desk ? "active" : "waiting"}, {"teamId", team >= 0 ? J(team_id(g, team)) : J(nullptr)}, {"portraitGuiKey", "gui:manager/photo/" + std::to_string(portrait)}, {"portraitStyle", "photo"}, - {"strengthChoice", nullptr}, {"employments", A{}}}; + {"strengthChoice", nullptr}, {"employments", manager_employments(g, manager)}}; +} + +// The commands the desk accepts now (the session refuses the others): none after the career's +// end; the national coach's desk has no toto, bank, camp, training, stadium, advertising, +// transfers, contracts or surroundings (ui/desk_state.cpp). +A allowed_commands(const ModernState& g, const UiContext& c) { + if (c.career_ended) return {}; + const int team = team_of(g, c.manager); + const bool national = team == no_team(); + static constexpr const char* kAll[] = { + "session.advance", "lineup.commit", "tactics.commit", "friendly.book", "manager.resign.commit", + "training.commit", "camp.book", "bank.commit", "toto.submit", "quote.request", "contract.offer", + "stadium.commit", "advertising.accept", "budget.commit", "ticket.commit", "transfer.submit", + "sponsor.answer", "environment.commit", "fixture.reschedule"}; + A out; + for (size_t i = 0; i < std::size(kAll); ++i) + if (!national || i < 5) out.push_back(kAll[i]); + return out; } J meta(const ModernState& g, const UiContext& c) { @@ -139,7 +162,7 @@ J meta(const ModernState& g, const UiContext& c) { {"teamId", team >= 0 ? J(team_id(g, team)) : J(nullptr)}, {"role", national ? "national" : "club"}, {"scopeId", "scope:desk-" + revision + "-" + std::to_string(c.manager)}, - {"allowedCommands", A{}}}}, + {"allowedCommands", allowed_commands(g, c)}}}, {"availability", available()}, {"currency", O{{"code", "EUR"}, {"minorUnits", 2}}}}; } @@ -226,11 +249,16 @@ J squad_data(const ModernState& g, const UiContext& c, int team, const std::stri std::string last = entry(g.player_last, size_t(id)); if (last.empty()) last = almanac_utf8(g.player_name(id)); const std::string& nation = entry(g.player_nation, size_t(id)); + const auto photo_of = [](const std::string& player) -> J { + const World* world = current_world(); + const WorldPlayer* wp = world ? world->player(player) : nullptr; + return wp && !wp->photo.empty() ? J(wp->photo) : J(nullptr); + }; const uint32_t absence = p.w364 >> 16 & 0x1f; // weeks out; bit 4: banned instead of injured const int injured = (absence & 0x10) ? 0 : int(absence), banned = (absence & 0x10) ? int(absence & 0xf) : 0; O skills; for (int k = 0; k < 7; ++k) skills[kSkills[k]] = int(p.w368 >> (2 * k) & 3); - O row{{"player", O{{"id", pid}, {"name", first.empty() ? last : first + " " + last}, {"photoAssetId", nullptr}, + O row{{"player", O{{"id", pid}, {"name", first.empty() ? last : first + " " + last}, {"photoAssetId", photo_of(pid)}, {"nationIds", nation.empty() ? A{} : A{J(nation)}}, {"positions", A{J(kPositions[p.position()])}}, {"shirtNumber", nullptr}}}, {"clubId", team < league_clubs() ? J(team_id(g, team)) : J(nullptr)}, diff --git a/src/modern/content/world.cpp b/src/modern/content/world.cpp index 5e9589f..e765bd8 100644 --- a/src/modern/content/world.cpp +++ b/src/modern/content/world.cpp @@ -45,6 +45,10 @@ const WorldClub* World::club(const std::string& id) const { const auto it = club_by_id.find(id); return it == club_by_id.end() ? nullptr : &clubs[it->second]; } +const WorldPlayer* World::player(const std::string& id) const { + const auto it = player_by_id.find(id); + return it == player_by_id.end() ? nullptr : &players[it->second]; +} World read_world(const obw::Documents& documents) { World w; @@ -189,6 +193,7 @@ World read_world(const obw::Documents& documents) { pl.w368 = word("skill_word"); pl.w364 = word("identity_word"); } + w.player_by_id[pl.id] = w.players.size(); w.players.push_back(std::move(pl)); } // Referees (data format F9): staff rows with role referee. Imported ones keep their diff --git a/src/modern/content/world.hpp b/src/modern/content/world.hpp index 3bdd371..4acabe7 100644 --- a/src/modern/content/world.hpp +++ b/src/modern/content/world.hpp @@ -78,10 +78,12 @@ struct World { std::vector referees; // in table order (data format F9) std::map nationality; // nation id -> Classic nationality code (0 = Germany) std::map club_by_id; // club id -> index in clubs + std::map player_by_id; // player id -> index in players // rule:training-camps with maps and camp details (data format F4); empty = built-in catalogue. std::optional camps; const WorldClub* club(const std::string& id) const; + const WorldPlayer* player(const std::string& id) const; }; // Reads the world tables. Throws FormatError for missing or malformed required fields. diff --git a/tests/modern_ui_contract_tests.cpp b/tests/modern_ui_contract_tests.cpp index 1b3fcf6..300a3f1 100644 --- a/tests/modern_ui_contract_tests.cpp +++ b/tests/modern_ui_contract_tests.cpp @@ -769,6 +769,17 @@ int main(int argc, char** argv) { std::printf("(journal test skipped: national desk)\n"); } } + // the manager's employments from the almanac, the desk's allowed commands + { + const J desk = session.ui_read(O{{"kind", "desktop"}}, rev); + const auto* jobs = at(at(at(desk, "data"), "manager"), "employments").array(); + const auto* allowed = at(at(at(desk, "meta"), "scope"), "allowedCommands").array(); + std::printf("(employments: %s)\n", jobs ? cli::dump_json(J(*jobs)).substr(0, 300).c_str() : "none"); + bool advance = false; + if (allowed) for (const auto& a : *allowed) advance |= s(a) == "session.advance"; + check(jobs && !jobs->empty() && at((*jobs)[0], "teamId").string(), "employments of the manager"); + check(allowed && advance, "allowed commands of the desk"); + } const J few = session.ui_read(O{{"kind", "news"}, {"limit", 3}}, rev); check(at(at(few, "data"), "events").array() && at(at(few, "data"), "events").array()->size() == 3, "news limit"); } diff --git a/tests/modern_world_info.cpp b/tests/modern_world_info.cpp index 251e423..f2cfdad 100644 --- a/tests/modern_world_info.cpp +++ b/tests/modern_world_info.cpp @@ -45,6 +45,14 @@ int main(int argc, char** argv) { std::printf("%d league clubs, %zu clubs in all; %zu players (%d original, %d without club, " "%d with a Classic nationality code); league squads %d-%d\n", league_clubs, w.clubs.size(), w.players.size(), legacy, without_team, coded, smallest, largest); + // asset keys and nations as the interface passes them on (TeamRef, squad rows) + int crests = 0, nations = 0, photos = 0; + for (const auto& c : w.clubs) crests += !c.crest.empty(), nations += !c.nation.empty(); + for (const auto& p : w.players) photos += !p.photo.empty(); + std::printf("clubs with crest %d, with nation %d; players with photo %d\n", crests, nations, photos); + if (!w.clubs.empty()) std::printf("e.g. club %s: nation %s, crest %s\n", w.clubs[0].id.c_str(), w.clubs[0].nation.c_str(), w.clubs[0].crest.c_str()); + for (const auto& p : w.players) + if (!p.photo.empty()) { std::printf("e.g. player %s: photo %s\n", p.id.c_str(), p.photo.c_str()); break; } return 0; } catch (const std::exception& e) { std::fprintf(stderr, "cannot open world: %s\n", e.what());