Skip to content

Commit 6df5efb

Browse files
committed
Add LobbyChat to LobbyScene
Add MapChoose. add events on WebSockets
1 parent bceb060 commit 6df5efb

File tree

10 files changed

+348
-23
lines changed

10 files changed

+348
-23
lines changed

csharp/Client/ChatWebSocket.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
#if CLIENT || GAME
2-
32
using EIV_Common.Coroutines;
43
using EIV_JsonLib.Lobby;
54
using System.Buffers;
@@ -15,6 +14,7 @@ namespace ExtractIntoVoid.Client;
1514
public class ChatWebSocket
1615
{
1716
public Queue<ChatMessage> MessageQueue = new();
17+
public Action<ChatMessage> NewMessage;
1818
ClientWebSocket webSocket = new();
1919
private static ChatWebSocket instance;
2020
public static ChatWebSocket Instance
@@ -56,7 +56,9 @@ public void Receive()
5656
var res = webSocket.ReceiveAsync(bytes, CancellationToken.None).Result;
5757
if (res.MessageType == WebSocketMessageType.Close)
5858
return;
59-
MessageQueue.Enqueue(JsonSerializer.Deserialize<ChatMessage>(Encoding.UTF8.GetString(bytes, 0, res.Count)));
59+
ChatMessage chat = JsonSerializer.Deserialize<ChatMessage>(Encoding.UTF8.GetString(bytes, 0, res.Count));
60+
MessageQueue.Enqueue(chat);
61+
NewMessage?.Invoke(chat);
6062
ArrayPool<byte>.Shared.Return(bytes);
6163
}
6264

csharp/Client/UserWebSocket.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ namespace ExtractIntoVoid.Client;
1414
public class UserWebSocket
1515
{
1616
public Queue<ClientSocketMessage> MessageQueue = new();
17+
public Action<ClientSocketMessage> NewMessage;
1718
ClientWebSocket webSocket = new();
1819
private static UserWebSocket instance;
1920
public static UserWebSocket Instance
@@ -55,7 +56,9 @@ public void Receive()
5556
var res = webSocket.ReceiveAsync(bytes, CancellationToken.None).Result;
5657
if (res.MessageType == WebSocketMessageType.Close)
5758
return;
58-
MessageQueue.Enqueue(JsonSerializer.Deserialize<ClientSocketMessage>(Encoding.UTF8.GetString(bytes, 0, res.Count)));
59+
ClientSocketMessage message = JsonSerializer.Deserialize<ClientSocketMessage>(Encoding.UTF8.GetString(bytes, 0, res.Count));
60+
MessageQueue.Enqueue(message);
61+
NewMessage?.Invoke(message);
5962
ArrayPool<byte>.Shared.Return(bytes);
6063
}
6164

csharp/Managers/GameManager.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,9 @@ private void ModManagerInstance_AllModsLoaded()
6666
#endif
6767
}
6868

69-
public void Quit()
69+
public void Quit(string reason = "")
7070
{
71+
Log.Information("Quiting! Reason: {Reason}", reason);
7172
MainLog.Close();
7273
GetTree().Quit();
7374
}

csharp/Managers/SceneManager.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ static SceneManager()
2323
{ "InputScene", "res://scenes/Menu/InputScene.tscn" },
2424
{ "LobbyScene", "res://scenes/Menu/LobbyScene.tscn" },
2525
{ "Inventory", "res://scenes/Menu/Inventory.tscn" },
26+
{ "MapChoose", "res://scenes/Menu/MapChoose.tscn" },
2627
// { "Escape", "res://scenes/Menu/EscapeScene.tscn" },
2728
#endif
2829
//others

csharp/Menus/LobbyScene.cs

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
using Godot;
99
using Serilog;
1010
using System;
11+
using System.Collections.Generic;
1112
using System.Text.Json;
1213

1314
namespace ExtractIntoVoid.Menus;
@@ -18,11 +19,14 @@ public partial class LobbyScene : Control
1819
public Label ServerText;
1920
[Export]
2021
public Label ServerLongDescription;
22+
[Export]
23+
public RichTextLabel LobbyChat;
2124
public string LobbyAddress;
2225
public string SelectedMap;
2326
private ConnectResponse ConnectResponse;
2427
private ServerInfoJson ServerInfo;
25-
28+
private MapChoose ChooseMapScene;
29+
private List<string> PlayableMaps = [];
2630
public System.Net.Http.HttpClient Client;
2731

2832
public override void _Ready()
@@ -57,17 +61,38 @@ public void Connect()
5761
foreach (var map in ServerInfo.Game.AvailableMaps)
5862
{
5963
Log.Information("Map {MAP} is available to play!", map);
64+
if (SceneManager.Scenes.ContainsKey(map))
65+
PlayableMaps.Add(map);
6066
}
6167
UserWebSocket.Instance.Connect(LobbyAddress, ConnectResponse.Id.ToString(), ConnectResponse.Ticket);
6268
ChatWebSocket.Instance.Connect(LobbyAddress, ConnectResponse.Ticket);
69+
ChatWebSocket.Instance.NewMessage += ChatNewMessage;
6370
}
6471

72+
private void ChatNewMessage(ChatMessage message)
73+
{
74+
LobbyChat.Text += $"{message.SendTime:HH:mm:ss} {message.SenderId} {message.Message}";
75+
}
6576

6677
public void StartPlay()
6778
{
68-
// Show maps that can be played.
69-
// If selected set SelectedMap. And run SendMapToQueue.
79+
GameManager.Instance.UIManager.LoadScreenStart();
80+
if (!SceneManager.TryGetPackedScene("MapChoose", out PackedScene scene))
81+
GameManager.Instance.Quit("MapChoose screen not found!");
82+
ChooseMapScene = scene.Instantiate<MapChoose>();
83+
ChooseMapScene.Maps = PlayableMaps;
84+
this.Hide();
85+
AddChild(ChooseMapScene);
86+
ChooseMapScene.ChoosenMap += ChoosenMap;
87+
}
7088

89+
private void ChoosenMap(string map)
90+
{
91+
ChooseMapScene.QueueFree();
92+
ChooseMapScene = null;
93+
this.Show();
94+
SelectedMap = map;
95+
SendMapToQueue();
7196
}
7297

7398
public void SendMapToQueue()
@@ -77,17 +102,20 @@ public void SendMapToQueue()
77102
Enum = ClientSocketEnum.MatchmakeCheck,
78103
JsonMessage = JsonSerializer.Serialize(new MatchmakeCheck() { Map = SelectedMap })
79104
});
105+
GetNode<Button>("Play").Hide();
106+
// unhide "Cancel"
80107
// Lock button, etc.
81108
CoroutineWorkerNode.CallPeriodically(TimeSpan.FromSeconds(1), CheckMap, CoroutineType.Process, "CheckMap");
82109
}
83110

84111

85112
public void CheckMap()
86113
{
87-
if (!UserWebSocket.Instance.MessageQueue.TryPeek(out var msg))
114+
if (!UserWebSocket.Instance.MessageQueue.TryPeek(out ClientSocketMessage msg))
88115
return;
89116
if (msg.Enum != ClientSocketEnum.GameStart)
90117
return;
118+
msg = UserWebSocket.Instance.MessageQueue.Dequeue();
91119
GameStart gamestart = JsonSerializer.Deserialize<GameStart>(msg.JsonMessage);
92120
var mainWorld = SceneManager.GetPackedScene("MainWorld").Instantiate<MainWorld>();
93121
this.CallDeferred("add_sibling", mainWorld);
@@ -97,6 +125,7 @@ public void CheckMap()
97125

98126
public void Quit()
99127
{
128+
ChatWebSocket.Instance.NewMessage -= ChatNewMessage;
100129
ChatWebSocket.Instance.Quit();
101130
UserWebSocket.Instance.Quit();
102131
CoroutineWorkerNode.KillCoroutineTagInstance("CheckMap");

csharp/Menus/MapChoose.cs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#if CLIENT || GAME
2+
using ExtractIntoVoid.Managers;
3+
using Godot;
4+
using System;
5+
using System.Collections.Generic;
6+
7+
namespace ExtractIntoVoid.Menus;
8+
9+
public partial class MapChoose : Control
10+
{
11+
[Export]
12+
public GridContainer Container;
13+
public Action<string> ChoosenMap;
14+
15+
public List<string> Maps = [];
16+
17+
public override void _Ready()
18+
{
19+
foreach (var map in Maps)
20+
{
21+
Control control = new()
22+
{
23+
Name = $"Control_{map}",
24+
CustomMinimumSize = new(180 ,90),
25+
};
26+
Button button = new()
27+
{
28+
Name = $"Button_{map}",
29+
Flat = true,
30+
};
31+
button.SetAnchorsPreset(LayoutPreset.FullRect);
32+
button.Pressed += () => { ChoosenMap.Invoke(map); };
33+
control.AddChild(button);
34+
Container.AddChild(control);
35+
}
36+
GameManager.Instance.UIManager.LoadScreenStop();
37+
}
38+
}
39+
#endif

csharp/Properties/BuildDate.txt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
update-deps
2-
9f9f50f
3-
2025. 01. 03.
4-
18:32
2+
bceb060
3+
2025. 01. 05.
4+
21:44

default_bus_layout.tres

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
resource_name = "Record"
55

66
[resource]
7-
bus/0/volume_db = 0.0
87
bus/1/name = &"SFX"
98
bus/1/solo = false
109
bus/1/mute = false

scenes/Menu/LobbyScene.tscn

Lines changed: 66 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,41 +2,96 @@
22

33
[ext_resource type="Script" path="res://csharp/Menus/LobbyScene.cs" id="1_xqe1h"]
44

5-
[node name="LobbyScene" type="Control" node_paths=PackedStringArray("ServerText")]
5+
[node name="LobbyScene" type="Control" node_paths=PackedStringArray("ServerText", "ServerLongDescription")]
66
layout_mode = 3
77
anchors_preset = 15
88
anchor_right = 1.0
99
anchor_bottom = 1.0
1010
grow_horizontal = 2
1111
grow_vertical = 2
1212
script = ExtResource("1_xqe1h")
13-
ServerText = NodePath("ServerText")
13+
ServerText = NodePath("Texts/ServerText")
14+
ServerLongDescription = NodePath("Texts/ServerDescription")
1415

15-
[node name="ServerText" type="Label" parent="."]
16+
[node name="Texts" type="Control" parent="."]
1617
layout_mode = 1
1718
anchors_preset = 5
1819
anchor_left = 0.5
1920
anchor_right = 0.5
2021
offset_left = -20.0
2122
offset_right = 20.0
23+
offset_bottom = 40.0
24+
grow_horizontal = 2
25+
26+
[node name="ServerText" type="Label" parent="Texts"]
27+
layout_mode = 1
28+
anchors_preset = 5
29+
anchor_left = 0.5
30+
anchor_right = 0.5
31+
offset_left = -44.0
32+
offset_right = 44.0
2233
offset_bottom = 23.0
2334
grow_horizontal = 2
2435
text = "Server Text"
2536

37+
[node name="ServerDescription" type="Label" parent="Texts"]
38+
layout_mode = 1
39+
anchors_preset = 5
40+
anchor_left = 0.5
41+
anchor_right = 0.5
42+
offset_left = -35.5
43+
offset_top = 30.0
44+
offset_right = 35.5
45+
offset_bottom = 53.0
46+
grow_horizontal = 2
47+
text = "Server Description"
48+
horizontal_alignment = 1
49+
50+
[node name="Chat" type="Control" parent="."]
51+
layout_mode = 1
52+
anchors_preset = 2
53+
anchor_top = 1.0
54+
anchor_bottom = 1.0
55+
grow_vertical = 0
56+
57+
[node name="LobbyChat" type="RichTextLabel" parent="Chat"]
58+
layout_mode = 1
59+
anchors_preset = 2
60+
anchor_top = 1.0
61+
anchor_bottom = 1.0
62+
offset_top = -275.0
63+
offset_right = 400.0
64+
offset_bottom = -35.0
65+
grow_vertical = 0
66+
text = "Lobby Chat"
67+
68+
[node name="ChatInput" type="LineEdit" parent="Chat"]
69+
layout_mode = 1
70+
anchors_preset = 2
71+
anchor_top = 1.0
72+
anchor_bottom = 1.0
73+
offset_top = -30.0
74+
offset_right = 400.0
75+
offset_bottom = 1.0
76+
grow_vertical = 0
77+
placeholder_text = "Lobby Chat Input"
78+
clear_button_enabled = true
79+
flat = true
80+
2681
[node name="Play" type="Button" parent="."]
2782
layout_mode = 0
28-
offset_left = 478.0
29-
offset_top = 238.0
30-
offset_right = 597.0
31-
offset_bottom = 269.0
83+
offset_left = 47.0
84+
offset_top = 67.0
85+
offset_right = 166.0
86+
offset_bottom = 98.0
3287
text = "Play on Server"
3388

3489
[node name="Quit" type="Button" parent="."]
3590
layout_mode = 0
36-
offset_left = 747.0
37-
offset_top = 225.0
38-
offset_right = 789.0
39-
offset_bottom = 256.0
91+
offset_left = 53.0
92+
offset_top = 127.0
93+
offset_right = 95.0
94+
offset_bottom = 158.0
4095
text = "Quit"
4196

4297
[connection signal="pressed" from="Play" to="." method="StartPlay"]

0 commit comments

Comments
 (0)