128 lines
2.9 KiB
C#
128 lines
2.9 KiB
C#
using System;
|
|
using System.Threading.Tasks;
|
|
using TMPro;
|
|
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
|
|
namespace TPSBR.UI
|
|
{
|
|
public class UIGameOverView : UIView
|
|
{
|
|
// PRIVATE MEMBERS
|
|
|
|
[SerializeField]
|
|
private UIBehaviour _winnerGroup;
|
|
[SerializeField]
|
|
private TextMeshProUGUI _winner;
|
|
[SerializeField]
|
|
private UIButton _restartButton;
|
|
[SerializeField]
|
|
private AudioSetup _openSound;
|
|
|
|
// UIView INTERFACE
|
|
private bool _hasReportedStats;
|
|
protected override void OnInitialize()
|
|
{
|
|
base.OnInitialize();
|
|
|
|
_restartButton.onClick.AddListener(OnRestartButton);
|
|
}
|
|
|
|
protected override void OnOpen()
|
|
{
|
|
base.OnOpen();
|
|
|
|
var winnerStatistics = GetWinner();
|
|
Player winner = null;
|
|
|
|
if (winnerStatistics.IsValid == true)
|
|
{
|
|
winner = Context.NetworkGame.GetPlayer(winnerStatistics.PlayerRef);
|
|
}
|
|
|
|
if (winner != null)
|
|
{
|
|
_winner.text = $"Winner is {winner.Nickname}";
|
|
_winnerGroup.SetActive(true);
|
|
}
|
|
else
|
|
{
|
|
_winnerGroup.SetActive(false);
|
|
}
|
|
|
|
PlaySound(_openSound);
|
|
if (_hasReportedStats == false)
|
|
{
|
|
_ = ReportGameCompletedAsync();
|
|
}
|
|
Global.Networking.StopGameOnDisconnect();
|
|
}
|
|
private async Task ReportGameCompletedAsync()
|
|
{
|
|
_hasReportedStats = true;
|
|
|
|
try
|
|
{
|
|
if (!PlayerPrefs.HasKey("WALLET_ADDRESS"))
|
|
return;
|
|
|
|
var wallet = PlayerPrefs.GetString("WALLET_ADDRESS");
|
|
if (string.IsNullOrWhiteSpace(wallet))
|
|
return;
|
|
|
|
var localPlayer = Context.NetworkGame.GetPlayer(Context.LocalPlayerRef);
|
|
if (localPlayer == null)
|
|
return;
|
|
|
|
await GameDb.IncGamesPlayedAsync(wallet);
|
|
|
|
int placement = localPlayer.Statistics.Position;
|
|
if (placement > 0)
|
|
{
|
|
await GameDb.SendGameWinAsync(wallet, placement);
|
|
}
|
|
else
|
|
{
|
|
Debug.LogWarning("[Supabase] Skipped reporting placement because it was not set.");
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Debug.LogError($"[Supabase] Failed to report game completion: {ex.Message}");
|
|
}
|
|
}
|
|
protected override void OnDeinitialize()
|
|
{
|
|
_restartButton.onClick.RemoveListener(OnRestartButton);
|
|
|
|
_hasReportedStats = false;
|
|
|
|
base.OnDeinitialize();
|
|
}
|
|
|
|
// PRIVATE MEMBERS
|
|
|
|
private PlayerStatistics GetWinner()
|
|
{
|
|
foreach (var player in Context.NetworkGame.ActivePlayers)
|
|
{
|
|
if (player == null)
|
|
continue;
|
|
|
|
var statistics = player.Statistics;
|
|
if (statistics.Position == 1)
|
|
{
|
|
return statistics;
|
|
}
|
|
}
|
|
|
|
return default;
|
|
}
|
|
|
|
private void OnRestartButton()
|
|
{
|
|
Global.Networking.StopGame();
|
|
}
|
|
}
|
|
}
|