mirror of
https://github.com/SoDOff-Project/sodoff-mmo.git
synced 2025-10-11 08:18:49 -07:00
fix lobby players list and racing room management
This commit is contained in:
parent
aa373cd2a9
commit
c35226cc4e
@ -3,28 +3,23 @@ using sodoffmmo.Data;
|
||||
|
||||
namespace sodoffmmo.Core;
|
||||
public class GauntletRoom : Room {
|
||||
static List<GauntletRoom> GauntletRooms = new();
|
||||
static GauntletRoom NextRoom = null;
|
||||
|
||||
public static GauntletRoom Get() {
|
||||
foreach (var room in GauntletRooms) {
|
||||
if (room.ClientsCount < 2) {
|
||||
lock (room.roomLock) {
|
||||
if (room.ClientsCount == 0)
|
||||
room.players.Clear();
|
||||
if (NextRoom != null && NextRoom.ClientsCount == 1) {
|
||||
var ret = NextRoom;
|
||||
NextRoom = null;
|
||||
return ret;
|
||||
} else {
|
||||
NextRoom = new GauntletRoom();
|
||||
return NextRoom;
|
||||
}
|
||||
return room;
|
||||
}
|
||||
}
|
||||
var newroom = new GauntletRoom("GauntletDO" + "_" + GauntletRooms.Count.ToString());
|
||||
GauntletRooms.Add(newroom);
|
||||
return newroom;
|
||||
}
|
||||
|
||||
public GauntletRoom(string name) : base (name, "GauntletDO") {
|
||||
public GauntletRoom(string name = null) : base (name, "GauntletDO", true) {
|
||||
base.RoomVariables.Add(NetworkArray.VlElement("IS_RACE_ROOM", true));
|
||||
}
|
||||
|
||||
|
||||
class Status {
|
||||
public string uid;
|
||||
public bool isReady = false;
|
||||
@ -120,7 +115,10 @@ public class GauntletRoom : Room {
|
||||
}
|
||||
}
|
||||
|
||||
static object joinLock = new object();
|
||||
|
||||
static public void Join(Client client, GauntletRoom room = null) {
|
||||
lock(joinLock) {
|
||||
if (room is null)
|
||||
room = GauntletRoom.Get();
|
||||
|
||||
@ -128,4 +126,5 @@ public class GauntletRoom : Room {
|
||||
client.JoinRoom(room);
|
||||
room.SendUJR();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -13,26 +13,13 @@ public enum RacingPlayerState {
|
||||
}
|
||||
|
||||
public class RacingRoom : Room {
|
||||
static List<RacingRoom> RacingRooms = new();
|
||||
static Random random = new Random();
|
||||
|
||||
public static RacingRoom Get() {
|
||||
foreach (var room in RacingRooms) {
|
||||
if (room.ClientsCount == 0) {
|
||||
room.Reset();
|
||||
return room;
|
||||
}
|
||||
}
|
||||
var newroom = new RacingRoom("RacingDragon" + "_" + RacingRooms.Count.ToString());
|
||||
RacingRooms.Add(newroom);
|
||||
return newroom;
|
||||
return new RacingRoom();
|
||||
}
|
||||
|
||||
public RacingRoom(string name, int? trackId = null) : base (name, "RacingDragon") {
|
||||
Reset(trackId);
|
||||
}
|
||||
|
||||
private void Reset(int? trackId = null) {
|
||||
public RacingRoom(string name = null, int? trackId = null) : base (name, "RacingDragon", true) {
|
||||
if (trackId is null) {
|
||||
TID = random.Next(105);
|
||||
} else {
|
||||
@ -267,7 +254,6 @@ public class RacingLobby {
|
||||
lock (lobbyLock) {
|
||||
if (GetPlayersCount(RacingPlayerState.Ready) >= Configuration.ServerConfiguration.RacingMinPlayers) {
|
||||
int i = 0;
|
||||
List<Client> toRemove = new();
|
||||
RacingRoom room = RacingRoom.Get();
|
||||
foreach (var player in lobbyPlayers) {
|
||||
if (player.Value.state == RacingPlayerState.Ready) {
|
||||
@ -284,12 +270,9 @@ public class RacingLobby {
|
||||
}
|
||||
}
|
||||
// join clients to racing room and start countdown
|
||||
// after change room, client will be removed from lobbyPlayers (in GetPS)
|
||||
room.Init();
|
||||
|
||||
foreach (var player in toRemove) {
|
||||
lobbyPlayers.Remove(player);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
@ -297,16 +280,26 @@ public class RacingLobby {
|
||||
}
|
||||
|
||||
public static NetworkPacket GetPS() {
|
||||
List<Client> toRemove = new();
|
||||
Room room = Room.Get("DragonRacingDO");
|
||||
|
||||
// {"a":13,"c":1,"p":{"c":"PS","p":{"arr":["RA","","PS","e6147216-8100-4552-864d-be8f1347e201","8cb5842d-735a-4259-80af-e2e204b9c2bd"]}}}
|
||||
List<string> info = new();
|
||||
info.Add("RA");
|
||||
info.Add("");
|
||||
info.Add("PS");
|
||||
foreach(var player in lobbyPlayers) {
|
||||
if (player.Value.state != RacingPlayerState.InRacingRoom) {
|
||||
if (player.Key.Room != room) {
|
||||
toRemove.Add(player.Key);
|
||||
} else if (player.Value.state != RacingPlayerState.InRacingRoom) {
|
||||
info.Add(player.Value.uid);
|
||||
}
|
||||
}
|
||||
|
||||
foreach (var player in toRemove) {
|
||||
lobbyPlayers.Remove(player);
|
||||
}
|
||||
|
||||
return Utils.ArrNetworkPacket(info.ToArray(), "PS");
|
||||
}
|
||||
}
|
||||
|
@ -3,7 +3,8 @@ using sodoffmmo.Data;
|
||||
|
||||
namespace sodoffmmo.Core;
|
||||
public class Room {
|
||||
static Dictionary<string, Room> rooms = new();
|
||||
public static int MaxId { get; private set; } = 2;
|
||||
protected static Dictionary<string, Room> rooms = new();
|
||||
|
||||
List<Client> clients = new();
|
||||
protected object roomLock = new object();
|
||||
@ -11,16 +12,23 @@ public class Room {
|
||||
public int Id { get; private set; }
|
||||
public string Name { get; private set; }
|
||||
public string Group { get; private set; }
|
||||
public bool AutoRemove { get; private set; }
|
||||
public bool IsRemoved { get; private set; } = false;
|
||||
public NetworkArray RoomVariables = new();
|
||||
|
||||
public Room(string name, string group = null) {
|
||||
Id = rooms.Count + 3;
|
||||
public Room(string name, string group = null, bool autoRemove = false) {
|
||||
Id = ++MaxId;
|
||||
if (name is null) {
|
||||
Name = group + "_" + MaxId;
|
||||
} else {
|
||||
Name = name;
|
||||
}
|
||||
if (group is null) {
|
||||
Group = name;
|
||||
} else {
|
||||
Group = group;
|
||||
}
|
||||
AutoRemove = autoRemove;
|
||||
rooms.Add(Name, this);
|
||||
}
|
||||
|
||||
@ -38,6 +46,8 @@ public class Room {
|
||||
|
||||
public void AddClient(Client client) {
|
||||
lock (roomLock) {
|
||||
if (IsRemoved)
|
||||
throw new Exception("Call AddClient on removed room");
|
||||
client.Send(RespondJoinRoom());
|
||||
// NOTE: send RespondJoinRoom() and add client to clients as atomic operation
|
||||
// to make sure to client get full list of players in room
|
||||
@ -48,6 +58,10 @@ public class Room {
|
||||
public void RemoveClient(Client client) {
|
||||
lock (roomLock) {
|
||||
clients.Remove(client);
|
||||
if (AutoRemove && ClientsCount == 0) {
|
||||
IsRemoved = true;
|
||||
rooms.Remove(Name);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user