Modern UI contract: squad read and contract tests
The squad read lists a squad (own club, any league club or a national team with a nominated squad) with ids, positions, values, absences and, for the manager's own club, monthly salary and remaining contract months. modern_ui_contract_tests checks envelope, identities, money, errors and that reads leave the state unchanged. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
3336d01004
commit
fec4c6c7e7
5 files changed
+207
-2
No files matched your search
@@ -8,6 +8,7 @@
|
||||
#include <variant>
|
||||
|
||||
#include "formats/executable_layout.hpp"
|
||||
#include "game/state.hpp"
|
||||
#include "game/table.hpp"
|
||||
#include "modern/content/world_package.hpp"
|
||||
#include "modern/game/almanac.hpp"
|
||||
@@ -179,6 +180,79 @@ J desktop_data(const ModernState& g, const UiContext& c, const std::string& toda
|
||||
return data;
|
||||
}
|
||||
|
||||
J metric(int value) {
|
||||
return O{{"value", value}, {"unit", "rating"}, {"scale", nullptr}, {"availability", available()}};
|
||||
}
|
||||
|
||||
// The team a request names (teamId: a world id or "team:<n>"), or the manager's own team.
|
||||
int requested_team(const ModernState& g, const UiContext& c, const J& request) {
|
||||
const std::string wanted = text_field(request, "teamId");
|
||||
if (wanted.empty()) return team_of(g, c.manager);
|
||||
for (int t = 0; t < int(g.world_club_id.size()); ++t)
|
||||
if (g.world_club_id[size_t(t)] == wanted) return t;
|
||||
if (wanted.starts_with("team:")) {
|
||||
try {
|
||||
const int t = std::stoi(wanted.substr(5));
|
||||
if (t >= 0 && t < world_layout().total_teams()) return t;
|
||||
} catch (const std::exception&) {
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
// A squad: every player with identity, position, values, absence and (for the manager's own
|
||||
// club, from his tactics board) monthly salary and remaining contract months.
|
||||
J squad_data(const ModernState& g, const UiContext& c, int team, const std::string& today) {
|
||||
static constexpr const char* kPositions[4] = {"gk", "def", "mid", "fwd"};
|
||||
static constexpr const char* kSkills[7] = {"teamplay", "speed", "shooting", "passing", "tackling", "free_kick", "corner"};
|
||||
const bool own = team == team_of(g, c.manager) && team < league_clubs();
|
||||
const size_t board = size_t(c.manager) * 0x840;
|
||||
const SquadRange r = squad_range(team);
|
||||
A players;
|
||||
for (size_t s = r.first; s < r.first + r.count && s < g.squad_slots.size(); ++s) {
|
||||
const uint16_t e = g.squad_slots[s];
|
||||
const int id = e & 0x0fff;
|
||||
if (id == 0x0fff) continue;
|
||||
const Player& p = g.players.at(size_t(id));
|
||||
std::string pid = entry(g.world_player_id, size_t(id));
|
||||
if (pid.empty()) pid = "player:legacy-" + std::to_string(id);
|
||||
const std::string& first = entry(g.player_first, size_t(id));
|
||||
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 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},
|
||||
{"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)},
|
||||
{"squadTeamId", team_id(g, team)},
|
||||
{"role", (e & 0x8000) ? "starter" : (e & 0x4000) ? "bench" : "reserve"},
|
||||
{"availability", injured ? "injured" : banned ? "suspended" : "fit"},
|
||||
{"injuryRemaining", O{{"value", injured}, {"unit", "weeks"}}},
|
||||
{"suspensionRemaining", O{{"value", banned}, {"unit", "weeks"}}},
|
||||
{"age", p.age()},
|
||||
{"metrics", O{{"strength", metric(p.strength())}, {"condition", metric(p.condition())},
|
||||
{"technique", metric(p.technique())}, {"form", metric(p.form())}}},
|
||||
{"skills", std::move(skills)}};
|
||||
const size_t at = board + (s - r.first) * 0x58;
|
||||
if (own && at + 0x58 <= g.tactics.size()) {
|
||||
const int64_t salary = int64_t(uint32_t(g.tactics[at + 0x50]) | uint32_t(g.tactics[at + 0x51]) << 8 |
|
||||
uint32_t(g.tactics[at + 0x52]) << 16 | uint32_t(g.tactics[at + 0x53]) << 24);
|
||||
row["salaryMonthly"] = ui_money(salary);
|
||||
row["contractRemaining"] = O{{"value", int(g.tactics[at + 0x0c])}, {"unit", "months"}};
|
||||
} else {
|
||||
row["salaryMonthly"] = nullptr;
|
||||
row["contractRemaining"] = nullptr;
|
||||
}
|
||||
row["contractUntil"] = nullptr;
|
||||
players.push_back(std::move(row));
|
||||
}
|
||||
return O{{"kind", "squad"}, {"team", team_ref(g, team, today)}, {"players", std::move(players)}};
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
void ensure_ui_identities(ModernState& g) {
|
||||
@@ -212,6 +286,12 @@ cli::Json ui_read(const ModernState& g, const UiContext& c, const cli::Json& req
|
||||
const std::string today = *std::get<O>(m.value).at("date").string();
|
||||
if (kind == "session") return snapshot(request_id, std::move(m), session_data(g, c));
|
||||
if (kind == "desktop") return snapshot(request_id, std::move(m), desktop_data(g, c, today));
|
||||
if (kind == "squad") {
|
||||
const int team = requested_team(g, c, request);
|
||||
if (team < 0 || !fields_players(g, team))
|
||||
return failure(request_id, "unknown_team", "Dieses Team hat keinen Kader.", c.state_revision);
|
||||
return snapshot(request_id, std::move(m), squad_data(g, c, team, today));
|
||||
}
|
||||
return failure(request_id, "unknown_read", "Unbekannte Abfrage: " + kind, c.state_revision);
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user