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