Modern UI contract: calendar read
club_calendar projects the season calendar of a club: the competitions of each day and the club's league matches on the day of their round, or on the replay day when moved or postponed. The calendar read lists a date range of it with fixtures, status and results. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
6e400d708c
commit
d3d526c2e4
5 files changed
+138
No files matched your search
@@ -370,6 +370,50 @@ cli::Json ui_read(const ModernState& g, const UiContext& c, const cli::Json& req
|
||||
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 == "camps") return snapshot(request_id, std::move(m), camps_data(g, c, std::to_string(c.state_revision)));
|
||||
if (kind == "calendar") {
|
||||
// the season calendar of the desk's team: days from `from` to `until` (ISO dates, default
|
||||
// the current month) with the day's competitions and the team's league matches (planned
|
||||
// day, or the replay day of a moved/postponed one with its original date). No draw.
|
||||
const int own = team_of(g, c.manager);
|
||||
const auto date_of = [&](int day) { return iso(almanac_date(g.season_start + uint32_t(day), g.display_start_year)); };
|
||||
const std::string from = text_field(request, "from").empty() ? today.substr(0, 8) + "01" : text_field(request, "from");
|
||||
const std::string until = text_field(request, "until").empty() ? today.substr(0, 8) + "31" : text_field(request, "until");
|
||||
if (from.size() != 10 || until.size() != 10 || until < from)
|
||||
return failure(request_id, "invalid_range", "from und until sind ISO-Daten, until nicht vor from.", c.state_revision);
|
||||
A days, fixtures;
|
||||
for (const ClubCalendarDay& d : club_calendar(g, own)) {
|
||||
const std::string date = date_of(d.day);
|
||||
if (date < from || date > until) continue;
|
||||
A kinds, ids;
|
||||
if (d.competitions & 0x1f) kinds.push_back("league");
|
||||
for (int bit = 5; bit < 15; ++bit)
|
||||
if (d.competitions >> bit & 1) kinds.push_back("cup:" + std::to_string(bit - 5));
|
||||
if (d.competitions & 0x8000) kinds.push_back("replay");
|
||||
if (d.competitions & 0x10000) kinds.push_back("indoor");
|
||||
if (d.competitions & 0x40000) kinds.push_back("special");
|
||||
if (d.competitions & 0x800000) kinds.push_back("national");
|
||||
if (d.friendly) kinds.push_back("friendly");
|
||||
for (const ClubCalendarMatch& cm : d.matches) {
|
||||
const std::string id = "fixture:league-" + std::to_string(cm.league) + "-" + std::to_string(cm.round + 1) + "-" + std::to_string(cm.match);
|
||||
ids.push_back(id);
|
||||
const bool open = (cm.result & 0xcc) == 0xcc;
|
||||
const bool past = d.day < g.season_day();
|
||||
const std::string league_id = cm.league < int(g.world_league_id.size()) && !g.world_league_id[size_t(cm.league)].empty()
|
||||
? g.world_league_id[size_t(cm.league)] : "league:" + std::to_string(cm.league);
|
||||
fixtures.push_back(O{{"id", id}, {"competition", O{{"id", league_id}, {"kind", "league"}}},
|
||||
{"round", cm.round + 1}, {"originalDate", cm.original_day >= 0 ? J(date_of(cm.original_day)) : J(date)},
|
||||
{"scheduledDate", date}, {"home", team_ref(g, cm.home, date)}, {"away", team_ref(g, cm.away, date)},
|
||||
{"status", !open ? "played" : cm.result == 0xcd && cm.original_day < 0 ? "postponed"
|
||||
: cm.original_day >= 0 ? "rescheduled" : past ? "void" : "scheduled"},
|
||||
{"result", open ? J(nullptr) : J(A{J(cm.result >> 4), J(cm.result & 0xf)})}});
|
||||
}
|
||||
if (kinds.empty() && ids.empty()) continue;
|
||||
days.push_back(O{{"date", date}, {"seasonDay", d.day}, {"today", d.day == g.season_day()},
|
||||
{"eventKinds", std::move(kinds)}, {"fixtureIds", std::move(ids)}});
|
||||
}
|
||||
return snapshot(request_id, std::move(m),
|
||||
O{{"kind", "calendar"}, {"from", from}, {"until", until}, {"days", std::move(days)}, {"fixtures", std::move(fixtures)}});
|
||||
}
|
||||
if (kind == "scheduling" || kind == "friendly") {
|
||||
const int own = team_of(g, c.manager);
|
||||
if (own < 0 || own >= league_clubs())
|
||||
|
||||
@@ -241,4 +241,54 @@ bool undo_reschedule(ModernState& g, int manager, int day) {
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
std::vector<ClubCalendarDay> club_calendar(const ModernState& g, int team) {
|
||||
std::vector<ClubCalendarDay> out;
|
||||
if (!memory_ok(g)) return out;
|
||||
const int league = team >= 0 && team != no_team() ? team_league(team) : kLeagueCount;
|
||||
// the club's replay entries: round, match, replay day
|
||||
struct Replay { int round, match, day; };
|
||||
std::vector<Replay> replays;
|
||||
if (league < kLeagueCount)
|
||||
for (int e = 0; e < kPostponedEntries; e++) {
|
||||
const uint32_t at = kPostponed + uint32_t(e) * 6;
|
||||
const int day = g.memory.u16(at), round = g.memory.u8(at + 2), match = g.memory.u8(at + 4);
|
||||
if (g.memory.u8(at + 3) != league || day == 0 || day >= kCalendarDays || round >= league_matchdays(league)) continue;
|
||||
const auto fx = round_fixtures(league, round);
|
||||
if (size_t(match) < fx.size() && (fx[size_t(match)].home == team || fx[size_t(match)].away == team))
|
||||
replays.push_back({round, match, day});
|
||||
}
|
||||
out.resize(kCalendarDays);
|
||||
std::vector<std::pair<int, ClubCalendarMatch>> moved; // replay day, match
|
||||
int ordinal = 0;
|
||||
for (int d = 0; d < kCalendarDays; d++) {
|
||||
ClubCalendarDay& day = out[size_t(d)];
|
||||
day.day = d;
|
||||
day.word = g.memory.u16(kCalendar + uint32_t(d) * 2);
|
||||
day.competitions = day_competitions(g, day.word);
|
||||
for (int i = 0; i < kFriendlyEntries && team >= 0; i++) {
|
||||
if (g.memory.u16(kFriendlyDays + uint32_t(i) * 2) != uint16_t(d)) continue;
|
||||
for (int side = 0; side < 2; side++)
|
||||
if (int16_t(g.memory.u16(kFriendlyTeams + uint32_t(i) * 4 + uint32_t(side) * 2)) == team) day.friendly = true;
|
||||
}
|
||||
if (league >= kLeagueCount || !(day.competitions >> kLeagueBit[league] & 1) || ordinal >= league_matchdays(league)) continue;
|
||||
const int round = ordinal++;
|
||||
const auto fx = round_fixtures(league, round);
|
||||
for (size_t m = 0; m < fx.size(); m++) {
|
||||
if (fx[m].home != team && fx[m].away != team) continue;
|
||||
ClubCalendarMatch match{league, round, int(m), fx[m].home, fx[m].away, -1, g.results.at(result_at(league, round, int(m)))};
|
||||
const auto replay = std::find_if(replays.begin(), replays.end(),
|
||||
[&](const Replay& r) { return r.round == round && r.match == int(m); });
|
||||
if (replay != replays.end() && replay->day != d) {
|
||||
match.original_day = d;
|
||||
moved.push_back({replay->day, match});
|
||||
} else {
|
||||
day.matches.push_back(match);
|
||||
}
|
||||
}
|
||||
}
|
||||
for (const auto& [day, match] : moved) out[size_t(day)].matches.push_back(match);
|
||||
return out;
|
||||
}
|
||||
|
||||
} // namespace bmh::modern
|
||||
@@ -59,4 +59,26 @@ std::vector<LeagueMatchRef> moved_matches(const ModernState& g, int manager);
|
||||
// freed. Returns false if there is no such match.
|
||||
bool undo_reschedule(ModernState& g, int manager, int day);
|
||||
|
||||
|
||||
// The season calendar of a club for the interface (pure, no random draw): per calendar day
|
||||
// (0 = the season's first day, as CompetitionCalendar) the calendar word, the day's
|
||||
// competitions (day_competitions) and whether the club has a friendly, plus the club's league
|
||||
// matches. A league match sits on the day of its round (the rounds counted along the league's
|
||||
// calendar bit, as advance_league_rounds does); one with a replay entry (moved, 0xCE, or
|
||||
// postponed in winter, 0xCD) sits on the replay day with `original_day` set to its planned day.
|
||||
struct ClubCalendarMatch {
|
||||
int league = 0, round = 0, match = 0; // 0-based round, index in the round's fixture list
|
||||
int home = 0, away = 0;
|
||||
int original_day = -1; // the planned day of a replay, else -1
|
||||
uint8_t result = 0xcc; // result byte: home << 4 | away, 0xCC..0xCF open
|
||||
};
|
||||
struct ClubCalendarDay {
|
||||
int day = 0;
|
||||
uint16_t word = 0;
|
||||
uint32_t competitions = 0;
|
||||
bool friendly = false;
|
||||
std::vector<ClubCalendarMatch> matches;
|
||||
};
|
||||
std::vector<ClubCalendarDay> club_calendar(const ModernState& g, int team);
|
||||
|
||||
} // namespace bmh::modern
|
||||
Reference in new issue
Block a user