Port achievements and player rankings to the Modern UI (by Codex)
This commit is contained in:
1 parent
d419943e86
commit
96e88c9579
29 files changed
+1362
-17
No files matched your search
@@ -0,0 +1,105 @@
|
||||
// SPDX-License-Identifier: GPL-3.0-only
|
||||
#include "modern/ui/demo_career_reports.hpp"
|
||||
#include <algorithm>
|
||||
#include <array>
|
||||
#include <chrono>
|
||||
#include <set>
|
||||
|
||||
namespace bmh::modern_ui {
|
||||
namespace {
|
||||
constexpr std::array<const char *,3> titles{"Meisterschaft","DFB-Pokal","Europapokal"};
|
||||
constexpr std::array<const char *,4> league_names{"Bundesliga","2. Bundesliga","3. Bundesliga","Regionalliga"};
|
||||
constexpr std::array<std::array<int,12>,3> samples{{
|
||||
{{8,7,5,6,4,3,1,5,4,2,3,1}},{{32,1,8,16,4,8,4,8,16,8,2,1}},{{0,0,16,8,0,4,16,8,32,4,8,8}}
|
||||
}};
|
||||
constexpr std::array<int,12> tiers{4,4,3,3,2,2,1,1,1,1,1,1};
|
||||
const Json &field(const Json &j,const char *key) {
|
||||
static const Json missing;
|
||||
if(j.is_object())if(const auto i=j.find(key);i!=j.end())return *i;
|
||||
return missing;
|
||||
}
|
||||
std::string text(const Json &j,const std::string &fallback="—") {
|
||||
return j.is_string()&&!j.get_ref<const std::string&>().empty()?j.get<std::string>():fallback;
|
||||
}
|
||||
bool date(const Json &value) {
|
||||
if(!value.is_string())return false;
|
||||
const auto &s=value.get_ref<const std::string&>();
|
||||
if(s.size()!=10||s[4]!='-'||s[7]!='-')return false;
|
||||
for(size_t i=0;i<s.size();++i)if(i!=4&&i!=7&&(s[i]<'0'||s[i]>'9'))return false;
|
||||
const int year=std::stoi(s.substr(0,4));
|
||||
return year>=1900&&std::chrono::year_month_day{std::chrono::year{year},
|
||||
std::chrono::month{unsigned(std::stoi(s.substr(5,2)))},std::chrono::day{unsigned(std::stoi(s.substr(8,2)))}}.ok();
|
||||
}
|
||||
struct Context {std::string club,manager="—";int last_year=2025;};
|
||||
Context context(const Catalog &world,const Json &state) {
|
||||
Context c;
|
||||
const auto &managers=field(state,"managers");
|
||||
if(managers.is_array()&&!managers.empty()&&managers[0].is_object()) {
|
||||
c.manager=text(field(managers[0],"name"));
|
||||
const auto id=text(field(managers[0],"club"),"");
|
||||
if(!id.empty())if(const auto *club=world.find("clubs",id);club&&club->value("type",std::string("club"))=="club")c.club=id;
|
||||
}
|
||||
auto reference=field(state,"date");
|
||||
if(!date(reference))reference=world.manifest().value("start_date",Json());
|
||||
if(date(reference)) {
|
||||
const auto s=reference.get<std::string>();
|
||||
c.last_year=std::stoi(s.substr(0,4))-(std::stoi(s.substr(5,2))<7?2:1);
|
||||
}
|
||||
return c;
|
||||
}
|
||||
int index(int year) {return ((year-2015)%12+12)%12;}
|
||||
std::string season(int year) {
|
||||
const int next=(year+1)%100;
|
||||
return std::to_string(year)+"/"+(next<10?"0":"")+std::to_string(next);
|
||||
}
|
||||
std::string label(int value,int competition) {
|
||||
if(!competition)return std::to_string(value)+". Platz";
|
||||
switch(value){case 0:return "Nicht teilgenommen";case 1:return "Sieger";case 2:return "Finale";
|
||||
case 4:return "Halbfinale";case 8:return "Viertelfinale";case 16:return "Achtelfinale";default:return "1. Runde";}
|
||||
}
|
||||
Json result(int year,int competition) {
|
||||
const int i=index(year),value=samples[size_t(competition)][size_t(i)],tier=tiers[size_t(i)];
|
||||
return {{"year",year},{"season",season(year)},{"value",value},{"label",label(value,competition)},
|
||||
{"caption",competition==0?std::to_string(value)+".":value==0?"–":label(value,competition)},
|
||||
{"tier",competition==0?Json(tier):Json()},{"tier_name",competition==0?Json(league_names[size_t(tier-1)]):Json()},
|
||||
{"winner",value==1},{"fictional",true}};
|
||||
}
|
||||
Json envelope(const Context &c) {
|
||||
return {{"ok",true},{"available",!c.club.empty()},{"fictional",true},
|
||||
{"club",c.club.empty()?Json():Json(c.club)},{"manager",c.manager}};
|
||||
}
|
||||
} // namespace
|
||||
Json demo_achievements(const Catalog &world,const Json &state,int page) {
|
||||
if(page<0||page>4)return {{"ok",false},{"error","invalid_career_page"}};
|
||||
const auto c=context(world,state);auto response=envelope(c);
|
||||
const int end=c.last_year-page;
|
||||
response["page"]=page;response["first_year"]=end-7;response["last_year"]=end;
|
||||
response["range"]=season(end-7)+" – "+season(end);
|
||||
response["charts"]=Json::array();response["history"]=Json::array();
|
||||
if(c.club.empty())return response;
|
||||
for(int k=0;k<3;++k) {
|
||||
Json rows=Json::array();for(int year=end-7;year<=end;++year)rows.push_back(result(year,k));
|
||||
response["charts"].push_back({{"kind",k},{"title",titles[size_t(k)]},{"latest",result(end,k)},{"seasons",rows}});
|
||||
}
|
||||
for(int i=0;i<12;++i) {
|
||||
const int year=c.last_year-i;Json values=Json::array();
|
||||
for(int k=0;k<3;++k)values.push_back(result(year,k));
|
||||
response["history"].push_back({{"year",year},{"season",season(year)},{"results",values}});
|
||||
}
|
||||
return response;
|
||||
}
|
||||
Json demo_highscores(const Catalog &world,const Json &state) {
|
||||
const auto c=context(world,state);auto response=envelope(c);response["rows"]=Json::array();
|
||||
if(c.club.empty())return response;
|
||||
std::vector<std::string> clubs{c.club};
|
||||
for(const auto *club:world.clubs()) {
|
||||
const auto id=club->value("id","");
|
||||
if(id!=c.club&&clubs.size()<8&&club->value("type",std::string("club"))=="club")clubs.push_back(id);
|
||||
}
|
||||
constexpr std::array<const char *,8> managers{"","Chris Winter","Sam Keller","Robin Faber","Kim Seidel","Dani Koch","Mika Sommer","Jules Walter"};
|
||||
for(size_t i=0;i<clubs.size();++i)response["rows"].push_back({{"rank",i+1},
|
||||
{"manager",i?std::string(managers[i]):c.manager},{"club",clubs[i]},{"points",18200-int(i)*1575},
|
||||
{"championships",8-int(i%5)},{"cups",6-int(i%4)},{"europe",3-int(i%3)},{"fictional",true}});
|
||||
return response;
|
||||
}
|
||||
} // namespace bmh::modern_ui
|
||||
Reference in new issue
Block a user