using Microsoft.AspNetCore.SignalR.Client; using qtc_net_client_2.Forms; using qtc_net_client_2.Services; using QtCNETAPI.Enums; using QtCNETAPI.Models; using QtCNETAPI.Schema; using QtCNETAPI.Services.ApiService; using System.Threading.Tasks; using qtc_net_client_2.ClientModel; namespace qtc_net_client_2 { public partial class TicTacToeGame : Form { private HubConnection? GameHubConnection { get; set; } private GameRoom? CurrentRoom { get; set; } private IApiService _apiService; private Config _config; private AudioService _audioService = new(); private int WinCount { get; set; } private bool JackpotWon { get; set; } = false; public TicTacToeGame(IApiService apiService, Config config) { _apiService = apiService; _config = config; InitializeComponent(); } private async void Main_Load(object sender, EventArgs e) { // first, connect to the game hub GameHubConnection = new HubConnectionBuilder() .WithUrl($"{_config.ServerUrl}/tttgame", options => { options.AccessTokenProvider = () => Task.FromResult(_apiService.SessionToken)!; }) .WithAutomaticReconnect() .Build(); pLoading.Visible = true; lblLoadStatus.Text = "Connecting\nTo\nHub..."; GameHubConnection.On("WaitingForPlayer", (room) => { CurrentRoom = room; BeginInvoke(delegate () { if (room.Player1 != null) { lblP1Username.Text = room.Player1.Username; lvUserlist.Items.Add(room.Player1.Id, room.Player1.Username, room.Player1.Status); } lblLoadStatus.Text = "Waiting\nFor\nPlayer..."; }); }); GameHubConnection.On("GameStart", (room) => { CurrentRoom = room; BeginInvoke(delegate () { pLoading.Visible = false; pSymbolSelection.Visible = false; }); }); GameHubConnection.On("GameEnd", (room, usernameDisconnected) => { CurrentRoom = room; BeginInvoke(delegate () { if (room.Player1 == null) lblP1Username.Text = "Username"; if (room.Player2 == null) lblP2Username.Text = "Username"; ToggleBoardControl(false); switch (room.Status) { case GameStatus.P1Win: { if (_apiService.CurrentUser.Id == room.Player1?.Id) { lblLoadStatus.Text = "You Won!"; WinCount++; if (WinCount >= 3) { JackpotWon = true; lblJackpotWon.Text = "You Won A Spin!"; } } else lblLoadStatus.Text = "You Lost!"; lblPlayAgain.Visible = true; btnPlayAgain.Visible = true; break; } case GameStatus.P2Win: { if (_apiService.CurrentUser.Id == room.Player2?.Id) { lblLoadStatus.Text = "You Won!"; WinCount++; if (WinCount >= 3) { JackpotWon = true; lblJackpotWon.Text = "You Won A Spin!"; } } else lblLoadStatus.Text = "You Lost!"; lblPlayAgain.Visible = true; btnPlayAgain.Visible = true; break; } case GameStatus.PlayerDisconnected: { if (!string.IsNullOrEmpty(usernameDisconnected)) lblLoadStatus.Text = $"Game Ended.\nUser {usernameDisconnected}\nDisconnected."; lblPlayAgain.Visible = false; btnPlayAgain.Visible = false; break; } case GameStatus.NoWin: lblLoadStatus.Text = "It's a draw!"; lblPlayAgain.Visible = true; btnPlayAgain.Visible = true; break; } pLoading.Visible = true; }); }); GameHubConnection.On("RestartGame", (room) => { CurrentRoom = room; BeginInvoke(delegate () { if (pLoading.Visible) pLoading.Visible = false; }); ClearBoard(); ToggleBoardControl(true); BeginInvoke(delegate () { List