Modern UI contract: press journal for business events
Business events cannot be derived from the almanac later, so a saved journal notes them once: squad changes of the human clubs at a kickoff (signed, free, sold, contract ended; clubs compared by identity across the season's new draw positions), toto settlements, season change, contract renewals and resigning. The news read merges it with the almanac events. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
9c436c753e
commit
651d262546
9 files changed
+318
-4
No files matched your search
@@ -0,0 +1,179 @@
|
||||
// SPDX-License-Identifier: GPL-3.0-only
|
||||
#include "modern/content/press_journal.hpp"
|
||||
|
||||
#include <cstdio>
|
||||
#include <map>
|
||||
#include <set>
|
||||
#include <variant>
|
||||
|
||||
#include "formats/executable_layout.hpp"
|
||||
#include "game/state.hpp"
|
||||
#include "modern/content/ui_contract.hpp"
|
||||
#include "modern/game/almanac.hpp"
|
||||
|
||||
namespace bmh::modern {
|
||||
|
||||
namespace {
|
||||
|
||||
using J = cli::Json;
|
||||
using A = J::Array;
|
||||
using O = J::Object;
|
||||
|
||||
constexpr size_t kJournalLimit = 4000; // the oldest entries go first beyond this
|
||||
|
||||
std::string iso(const ModernState& g, uint32_t counter) {
|
||||
const AlmanacDate d = almanac_date(counter, g.display_start_year);
|
||||
char out[16];
|
||||
std::snprintf(out, sizeof out, "%04d-%02d-%02d", d.year, d.month, d.day);
|
||||
return out;
|
||||
}
|
||||
|
||||
std::string season_text(const ModernState& g) {
|
||||
const int year = almanac_date(g.season_start, g.display_start_year).year;
|
||||
char out[16];
|
||||
std::snprintf(out, sizeof out, "%04d/%02d", year, (year + 1) % 100);
|
||||
return out;
|
||||
}
|
||||
|
||||
// Clubs are compared by their identity: the runtime team numbers are draw positions that change
|
||||
// at the season's start. The identity is the world's club id, without a world the club's name
|
||||
// (the original's club_ids are not unique).
|
||||
std::string identity_of(const ModernState& g, int team) {
|
||||
if (team < 0) return {};
|
||||
if (size_t(team) < g.world_club_id.size() && !g.world_club_id[size_t(team)].empty()) return g.world_club_id[size_t(team)];
|
||||
return std::string(g.club_name(team));
|
||||
}
|
||||
int team_of_identity(const ModernState& g, const std::string& identity) {
|
||||
for (int t = 0; t < no_team(); ++t)
|
||||
if (identity_of(g, t) == identity) return t;
|
||||
return -1;
|
||||
}
|
||||
|
||||
// The club (identity) of every player in the club squads.
|
||||
std::map<int, std::string> player_clubs(const ModernState& g) {
|
||||
std::map<int, std::string> out;
|
||||
for (int t = 0; t < no_team(); ++t) { // clubs only: national squads list their players again
|
||||
const SquadRange r = squad_range(t);
|
||||
for (size_t row = 0; row < r.count && r.first + row < g.squad_slots.size(); ++row) {
|
||||
const int p = g.squad_slots[r.first + row] & 0x0fff;
|
||||
if (p != 0x0fff) out.emplace(p, identity_of(g, t)); // the first squad that lists him (a loan lists him twice)
|
||||
}
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
// The clubs of the human managers. While one coaches the national team his club's manager
|
||||
// record waits in slot 4 (national_coach_take_over), so it is read from there.
|
||||
int club_of_manager(const ModernState& g, size_t m) {
|
||||
int team = m < g.manager_teams.size() ? g.manager_teams[m] : -1;
|
||||
if (team == no_team() && m == 0 && g.memory.has(::bmh::layout::CoachActive) && g.memory.u8(::bmh::layout::CoachActive) != 0) {
|
||||
const size_t at = 4 * size_t(0xb0c) + 0x290;
|
||||
if (at + 1 < g.manager_records.size()) team = int16_t(g.manager_records[at] | g.manager_records[at + 1] << 8);
|
||||
}
|
||||
return team >= 0 && team < league_clubs() ? team : -1;
|
||||
}
|
||||
// The identities of the human managers' clubs.
|
||||
std::set<std::string> human_clubs(const ModernState& g) {
|
||||
std::set<std::string> out;
|
||||
for (size_t m = 0; m < g.manager_teams.size(); ++m)
|
||||
if (const int team = club_of_manager(g, m); team >= 0) out.insert(identity_of(g, team));
|
||||
return out;
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
J press_team(const ModernState& g, int team) {
|
||||
std::string name = size_t(team) < g.world_club_name.size() ? g.world_club_name[size_t(team)] : std::string{};
|
||||
if (name.empty()) name = almanac_utf8(g.club_name(team));
|
||||
return O{{"id", ui_team_id(g, team)}, {"name", name}, {"kind", team >= no_team() ? "national" : "club"}};
|
||||
}
|
||||
|
||||
J press_player(const ModernState& g, int player) {
|
||||
const std::string first = size_t(player) < g.player_first.size() ? g.player_first[size_t(player)] : std::string{};
|
||||
std::string last = size_t(player) < g.player_last.size() ? g.player_last[size_t(player)] : std::string{};
|
||||
if (last.empty()) last = almanac_utf8(g.player_name(player));
|
||||
return O{{"id", ui_player_id(g, player)}, {"name", first.empty() ? last : first + " " + last}};
|
||||
}
|
||||
|
||||
void note_press(ModernState& g, const std::string& type, const std::string& team, J data) {
|
||||
ModernState::PressEntry e;
|
||||
e.id = "press-event:" + type + ":j" + std::to_string(++g.press_serial) + ":" + team;
|
||||
e.type = type, e.team = team, e.date = g.date, e.data = cli::dump_json(data);
|
||||
g.press.push_back(std::move(e));
|
||||
if (g.press.size() > kJournalLimit) g.press.erase(g.press.begin(), g.press.begin() + (g.press.size() - kJournalLimit));
|
||||
}
|
||||
|
||||
void journal_advance(ModernState& g, const ModernState& before) {
|
||||
// the human clubs' squads: arrivals and departures
|
||||
const std::map<int, std::string> was = player_clubs(before), now = player_clubs(g);
|
||||
// the clubs of human managers in both states (a squad change is only seen on a club held throughout)
|
||||
std::set<std::string> clubs, then = human_clubs(before);
|
||||
for (const std::string& club : human_clubs(g))
|
||||
if (then.count(club)) clubs.insert(club);
|
||||
const auto team_entity = [&](const std::string& identity) {
|
||||
const int t = team_of_identity(g, identity);
|
||||
return t >= 0 ? press_team(g, t) : J(nullptr);
|
||||
};
|
||||
for (const std::string& club : clubs) {
|
||||
const int team = team_of_identity(g, club);
|
||||
const J me = press_team(g, team);
|
||||
const std::string id = ui_team_id(g, team);
|
||||
for (const auto& [p, identity] : now) {
|
||||
if (identity != club) continue;
|
||||
const auto old = was.find(p);
|
||||
if (old != was.end() && old->second == club) continue;
|
||||
if (old == was.end())
|
||||
note_press(g, "transfer.free", id, O{{"team", me}, {"player", press_player(g, p)}, {"until", nullptr}});
|
||||
else
|
||||
note_press(g, "transfer.signed", id, O{{"team", me}, {"player", press_player(g, p)}, {"fromClub", team_entity(old->second)}, {"fee", nullptr}});
|
||||
}
|
||||
for (const auto& [p, identity] : was) {
|
||||
if (identity != club) continue;
|
||||
const auto next = now.find(p);
|
||||
if (next != now.end() && next->second == club) continue;
|
||||
if (next == now.end())
|
||||
note_press(g, "contract.ended", id, O{{"team", me}, {"player", press_player(before, p)}});
|
||||
else
|
||||
note_press(g, "transfer.sold", id, O{{"team", me}, {"player", press_player(g, p)}, {"toClub", team_entity(next->second)}, {"fee", nullptr}});
|
||||
}
|
||||
}
|
||||
// a new toto settlement of a club manager
|
||||
for (size_t m = 0; m < g.pools.size() && m < before.pools.size() && m < g.manager_teams.size(); ++m) {
|
||||
const auto& a = before.pools[m];
|
||||
const auto& b = g.pools[m];
|
||||
const int team = club_of_manager(g, m);
|
||||
if (b.hits.empty() || (a.hits == b.hits && a.paid == b.paid && a.won == b.won) || team < 0) continue;
|
||||
const std::string id = ui_team_id(g, team);
|
||||
if (b.won > 0) note_press(g, "toto.win", id, O{{"team", press_team(g, team)}, {"amount", ui_money(b.won)}});
|
||||
else if (b.paid > 0) note_press(g, "toto.loss", id, O{{"team", press_team(g, team)}, {"amount", ui_money(b.paid)}});
|
||||
}
|
||||
// a new season
|
||||
if (g.season != before.season)
|
||||
for (const std::string& club : human_clubs(g)) {
|
||||
const int team = team_of_identity(g, club);
|
||||
note_press(g, "career.season_finished", ui_team_id(g, team), O{{"team", press_team(g, team)}, {"season", season_text(before)}});
|
||||
note_press(g, "career.season_started", ui_team_id(g, team), O{{"team", press_team(g, team)}, {"season", season_text(g)}});
|
||||
}
|
||||
}
|
||||
|
||||
A journal_events(const ModernState& g, const std::string& team, const std::string& from, const std::string& until) {
|
||||
A out;
|
||||
for (const auto& e : g.press) {
|
||||
if (e.team != team) continue;
|
||||
const std::string date = iso(g, e.date);
|
||||
if (date < from || date > until) continue;
|
||||
J data;
|
||||
try { data = cli::parse_json(e.data); } catch (const std::exception&) { continue; }
|
||||
const auto* d = data.object();
|
||||
J scope = O{{"kind", "club"}, {"id", team}, {"name", nullptr}};
|
||||
if (d && d->count("team")) {
|
||||
const auto& t = *d->at("team").object();
|
||||
scope = O{{"kind", t.at("kind")}, {"id", team}, {"name", t.at("name")}};
|
||||
}
|
||||
out.push_back(O{{"id", e.id}, {"type", e.type}, {"date", date}, {"scope", std::move(scope)}, {"data", std::move(data)},
|
||||
{"variant", nullptr}});
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
} // namespace bmh::modern
|
||||
@@ -0,0 +1,41 @@
|
||||
// SPDX-License-Identifier: GPL-3.0-only
|
||||
#pragma once
|
||||
// The press journal of a Modern career (docs/new_engine/ui-anbindung.md, read "news"; the
|
||||
// interface's press contract is docs/new_ui/integration/PRESS.md).
|
||||
//
|
||||
// Match events come from the almanac (statistics.hpp, query_news). Business events cannot be
|
||||
// derived later, so they are noted once when they happen and saved with the career
|
||||
// (ModernState::press): their id stays the same on every read, a deleted article is never
|
||||
// produced again. Noting draws no random number and changes nothing else of the game.
|
||||
//
|
||||
// Sources:
|
||||
// - a kickoff (session.advance, also live): the squads of the human clubs before and after
|
||||
// give transfer.signed (with fromClub, or transfer.free from the pool), transfer.sold
|
||||
// (toClub) and contract.ended (to the pool); a new toto settlement gives toto.win /
|
||||
// toto.loss; a season change career.season_finished and career.season_started;
|
||||
// - confirmed commands: contract.extended (contract.offer), career.manager_resigned.
|
||||
// Fees are not known from the squads: the transfer events carry no fee (the renderer's
|
||||
// fee-undisclosed variants).
|
||||
|
||||
#include <string>
|
||||
|
||||
#include "cli/json.hpp"
|
||||
#include "modern/game/modern_state.hpp"
|
||||
|
||||
namespace bmh::modern {
|
||||
|
||||
// Notes an event of `team` (contract team id) on today's date with its facts.
|
||||
void note_press(ModernState& g, const std::string& type, const std::string& team, cli::Json data);
|
||||
|
||||
// The events of the kickoff from `before` to `g` (see above).
|
||||
void journal_advance(ModernState& g, const ModernState& before);
|
||||
|
||||
// The journal's events of `team` with from <= date <= until (ISO), in the form of query_news.
|
||||
cli::Json::Array journal_events(const ModernState& g, const std::string& team, const std::string& from,
|
||||
const std::string& until);
|
||||
|
||||
// A team / a player as the press facts name them ({id, name, kind} / {id, name}).
|
||||
cli::Json press_team(const ModernState& g, int team);
|
||||
cli::Json press_player(const ModernState& g, int player);
|
||||
|
||||
} // namespace bmh::modern
|
||||
@@ -11,6 +11,7 @@
|
||||
#include "game/state.hpp"
|
||||
#include "game/table.hpp"
|
||||
#include "modern/content/projections.hpp"
|
||||
#include "modern/content/press_journal.hpp"
|
||||
#include "modern/content/statistics.hpp"
|
||||
#include "modern/content/world_package.hpp"
|
||||
#include "modern/game/almanac.hpp"
|
||||
@@ -382,12 +383,16 @@ cli::Json ui_read(const ModernState& g, const UiContext& c, const cli::Json& req
|
||||
const int64_t limit = limit_field && limit_field->integer() && *limit_field->integer() > 0 ? *limit_field->integer() : 50;
|
||||
J news = query_news(g, team_id(g, team), from, until);
|
||||
A events = std::move(std::get<A>(std::get<O>(news.value).at("events").value));
|
||||
for (J& e : journal_events(g, team_id(g, team), from, until)) events.push_back(std::move(e));
|
||||
std::stable_sort(events.begin(), events.end(), [](const J& a, const J& b) {
|
||||
return *std::get<O>(a.value).at("date").string() < *std::get<O>(b.value).at("date").string();
|
||||
});
|
||||
const size_t total = events.size();
|
||||
if (events.size() > size_t(limit)) events.erase(events.begin(), events.end() - limit);
|
||||
return snapshot(request_id, std::move(m),
|
||||
O{{"kind", "news"}, {"teamId", team_id(g, team)}, {"from", from}, {"until", until}, {"total", int64_t(total)},
|
||||
{"events", std::move(events)},
|
||||
{"source", "almanac"}, {"note", "Spielereignisse, Serien und Saisonabschlüsse; Geschäftsereignisse folgen"}});
|
||||
{"sources", A{J("almanac"), J("journal")}}});
|
||||
}
|
||||
if (kind == "calendar") {
|
||||
// the season calendar of the desk's team: days from `from` to `until` (ISO dates, default
|
||||
|
||||
@@ -71,6 +71,20 @@ std::vector<ModernState::ExclusiveSponsor> sponsors_json(const J& value) {
|
||||
return out;
|
||||
}
|
||||
|
||||
// The press journal (content/press_journal.hpp): the entries with their facts as JSON text.
|
||||
J press_json(const std::vector<ModernState::PressEntry>& press) {
|
||||
A out;
|
||||
for (const auto& e : press)
|
||||
out.push_back(O{{"id", e.id}, {"type", e.type}, {"team", e.team}, {"date", int64_t(e.date)}, {"data", e.data}});
|
||||
return out;
|
||||
}
|
||||
std::vector<ModernState::PressEntry> press_json(const J& value) {
|
||||
std::vector<ModernState::PressEntry> out;
|
||||
for (const J& row : arr(value))
|
||||
out.push_back({str(get(row, "id")), str(get(row, "type")), str(get(row, "team")), uint32_t(num(get(row, "date"))), str(get(row, "data"))});
|
||||
return out;
|
||||
}
|
||||
|
||||
// The Toto columns 2-6 and the last settlement per manager (docs/new_engine/toto.md).
|
||||
J pools_json(const std::vector<ModernState::PoolsColumns>& pools) {
|
||||
A out;
|
||||
@@ -139,7 +153,9 @@ void save_world_career(const std::filesystem::path& path, const World& world, co
|
||||
{"career_id", state.career_id},
|
||||
{"manager_ids", strings(state.manager_ids)},
|
||||
{"manager_serial", int64_t(state.manager_serial)},
|
||||
{"traffic_scale", int64_t(state.traffic_scale)}});
|
||||
{"traffic_scale", int64_t(state.traffic_scale)},
|
||||
{"press", press_json(state.press)},
|
||||
{"press_serial", int64_t(state.press_serial)}});
|
||||
// The almanac: finished seasons unchanged, the running one encoded now.
|
||||
for (const auto& [name, bytes] : state.almanac.finished) docs[name] = bytes;
|
||||
if (state.almanac.current.season >= 0) docs["history/current.bin"] = encode_almanac_season(state.almanac.current);
|
||||
@@ -195,6 +211,10 @@ SavedWorldCareer load_world_career(const std::filesystem::path& path, const Worl
|
||||
out.state.manager_ids = strings(get(modern, "manager_ids"));
|
||||
out.state.manager_serial = uint32_t(num(get(modern, "manager_serial")));
|
||||
if (o->count("traffic_scale")) out.state.traffic_scale = uint8_t(num(get(modern, "traffic_scale")));
|
||||
if (o->count("press")) {
|
||||
out.state.press = press_json(get(modern, "press"));
|
||||
out.state.press_serial = uint32_t(num(get(modern, "press_serial")));
|
||||
}
|
||||
}
|
||||
for (const auto& [name, bytes] : docs) {
|
||||
if (!name.starts_with("history/season-")) continue;
|
||||
|
||||
Reference in new issue
Block a user