diff --git a/FunGame.Core/Entity/System/Room.cs b/FunGame.Core/Entity/System/Room.cs index ac205e5..ec6c850 100644 --- a/FunGame.Core/Entity/System/Room.cs +++ b/FunGame.Core/Entity/System/Room.cs @@ -1,28 +1,53 @@ -using Milimoe.FunGame.Core.Library.Constant; +using Milimoe.FunGame.Core.Interface.Entity; +using Milimoe.FunGame.Core.Library.Constant; using System.Collections; namespace Milimoe.FunGame.Core.Entity { - public class Room + public class Room : BaseEntity { - public int Id { get; set; } public string Roomid { get; set; } = ""; public DateTime Time { get; set; } = DateTime.Now; public Hashtable PlayerList { get; set; } = new Hashtable(); public User? RoomMaster { get; set; } public RoomType RoomType { get; set; } public RoomState RoomState { get; set; } + public bool HasPass + { + get + { + if (RoomType == RoomType.MixHasPass || RoomType == RoomType.TeamHasPass) + return true; + else return false; + } + } + public string Password { get; set; } = ""; public GameStatistics? Statistics { get; set; } = null; internal Room(User? master = null) { if (master != null) RoomMaster = master; } - + internal Room(string roomid, User? master = null) { Roomid = roomid; if (master != null) RoomMaster = master; } + + public bool Equals(Room other) + { + return other.Id == Id; + } + + public override IEnumerator GetEnumerator() + { + return GetEnumerator(); + } + + public override bool Equals(IBaseEntity? other) + { + return Equals(other); + } } } diff --git a/FunGame.Core/Library/Common/Network/ClientSocket.cs b/FunGame.Core/Library/Common/Network/ClientSocket.cs index 218b964..97f8d15 100644 --- a/FunGame.Core/Library/Common/Network/ClientSocket.cs +++ b/FunGame.Core/Library/Common/Network/ClientSocket.cs @@ -23,12 +23,13 @@ namespace Milimoe.FunGame.Core.Library.Common.Network private bool _Receiving; private string _ClientName; - public ClientSocket(System.Net.Sockets.Socket Instance, int ServerPort, string ClientIP, string ClientName) + public ClientSocket(System.Net.Sockets.Socket Instance, int ServerPort, string ClientIP, string ClientName, Guid Token) { this.Instance= Instance; this.ServerPort = ServerPort; this.ClientIP = ClientIP; this._ClientName = ClientName; + this.Token = Token; } public void Close() diff --git a/FunGame.Core/Library/Common/Network/ServerSocket.cs b/FunGame.Core/Library/Common/Network/ServerSocket.cs index 64ec1e0..08748d2 100644 --- a/FunGame.Core/Library/Common/Network/ServerSocket.cs +++ b/FunGame.Core/Library/Common/Network/ServerSocket.cs @@ -38,14 +38,14 @@ namespace Milimoe.FunGame.Core.Library.Common.Network else throw new SocketCreateListenException(); } - public ClientSocket Accept() + public ClientSocket Accept(Guid Token) { object[] result = SocketManager.Accept(); if (result != null && result.Length == 2) { string ClientIP = (string)result[0]; System.Net.Sockets.Socket Client = (System.Net.Sockets.Socket)result[1]; - return new ClientSocket(Client, ServerPort, ClientIP, ClientIP); + return new ClientSocket(Client, ServerPort, ClientIP, ClientIP, Token); } throw new SocketGetClientException(); } diff --git a/FunGame.Core/Library/Server/RoomList.cs b/FunGame.Core/Library/Server/RoomList.cs new file mode 100644 index 0000000..4e96bd4 --- /dev/null +++ b/FunGame.Core/Library/Server/RoomList.cs @@ -0,0 +1,45 @@ +using System.Collections; +using Milimoe.FunGame.Core.Entity; +using Milimoe.FunGame.Core.Library.Common.Network; + +namespace Milimoe.FunGame.Core.Library.Server +{ + public class RoomList + { + public ServerSocket Server => _Server; + public int Count => _List.Count; + + private readonly ServerSocket _Server; + private readonly Hashtable _List = new(); + + public RoomList(ServerSocket Server) + { + _Server = Server; + } + + public List GetList() + { + return _List.Values.Cast().ToList(); + } + + public void AddRoom(Room Room) + { + _List.Add(Room.Name, Room); + } + + public void RemoveRoom(Room Room) + { + _List.Remove(Room.Name); + } + + public Room? GetRoom(string RoomID) + { + Room? room = null; + if (_List.ContainsKey(RoomID)) + { + room = (Room?)_List[RoomID]; + } + return room; + } + } +} diff --git a/FunGame.Core/Service/SocketManager.cs b/FunGame.Core/Service/SocketManager.cs index 3366fbf..addf503 100644 --- a/FunGame.Core/Service/SocketManager.cs +++ b/FunGame.Core/Service/SocketManager.cs @@ -155,7 +155,7 @@ namespace Milimoe.FunGame.Core.Service /// /// 用于客户端接收服务器信息 /// - /// 通信类型[0]和参数[1] + /// SocketObject internal static Library.Common.Network.SocketObject Receive() { Library.Common.Network.SocketObject result = default; @@ -184,7 +184,7 @@ namespace Milimoe.FunGame.Core.Service /// 用于服务器接收客户端信息 /// /// 客户端Socket - /// 通信类型[0]、Token[1]和参数[2] + /// SocketObject internal static Library.Common.Network.SocketObject Receive(Socket ClientSocket) { Library.Common.Network.SocketObject result = default; diff --git a/FunGame.Desktop/Model/RunTimeModel.cs b/FunGame.Desktop/Model/RunTimeModel.cs index d0e61fc..fbf4c25 100644 --- a/FunGame.Desktop/Model/RunTimeModel.cs +++ b/FunGame.Desktop/Model/RunTimeModel.cs @@ -137,6 +137,11 @@ namespace Milimoe.FunGame.Desktop.Model } } } + else + { + Config.FunGame_isRetrying = false; + Socket.Close(); + } }); return ConnectResult.Success; } @@ -238,7 +243,7 @@ namespace Milimoe.FunGame.Desktop.Model switch (type) { case SocketMessageType.Connect: - SocketHandler_Connect(ServerMessage); + if (!SocketHandler_Connect(ServerMessage)) return SocketMessageType.Unknown; break; case SocketMessageType.Disconnect: @@ -267,12 +272,20 @@ namespace Milimoe.FunGame.Desktop.Model #region SocketHandler - private void SocketHandler_Connect(SocketObject ServerMessage) + private bool SocketHandler_Connect(SocketObject ServerMessage) { string msg = ""; Guid token = Guid.Empty; if (ServerMessage.Parameters.Length > 0) msg = ServerMessage.GetParam(0)!; string[] strings = msg.Split(';'); + if (strings.Length != 2) + { + // 服务器拒绝连接 + msg = strings[0]; + Main.GetMessage(msg); + ShowMessage.ErrorMessage(msg); + return false; + } string ServerName = strings[0]; string ServerNotice = strings[1]; Config.FunGame_ServerName = ServerName; @@ -283,6 +296,7 @@ namespace Milimoe.FunGame.Desktop.Model Main.GetMessage($"已连接服务器:{ServerName}。\n\n********** 服务器公告 **********\n\n{ServerNotice}\n\n"); // 设置等待登录的黄灯 Main.UpdateUI(MainInvokeType.WaitLoginAndSetYellow); + return true; } private void SocketHandler_Disconnect(SocketObject ServerMessage)