mirror of
https://github.com/SoDOff-Project/sodoff-mmo.git
synced 2025-10-11 08:18:49 -07:00
fix race condition issues - WorldEvent and others
This commit is contained in:
parent
c909d07462
commit
cf41c0175a
@ -3,16 +3,19 @@ using sodoffmmo.Data;
|
|||||||
|
|
||||||
namespace sodoffmmo.Core;
|
namespace sodoffmmo.Core;
|
||||||
public class GauntletRoom : Room {
|
public class GauntletRoom : Room {
|
||||||
|
static object NextRoomLock = new object();
|
||||||
static GauntletRoom? NextRoom = null;
|
static GauntletRoom? NextRoom = null;
|
||||||
|
|
||||||
public static GauntletRoom Get() {
|
public static GauntletRoom Get() {
|
||||||
if (NextRoom != null && NextRoom.ClientsCount == 1) {
|
lock(NextRoomLock) {
|
||||||
var ret = NextRoom!;
|
if (NextRoom != null && NextRoom.ClientsCount == 1) {
|
||||||
NextRoom = null;
|
var ret = NextRoom!;
|
||||||
return ret;
|
NextRoom = null;
|
||||||
} else {
|
return ret;
|
||||||
NextRoom = new GauntletRoom();
|
} else {
|
||||||
return NextRoom!;
|
NextRoom = new GauntletRoom();
|
||||||
|
return NextRoom!;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -4,6 +4,7 @@ using sodoffmmo.Data;
|
|||||||
namespace sodoffmmo.Core;
|
namespace sodoffmmo.Core;
|
||||||
public class Room {
|
public class Room {
|
||||||
public static int MaxId { get; private set; } = 2;
|
public static int MaxId { get; private set; } = 2;
|
||||||
|
static object RoomsListLock = new object();
|
||||||
protected static Dictionary<string, Room> rooms = new();
|
protected static Dictionary<string, Room> rooms = new();
|
||||||
|
|
||||||
List<Client> clients = new();
|
List<Client> clients = new();
|
||||||
@ -70,9 +71,11 @@ public class Room {
|
|||||||
public static Room Get(string name) => rooms[name];
|
public static Room Get(string name) => rooms[name];
|
||||||
|
|
||||||
public static Room GetOrAdd(string name) {
|
public static Room GetOrAdd(string name) {
|
||||||
if (!Room.Exists(name))
|
lock(RoomsListLock) {
|
||||||
return new Room(name);
|
if (!Room.Exists(name))
|
||||||
return rooms[name];
|
return new Room(name);
|
||||||
|
return rooms[name];
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Room[] AllRooms() {
|
public static Room[] AllRooms() {
|
||||||
|
@ -10,15 +10,17 @@ class WorldEvent {
|
|||||||
NotActive
|
NotActive
|
||||||
}
|
}
|
||||||
private static WorldEvent? _instance = null;
|
private static WorldEvent? _instance = null;
|
||||||
private object EventLock = new object();
|
private static object EventLock = new object();
|
||||||
private Random random = new Random();
|
private Random random = new Random();
|
||||||
private System.Timers.Timer? timer = null;
|
private System.Timers.Timer? timer = null;
|
||||||
|
|
||||||
public static WorldEvent Get() {
|
public static WorldEvent Get() {
|
||||||
if (_instance == null) {
|
lock(EventLock) {
|
||||||
_instance = new WorldEvent();
|
if (_instance == null) {
|
||||||
|
_instance = new WorldEvent();
|
||||||
|
}
|
||||||
|
return _instance;
|
||||||
}
|
}
|
||||||
return _instance;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private WorldEvent() {
|
private WorldEvent() {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user