Skip to content

Commit

Permalink
Fix: Users can repeatedly join rooms
Browse files Browse the repository at this point in the history
  • Loading branch information
NuanRMxi committed Sep 9, 2024
1 parent 0924819 commit 59fef9f
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 29 deletions.
27 changes: 24 additions & 3 deletions ConnectionMessage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -120,9 +120,9 @@ public class LeaveRoomSuccess : Message
public new readonly string action = "leaveRoomSuccess";
}
/// <summary>
/// New user join room message | 新用户加入房间消息
/// User join room message | 新用户加入房间消息
/// </summary>
public class NewUserJoinRoom : Message
public class UserJoinRoom : Message
{
public readonly string action = "newUserJoinRoom";

Check warning on line 127 in ConnectionMessage.cs

View workflow job for this annotation

GitHub Actions / build-windows

'ConnectionMessage.Server.UserJoinRoom.action' hides inherited member 'ConnectionMessage.Message.action'. Use the new keyword if hiding was intended.

Check warning on line 127 in ConnectionMessage.cs

View workflow job for this annotation

GitHub Actions / build-linux

'ConnectionMessage.Server.UserJoinRoom.action' hides inherited member 'ConnectionMessage.Message.action'. Use the new keyword if hiding was intended.
public Data data = new Data();
Expand All @@ -133,14 +133,35 @@ public class Data
public string avatarUrl = "";
public string roomID = "";
}
public NewUserJoinRoom(string userName, bool isSpectator, string avatarUrl, string roomID)
public UserJoinRoom(string userName, bool isSpectator, string avatarUrl, string roomID)
{
data.userName = userName;
data.isSpectator = isSpectator;
data.avatarUrl = avatarUrl;
data.roomID = roomID;
}
}

/// <summary>
/// User leave room message | 用户离开房间消息
/// </summary>
public class UserLeaveRoom : Message
{
public new readonly string action = "userLeaveRoom";
public Data data = new Data();
public class Data
{
public string userName = "";
public string roomID = "";
}
public UserLeaveRoom(string userName, string roomID)
{
data.userName = userName;
data.roomID = roomID;
}
}


}

/// <summary>
Expand Down
38 changes: 17 additions & 21 deletions GameManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public static class RoomManager
public static void AddRoom(User user, string roomID)
{
RoomList.Add(new Room(user, roomID));

user.status = User.Status.InRoom;
}

Expand Down Expand Up @@ -73,30 +73,18 @@ public static void AddUser(string name, IWebSocketConnection socket, User.UserCo
UserList.Add(new User(name, socket, config));
}

[Obsolete("Use RemoveUser instead. | 使用 RemoveUser 代替.")]
public static void Drop(IWebSocketConnection socket)
{
RemoveUser(socket);
}

/// <summary>
/// Remove user | 移除用户
/// </summary>
/// <param name="socket">用户对应Socket</param>
public static void RemoveUser(IWebSocketConnection socket)
{
for (int i = UserList.Count - 1; i >= 0; i--)
var user = GetUser(socket);
if (user.room != null)
{
if (UserList[i].userSocket == socket)
{
if (UserList[i].room != null)
{
UserList[i].room!.Leave(UserList[i]);
}

UserList.RemoveAt(i);
}
user.room!.Leave(user);
}
UserList.Remove(user);
}

public static bool Contains(IWebSocketConnection socket)
Expand Down Expand Up @@ -153,14 +141,14 @@ public void Join(User user)
{
userList.Add(user);
user.room = this;
user.status = User.Status.InRoom;
Broadcast(JsonConvert.SerializeObject(
new ConnectionMessage.Server.NewUserJoinRoom(
new ConnectionMessage.Server.UserJoinRoom(
user.name,
user.userConfig!.isSpectator,
user.avatarUrl,
roomID
)),user);

)), user);
}

/// <summary>
Expand All @@ -175,13 +163,21 @@ public void Leave(User user)
RoomManager.RemoveRoom(this);
LogManager.WriteLog($"{roomID} user all left, room removed.");
}
else
{
Broadcast(JsonConvert.SerializeObject(
new ConnectionMessage.Server.UserLeaveRoom(
user.name,
roomID
)), user);
}
}

/// <summary>
/// Broadcast message to room | 广播消息到房间
/// </summary>
/// <param name="message">被广播信息</param>
public void Broadcast(string message, User? exceptUser = null)
private void Broadcast(string message, User? exceptUser = null)
{
foreach (var user in userList)
{
Expand Down
6 changes: 1 addition & 5 deletions Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ private static void Main(string[] args)
);
}

LogManager.WriteLog("The server has been started");
//LogManager.WriteLog("The server has been started");
if (config.ws)
{
LogManager.WriteLog($"The server is listening on ws://{config.wsOptions.Host}:{config.wsOptions.Port}");
Expand Down Expand Up @@ -186,9 +186,6 @@ private static void Main(string[] args)
{
LogManager.WriteLog("Invalid command, please check the command again.", LogManager.LogLevel.Warning);
}
//catch
//{ }

}
}

Expand Down Expand Up @@ -307,7 +304,6 @@ await socket.Send(JsonConvert.SerializeObject(new ConnectionMessage.Server.JoinS
// To ConnectionMessage.Client.NewRoom | 将客户端发送的消息转换为 ConnectionMessage.Client.NewRoom
ConnectionMessage.Client.NewRoom newRoom =
JsonConvert.DeserializeObject<ConnectionMessage.Client.NewRoom>(message)!;
// TODO: User creates room logic | 创建房间逻辑
var user = GameManager.UserManager.GetUser(socket);
if (user.status == GameManager.User.Status.InRoom)
{
Expand Down

0 comments on commit 59fef9f

Please sign in to comment.