Files
Dreierpack-Manager/tests/modern_rankings_read_tests.cpp
T

38 lines
4.5 KiB
C++

// SPDX-License-Identifier: GPL-3.0-only
// Authored archive events; no game data or assets.
#include "modern/content/statistics.hpp"
#include "modern/content/ui_contract.hpp"
#include "game/schedule.hpp"
#include <iostream>
#include <stdexcept>
using namespace bmh;
namespace {
using J=cli::Json;using O=J::Object;
const J& at(const J& j,const char* k){return j.object()->at(k);}
int n(const J& j){return int(*j.integer());}
void check(bool yes,const char* why){if(!yes)throw std::runtime_error(why);}
}
int main() try {
modern::ModernState g;g.manager_teams={0};g.manager_records.resize(0xb0c);g.display_start_year=2026;
for(int l=0;l<kLeagueCount;++l){g.world_league_id.push_back("league:"+std::to_string(l));g.world_league_name.push_back("Synthetic league "+std::to_string(l));}
for(int t=0;t<modern::league_clubs();++t){g.world_club_id.push_back("club:"+std::to_string(t));g.world_club_name.push_back("Synthetic club "+std::to_string(t));}
auto &s=g.almanac.current;s.season=0;s.start_year=2026;s.display_start_year=2026;s.clubs={{"club:0","Club A"},{"club:1","Club B"}};s.players={{"player:a","Alpha","One",""},{"player:b","Beta","Two",""},{"player:c","Gamma","Three",""}};s.leagues={{0,"league:0","Synthetic league 0"}};
modern::AlmanacMatch m;m.type=0;m.league=0;m.home=0;m.away=1;m.home_goals=1;m.date=100;
m.lineup[0].assign(12,modern::kAlmanacNone);m.lineup[0][0]=0;m.lineup[0][11]=1;m.lineup[1]={2};
using K=modern::AlmanacEventKind;
m.events={{K::Substitution,45,0,0,1,modern::kAlmanacNone},{K::Goal,60,0,0,0,1},{K::YellowCard,70,0,0,0,modern::kAlmanacNone},{K::SecondYellow,80,0,0,0,modern::kAlmanacNone},{K::RedCard,85,1,0,2,modern::kAlmanacNone},{K::ShootoutKick,121,0,1,0,modern::kAlmanacNone}};
s.matches.push_back(m);
auto second=m;second.home=1;second.away=0;second.date=130;second.home_goals=1;second.away_goals=1;second.lineup[0]={0,2};second.lineup[1]={1};second.events={{K::Goal,50,0,0,0,2},{K::Goal,55,1,0,1,modern::kAlmanacNone}};s.matches.push_back(second);
auto cup=m;cup.type=1;cup.league=0xff;s.matches.push_back(cup);
const auto read=[&](int scope){return modern::query_rankings(g,0,O{{"kind","player-rankings"},{"leagueId","league:0"},{"scope",scope}});};
const auto all=read(0);const auto &cats=*at(all,"categories").array();check(cats.size()==4&&cats[0].array()->size()==2,"rankings categories missing");
const auto &leader=cats[0].array()->at(0);check(*at(at(leader,"player"),"id").string()=="player:a"&&n(at(leader,"value"))==2,"goals include cup/shootout or lost league goal");check(*at(at(leader,"club"),"id").string()=="club:1","last recorded club not retained");check(n(at(leader,"appearances"))==2,"appearances changed");check(n(at(cats[1].array()->at(0),"value"))==1,"assist missing");check(cats[3].array()->size()==2&&n(at(cats[3].array()->at(0),"rank"))==1&&n(at(cats[3].array()->at(1),"rank"))==1,"red plus second-yellow tied rank changed");
const auto own=read(1);const auto &own_goals=*at(own,"categories").array()->at(0).array();check(own_goals.size()==2&&n(at(own_goals[0],"value"))==1&&n(at(own_goals[1],"value"))==1,"own scope counted other club contributions");for(const auto &r:own_goals)check(*at(at(r,"club"),"id").string()=="club:0","own scope has foreign club");
const auto fair=modern::query_rankings(g,0,O{{"kind","fairplay"}});check(n(at(fair,"recordedMatches"))==2,"fairplay counted cup");const auto &rows=*at(fair,"rows").array();check(rows.size()==size_t(kLeagueSizes[0]),"zero-card clubs missing");int total=0;for(const auto &r:rows)total+=n(at(r,"points"));check(total==9,"fairplay weighting or card side changed");check(n(at(rows.back(),"yellow"))==1&&n(at(rows.back(),"red"))==1&&n(at(rows.back(),"points"))==5,"second yellow counted incorrectly");
s.start_year=2025;const auto empty=read(0);for(const auto &cat:*at(empty,"categories").array())check(cat.array()->empty(),"previous season counted as current");s.start_year=2026;
check(!std::get<bool>(at(read(2),"ok").value),"invalid scope accepted");
modern::UiContext context;context.manager=0;context.state_revision=9;const auto wrapped=modern::ui_read(g,context,O{{"kind","player-rankings"},{"leagueId","league:0"},{"scope",0}});check(*at(wrapped,"messageType").string()=="snapshot"&&*at(at(wrapped,"meta"),"stateRevision").string()=="9","runtime envelope missing");
check(s.matches.size()==3&&s.matches[0].events.size()==6,"read changed archive");
std::cout<<"current season, own-club transfers, tied ranks, appearances, cards and pure reads passed\n";return 0;
} catch(const std::exception& e){std::cerr<<e.what()<<'\n';return 1;}