Skip to content

Commit

Permalink
feat: Improve the logic of most of the room parts
Browse files Browse the repository at this point in the history
  • Loading branch information
NuanRMxi committed Sep 7, 2024
1 parent a5a817b commit 8b59e4e
Show file tree
Hide file tree
Showing 3 changed files with 241 additions and 49 deletions.
70 changes: 62 additions & 8 deletions ConnectionMessage.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using System.Text.RegularExpressions;

using System;
namespace Phi_MGUS
{
public static class ConnectionMessage
Expand Down Expand Up @@ -45,20 +45,50 @@ public class JoinServerSuccess : Message
}

/// <summary>
/// Room add failed message | 房间新建失败消息
/// Room new failed message | 房间新建失败消息
/// </summary>
public class AddRoomFailed : Message
public class NewRoomFailed : Message
{
public new readonly string action = "addRoomFailed";
public new readonly string action = "newRoomFailed";
public string reason = "unknown";
}

/// <summary>
/// Room success message | 房间新建成功消息
/// Room new success message | 房间新建成功消息
/// </summary>
public class NewRoomSuccess : Message
{
public new readonly string action = "newRoomSuccess";
}
/// <summary>
/// Room join failed message | 房间加入成功消息
/// </summary>
public class JoinRoomFailed : Message
{
public new readonly string action = "joinRoomFaild";
public string reason = "unknown";
}
/// <summary>
/// Room join success message | 房间加入成功消息
/// </summary>
public class JoinRoomSuccess : Message
{
public new readonly string action = "joinRoomSuccess";
}
/// <summary>
/// Leave room failed message | 离开房间失败消息
/// </summary>
public class AddRoomSuccess : Message
public class LeaveRoomFailed : Message
{
public new readonly string action = "addRoomSuccess";
public new readonly string action = "leaveRoomFailed";
public string reason = "unknown";
}
/// <summary>
/// Leave room success message | 离开房间成功消息
/// </summary>
public class LeaveRoomSuccess : Message
{
public new readonly string action = "leaveRoomSuccess";
}
}

Expand Down Expand Up @@ -107,7 +137,7 @@ public class NewRoom : Message
public Data data = new Data
{
//RoomID is a random string, length is 16 | 房间ID是随机字符串,长度为16
roomID = Guid.NewGuid().ToString().Substring(0, 16)
roomID = Guid.NewGuid().ToString().Replace("-", "").Substring(0, 16)
};

public class Data
Expand Down Expand Up @@ -135,6 +165,25 @@ public string roomID
}// Only English or numbers can be used, and cannot exceed 32 digits | 只能使用英文或数字,且不超过32位

}
public NewRoom(int? maxUser = 8, string roomID = null)

Check warning on line 168 in ConnectionMessage.cs

View workflow job for this annotation

GitHub Actions / build-windows

Cannot convert null literal to non-nullable reference type.

Check warning on line 168 in ConnectionMessage.cs

View workflow job for this annotation

GitHub Actions / build-linux

Cannot convert null literal to non-nullable reference type.
{
if (roomID == null)
{
roomID = Guid.NewGuid().ToString().Replace("-", "").Substring(0, 16);
}
else
{
data.roomID = roomID;
}
if (maxUser == null)
{
maxUser = 8;
}
else
{
data.maxUser = maxUser.Value;
}
}
}

/// <summary>
Expand All @@ -149,6 +198,11 @@ public class Data
{
public string roomID = "";
}

public JoinRoom(string roomID)
{
data.roomID = roomID;
}
}
/// <summary>
/// Leave room | 离开房间
Expand Down
81 changes: 63 additions & 18 deletions GameManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,51 @@ public static class GameManager
/// </summary>
public static class RoomManager
{
private static readonly List<Room> roomList = new();
public static readonly List<Room> roomList = new();
public static void AddRoom(User user, string roomID)
{
roomList.Add(new Room(user, roomID));
user.userStatus = User.Status.InRoom;
}

public static void RemoveRoom(Room instance)
/// <summary>
/// Dissolve the room | 解散房间
/// </summary>
/// <param name="room">房间</param>
public static void RemoveRoom(Room room)
{
for(var i = roomList.Count - 1; i >= 0; i--)
{
if(roomList[i] == room)
{
roomList.RemoveAt(i);
}
}
}
/// <summary>
/// Remove room by roomID | 根据房间ID删除房间
/// </summary>
/// <param name="roomID">房间ID</param>
public static void RemoveRoom(string roomID)
{
for(int i = roomList.Count - 1; i >= 0; i--)
for(var i = roomList.Count - 1; i >= 0; i--)
{
if(roomList[i] == instance)
if(roomList[i].roomID == roomID)
{
roomList.RemoveAt(i);
}
}
}

/// <summary>
/// Get room by roomID | 根据房间ID获取房间
/// </summary>
/// <param name="roomID">房间ID</param>
/// <returns>Room | 房间</returns>
public static Room? GetRoom(string roomID)
{
return roomList.FirstOrDefault(x => x.roomID == roomID);
}
}

/// <summary>
Expand All @@ -48,7 +77,7 @@ public static void Drop(IWebSocketConnection socket)
/// <summary>
/// Remove user | 移除用户
/// </summary>
/// <param name="socket"></param>
/// <param name="socket">用户对应Socket</param>
public static void RemoveUser(IWebSocketConnection socket)
{
for(int i = userList.Count - 1; i >= 0; i--)
Expand All @@ -73,7 +102,6 @@ public static User GetUser(IWebSocketConnection socket)
{
return userList.First(x => x.userSocket == socket);
}

}

/// <summary>
Expand All @@ -87,22 +115,24 @@ public class Room
/// <summary>
/// Create room | 创建房间
/// </summary>
/// <param name="owner"></param>
/// <param name="roomID"></param>
/// <param name="owner">房间所有者</param>
/// <param name="roomID">房间ID</param>
/// <exception cref="ArgumentException">Illegal room ID | 非法房间ID</exception>
public Room(User owner, string roomID)
{
if (roomID.Length > 32)
{
throw new ArgumentException("RoomIdentifier cannot exceed 32 digits.");
throw new ArgumentException("RoomIdentifier cannot exceed 32 digits."); // 房间ID长度不能超过32位
}
if (!Regex.IsMatch(this.roomID, @"^[a-zA-Z0-9]+$"))
if (!Regex.IsMatch(roomID, @"^[a-zA-Z0-9]+$"))
{
throw new ArgumentException("RoomIdentifier can only use pure English or numbers.");
throw new ArgumentException("RoomIdentifier can only use English or numbers.");// 房间ID只能使用英文或数字
}
this.owner = owner;
owner.userRoom = this;
this.roomID = roomID;
userList = new List<User>();
userList = new();
LogManager.WriteLog($"New room created: {roomID} by {owner.userName}");
}
/// <summary>
/// User join room | 用户加入房间
Expand All @@ -111,6 +141,7 @@ public Room(User owner, string roomID)
public void Join(User user)
{
userList.Add(user);
user.userRoom = this;
}
/// <summary>
/// User leave room | 用户离开房间
Expand All @@ -119,6 +150,11 @@ public void Join(User user)
public void Leave(User user)
{
userList.Remove(user);
if (userList.Count == 0)
{
RoomManager.RemoveRoom(this);
LogManager.WriteLog($"{roomID} user all left, room removed.");
}
}
/// <summary>
/// Broadcast message to room | 广播消息到房间
Expand All @@ -145,11 +181,8 @@ public void Broadcast(string message,User? exceptUser)
/// <param name="index">索引</param>
public User this[int index]
{
get { return userList[index]; }
set
{
userList[index] = value;
}
get => userList[index];
set => userList[index] = value;
}
}

Expand Down Expand Up @@ -213,9 +246,21 @@ public void LeaveRoom()
userStatus = Status.AFK;
}

/// <summary>
/// New Room | 创建房间
/// </summary>
/// <param name="roomID">房间ID</param>
/// <exception cref="ArgumentException">房间已存在</exception>
public void CreateRoom(string roomID)
{
RoomManager.AddRoom(this, roomID);
if (RoomManager.GetRoom(roomID) != null)
{
throw new ArgumentException("RoomID already exists.");
}
else
{
RoomManager.AddRoom(this, roomID);
}
}

public void Remove()
Expand Down
Loading

0 comments on commit 8b59e4e

Please sign in to comment.