Port planning menus and indoor tables (by Codex)

This commit is contained in:
Codex committed 2026-10-24 00:38:12 +02:00
1 parent 13461c693a
commit 9a93c4fcc8
32 files changed
+1667 -169

No files matched your search

+1 -1
View File
@@ -100,7 +100,7 @@ int main(int argc, char **argv) try {
std::ostringstream css;
for (const auto *name : {"theme.rcss", "ticket.rcss", "sports.rcss",
"calendar.rcss", "reports.rcss", "report_details.rcss",
"report_pickers.rcss"}) {
"report_pickers.rcss", "indoor.rcss", "planning.rcss"}) {
std::ifstream input(root / name);
require(bool(input), "Production stylesheet missing");
css << input.rdbuf() << '\n';
+227
View File
@@ -0,0 +1,227 @@
// SPDX-License-Identifier: GPL-3.0-only
#include "modern/ui/demo_calendar.hpp"
#include <algorithm>
#include <array>
#include <chrono>
#include <fstream>
#include <iostream>
#include <map>
#include <set>
#include <stdexcept>
using namespace bmh::modern_ui;
namespace {
void require(bool ok, const char *message) {
if (!ok) throw std::runtime_error(message);
}
std::string league(int group) { return "competition:planning-" + std::to_string(group); }
std::string club(int group, int index) {
return "club:planning-" + std::to_string(group) + "-" + std::to_string(index);
}
struct Fixture {
fs::path root;
std::shared_ptr<const Catalog> world;
explicit Fixture(int own_count = 18, bool populated = true) {
root = fs::temp_directory_path() / ("openbmh-planning-" +
std::to_string(std::chrono::steady_clock::now().time_since_epoch().count()));
fs::create_directories(root / "world");
const auto write = [&](const std::string &file, const Json &value) {
std::ofstream out(root / file); out << value.dump();
require(bool(out), "Cannot write synthetic fixture");
};
write("manifest.json", {{"format", "openbmh-world"}, {"kind", "world"},
{"format_version", "1.1"}, {"id", "world:synthetic-planning"},
{"requires", {"editor:world-v1"}},
{"x-editor", {{"profile", "editor-world-v1"}}}});
write("world/nations.json", {{"items", Json::array({
{{"id", "nation:de"}}, {{"id", "nation:foreign"}}, {{"id", "nation:empty"}}})}});
Json competitions = Json::array(), clubs = Json::array();
// Five manageable German leagues, plus foreign 20- and 18-club previews.
if (populated) for (int group = 0; group < 7; ++group) {
const auto nation = group < 5 ? "nation:de" : "nation:foreign";
competitions.push_back({{"id", league(group)}, {"nation", nation}, {"type", "league"}});
const int count = group == 0 ? own_count : group == 5 ? 20 : 18;
for (int index = 0; index < count; ++index)
clubs.push_back({{"id", club(group, index)}, {"name", "Synthetic club"},
{"nation", nation}, {"competition", league(group)}});
}
write("world/competitions.json", {{"items", competitions}});
write("world/clubs.json", {{"items", clubs}});
write("world/players.json", {{"items", Json::array()}});
world = Catalog::open(root);
}
~Fixture() { std::error_code ec; fs::remove_all(root, ec); }
};
void edit(Json &state, const Catalog &world, const Json &action,
const std::string &managed = club(0, 3)) {
const auto error = edit_demo_calendar(state, action, world, managed);
if (!error.empty()) throw std::runtime_error("Unexpected rejection: " + error + " " + action.dump());
require(valid_demo_calendar(state, world, managed), "Action produced invalid calendar");
}
void reject(Json &state, const Catalog &world, const Json &action,
const std::string &managed = club(0, 3)) {
const auto before = state;
require(!edit_demo_calendar(state, action, world, managed).empty() && state == before,
"Rejected action threw or changed state");
}
void select(Json &state, const Catalog &world, int day) {
edit(state, world, {{"kind", "select"}, {"value", day}});
}
void move(Json &state, const Catalog &world, int source, int target) {
select(state, world, source); edit(state, world, {{"kind", "postpone-source"}});
select(state, world, target); edit(state, world, {{"kind", "postpone-move"}});
}
bool same(const std::vector<CalendarFixture> &a, const std::vector<CalendarFixture> &b) {
if (a.size() != b.size()) return false;
for (size_t i = 0; i < a.size(); ++i)
if (a[i].day != b[i].day || a[i].from != b[i].from || a[i].round != b[i].round ||
a[i].home != b[i].home || a[i].away != b[i].away) return false;
return true;
}
} // namespace
int main() try {
Fixture fixture;
const auto &world = *fixture.world;
const auto immutable_clubs = world.rows("clubs"), immutable_competitions = world.rows("competitions");
auto state = demo_calendar(world, "2027-07-01", club(0, 3));
require(valid_demo_calendar(state, world, club(0, 3)) && state["selected"] == 6 &&
state["opponent"] == club(0, 0), "Default planning state is invalid");
require(calendar_days(state["start"]).size() == 366 &&
calendar_days("2099-07-01").size() == 365, "Leap-year season boundary incorrect");
require(calendar_own_league(world, club(2, 7)) == league(2) &&
calendar_own_league(world) == league(0) &&
!valid_demo_calendar(state, world, club(5, 0)), "Manager identity fallback is unsafe");
for (int group = 0; group < 7; ++group) {
auto shown = state; shown["league"] = league(group);
const auto managed = group < 5 ? club(group, 7) : club(0, 3);
const auto fixtures = calendar_fixtures(shown, world, managed);
require(fixtures.size() == size_t(group == 5 ? 38 : 34), "Wrong preview league size");
std::map<std::string, int> opponents;
const auto followed = group < 5 ? managed : club(group, 0);
for (const auto &f : fixtures) {
require((f.home == followed || f.away == followed) && f.home != f.away &&
f.from == 30 + 7 * f.round && f.day == f.from, "Incorrect stable fixture identity");
++opponents[f.home == followed ? f.away : f.home];
}
require(opponents.size() == size_t(group == 5 ? 19 : 17) &&
std::all_of(opponents.begin(), opponents.end(), [](const auto &p) { return p.second == 2; }),
"Preview does not meet every opponent twice");
}
edit(state, world, {{"kind", "friendly-opponent"}, {"value", club(0, 4)}});
require(calendar_friendly_fee(state, world) == 27000, "Fee filtered own club out of opponent index");
edit(state, world, {{"kind", "friendly-venue"}, {"value", false}});
require(calendar_friendly_fee(state, world) == 14000, "Away fee incorrect");
edit(state, world, {{"kind", "friendly-country"}, {"value", "nation:foreign"}});
require(state["friendly_league"] == league(5) && state["opponent"] == club(5, 0) &&
state["league"] == league(0), "Friendly picker overwrote league preview");
edit(state, world, {{"kind", "friendly-stage"}, {"value", 1}});
select(state, world, 7); edit(state, world, {{"kind", "friendly-book"}});
require(state["friendlies"].front() == Json({{"day", 7}, {"opponent", club(5, 0)},
{"league", league(5)}, {"home", false}}), "Booking did not retain stable identity and venue");
reject(state, world, {{"kind", "friendly-book"}});
select(state, world, 30); reject(state, world, {{"kind", "friendly-book"}});
move(state, world, 30, 8);
auto own_fixtures = calendar_fixtures(state, world, club(0, 3));
require(own_fixtures.front().from == 30 && own_fixtures.front().day == 8 &&
state["source_day"] == -1 && !calendar_day_occupied(state, world, club(0, 3), 30),
"Rescheduling lost source identity or did not free original date");
move(state, world, 37, 9); move(state, world, 8, 10);
require(state["moves"] == Json::array({{{"from", 37}, {"to", 9}}, {{"from", 30}, {"to", 10}}}),
"Repeated move failed to replace source and append to undo order");
edit(state, world, {{"kind", "postpone-undo"}});
require(calendar_fixtures(state, world, club(0, 3)).front().day == 30 &&
state["moves"].size() == 1, "Undo did not restore original round date");
move(state, world, 30, 11); select(state, world, 30);
edit(state, world, {{"kind", "friendly-book"}});
reject(state, world, {{"kind", "postpone-undo"}});
select(state, world, 11); edit(state, world, {{"kind", "postpone-source"}});
select(state, world, 7); reject(state, world, {{"kind", "postpone-move"}});
select(state, world, 44); reject(state, world, {{"kind", "postpone-move"}});
edit(state, world, {{"kind", "postpone-cancel"}});
reject(state, world, {{"kind", "postpone-move"}});
select(state, world, 365); edit(state, world, {{"kind", "friendly-book"}});
auto foreign = state; foreign["league"] = league(5);
auto foreign_base = foreign; foreign_base["moves"] = Json::array();
require(same(calendar_fixtures(foreign, world, club(0, 3)),
calendar_fixtures(foreign_base, world, club(0, 3))), "Own moves leaked into foreign preview");
require(calendar_day_occupied(foreign, world, club(0, 3), 11) &&
!calendar_day_occupied(foreign, world, club(0, 3), 30 + 7 * 35),
"Planning occupancy used selected preview league");
auto full = demo_calendar(world, "2026-07-01", club(0, 3));
for (int day = 7; full["friendlies"].size() < 20; ++day) {
if (calendar_day_occupied(full, world, club(0, 3), day)) continue;
select(full, world, day); edit(full, world, {{"kind", "friendly-book"}});
}
select(full, world, 29); reject(full, world, {{"kind", "friendly-book"}});
const std::vector<Json> bad_actions = {nullptr, Json::array(), "bad", Json::object(),
{{"kind", 1}}, {{"kind", "unknown"}}, {{"kind", "select"}},
{{"kind", "select"}, {"value", true}}, {{"kind", "select"}, {"value", 7.0}},
{{"kind", "select"}, {"value", uint64_t(-1)}},
{{"kind", "select"}, {"value", -1}}, {{"kind", "select"}, {"value", 366}},
{{"kind", "friendly-country"}, {"value", "nation:empty"}},
{{"kind", "friendly-country"}, {"value", "nation:unknown"}},
{{"kind", "friendly-league"}, {"value", "competition:unknown"}},
{{"kind", "friendly-opponent"}, {"value", club(0, 3)}},
{{"kind", "friendly-opponent"}, {"value", club(1, 0)}},
{{"kind", "friendly-opponent"}, {"value", ""}},
{{"kind", "friendly-venue"}, {"value", 1}},
{{"kind", "friendly-stage"}, {"value", 2}},
{{"kind", "friendly-stage"}, {"value", false}},
{{"kind", "postpone-cancel"}, {"value", 0}},
{{"kind", "friendly-book"}, {"unexpected", 1}}};
for (const auto &action : bad_actions) reject(state, world, action);
std::vector<Json> bad_states;
for (const auto &value : std::vector<Json>{true, 1.5, -2, 366, uint64_t(-1)}) {
auto bad = state; bad["source_day"] = value; bad_states.push_back(bad);
}
auto bad = state; bad["source_day"] = 31; bad_states.push_back(bad);
bad = state; bad["moves"].push_back(bad["moves"].front()); bad_states.push_back(bad);
bad = state; bad["moves"][0]["to"] = bad["moves"][1]["to"]; bad_states.push_back(bad);
bad = state; bad["moves"][0]["to"] = 44; bad_states.push_back(bad);
bad = state; bad["moves"][0]["from"] = 31; bad_states.push_back(bad);
bad = state; bad["moves"][0]["to"] = 7; bad_states.push_back(bad);
bad = state; bad["moves"][0]["to"] = 366; bad_states.push_back(bad);
bad = state; bad["friendlies"].push_back(bad["friendlies"].front()); bad_states.push_back(bad);
bad = state; bad["friendlies"][0]["opponent"] = club(0, 3); bad_states.push_back(bad);
bad = state; bad["friendlies"][0]["league"] = league(0); bad_states.push_back(bad);
bad = state; bad["friendlies"][0]["day"] = 44; bad_states.push_back(bad);
bad = state; bad["friendlies"][0]["home"] = 1; bad_states.push_back(bad);
bad = state; bad["friendlies"][0]["unexpected"] = true; bad_states.push_back(bad);
bad = state; bad["unexpected"] = 1; bad_states.push_back(bad);
for (auto &invalid : bad_states) {
if (valid_demo_calendar(invalid, world, club(0, 3)))
throw std::runtime_error("Corrupt schedule accepted: " + invalid.dump());
reject(invalid, world, {{"kind", "postpone-cancel"}});
const auto before = invalid;
require(!migrate_demo_calendar(invalid, world, club(0, 3)) && invalid == before,
"Migration repaired present corrupt data");
}
auto old = demo_calendar(world, "2027-07-01", club(0, 3));
for (const auto *key : {"source_day", "friendly_league", "opponent", "home", "friendly_stage"})
old.erase(key);
require(!valid_demo_calendar(old, world, club(0, 3)) &&
migrate_demo_calendar(old, world, club(0, 3)) &&
valid_demo_calendar(old, world, club(0, 3)), "Old checkpoint migration failed");
const auto defaults = demo_calendar(world, "2027-07-01", club(0, 3));
for (const auto &entry : defaults.items())
require(old.at(entry.key()) == entry.value(), "Migration changed an old field or default");
require(valid_demo_calendar(Json::parse(state.dump()), world, club(0, 3)),
"JSON checkpoint roundtrip lost valid planning data");
auto different_manager = demo_calendar(world, "2026-07-01", club(3, 7));
require(different_manager["friendly_league"] == league(3) &&
different_manager["opponent"] == club(3, 0), "Nonfirst manager adopted first career league");
Fixture tiny(1); auto one = demo_calendar(*tiny.world, "2026-07-01", club(0, 0));
require(one["opponent"] == "" && calendar_friendly_fee(one, *tiny.world) == 0,
"Single-club league invented an opponent");
require(edit_demo_calendar(one, {{"kind", "friendly-book"}}, *tiny.world, club(0, 0)) != "",
"Single-club friendly booking accepted");
Fixture empty(0, false); const auto none = demo_calendar(*empty.world, "2026-07-01");
require(valid_demo_calendar(none, *empty.world) && none["opponent"] == "" &&
calendar_fixtures(none, *empty.world, "").empty(), "Empty world preview is unsafe");
require(world.rows("clubs") == immutable_clubs && world.rows("competitions") == immutable_competitions,
"Planning changed immutable world data");
std::cout << "Modern planning checks passed\n";
return 0;
} catch (const std::exception &error) {
std::cerr << error.what() << '\n'; return 1;
}
+84 -1
View File
@@ -86,6 +86,84 @@ std::map<std::string, Json> by_club(const Json &rows) {
for (const auto &r : rows) result.emplace(r.at("club").get<std::string>(), r);
return result;
}
void check_indoor(const Catalog &world) {
auto state = demo_reports(world);
require(state["sorts"]["indoor"] == Json({{"key", "points"}, {"desc", true}}),
"Indoor default sort must be points descending");
const auto ranked = report_indoor_rows(world, state);
require(ranked.size() == 16, "Indoor preview must cap the first German league at16 clubs");
for (size_t i = 0; i < ranked.size(); ++i)
require(ranked[i]["club"] == club(1, int(i)) && ranked[i]["rank"] == i + 1 &&
ranked[i]["original"] == i && ranked[i]["points"] == 324 - int(i) * 19,
"Indoor projection changed catalog IDs, ranks or Gold points");
edit(state, {{"kind", "league"}, {"value", league(0)}}, world);
edit(state, {{"kind", "filter"}, {"value", 4}}, world);
edit(state, {{"kind", "competition"}, {"value", 8}}, world);
state["round"] = 37;
require(report_indoor_rows(world, state) == ranked,
"Indoor preview must ignore selected foreign league, round and report filters");
const auto table_sort = state["sorts"]["tables"];
const auto strength_sort = state["sorts"]["strengths"];
edit(state, {{"kind", "sort"}, {"view", "indoor"}, {"key", "points"}}, world);
auto reversed = ranked;
std::reverse(reversed.begin(), reversed.end());
require(state["sorts"]["indoor"]["desc"] == false &&
report_indoor_rows(world, state) == reversed,
"Indoor points toggle must reverse numeric order without renumbering places");
edit(state, {{"kind", "sort"}, {"view", "indoor"}, {"key", "rank"}}, world);
require(state["sorts"]["indoor"]["desc"] == false && report_indoor_rows(world, state) == ranked,
"Indoor rank selection must start ascending");
edit(state, {{"kind", "sort"}, {"view", "indoor"}, {"key", "rank"}}, world);
require(state["sorts"]["indoor"]["desc"] == true && report_indoor_rows(world, state) == reversed,
"Indoor rank toggle must descend");
edit(state, {{"kind", "sort"}, {"view", "indoor"}, {"key", "original"}}, world);
require(state["sorts"]["indoor"]["desc"] == false && report_indoor_rows(world, state) == ranked,
"Indoor original order must follow catalog order");
edit(state, {{"kind", "sort"}, {"view", "indoor"}, {"key", "original"}}, world);
require(report_indoor_rows(world, state) == reversed,
"Indoor original order must honor its direction");
require(state["sorts"]["tables"] == table_sort && state["sorts"]["strengths"] == strength_sort,
"Indoor sorting must not alter other report sorts");
const auto indoor_sort = state["sorts"]["indoor"];
edit(state, {{"kind", "reset"}}, world);
require(state["sorts"]["indoor"] == indoor_sort,
"Table reset must retain independent indoor sort");
const auto current = state;
require(migrate_demo_reports(state, world) && state == current,
"Current reports migration must be idempotent");
auto legacy = current;
legacy["sorts"].erase("indoor");
require(!valid_demo_reports(legacy, world), "Missing indoor sort must require migration");
require(migrate_demo_reports(legacy, world) && valid_demo_reports(legacy, world),
"Legacy reports did not acquire indoor default");
auto expected = current;
expected["sorts"]["indoor"] = {{"key", "points"}, {"desc", true}};
require(legacy == expected, "Migration altered existing report choices");
auto legacy_invalid = legacy;
legacy_invalid["sorts"].erase("indoor");
legacy_invalid["sorts"]["tables"]["desc"] = 1;
const std::vector<Json> invalid = {
nullptr, Json::array(), Json{{"sorts", Json::object()}}, legacy_invalid,
[&] { auto v = legacy; v["sorts"]["indoor"] = nullptr; return v; }(),
[&] { auto v = legacy; v["sorts"]["indoor"]["key"] = "club"; return v; }(),
[&] { auto v = legacy; v["sorts"]["indoor"]["desc"] = "false"; return v; }(),
[&] { auto v = legacy; v["sorts"]["indoor"]["extra"] = 1; return v; }(),
[&] { auto v = legacy; v["sorts"]["indoor"] = Json::array(); return v; }(),
[&] { auto v = legacy; v["sorts"].erase("indoor"); v["round"] = -1; return v; }(),
[&] { auto v = legacy; v["sorts"].erase("indoor"); v["sorts"]["extra"] = 1; return v; }()};
for (auto value : invalid) {
const auto before = value;
require(!valid_demo_reports(value, world) && !migrate_demo_reports(value, world) && value == before &&
report_indoor_rows(world, value).empty(),
"Malformed indoor/legacy reports were repaired or partially mutated");
}
for (const auto &key : {"club", "strength", "name"}) {
const auto before = state;
require(!edit_demo_reports(state, {{"kind", "sort"}, {"view", "indoor"}, {"key", key}}, world).empty() &&
state == before, "Invalid indoor sort mutated reports");
}
}
void check_league(const Catalog &world, const std::string &id, int count) {
auto state = demo_reports(world); edit(state, {{"kind", "league"}, {"value", id}}, world);
const int rounds = report_round_count(world, state);
@@ -169,6 +247,7 @@ int main() try {
auto state = demo_reports(world);
require(valid_demo_reports(state, world) && state.at("league") == league(1) &&
state.at("round") == 26, "Default state did not choose first German league");
check_indoor(world);
check_league(world, league(1), 18); check_league(world, league(0), 20);
auto first = state; first["round"] = 0;
const auto first_match = report_match_rows(world, first, false).front();
@@ -295,13 +374,17 @@ int main() try {
for (const auto &v : bad_states) require(!valid_demo_reports(v, world), "Malformed reports state accepted");
Fixture tiny(2); auto small = demo_reports(*tiny.world);
require(small["round"] == 1, "Default round did not clamp for two clubs");
const auto tiny_indoor = report_indoor_rows(*tiny.world, small);
require(tiny_indoor.size() == 2 && tiny_indoor.back()["points"] == 305,
"Indoor preview invented clubs in a tiny German league");
edit(small, {{"kind", "competition"}, {"value", 1}}, *tiny.world); small["round"] = 33;
require(report_match_rows(*tiny.world, small, false)[0]["round"] == 1, "Local league projection did not clamp shared round");
edit(small, {{"kind", "competition"}, {"value", 0}}, *tiny.world);
require(small["round"] == 1, "Switching back to league clamped to the wrong round");
Fixture empty(0, false); const auto none = demo_reports(*empty.world);
require(valid_demo_reports(none, *empty.world) && report_round_count(*empty.world, none) == 1 &&
report_table_rows(*empty.world, none).empty() && report_opening_pairs(*empty.world).empty(),
report_table_rows(*empty.world, none).empty() && report_opening_pairs(*empty.world).empty() &&
report_indoor_rows(*empty.world, none).empty(),
"Empty presentation catalogue is unsafe");
require(world.rows("clubs") == immutable_clubs && world.rows("players") == immutable_players,
"Report projection mutated immutable world data");
+45
View File
@@ -372,6 +372,51 @@ int main(int argc, char **argv) try {
{{"kind", "league"}, {"value", "missing"}}}).ok &&
restored_reports.snapshot().state == accepted_reports.state,
"Rejected report load/action partially mutated the session");
const auto planning_before = restored_reports.snapshot();
const auto plan_action = [&](const Json &change) {
return restored_reports.apply({restored_reports.snapshot().revision, "calendar", change}).ok;
};
require(plan_action({{"kind", "friendly-league"}, {"value", "comp:other"}}) &&
plan_action({{"kind", "friendly-opponent"}, {"value", "club:b"}}) &&
plan_action({{"kind", "friendly-venue"}, {"value", true}}) &&
plan_action({{"kind", "friendly-stage"}, {"value", 1}}) &&
plan_action({{"kind", "select"}, {"value", 7}}) &&
plan_action({{"kind", "friendly-book"}}),
"Planning through the shared source failed");
require(restored_reports.snapshot().state["cash"] == planning_before.state["cash"] &&
restored_reports.snapshot().state["managers"] == planning_before.state["managers"] &&
restored_reports.snapshot().state["training"] == planning_before.state["training"],
"Presentation planning mutated finances, managers or training");
require(plan_action({{"kind", "select"}, {"value", 30}}) &&
plan_action({{"kind", "postpone-source"}}) &&
plan_action({{"kind", "select"}, {"value", 8}}) &&
plan_action({{"kind", "postpone-move"}}),
"Rescheduling used the foreign preview instead of the manager schedule");
restored_reports.save(root / "planning.ui.json", default_desktop());
DemoSource restored_planning(calendar_world);
restored_planning.load(root / "planning.ui.json");
require(restored_planning.snapshot().state == restored_reports.snapshot().state,
"Bookings or moves were lost on source round-trip");
const auto planning_accepted = restored_planning.snapshot();
auto colliding_plan = Json::parse(bmh::editor::read_file(root / "planning.ui.json"));
colliding_plan["calendar"]["friendlies"][0]["day"] = 8;
write(root, "colliding-plan.ui.json", colliding_plan);
rejected = false;
try { restored_planning.load(root / "colliding-plan.ui.json"); }
catch (const std::exception &) { rejected = true; }
require(rejected && restored_planning.snapshot().state == planning_accepted.state &&
restored_planning.snapshot().revision == planning_accepted.revision,
"Conflicting plan partially loaded");
auto old_planning = Json::parse(bmh::editor::read_file(root / "calendar.ui.json"));
for (const auto *key : {"source_day", "friendly_league", "opponent", "home", "friendly_stage"})
old_planning["calendar"].erase(key);
old_planning["reports"]["sorts"].erase("indoor");
write(root, "old-planning.ui.json", old_planning);
restored_planning.load(root / "old-planning.ui.json");
require(restored_planning.snapshot().state["calendar"]["league"] == "comp:other" &&
restored_planning.snapshot().state["calendar"]["friendly_league"] == "comp:home" &&
restored_planning.snapshot().state["reports"]["sorts"]["indoor"]["key"] == "points",
"Old checkpoint lost league preview or failed planning/indoor migration");
GuiAssets absent(root / "missing-gui");
require(!absent.available(),
"World resources were mistaken for GUI resources");