add main lobby countdown

This commit is contained in:
Robert Paciorek 2024-03-07 01:29:56 +00:00
parent cf41c0175a
commit 13248e70c3
3 changed files with 44 additions and 13 deletions

View File

@ -23,7 +23,7 @@ class RacingPlayerReadyHandler : ICommandHandler
Console.WriteLine($"IMR Lobby: {client.ClientID} {ready}"); Console.WriteLine($"IMR Lobby: {client.ClientID} {ready}");
room.TryLoad(); room.TryLoad();
} else { } else {
RacingLobby.SetPlayerState(client, ready); RacingLobby.Lobby.SetPlayerState(client, ready);
Console.WriteLine($"IMR: {client.ClientID} {ready}"); Console.WriteLine($"IMR: {client.ClientID} {ready}");
} }
} }
@ -34,8 +34,7 @@ class RacingPlayerReadyHandler : ICommandHandler
class RacingPlayerStatusHandler : ICommandHandler class RacingPlayerStatusHandler : ICommandHandler
{ {
public void Handle(Client client, NetworkObject receivedObject) { public void Handle(Client client, NetworkObject receivedObject) {
RacingLobby.SendToRacingRoom(); client.Send(RacingLobby.Lobby.GetPS());
client.Send(RacingLobby.GetPS());
} }
} }

View File

@ -30,6 +30,7 @@ internal sealed class ServerConfiguration {
public int FirstEventTimer { get; set; } = 10; public int FirstEventTimer { get; set; } = 10;
public int RacingMaxPlayers { get; set; } = 6; public int RacingMaxPlayers { get; set; } = 6;
public int RacingMinPlayers { get; set; } = 2; public int RacingMinPlayers { get; set; } = 2;
public int RacingMainLobbyTimer { get; set; } = 15;
public bool EnableChat { get; set; } = true; public bool EnableChat { get; set; } = true;
public bool AllowChaos { get; set; } = false; public bool AllowChaos { get; set; } = false;
} }

View File

@ -211,6 +211,39 @@ public class RacingRoom : Room {
} }
public class RacingLobby { public class RacingLobby {
static object lobbyLock = new object();
public readonly static RacingLobby Lobby = new RacingLobby();
private RacingLobby() {
timer = new System.Timers.Timer(1000);
timer.AutoReset = true;
timer.Enabled = true;
timer.Elapsed += CheckRacingRoomCountdown;
}
System.Timers.Timer timer;
int counter;
private void CheckRacingRoomCountdown(Object? source, System.Timers.ElapsedEventArgs e) {
lock (lobbyLock) {
int readyPlayersCount = GetPlayersCount(RacingPlayerState.Ready);
if (readyPlayersCount >= Configuration.ServerConfiguration.RacingMaxPlayers) {
SendToRacingRoom();
counter = -1;
} else if (readyPlayersCount >= Configuration.ServerConfiguration.RacingMinPlayers) {
if (counter < 0) {
counter = Configuration.ServerConfiguration.RacingMainLobbyTimer;
}
if (--counter == 0) {
SendToRacingRoom();
counter = -1;
}
} else {
counter = -1;
}
}
}
class Status { class Status {
public string uid; public string uid;
public RacingPlayerState state = RacingPlayerState.NotReady; public RacingPlayerState state = RacingPlayerState.NotReady;
@ -219,11 +252,9 @@ public class RacingLobby {
} }
} }
static object lobbyLock = new object(); Dictionary<Client, Status> lobbyPlayers = new();
static Dictionary<Client, Status> lobbyPlayers = new(); public void SetPlayerState(Client client, RacingPlayerState state) {
public static void SetPlayerState(Client client, RacingPlayerState state) {
lock (lobbyLock) { lock (lobbyLock) {
if (!lobbyPlayers.ContainsKey(client)) { if (!lobbyPlayers.ContainsKey(client)) {
lobbyPlayers[client] = new Status(client.PlayerData.Uid); lobbyPlayers[client] = new Status(client.PlayerData.Uid);
@ -232,7 +263,7 @@ public class RacingLobby {
} }
} }
public static bool IsPlayerState(Client client, RacingPlayerState state) { public bool IsPlayerState(Client client, RacingPlayerState state) {
lock (lobbyLock) { lock (lobbyLock) {
if (lobbyPlayers.TryGetValue(client, out var info)) { if (lobbyPlayers.TryGetValue(client, out var info)) {
return info.state == state; return info.state == state;
@ -241,7 +272,7 @@ public class RacingLobby {
} }
} }
public static int GetPlayersCount(RacingPlayerState state) { public int GetPlayersCount(RacingPlayerState state) {
lock (lobbyLock) { lock (lobbyLock) {
int count = 0; int count = 0;
foreach(var player in lobbyPlayers) { foreach(var player in lobbyPlayers) {
@ -251,8 +282,8 @@ public class RacingLobby {
} }
} }
public static bool SendToRacingRoom() { private bool SendToRacingRoom() {
lock (lobbyLock) { // lock (lobbyLock) {
if (GetPlayersCount(RacingPlayerState.Ready) >= Configuration.ServerConfiguration.RacingMinPlayers) { if (GetPlayersCount(RacingPlayerState.Ready) >= Configuration.ServerConfiguration.RacingMinPlayers) {
int i = 0; int i = 0;
RacingRoom room = RacingRoom.Get(); RacingRoom room = RacingRoom.Get();
@ -275,10 +306,10 @@ public class RacingLobby {
return true; return true;
} }
return false; return false;
} // }
} }
public static NetworkPacket GetPS() { public NetworkPacket GetPS() {
List<Client> toRemove = new(); List<Client> toRemove = new();
Room room = Room.Get("DragonRacingDO"); Room room = Room.Get("DragonRacingDO");