Bind season calendar and confirmed fixture rescheduling to engine (by Codex)
This commit is contained in:
1 parent
7e170a00c7
commit
6119d7f21e
17 files changed
+388
-16
No files matched your search
@@ -5,6 +5,8 @@
|
||||
#include <charconv>
|
||||
#include <set>
|
||||
#include <limits>
|
||||
#include <chrono>
|
||||
#include "modern/ui/demo_calendar.hpp"
|
||||
|
||||
namespace bmh::modern_ui {
|
||||
namespace {
|
||||
@@ -569,4 +571,92 @@ Json engine_contract_cancel(const Json &bundle, const std::string &id, const std
|
||||
require(q.is_object() && q.at("id") == id, "Diese Verhandlung ist nicht mehr geöffnet.");
|
||||
return sports_command(bundle, "contract.cancel", {{"quoteId", id}}, request_id);
|
||||
}
|
||||
|
||||
namespace {
|
||||
std::chrono::sys_days calendar_iso_day(const std::string &s) {
|
||||
require(s.size() == 10 && s[4] == '-' && s[7] == '-', "Ungültiges Kalenderdatum.");
|
||||
for (size_t i = 0; i < s.size(); ++i) if (i != 4 && i != 7) require(s[i] >= '0' && s[i] <= '9', "Ungültige Datumsziffer.");
|
||||
const auto d = std::chrono::year_month_day{std::chrono::year(std::stoi(s.substr(0,4))), std::chrono::month(unsigned(std::stoi(s.substr(5,2)))), std::chrono::day(unsigned(std::stoi(s.substr(8,2))))};
|
||||
require(d.ok(), "Ungültiger Kalendertag."); return std::chrono::sys_days{d};
|
||||
}
|
||||
int calendar_index(const Json &view, const Json &date) {
|
||||
require(date.is_string(), "Kalenderdatum fehlt.");
|
||||
const auto index = (calendar_iso_day(date.get<std::string>()) - calendar_iso_day(view.at("start"))).count();
|
||||
require(index >= 0 && index < view.at("count").get<int>(), "Datum liegt außerhalb der angezeigten Saison."); return int(index);
|
||||
}
|
||||
bool schedule_permission(const Json &bundle) {
|
||||
const auto commands = bundle.at("desktop").at("meta").at("scope").value("allowedCommands", Json::array());
|
||||
return commands.is_array() && std::find(commands.begin(),commands.end(),Json("fixture.reschedule")) != commands.end();
|
||||
}
|
||||
}
|
||||
Json engine_calendar_projection(const Json &bundle) {
|
||||
validate_engine_desk(bundle);
|
||||
const auto &meta = bundle.at("desktop").at("meta"), &data = lineup_read(bundle, "calendar");
|
||||
require(meta.at("scope").at("role") == "club", "Der Vereinskalender ist am National-Schreibtisch nicht verfügbar.");
|
||||
const auto start = meta.at("season").at("startDate").get<std::string>(), end = meta.at("season").at("endDate").get<std::string>();
|
||||
const auto days = calendar_days(start); // Gregorian geometry only; no demo fixtures or date rules.
|
||||
require(!days.empty() && days.back().iso == end && data.at("from") == start && data.at("until") == end, "Unvollständiger Saisonkalender.");
|
||||
Json view = {{"start",start},{"end",end},{"count",days.size()},{"events",Json::object()},{"fixtures",Json::array()},{"league",nullptr}};
|
||||
const auto today = (calendar_iso_day(meta.at("date")) - calendar_iso_day(start)).count(); view["today"] = today >= 0 && today < int(days.size()) ? Json(today) : Json();
|
||||
require(data.at("fixtures").is_array() && data.at("days").is_array(), "Kalenderdaten fehlen.");
|
||||
const auto own = meta.at("scope").at("teamId"); std::map<std::string,Json> fixtures; std::set<std::string> leagues;
|
||||
for (const auto &f : data.at("fixtures")) {
|
||||
const auto id = f.at("id").get<std::string>(); require(!id.empty() && !fixtures.contains(id), "Doppelte Kalenderpartie.");
|
||||
require(f.at("home").at("id") == own || f.at("away").at("id") == own, "Kalenderpartie gehört einem anderen Verein.");
|
||||
auto row = f; row["day"] = calendar_index(view,f.at("scheduledDate")); (void)calendar_index(view,f.at("originalDate"));
|
||||
(void)whole_in_range(f.at("round"),1,10000);
|
||||
const auto status=f.at("status").get<std::string>();require(status=="scheduled"||status=="played"||status=="rescheduled"||status=="postponed"||status=="void","Unbekannter Spielstatus.");
|
||||
if(!f.at("result").is_null()) {require(f.at("result").is_array()&&f.at("result").size()==2,"Ungültiges Ergebnis.");for(const auto &v:f.at("result"))(void)whole_in_range(v,0,10000);}
|
||||
if(f.at("competition").value("kind","")=="league")leagues.insert(f.at("competition").at("id").get<std::string>());
|
||||
fixtures.emplace(id,row); view["fixtures"].push_back(row);
|
||||
}
|
||||
if(leagues.size()==1)view["league"]=*leagues.begin();
|
||||
std::set<int> seen;
|
||||
for(const auto &raw:data.at("days")) {
|
||||
const int index=calendar_index(view,raw.at("date"));require(seen.insert(index).second,"Doppelter Kalendertag.");
|
||||
(void)whole_in_range(raw.at("seasonDay"),0,366);
|
||||
require(raw.at("eventKinds").is_array()&&raw.at("fixtureIds").is_array(),"Kalenderereignisse fehlen.");
|
||||
std::set<std::string> ids;
|
||||
for(const auto &id:raw.at("fixtureIds")) {require(id.is_string()&&fixtures.contains(id.get<std::string>())&&ids.insert(id.get<std::string>()).second,"Unbekannte oder doppelte Kalenderreferenz.");require(fixtures.at(id.get<std::string>()).at("day")==index,"Partie steht am falschen Datum.");}
|
||||
for(const auto &kind:raw.at("eventKinds"))require(kind.is_string(),"Ungültiger Kalendertermin.");
|
||||
view["events"][std::to_string(index)]={{"kinds",raw.at("eventKinds")},{"ids",raw.at("fixtureIds")}};
|
||||
}
|
||||
for(const auto &[id,f]:fixtures) {
|
||||
const auto key=std::to_string(f.at("day").get<int>());
|
||||
require(view.at("events").contains(key),"Partie ohne Kalendertag.");
|
||||
const auto &ids=view.at("events").at(key).at("ids");require(std::find(ids.begin(),ids.end(),Json(id))!=ids.end(),"Partie fehlt in den Tagesreferenzen.");
|
||||
}
|
||||
return view;
|
||||
}
|
||||
Json engine_scheduling_projection(const Json &bundle) {
|
||||
const auto calendar=engine_calendar_projection(bundle);const auto &data=lineup_read(bundle,"scheduling"),&own=bundle.at("desktop").at("meta").at("scope").at("teamId");
|
||||
require(data.at("movable").is_array()&&data.at("moved").is_array(),"Verlegungsdaten fehlen.");
|
||||
Json result={{"movable",Json::array()},{"moved",Json::array()},{"allowed",schedule_permission(bundle)}};std::set<std::string> ids;
|
||||
for(const auto *key:{"movable","moved"})for(const auto &raw:data.at(key)) {
|
||||
auto row=raw;row["day"]=calendar_index(calendar,raw.at("date"));
|
||||
require(raw.at("home").at("id")==own||raw.at("away").at("id")==own,"Verlegung gehört einem anderen Verein.");
|
||||
(void)whole_in_range(raw.at("round"),1,10000);
|
||||
bool linked=false;for(const auto &f:calendar.at("fixtures"))linked|=f.at("day")==row.at("day")&&f.at("round")==row.at("round")&&f.at("home").at("id")==row.at("home").at("id")&&f.at("away").at("id")==row.at("away").at("id");
|
||||
require(linked,"Verlegung ohne passende Kalenderpartie.");
|
||||
if(std::string(key)=="movable") {
|
||||
require(raw.at("id").is_string()&&!raw.at("id").get<std::string>().empty()&&ids.insert(raw.at("id").get<std::string>()).second,"Doppelte Verlegungs-ID.");
|
||||
require(raw.at("targetDates").is_array(),"Zieltermine fehlen.");row["targets"]=Json::array();std::set<int> targets;
|
||||
for(const auto &date:raw.at("targetDates")) {const int d=calendar_index(calendar,date);require(targets.insert(d).second&&d!=row.at("day"),"Doppelter oder unveränderter Zieltermin.");row["targets"].push_back(d);}
|
||||
}
|
||||
result[key].push_back(row);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
Json engine_reschedule_command(const Json &bundle,const std::string &match,const std::string &date,const std::string &request_id) {
|
||||
const auto view=engine_scheduling_projection(bundle);bool found=false;
|
||||
for(const auto &row:view.at("movable"))if(row.at("id")==match)for(const auto &target:row.at("targetDates"))found|=target==date;
|
||||
require(found,"Partie oder Zieltermin ist nicht mehr verfügbar.");
|
||||
return sports_command(bundle,"fixture.reschedule",{{"matchId",match},{"date",date}},request_id);
|
||||
}
|
||||
Json engine_reschedule_undo(const Json &bundle,const std::string &date,const std::string &request_id) {
|
||||
const auto view=engine_scheduling_projection(bundle);bool found=false;
|
||||
for(const auto &row:view.at("moved"))found|=row.at("date")==date;
|
||||
require(found,"An diesem Termin ist keine Rücknahme verfügbar.");
|
||||
return sports_command(bundle,"fixture.reschedule",{{"action","undo"},{"date",date}},request_id);
|
||||
}
|
||||
} // namespace bmh::modern_ui
|
||||
Reference in new issue
Block a user