Skip to content

Commit

Permalink
add basic usersocket
Browse files Browse the repository at this point in the history
Update deps
  • Loading branch information
SlejmUr committed Dec 25, 2024
1 parent cf994be commit 0c89531
Show file tree
Hide file tree
Showing 10 changed files with 128 additions and 31 deletions.
4 changes: 2 additions & 2 deletions EIV_Game.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,9 @@
</EmbeddedResource>
</ItemGroup>
<ItemGroup>
<PackageReference Include="EIV_Common" Version="0.0.1.8" />
<PackageReference Include="EIV_Common" Version="0.0.2" />
<PackageReference Include="EIV_DataPack" Version="1.0.3.1" />
<PackageReference Include="EIV_JsonLib" Version="2.0.4.1" />
<PackageReference Include="EIV_JsonLib" Version="2.0.5.2" />
<PackageReference Include="ini-parser-netstandard" Version="2.5.2" />
<PackageReference Include="ModAPI" Version="0.0.2.1" />
<PackageReference Include="SemanticVersioning" Version="2.0.2" />
Expand Down
8 changes: 8 additions & 0 deletions csharp/Client/ChatWebSocket.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#if CLIENT || GAME

namespace ExtractIntoVoid.Client;

public class ChatWebSocket
{
}
#endif
71 changes: 71 additions & 0 deletions csharp/Client/UserWebSocket.cs
Original file line number Diff line number Diff line change
@@ -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<ClientSocketMessage> 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<byte>.Shared.Rent(2048);
var res = webSocket.ReceiveAsync(bytes, CancellationToken.None).Result;
if (res.MessageType == WebSocketMessageType.Close)
return null;
ClientSocketMessage resp = JsonSerializer.Deserialize<ClientSocketMessage>(Encoding.UTF8.GetString(bytes, 0, res.Count));
ArrayPool<byte>.Shared.Return(bytes);
return resp;
}


public void Receive()
{
byte[] bytes = ArrayPool<byte>.Shared.Rent(2048);
var res = webSocket.ReceiveAsync(bytes, CancellationToken.None).Result;
if (res.MessageType == WebSocketMessageType.Close)
return;
MessageQueue.Enqueue(JsonSerializer.Deserialize<ClientSocketMessage>(Encoding.UTF8.GetString(bytes, 0, res.Count)));
ArrayPool<byte>.Shared.Return(bytes);
}

public void Quit()
{
MessageQueue.Clear();
webSocket.CloseAsync(WebSocketCloseStatus.NormalClosure, null, CancellationToken.None);
webSocket.Dispose();
}
}
#endif
2 changes: 1 addition & 1 deletion csharp/Effects/EffectBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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; }

Expand Down
3 changes: 1 addition & 2 deletions csharp/Items/GunBase.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
3 changes: 1 addition & 2 deletions csharp/Items/HealingBase.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using EIV_Common.JsonStuff;
using EIV_JsonLib;
using EIV_JsonLib;
using EIV_JsonLib.Extension;
using ExtractIntoVoid.Modules;

Expand Down
11 changes: 0 additions & 11 deletions csharp/Items/ItemExt.cs

This file was deleted.

48 changes: 39 additions & 9 deletions csharp/Menus/LobbyScene.cs
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -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;

Expand All @@ -26,25 +33,48 @@ public void Connect()
if (!connectasync.IsCompletedSuccessfully)
Quit();

UserToken = connectasync.Result.Content.ReadAsStringAsync().Result;
ConnectResponse = JsonSerializer.Deserialize<ConnectResponse>(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<GameStart>(msg.JsonMessage);
var mainWorld = SceneManager.GetPackedScene("MainWorld").Instantiate<MainWorld>();
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)
{
Expand Down
5 changes: 3 additions & 2 deletions csharp/Modules/InventoryModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}

Expand Down
4 changes: 2 additions & 2 deletions csharp/Properties/BuildDate.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
update-deps
2024-12-16T16:25:44
8746db9
2024-12-25T22:36:42
cf994be

0 comments on commit 0c89531

Please sign in to comment.