diff --git a/PolygonBazooka/Networking/Authentication.cs b/PolygonBazooka/Networking/Authentication.cs index fd00380..a01f0cc 100644 --- a/PolygonBazooka/Networking/Authentication.cs +++ b/PolygonBazooka/Networking/Authentication.cs @@ -1,3 +1,4 @@ +using System; using System.IO; using System.Net; using System.Net.Http; @@ -21,6 +22,9 @@ public Authentication() public async Task LoginAsync(string username, string password, bool stayLoggedIn = false) { + if (IsLoggedIn) + return true; + _stayLoggedIn = stayLoggedIn; var clientHandler = new HttpClientHandler(); @@ -33,10 +37,19 @@ public async Task LoginAsync(string username, string password, bool stayLo request.Content = new StringContent("{\"username\":\"" + username + "\",\"password\":\"" + password + "\"}", Encoding.UTF8, "application/json"); - var response = await client.SendAsync(request); - + HttpResponseMessage response; + + try + { + response = await client.SendAsync(request); + } + catch (Exception) + { + return false; + } + var cookies = clientHandler.CookieContainer.GetCookies(new(uri)); - + foreach (Cookie cookie in cookies) { if (cookie.Name == "token") @@ -51,22 +64,34 @@ public async Task LoginAsync(string username, string password, bool stayLo public async Task RegisterAsync(string username, string password, string email, bool stayLoggedIn = false) { + if (IsLoggedIn) + return true; + _stayLoggedIn = stayLoggedIn; var clientHandler = new HttpClientHandler(); var client = new HttpClient(clientHandler); string uri = "http://localhost:8080/api/auth/register"; - + var request = new HttpRequestMessage(HttpMethod.Post, uri); request.Content = new StringContent( $"{{\"username\":\"{username}\",\"password\":\"{password}\",\"email\":\"{email}\"}}", Encoding.UTF8, "application/json"); - var response = await client.SendAsync(request); + HttpResponseMessage response; + + try + { + response = await client.SendAsync(request); + } + catch (Exception) + { + return false; + } var cookies = clientHandler.CookieContainer.GetCookies(new(uri)); - + foreach (Cookie cookie in cookies) { if (cookie.Name == "token") @@ -91,7 +116,7 @@ public void Dispose() { if (!_stayLoggedIn) Logout(); - else + else if (IsLoggedIn) File.WriteAllText("token.txt", Token.Value); } } \ No newline at end of file diff --git a/PolygonBazooka/Networking/RankedSocket.cs b/PolygonBazooka/Networking/RankedSocket.cs index 14d5f14..9fa0701 100644 --- a/PolygonBazooka/Networking/RankedSocket.cs +++ b/PolygonBazooka/Networking/RankedSocket.cs @@ -12,6 +12,11 @@ public class RankedSocket(PolygonBazookaGame game) public const string LeaveQueue = ""; public const string Forfeit = ""; + public const string ConnectionInitialized = ""; + public const string ConnectionRejected = ""; + + public const string RequestDisconnect = ""; + public const string ChatPrefix = "MESSAGE:"; public const string MoveLeft = "l"; @@ -27,8 +32,21 @@ public class RankedSocket(PolygonBazookaGame game) public ClientWebSocket Socket { get; private set; } + public bool Connected + { + get + { + if (Socket == null) + return false; + return Socket.State == WebSocketState.Open; + } + } + public async Task ConnectAsync() { + if (!game.Authentication.IsLoggedIn) + return; + string uri = "ws://localhost:8080/api/ws/ranked"; Socket = new ClientWebSocket(); @@ -37,11 +55,28 @@ public async Task ConnectAsync() clientWebSocketOptions.SetRequestHeader("Cookie", "token=" + game.Authentication.Token.Value); - await Socket.ConnectAsync(new(uri), CancellationToken.None); + try + { + await Socket.ConnectAsync(new(uri), CancellationToken.None); + } + catch (Exception) + { + return; + } + + Console.WriteLine("wait"); + var result = await ReceiveAsync(); + Console.WriteLine("res" + result); + + if (result != ConnectionInitialized) + await DisconnectAsync(); } public async Task ReceiveAsync() { + if (!Connected) + return null; + var buffer = new byte[4096]; var segment = new ArraySegment(buffer); @@ -60,16 +95,27 @@ public async Task ReceiveAsync() public async Task DisconnectAsync() { + if (!Connected) + return; + + await SendAsync(RequestDisconnect); + await Socket.CloseAsync(WebSocketCloseStatus.NormalClosure, "disconnect", CancellationToken.None); } public async Task ChatAsync(string message) { + if (!Connected) + return; + await SendAsync(ChatPrefix + message); } public async Task SendAsync(string message) { + if (!Connected) + return; + byte[] messageBytes = Encoding.UTF8.GetBytes(message); var messageSegment = new ArraySegment(messageBytes); diff --git a/PolygonBazooka/PolygonBazookaGame.cs b/PolygonBazooka/PolygonBazookaGame.cs index ec5b212..ca42237 100644 --- a/PolygonBazooka/PolygonBazookaGame.cs +++ b/PolygonBazooka/PolygonBazookaGame.cs @@ -130,6 +130,8 @@ protected override void Draw(GameTime gameTime) protected override void Dispose(bool disposing) { base.Dispose(disposing); + + RankedSocket.DisconnectAsync().Wait(); Preferences.Save(); Authentication.Dispose(); diff --git a/PolygonBazooka/Screens/MainMenuScreen.cs b/PolygonBazooka/Screens/MainMenuScreen.cs index e9aeffd..34a3366 100644 --- a/PolygonBazooka/Screens/MainMenuScreen.cs +++ b/PolygonBazooka/Screens/MainMenuScreen.cs @@ -274,7 +274,8 @@ private void UpdateMultiplayerMenu(GameTime gameTime, MouseState mouseState, Rec if (_rankedButtonHovered) { - _game.LoadScreen(ScreenName.RankedMenu); + // _game.LoadScreen(ScreenName.RankedMenu); + _game.ChangeGameState(GameState.RankedPlaying); } } }