// SPDX-License-Identifier: GPL-3.0-only #include "formats/executable_layout.hpp" #include "game/league_pairings.hpp" #include #include #include #include #include "game/matchday.hpp" #include "game/matchday_setup.hpp" #include "game/match_engine.hpp" #include "game/schedule.hpp" #include "game/source_tip.hpp" #include "game/table.hpp" namespace bmh { namespace { void require(bool value,const char* reason) { if(!value) throw std::invalid_argument(reason); } void clear_computer_tip(LeaguePairingsOwner& owner) { owner.computer_tip_active=false;owner.computer_tip_league=owner.computer_tip_round=-1; owner.computer_tip_original.fill(0);owner.computer_tip_results.clear(); } void prepare_computer_tip(GameState& g,LeaguePairingsOwner& owner, const LeaguePairingsConstants& c,GameRandom& game) { const uint32_t preferences_at=::bmh::layout::CareerSession_CloseLeagueFamily_ExecutableField+uint32_t(owner.manager); require(!owner.filter_active&&!owner.postponed_active&& (g.memory.u8(preferences_at)&0x0f)<=3, "pairings: Computer Tip supports only ordinary/search modes 0..3"); require(owner.round>=0&&size_t(owner.league)=kResultsSize, "pairings: Computer Tip Native seed/results unavailable"); const auto page=league_pairings_view_source(g,owner,c); const size_t expected=league_fixtures(owner.league,owner.round+1).size(); require(page.rows.size()==expected&&page.total_rows==int(expected), "pairings: Computer Tip requires the complete ordinary fixture row set"); for(const auto& row:page.rows) require(row.raw_result==0xcc,"pairings: Computer Tip requires every visible result to be 0xcc"); const int32_t source_seed=std::bit_cast(g.memory.u32(::bmh::layout::CurrentMatchday)); require(source_seed>=0&&source_seed<=10000, "pairings: Computer Tip rejects an unqualified signed Native seed"); const size_t first=result_index(owner.league,owner.round+1,0); require(first+10<=g.results.size(),"pairings: Computer Tip ten-byte Source backup is unavailable"); std::array original{}; if(owner.computer_tip_active) { require(owner.computer_tip_league==owner.league&&owner.computer_tip_round==owner.round&& owner.computer_tip_results.size()==expected, "pairings: stale Computer Tip forecast identity"); original=owner.computer_tip_original; for(size_t i=0;i<10;++i) require(g.results[first+i]==original[i],"pairings: Native result slice changed under Computer Tip"); } else { std::copy_n(g.results.begin()+static_cast(first),10,original.begin()); } struct Input { const LeaguePairingsRow* row; int home_rating,away_rating,home_secondary,away_secondary; }; std::vector inputs;inputs.reserve(expected); for(const auto& row:page.rows) { require(row.source_round==owner.round&&row.fixture_index>=0&& size_t(row.fixture_index) results;results.reserve(expected); for(const auto& input:inputs) { const auto& row=*input.row; results.push_back({owner.league,row.source_round,row.fixture_index, row.home.runtime_team,row.home.club_identity,row.away.runtime_team,row.away.club_identity, row.raw_result,source_tip_result(staged_game,c.computer_tip_gap,input.home_rating,input.away_rating, input.home_secondary,input.away_secondary)}); } game=staged_game;owner.computer_tip_active=true; owner.computer_tip_league=owner.league;owner.computer_tip_round=owner.round; owner.computer_tip_original=original;owner.computer_tip_results=std::move(results); g.memory.set8(preferences_at,uint8_t(g.memory.u8(preferences_at)|0x10)); } int filtered_fixture_count(int league,int team) { int count=0; for(int round=0;round=0&&o.manager<4&& size_t(o.manager)=0&&o.runtime_club<138))&& size_t(o.runtime_club)=0&&o.league<9&&o.round>=0&&o.round=-1&&(!o.active_phase||o.active_group>=0), "pairings: invalid active presentation phase"); require(g.memory.has(::bmh::layout::CareerSession_CloseLeagueFamily_ExecutableField+uint32_t(o.manager))&& (((g.memory.u8(::bmh::layout::CareerSession_CloseLeagueFamily_ExecutableField+uint32_t(o.manager))&0x0c)==0)|| (g.memory.u8(::bmh::layout::CareerSession_CloseLeagueFamily_ExecutableField+uint32_t(o.manager))&0x0f)==4|| (g.memory.u8(::bmh::layout::CareerSession_CloseLeagueFamily_ExecutableField+uint32_t(o.manager))&0x0f)==8)&& g.memory.has(::bmh::layout::LeagueTableDisplayModes+uint32_t(o.manager)*2+1), "pairings: missing preferences or unsupported saved list mode"); require(g.results.size()>=kResultsSize&&g.club_ids.size()>=138&&g.league_rounds.size()>=9&& g.memory.has(::bmh::layout::Postponed+0xef)&&g.memory.has(::bmh::layout::CompetitionCalendar+365*2+1), "pairings: incomplete Native schedule"); const auto selection_valid=[&](int team,int identity) { return team<0 ? team>=-2&&identity==-1 : team<138&&team_league(team)==o.league&&g.club_ids.at(size_t(team))==identity; }; require(selection_valid(o.search_first,o.search_first_identity)&& selection_valid(o.search_second,o.search_second_identity)&& (o.search_first!=-1 || (o.search_second==-1&&!o.search_second_click))&& (o.search_first!=-2 || (o.search_second==-2&&!o.search_second_click))&& (o.search_first==-1 || (g.memory.u8(::bmh::layout::CareerSession_CloseLeagueFamily_ExecutableField+uint32_t(o.manager))&0x0f)==2)&& (!o.search_second_click || (o.search_first>=0&&o.search_second>=-1)), "pairings: stale or invalid search selection"); const uint8_t mode=uint8_t(g.memory.u8(::bmh::layout::CareerSession_CloseLeagueFamily_ExecutableField+uint32_t(o.manager))&0x0f); const bool mode8=mode==8; if(mode8) { require(o.postponed_active&&!o.filter_active&&o.filter_team==-1&&o.filter_identity==-1&&o.filter_offset==0&& o.search_first==-1&&o.search_second==-1&&o.search_first_identity==-1&&o.search_second_identity==-1&& !o.search_second_click&&o.postponed_offset>=0&&o.postponed_offset%10==0, "pairings: invalid postponed-list owner"); const int count=postponed_fixture_count(g,o.league); require((count==0&&o.postponed_offset==0)||(count>0&&o.postponed_offset=int(g.league_rounds.at(size_t(o.league)))&& o.computer_tip_results.size()==expected&&g.memory.has(::bmh::layout::CurrentMatchday), "pairings: invalid Computer Tip owner scope"); const size_t first=result_index(o.league,o.round+1,0); require(first+10<=g.results.size(),"pairings: Computer Tip result backup is unavailable"); for(size_t n=0;n<10;++n) require(g.results[first+n]==o.computer_tip_original[n], "pairings: Computer Tip original result slice changed"); const auto fixtures=league_fixtures(o.league,o.round+1); require(fixtures.size()==expected,"pairings: Computer Tip fixture count changed"); for(size_t n=0;n=0&&o.filter_offset%10==0&&o.filter_team>=-2, "pairings: invalid Source club-filter mode"); if(o.filter_team>=0) { require(o.filter_team<138&&team_league(o.filter_team)==o.league&& g.club_ids.at(size_t(o.filter_team))==o.filter_identity&& (o.filter_offset==0||o.filter_offset(g.league_rounds.at(size_t(league)),league_matchdays(league)-1); } LeaguePairingsTeam team_view(const GameState& g,int team,int played) { LeaguePairingsTeam out; out.runtime_team=team;out.club_identity=g.club_ids.at(size_t(team)); if(played) { const int r=played-1; const size_t at=(size_t(team)*13+size_t(r/3))*2; require(at+1>((r%3)*5))&31)+1; } constexpr std::array flags{0x1f1,0x1f2,0x1f4,0x1f8,0x1ff}; for(size_t n=0;n0&&c.domestic_league_count<=9, "pairings: unsupported Source calendar constants"); require(c.calendar_class.at(size_t(league))<32,"pairings: invalid calendar mask shift"); const uint32_t mask=league>=c.domestic_league_count?0x10u:1u<0xcc;} std::optional league_pairings_active_packet_source( const GameState& g,const LeaguePairingsOwner& owner,const std::vector& groups, std::span referee_profiles) { validate(g,owner); // Mode4's armed -2 sentinel still uses the ordinary selected-round list. // A selected filter and Mode8 explicitly clear local74 in Move. if(owner.postponed_active||(owner.filter_active&&owner.filter_team>=0))return std::nullopt; const auto fixtures=league_fixtures(owner.league,owner.round+1); int found=-1; for(size_t n=0;n=2&&group.fixtures[0]==fixtures.front().home&& group.fixtures[1]==fixtures.front().away)found=int(n); } if(found<0)return std::nullopt; const auto& group=groups[size_t(found)]; require(group.fixtures.size()==fixtures.size()*2, "pairings: incomplete active fixture vector"); LeaguePairingsActivePacket packet; packet.league=owner.league;packet.round=owner.round;packet.group_index=found; packet.rows.reserve(fixtures.size()); for(size_t n=0;n0x50+n, "pairings: incomplete active referee/weather descriptor"); const uint8_t referee=group.desc[0x10+n]; require(size_t(referee)=0)&& packet.league==owner.league&&packet.round==owner.round&&packet.group_index>=0&& packet.group_index==owner.active_group,"pairings: stale active descriptor owner"); const auto fixtures=league_fixtures(owner.league,owner.round+1); require(packet.rows.size()==fixtures.size(),"pairings: incomplete active presentation rows"); for(size_t n=0;n=0&&manager<4&&size_t(manager)=0&&out.runtime_club<138))&& size_t(out.runtime_club)=0&&(e.value==0||e.value==1)&&bool(e.value)!=before.active_phase, "pairings: invalid active presentation redraw"); out.active_phase=bool(e.value);break; case LeaguePairingsAction::reset: out.round=current_round(g,out.league);out.title_round=g.league_rounds.at(size_t(out.league));reset();break; case LeaguePairingsAction::previous: if(out.postponed_active) {if(out.postponed_offset>=10)out.postponed_offset-=10;break;} if((g.memory.u8(::bmh::layout::CareerSession_CloseLeagueFamily_ExecutableField+uint32_t(out.manager))&3)==0) { if(out.filter_active&&out.filter_offset>=10)out.filter_offset-=10; break; } out.round=(out.round+league_matchdays(out.league)-1)%league_matchdays(out.league);out.title_round=out.round;break; case LeaguePairingsAction::next: if(out.postponed_active) { const auto page=league_pairings_view_source(g,before,c); if(page.rows.size()==10&&out.postponed_offset+10=0&& out.filter_offset+10=0&&out.league<9,"pairings: invalid manager league preference"); [[fallthrough]]; case LeaguePairingsAction::select_league: if(e.kind==LeaguePairingsAction::select_league) { require(e.value>=0&&e.value<9,"pairings: invalid league selection");out.league=e.value; } if(out.league!=before.league) { // LeagueMatchdays: retained round must be in the league and at/before // its next round. Directparam0 does not apply the automatic reset. if(out.round>=league_matchdays(out.league)||out.round>g.league_rounds.at(size_t(out.league))) out.round=current_round(g,out.league); out.title_round=out.round; reset(); } break; case LeaguePairingsAction::team_info: case LeaguePairingsAction::search_team: { require(!out.filter_active&&!(e.kind==LeaguePairingsAction::search_team&&out.postponed_active), "pairings: filtered rows cannot open ordered search"); require((e.kind==LeaguePairingsAction::search_team)==(out.search_first!=-1), "pairings: row action does not match Source search ownership"); const auto view=league_pairings_view_source(g,before,c);bool found=false; for(const auto& row:view.rows)for(const auto* team:{&row.home,&row.away}) found|=team->runtime_team==e.value&&team->club_identity==e.expected_club_identity; require(found,"pairings: stale fixture team identity"); if(e.kind==LeaguePairingsAction::team_info)break; if(!out.search_second_click) { out.search_first=e.value;out.search_first_identity=e.expected_club_identity; out.search_second=-1;out.search_second_identity=-1;out.search_second_click=true; } else { out.search_second=e.value;out.search_second_identity=e.expected_club_identity; // LeagueMatchdays2 scans directed home/away fixtures in ascending round // then row order. A missing self-pair keeps round and local30=1. for(int round=0;roundruntime_team==e.value&&team->club_identity==e.expected_club_identity; require(found,"pairings: stale filtered-row team identity"); out.filter_team=e.value;out.filter_identity=e.expected_club_identity; while(out.filter_offset>=filtered_fixture_count(out.league,out.filter_team)&&out.filter_offset>0) out.filter_offset-=10; break; } case LeaguePairingsAction::close: reset();g.memory.set8(::bmh::layout::CareerSession_CloseLeagueFamily_ExecutableField+uint32_t(out.manager), uint8_t(g.memory.u8(::bmh::layout::CareerSession_CloseLeagueFamily_ExecutableField+uint32_t(out.manager))&0xef)); out.active_phase=false;out.active_group=-1; out.closed=true;stir_random(game,rand);return out; case LeaguePairingsAction::unsupported: throw std::invalid_argument("pairings: Original Zusatzaktion ist noch nicht verfügbar"); } validate(g,out); const uint32_t at=::bmh::layout::CareerSession_CloseLeagueFamily_ExecutableField+uint32_t(out.manager); if(e.kind!=LeaguePairingsAction::computer_tip) g.memory.set8(at,uint8_t(g.memory.u8(at)&0xef)); return out; } LeaguePairingsView league_pairings_view_source(const GameState& g,const LeaguePairingsOwner& o, const LeaguePairingsConstants& c) { validate(g,o);LeaguePairingsView out;out.owner=o;out.played_rounds=g.league_rounds.at(size_t(o.league)); out.total_rounds=league_matchdays(o.league);out.preferences=g.memory.u8(::bmh::layout::CareerSession_CloseLeagueFamily_ExecutableField+uint32_t(o.manager)); const bool postponed=o.postponed_active; if(!postponed)out.match_date=round_date(g,o.league,o.round,c); auto append_row=[&](int round,size_t n,const Fixture& fixture) { LeaguePairingsRow row; row.source_round=round;row.fixture_index=int(n); row.home=team_view(g,fixture.home,out.played_rounds); row.away=team_view(g,fixture.away,out.played_rounds); row.raw_result=g.results.at(result_index(o.league,round+1,int(n))); if(o.computer_tip_active) { const auto match=std::find_if(o.computer_tip_results.begin(),o.computer_tip_results.end(), [&](const auto& tip) {return tip.league==o.league&&tip.source_round==round&& tip.fixture_index==int(n)&&tip.home_runtime_team==row.home.runtime_team&& tip.home_club_identity==row.home.club_identity&& tip.away_runtime_team==row.away.runtime_team&& tip.away_club_identity==row.away.club_identity;}); if(match!=o.computer_tip_results.end()) { require(row.raw_result==match->original_result, "pairings: Native result changed under Computer Tip forecast"); row.computer_tip_result=match->predicted_result; } } if(postponed) { for(uint32_t k=0;k<40;++k) { const uint32_t at=::bmh::layout::Postponed+k*6;const uint16_t day=g.memory.u16(at); if(day&&g.memory.u8(at+3)==o.league&&g.memory.u8(at+2)==round&&g.memory.u8(at+4)==n) { row.postponed_queue_slot=uint8_t(k);row.postponed_day=day; row.postponed_date=g.season_start+day;break; } } } else if(!league_pairings_played_source(row.raw_result)&&row.raw_result!=0xcc) { for(uint32_t k=0;k<40;++k) { const uint32_t at=::bmh::layout::Postponed+k*6;const uint16_t day=g.memory.u16(at); if(day&&g.memory.u8(at+3)==o.league&&g.memory.u8(at+2)==round&&g.memory.u8(at+4)==n) { row.postponed_date=g.season_start+day;break; } } } if(!o.active_phase&&!postponed&&!(o.filter_active&&o.filter_team>=0)&&round>=out.total_rounds/2) { const uint8_t raw=g.results.at(result_index(o.league,round+1-out.total_rounds/2,int(n))); if(league_pairings_played_source(raw))row.first_leg_result=raw; } if(o.filter_active&&o.filter_team>=0&&row.raw_result==0xcc) row.scheduled_date=round_date(g,o.league,round,c); out.rows.push_back(std::move(row)); }; if(postponed) { out.page_offset=o.postponed_offset; for(int round=0;round=o.postponed_offset&&out.rows.size()<10)append_row(round,n,fixtures[n]); ++out.total_rows; } } } else if(o.filter_active&&o.filter_team>=0) { out.page_offset=o.filter_offset; for(int round=0;round=o.filter_offset&&out.rows.size()<10)append_row(round,n,fixtures[n]); ++out.total_rows; } } } else { const auto fixtures=league_fixtures(o.league,o.round+1); out.total_rows=int(fixtures.size()); for(size_t n=0;n