fix race condition issues - WorldEvent and others

This commit is contained in:
Robert Paciorek 2024-03-06 12:18:44 +00:00
parent c909d07462
commit cf41c0175a
3 changed files with 22 additions and 14 deletions

View File

@ -3,9 +3,11 @@ using sodoffmmo.Data;
namespace sodoffmmo.Core;
public class GauntletRoom : Room {
static object NextRoomLock = new object();
static GauntletRoom? NextRoom = null;
public static GauntletRoom Get() {
lock(NextRoomLock) {
if (NextRoom != null && NextRoom.ClientsCount == 1) {
var ret = NextRoom!;
NextRoom = null;
@ -15,6 +17,7 @@ public class GauntletRoom : Room {
return NextRoom!;
}
}
}
public GauntletRoom() : base (null, "GauntletDO", true) {
base.RoomVariables.Add(NetworkArray.VlElement("IS_RACE_ROOM", true));

View File

@ -4,6 +4,7 @@ using sodoffmmo.Data;
namespace sodoffmmo.Core;
public class Room {
public static int MaxId { get; private set; } = 2;
static object RoomsListLock = new object();
protected static Dictionary<string, Room> rooms = new();
List<Client> clients = new();
@ -70,10 +71,12 @@ public class Room {
public static Room Get(string name) => rooms[name];
public static Room GetOrAdd(string name) {
lock(RoomsListLock) {
if (!Room.Exists(name))
return new Room(name);
return rooms[name];
}
}
public static Room[] AllRooms() {
return rooms.Values.ToArray();

View File

@ -10,16 +10,18 @@ class WorldEvent {
NotActive
}
private static WorldEvent? _instance = null;
private object EventLock = new object();
private static object EventLock = new object();
private Random random = new Random();
private System.Timers.Timer? timer = null;
public static WorldEvent Get() {
lock(EventLock) {
if (_instance == null) {
_instance = new WorldEvent();
}
return _instance;
}
}
private WorldEvent() {
startTime = DateTime.UtcNow.AddMinutes(-60);