// SPDX-License-Identifier: GPL-3.0-only // Developer probe: advances a Modern career through the UI command path and writes the // engine's kickoff recording, post-match read and line-up read as JSON files. // usage: modern_live_dump [kickoffs] #include "modern/ui/engine_bridge.hpp" #include #include #include using bmh::modern_ui::EngineBridge; using bmh::modern_ui::Json; namespace fs = std::filesystem; int main(int argc, char **argv) { if (argc < 4) { std::cerr << "usage: modern_live_dump [kickoffs]\n"; return 2; } try { const fs::path out = argv[3]; fs::create_directories(out); const int wanted = argc > 4 ? std::atoi(argv[4]) : 1; auto link = EngineBridge::open(argv[1], argv[2]); const auto write = [&](const std::string &name, const Json &value) { std::ofstream(out / name, std::ios::binary) << value.dump(1); }; int found = 0, decisions = 0; const bool live = std::getenv("MODERN_DUMP_LIVE") != nullptr, steps = std::getenv("MODERN_DUMP_STEPS") != nullptr, quiet = std::getenv("MODERN_DUMP_QUIET") != nullptr; Json expiry = Json::object(); size_t postponed = 0; // MODERN_DUMP_SET="P5=0,J8=1": game options committed before the first kickoff if (const char *wanted_settings = std::getenv("MODERN_DUMP_SET")) { Json edits = Json::array(); std::string text = wanted_settings; for (size_t at = 0; at < text.size();) { const size_t end = std::min(text.find(',', at), text.size()); const std::string item = text.substr(at, end - at); const size_t eq = item.find('='); if (eq != std::string::npos) edits.push_back({{"code", item.substr(0, eq)}, {"value", std::stoi(item.substr(eq + 1))}}); at = end + 1; } const auto bundle = link->snapshot(); const auto &meta = bundle.at("desktop").at("meta"); const Json commit = {{"messageType", "command"}, {"contractVersion", meta.at("contractVersion")}, {"requestId", "request:dump-settings"}, {"careerId", meta.at("careerId")}, {"expectedRevision", meta.at("stateRevision")}, {"owner", {{"managerId", meta.at("scope").at("managerId")}}}, {"kind", "settings.commit"}, {"payload", {{"edits", edits}}}}; const auto answer = link->command(commit); std::cout << "settings " << edits.dump() << ": " << answer.value("messageType", "?") << " " << answer.value("code", "") << " " << (answer.contains("message") && answer.at("message").is_object() ? answer.at("message").value("text", std::string()) : std::string()) << std::endl; } for (int step = 0; step < 400 && found < wanted; ++step) { const auto bundle = link->snapshot(); const auto &meta = bundle.at("desktop").at("meta"); if (step == 0) write("bundle.json", bundle); { // the desk before each kickoff: its date and the fixture it announces const auto &next = bundle.at("desktop").at("data").at("nextFixture"); std::cout << "desk " << meta.at("date").get() << ": next " << (next.is_object() ? next.at("home").at("name").get() + " - " + next.at("away").at("name").get() + " (" + next.at("competition").value("text", std::string()) + ")" : std::string("none")) << std::endl; } const Json command = {{"messageType", "command"}, {"contractVersion", meta.at("contractVersion")}, {"requestId", "request:dump-" + std::to_string(step)}, {"careerId", meta.at("careerId")}, {"expectedRevision", meta.at("stateRevision")}, {"owner", {{"managerId", meta.at("scope").at("managerId")}}}, {"kind", "session.advance"}, {"payload", live ? Json{{"contractExpiry", expiry}, {"live", true}} : Json{{"contractExpiry", expiry}}}}; if (std::getenv("MODERN_DUMP_REHEARSE")) { // as the interface does: a rehearsal that is taken back, then the kickoff itself Json rehearsal = command; rehearsal["requestId"] = "request:dump-rehearsal-" + std::to_string(step); rehearsal["payload"] = {{"contractExpiry", expiry}, {"dryRun", true}}; const auto tried = link->command(rehearsal); const auto after = link->snapshot(); if (after.at("desktop") != bundle.at("desktop")) std::cout << "REHEARSAL CHANGED THE DESK at step " << step << std::endl; if (after != bundle) std::cout << "rehearsal changed a read at step " << step << std::endl; } auto reply = link->command(command); write("advance-" + std::to_string(step) + ".json", reply); if (reply.value("messageType", "") != "command_result") { if (reply.value("code", "") == "decision_pending") { const auto &args = reply.at("message").at("args"); const auto player = args.at("playerId").get(); // extend every second expiring contract at the demand for two years, release the others const auto demands = args.value("demands", Json::array()); const bool renew = ++decisions % 2 == 1 && demands.size() > 1 && demands[1].is_object(); std::cout << "step " << step << ": contract of " << player << " runs out, demands " << demands.dump() << (renew ? " -> renew" : " -> release") << std::endl; if (renew) expiry[player] = {{"action", "renew"}, {"years", 2}, {"monthlySalary", demands[1]}}; else expiry[player] = {{"action", "release"}}; continue; } std::cerr << "advance refused: " << reply.dump() << "\n"; return 1; } // MODERN_DUMP_LIVE=1: the kickoff is played live and finished with live.finish auto fixture = link->read_live({{"kind", "fixture"}}); for (int guard = 0; live && fixture.value("ok", false) && fixture.value("live", false) && guard < 300; ++guard) { const Json finish = {{"messageType", "command"}, {"contractVersion", meta.at("contractVersion")}, {"requestId", "request:dump-live-" + std::to_string(step) + "-" + std::to_string(guard)}, {"careerId", meta.at("careerId")}, {"expectedRevision", reply.at("newRevision")}, {"owner", {{"managerId", meta.at("scope").at("managerId")}}}, {"kind", steps ? "live.step" : "live.finish"}, {"payload", steps ? Json{{"minutes", 1}} : Json::object()}}; reply = link->command(finish); if (reply.value("messageType", "") != "command_result") { std::cerr << "live refused: " << reply.dump() << "\n"; return 1; } fixture = link->read_live({{"kind", "fixture"}}); } std::cout << "step " << step << ": " << (fixture.value("ok", false) ? "fixture" : fixture.value("error", "?")); if (fixture.value("ok", false) && fixture.contains("matches")) for (const auto &m : fixture.at("matches")) std::cout << " | " << fixture.value("date", std::string()) << " " << m.at("home").at("name").get() << " - " << m.at("away").at("name").get() << " (" << m.value("competition", std::string()) << ")"; std::cout << std::endl; if (fixture.value("ok", false)) { // every match of the kickoff that was a replay of a postponed one const std::string all = fixture.dump(); for (size_t at = all.find("Verlegte Spiele"); at != std::string::npos; at = all.find("Verlegte Spiele", at + 1)) ++postponed; } if (quiet) { if (fixture.value("ok", false)) ++found; continue; } if (!fixture.value("ok", false)) continue; const std::string tag = std::to_string(found++); write("fixture-" + tag + ".json", fixture); write("postmatch-" + tag + ".json", link->read({{"kind", "postmatch"}})); write("lineup-" + tag + ".json", link->read({{"kind", "lineup"}})); write("tactics-" + tag + ".json", link->read({{"kind", "tactics"}})); } { const auto news = link->read({{"kind", "news"}}); size_t messages = 0, transfers = 0; if (news.contains("data") && news.at("data").contains("messages")) for (const auto &m : news.at("data").at("messages")) { ++messages; const std::string text = m.dump(); if (text.find("Transfer") != std::string::npos || text.find("wechselt") != std::string::npos || text.find("verpflichtet") != std::string::npos) ++transfers; } const auto desk = link->snapshot().at("desktop").at("meta"); std::cout << "summary: date " << desk.at("date").get() << ", mentions of replayed postponed matches " << postponed << ", mailbox messages " << messages << ", of them about transfers " << transfers << std::endl; } std::cout << found << " recordings written\n"; return found ? 0 : 1; } catch (const std::exception &e) { std::cerr << e.what() << "\n"; return 1; } }