From 0c895312afcc89956bf702940b971e221c860182 Mon Sep 17 00:00:00 2001 From: SlejmUr Date: Thu, 26 Dec 2024 00:16:44 +0100 Subject: [PATCH] add basic usersocket Update deps --- EIV_Game.csproj | 4 +- csharp/Client/ChatWebSocket.cs | 8 ++++ csharp/Client/UserWebSocket.cs | 71 +++++++++++++++++++++++++++++++ csharp/Effects/EffectBase.cs | 2 +- csharp/Items/GunBase.cs | 3 +- csharp/Items/HealingBase.cs | 3 +- csharp/Items/ItemExt.cs | 11 ----- csharp/Menus/LobbyScene.cs | 48 +++++++++++++++++---- csharp/Modules/InventoryModule.cs | 5 ++- csharp/Properties/BuildDate.txt | 4 +- 10 files changed, 128 insertions(+), 31 deletions(-) create mode 100644 csharp/Client/ChatWebSocket.cs create mode 100644 csharp/Client/UserWebSocket.cs delete mode 100644 csharp/Items/ItemExt.cs diff --git a/EIV_Game.csproj b/EIV_Game.csproj index eaca3ce..ac9d8c0 100644 --- a/EIV_Game.csproj +++ b/EIV_Game.csproj @@ -46,9 +46,9 @@ - + - + diff --git a/csharp/Client/ChatWebSocket.cs b/csharp/Client/ChatWebSocket.cs new file mode 100644 index 0000000..21c2de9 --- /dev/null +++ b/csharp/Client/ChatWebSocket.cs @@ -0,0 +1,8 @@ +#if CLIENT || GAME + +namespace ExtractIntoVoid.Client; + +public class ChatWebSocket +{ +} +#endif \ No newline at end of file diff --git a/csharp/Client/UserWebSocket.cs b/csharp/Client/UserWebSocket.cs new file mode 100644 index 0000000..bb70516 --- /dev/null +++ b/csharp/Client/UserWebSocket.cs @@ -0,0 +1,71 @@ +#if CLIENT || GAME +using EIV_JsonLib.Lobby; +using System.Buffers; +using System.Collections.Generic; +using System.Net.WebSockets; +using System.Text; +using System.Text.Json; +using System.Threading; + +namespace ExtractIntoVoid.Client; + +public class UserWebSocket +{ + ClientWebSocket webSocket = new(); + private static UserWebSocket instance; + public static UserWebSocket Instance + { + get + { + if (instance == null) + instance = new(); + return instance; + } + } + + public Queue MessageQueue = new(); + + public void Connect(string ip_port, string guid, string ticket) + { + webSocket.Options.SetRequestHeader("authorization", ticket); + webSocket.ConnectAsync(new($"ws://{ip_port}/Socket/Client/{guid}"), CancellationToken.None); + } + + public void Send(ClientSocketMessage clientSocketMessage) + { + if (webSocket.State != WebSocketState.Open) + return; + webSocket.SendAsync(Encoding.UTF8.GetBytes(JsonSerializer.Serialize(clientSocketMessage)), WebSocketMessageType.Binary, false, CancellationToken.None); + } + + public ClientSocketMessage SendAndReceive(ClientSocketMessage clientSocketMessage) + { + Send(clientSocketMessage); + byte[] bytes = ArrayPool.Shared.Rent(2048); + var res = webSocket.ReceiveAsync(bytes, CancellationToken.None).Result; + if (res.MessageType == WebSocketMessageType.Close) + return null; + ClientSocketMessage resp = JsonSerializer.Deserialize(Encoding.UTF8.GetString(bytes, 0, res.Count)); + ArrayPool.Shared.Return(bytes); + return resp; + } + + + public void Receive() + { + byte[] bytes = ArrayPool.Shared.Rent(2048); + var res = webSocket.ReceiveAsync(bytes, CancellationToken.None).Result; + if (res.MessageType == WebSocketMessageType.Close) + return; + MessageQueue.Enqueue(JsonSerializer.Deserialize(Encoding.UTF8.GetString(bytes, 0, res.Count))); + ArrayPool.Shared.Return(bytes); + } + + public void Quit() + { + MessageQueue.Clear(); + webSocket.CloseAsync(WebSocketCloseStatus.NormalClosure, null, CancellationToken.None); + webSocket.Dispose(); + } +} +#endif \ No newline at end of file diff --git a/csharp/Effects/EffectBase.cs b/csharp/Effects/EffectBase.cs index 8d969fc..b498d13 100644 --- a/csharp/Effects/EffectBase.cs +++ b/csharp/Effects/EffectBase.cs @@ -8,7 +8,7 @@ namespace ExtractIntoVoid.Effects; public abstract partial class EffectBase : Node { - Coroutine? TimeCoroutine; + CoroutineHandle? TimeCoroutine; public Effect CoreEffect { get; internal set; } public Node ParentNode { get; internal set; } diff --git a/csharp/Items/GunBase.cs b/csharp/Items/GunBase.cs index cc28734..bf5c422 100644 --- a/csharp/Items/GunBase.cs +++ b/csharp/Items/GunBase.cs @@ -1,10 +1,9 @@ using EIV_Common.Extensions; -using EIV_Common.JsonStuff; using EIV_JsonLib; +using EIV_JsonLib.Extension; using ExtractIntoVoid.Physics; using Godot; using MemoryPack; -using System.Collections.Generic; using System.Linq; namespace ExtractIntoVoid.Items; diff --git a/csharp/Items/HealingBase.cs b/csharp/Items/HealingBase.cs index 472d519..d5a065e 100644 --- a/csharp/Items/HealingBase.cs +++ b/csharp/Items/HealingBase.cs @@ -1,5 +1,4 @@ -using EIV_Common.JsonStuff; -using EIV_JsonLib; +using EIV_JsonLib; using EIV_JsonLib.Extension; using ExtractIntoVoid.Modules; diff --git a/csharp/Items/ItemExt.cs b/csharp/Items/ItemExt.cs deleted file mode 100644 index d24ce22..0000000 --- a/csharp/Items/ItemExt.cs +++ /dev/null @@ -1,11 +0,0 @@ -using EIV_JsonLib.Base; - -namespace ExtractIntoVoid.Items; - -public static class ItemExt -{ - public static bool HasValidAssetPath(this CoreItem item) - { - return Godot.FileAccess.FileExists(item.AssetPath) || Godot.ResourceLoader.Exists(item.AssetPath); - } -} diff --git a/csharp/Menus/LobbyScene.cs b/csharp/Menus/LobbyScene.cs index 7088b42..2c63e66 100644 --- a/csharp/Menus/LobbyScene.cs +++ b/csharp/Menus/LobbyScene.cs @@ -1,5 +1,12 @@ #if CLIENT || GAME +using EIV_Common.Coroutines; +using EIV_JsonLib.Lobby; +using ExtractIntoVoid.Client; +using ExtractIntoVoid.Managers; +using ExtractIntoVoid.Worlds; using Godot; +using System; +using System.Text.Json; namespace ExtractIntoVoid.Menus; @@ -8,8 +15,8 @@ public partial class LobbyScene : Control [Export] public Label ServerText; public string LobbyAddress; - - public string UserToken; + public string SelectedMap; + public ConnectResponse ConnectResponse; public System.Net.Http.HttpClient Client; @@ -26,25 +33,48 @@ public void Connect() if (!connectasync.IsCompletedSuccessfully) Quit(); - UserToken = connectasync.Result.Content.ReadAsStringAsync().Result; + ConnectResponse = JsonSerializer.Deserialize(connectasync.Result.Content.ReadAsStringAsync().Result); + UserWebSocket.Instance.Connect(LobbyAddress, ConnectResponse.Id.ToString(), ConnectResponse.Ticket); + // start chat. } public void StartPlay() { - // Use the Socket. Sadly we now have to! - // All those should be inside CLIENT! + // Show maps that can be played. + // If selected set SelectedMap. And run SendMapToQueue. + + } + + public void SendMapToQueue() + { + UserWebSocket.Instance.Send(new ClientSocketMessage() + { + Enum = ClientSocketEnum.MatchmakeCheck, + JsonMessage = JsonSerializer.Serialize(new MatchmakeCheck() { Map = SelectedMap }) + }); + // Lock button, etc. + CoroutineWorkerNode.CallPeriodically(TimeSpan.FromSeconds(1), CheckMap, CoroutineType.Process, "CheckMap"); + } - /* - // Map Join + + public void CheckMap() + { + if (!UserWebSocket.Instance.MessageQueue.TryPeek(out var msg)) + return; + if (msg.Enum != ClientSocketEnum.GameStart) + return; + GameStart gamestart = JsonSerializer.Deserialize(msg.JsonMessage); var mainWorld = SceneManager.GetPackedScene("MainWorld").Instantiate(); this.CallDeferred("add_sibling", mainWorld); - mainWorld.CallDeferred("StartClient", ServerAddress, ServerPort, ServerMap); - */ + mainWorld.CallDeferred("StartClient", gamestart.Address, gamestart.Port, SelectedMap); + CoroutineWorkerNode.KillCoroutineTagInstance("CheckMap"); } public void Quit() { + UserWebSocket.Instance.Quit(); + CoroutineWorkerNode.KillCoroutineTagInstance("CheckMap"); Client.Dispose(); if (GetParent() is ConnectionScreen connectionScreen) { diff --git a/csharp/Modules/InventoryModule.cs b/csharp/Modules/InventoryModule.cs index bd2db37..226039c 100644 --- a/csharp/Modules/InventoryModule.cs +++ b/csharp/Modules/InventoryModule.cs @@ -2,6 +2,7 @@ using EIV_JsonLib; using EIV_JsonLib.Base; using EIV_JsonLib.Extension; +using EIV_JsonLib.Profile; using ExtractIntoVoid.Items; using ExtractIntoVoid.Managers; using ExtractIntoVoid.Physics; @@ -35,9 +36,9 @@ public InventoryModule() public void Select(int index) { - if (index > Inventory.Items.Count) + if (index > Inventory.ToolBelt.Count) return; - Inventory.Hand = Inventory.Items[index]; + Inventory.Hand = Inventory.ToolBelt[index]; Select(Inventory.Hand); } diff --git a/csharp/Properties/BuildDate.txt b/csharp/Properties/BuildDate.txt index a91591d..1f1a2f2 100644 --- a/csharp/Properties/BuildDate.txt +++ b/csharp/Properties/BuildDate.txt @@ -1,3 +1,3 @@ update-deps -2024-12-16T16:25:44 -8746db9 +2024-12-25T22:36:42 +cf994be