using Fusion; using UnityEngine; using Hallucinate.Game; using Hallucinate.UI; using System.Linq; namespace Hallucinate.Network { public class MatchResultManager : NetworkBehaviour { public static MatchResultManager Instance { get; private set; } public override void Spawned() { if (Object.HasStateAuthority) Instance = this; } [Rpc(RpcSources.StateAuthority, RpcTargets.All)] public void RPC_BroadcastResult(PlayerRef winner, EloResult eloResult) { Debug.Log($"Game Over! Winner: {winner}. Elo updated."); // Update local Elo display and show Result UI if (Runner.LocalPlayer == winner) { ShowResultUI(true, eloResult.DeltaA, eloResult.NewRatingA); } else { ShowResultUI(false, eloResult.DeltaB, eloResult.NewRatingB); } } private void ShowResultUI(bool isWin, int delta, int newRating) { var hud = FindFirstObjectByType(); if (hud != null) { // In a real scenario, we might push a new Result screen // For now, let's assume HUD has a result panel Debug.Log($"RESULT: {(isWin ? "WIN" : "LOSS")} | Delta: {delta} | New Rating: {newRating}"); // Save to PlayerPrefs as a dummy "Server" persistence PlayerPrefs.SetInt("EloRating", newRating); int gamesPlayed = PlayerPrefs.GetInt("GamesPlayed", 0); PlayerPrefs.SetInt("GamesPlayed", gamesPlayed + 1); PlayerPrefs.Save(); } } public void ProcessMatchEnd(PlayerRef winner) { if (!Object.HasStateAuthority) return; // Get ratings for both players // In a real game, these would come from the server/metadata int ratingA = PlayerPrefs.GetInt("EloRating", 1000); int ratingB = 1000; // Placeholder for opponent int gamesA = PlayerPrefs.GetInt("GamesPlayed", 0); int gamesB = 0; // Placeholder float resultA = (Runner.LocalPlayer == winner) ? 1.0f : 0.0f; EloResult elo = EloSystem.Calculate(ratingA, ratingB, gamesA, gamesB, resultA); RPC_BroadcastResult(winner, elo); // Shut down runner after some delay Invoke(nameof(ShutdownRunner), 5.0f); } private void ShutdownRunner() { Runner.Shutdown(); } } }