Bind native statistics to recorded careers and correct cup winners (by Codex)

This commit is contained in:
Codex committed 2026-10-29 03:35:43 +01:00
1 parent e3ea03a05d
commit eb140cdb1c
24 files changed
+296 -33

No files matched your search

+40
View File
@@ -0,0 +1,40 @@
// SPDX-License-Identifier: GPL-3.0-only
#include "modern/ui/engine_bridge.hpp"
#include <algorithm>
#include <array>
#include <cmath>
namespace bmh::modern_ui {
namespace {
void check(bool condition,const char *message){if(!condition)throw editor::Error(message);}
}
Json engine_statistics_projection(const Json &bundle,const Json &read) {
validate_engine_desk(bundle);
check(read.value("messageType","")=="snapshot"&&read.at("data").value("kind","")=="statistics","Diese Statistik ist derzeit nicht verfügbar.");
for(const auto *key:{"contractVersion","careerId","worldId","worldRevision","stateRevision","scope"})
check(read.at("meta").at(key)==bundle.at("desktop").at("meta").at(key),"Der Statistikkontext hat sich geändert.");
auto data=read.at("data").at("report");
check(data.value("ok",false)&&data.at("fictional")==false,"Eine echte Statistikprojektion fehlt.");
check(data.at("section").is_number_integer()&&data.at("section").get<int>()>=0&&data.at("section").get<int>()<=8&&data.at("available").is_boolean(),"Ungültige Statistiksektion.");
check(data.at("columns").is_array()&&data.at("rows").is_array()&&data.at("series").is_array(),"Statistikreihen fehlen.");
for(const auto &row:data.at("rows")) {
check(row.at("cells").is_array()&&row.at("cells").size()==data.at("columns").size(),"Unvollständige Statistikzeile.");
for(const auto &cell:row.at("cells"))check(cell.at("text").is_string(),"Statistiktext fehlt.");
}
// Geometry is presentation only. Every point retains its saved source value.
constexpr std::array<const char*,6> colors{"#e3da6a","#79b5c8","#a9d67b","#e58a66","#ded9bd","#b295cd"};
check(data.at("series").size()<=colors.size(),"Zu viele Statistikreihen.");
size_t index=0;
for(auto &series:data["series"]) {
const auto &values=series.at("values");check(values.is_array()&&values.size()<=1024&&series.at("active").is_boolean(),"Ungültige Statistikwerte.");
double low=0,high=0;bool first=true;
for(const auto &v:values){check(v.is_number_integer(),"Ein Statistikwert ist keine Ganzzahl.");const auto n=v.get<double>();check(std::isfinite(n),"Ungültiger Statistikwert.");if(first){low=high=n;first=false;}else{low=std::min(low,n);high=std::max(high,n);}}
if(data.at("absolute")==true){check(series.at("absoluteMin").is_number_integer()&&series.at("absoluteMax").is_number_integer(),"Die absolute Skala fehlt.");low=series.at("absoluteMin").get<double>();high=series.at("absoluteMax").get<double>();}
Json points=Json::array();const double range=std::abs(high-low);
for(size_t at=0;at<values.size();++at){double value=values[at].get<double>();if(value<low)value=low;if(high<value)value=high;const double fraction=range?std::clamp(std::abs(value-low)/range,0.,1.):.5;const double x=50.+double(at)*3280./double(std::max(size_t(1),values.size()-1));const double y=index==0?50.+680.*fraction:730.-680.*fraction;points.push_back({x,y});}
series["points"]=std::move(points);series["color"]=colors[index++];
}
// A trophy can open only when the archive actually names a winner.
if(data.at("section")==7)for(size_t column=0;column<data["columns"].size();++column){auto &heading=data["columns"][column];if(heading.value("trophy","").empty())continue;for(const auto &row:data["rows"]){const auto &cell=row.at("cells")[column];if(cell.contains("club")&&cell.at("club").is_string()){heading["winner"]=cell.at("club");heading["season"]=row.at("cells")[0].at("text");break;}}}
return data;
}
} // namespace bmh::modern_ui