CareerFinale.cs

Partial of ClubCareer for a football manager career mode. Adds the end-game 'final' Champions Cup: checks whether the player finished top two in Division 5, unlocks the tournament once via EnsureFinalCupUnlocked, and detects a career victory from stored final match results.

using System;
using System.Linq;
using System.Text.Json.Serialization;

namespace Socer;

public sealed partial class ClubCareer
{
    public const string FinalCupId="final";
    [JsonIgnore] public CupTournament FinalCup=>Tournaments?.FirstOrDefault(t=>t.Id==FinalCupId);
    [JsonIgnore] public bool FinalCupQualified=>Division==5 && LeagueComplete && Standings().FindIndex(r=>r.Team==0) is 0 or 1;

    // The actual final result is the receipt. A league title, aggregate Cups or
    // old saves without this competition must never fabricate campaign victory.
    [JsonIgnore] public int CareerVictorySeason=>CupResults.Where(r=>r.CupId==FinalCupId && r.Division==5 &&
        r.PlaceFrom==1 && r.PlaceTo==1 && r.Matches.Any(WonCareerFinal)).Select(r=>r.Season).DefaultIfEmpty().Min();
    [JsonIgnore] public bool CareerWon=>CareerVictorySeason>0;
    [JsonIgnore] public bool FinalCupRequired=>FinalCupQualified && !CareerWon;

    static bool WonCareerFinal(CupMatchResult f)
    {
        if(f.Round!=3 || f.WinnerClubId!=0 || (f.HomeClubId!=0 && f.AwayClubId!=0))return false;
        if(f.HomeGoals==f.AwayGoals)
            return f.Penalties && (f.HomeClubId==0?f.HomePenalties>f.AwayPenalties:f.AwayPenalties>f.HomePenalties);
        return f.HomeClubId==0?f.HomeGoals>f.AwayGoals:f.AwayGoals>f.HomeGoals;
    }

    /// <summary>Unlock once from a completed top-two Premier League table; no rewards or results are granted.</summary>
    public bool EnsureFinalCupUnlocked()
    {
        if(!FinalCupRequired || FinalCup!=null)return false;
        Tournaments.Add(CreateTournament(FinalCupId,"Champions Cup","★",LeagueRoundCount,0,600,2400));
        return true;
    }
}