diff --git a/Api/Factory/RoomFactory.cs b/Api/Factory/RoomFactory.cs index 888cdd3..eb3400a 100644 --- a/Api/Factory/RoomFactory.cs +++ b/Api/Factory/RoomFactory.cs @@ -13,7 +13,7 @@ namespace Milimoe.FunGame.Core.Api.Factory return RoomFactory.Create(); } - public static Room Create(long Id = 0, string Roomid = "-1", DateTime? CreateTime = null, User? RoomMaster = null, RoomType RoomType = RoomType.None, RoomState RoomState = RoomState.Created, string Password = "") + public static Room Create(long Id = 0, string Roomid = "-1", DateTime? CreateTime = null, User? RoomMaster = null, RoomType RoomType = RoomType.All, RoomState RoomState = RoomState.Created, string Password = "") { return new Room(Id, Roomid, CreateTime, RoomMaster, RoomType, RoomState, Password); } diff --git a/Api/Transmittal/DataRequest.cs b/Api/Transmittal/DataRequest.cs index c4bbf10..90a3e7f 100644 --- a/Api/Transmittal/DataRequest.cs +++ b/Api/Transmittal/DataRequest.cs @@ -3,13 +3,34 @@ using Milimoe.FunGame.Core.Library.Common.Architecture; using Milimoe.FunGame.Core.Library.Common.Network; using Milimoe.FunGame.Core.Library.Constant; using Milimoe.FunGame.Core.Library.Exception; +using Milimoe.FunGame.Core.Service; namespace Milimoe.FunGame.Core.Api.Transmittal { + /// + /// 需要配合Milimoe.FunGame.Core.Library.Constant.DataRequestType使用 + /// 确保已添加对应的枚举 + /// public class DataRequest { + /// + /// 数据请求结果 + /// public RequestResult Result => Worker.Result; + + /// + /// 详细错误信息 + /// public string Error => Worker.Error; + + /// + /// 获取ResultData中key值对应的Json字符串 + /// -- 此索引器仅返回Json字符串,对象类型请使用反序列化方法GetResult() -- + /// -- 当然也可以自己反序列化 -- + /// -- 基本类型可能有效,但仍建议使用反序列化方法 -- + /// + /// + /// public object? this[string key] { get @@ -22,31 +43,55 @@ namespace Milimoe.FunGame.Core.Api.Transmittal } } + /// + /// 私有的实现类 + /// private readonly Request Worker; - public DataRequest(Socket Socket, DataRequestType RequestType) + /// + /// 基于本地已连接的Socket创建新的数据请求 + /// 使用RunTimeModel中的NewDataRequest创建一个新的请求 + /// + /// + /// + internal DataRequest(Socket Socket, DataRequestType RequestType) { Worker = new(Socket, RequestType); } + /// + /// 添加数据 + /// + /// + /// public void AddRequestData(string key, object? value) { if (Worker.RequestData.ContainsKey(key)) Worker.RequestData[key] = value; else Worker.RequestData.Add(key, value); } + /// + /// 向服务器发送数据请求 + /// + /// public RequestResult SendRequest() { Worker.SendRequest(); return Result; } + /// + /// 获取指定key对应的反序列化对象 + /// + /// + /// + /// public T? GetResult(string key) { object? obj = this[key]; if (obj != null) { - return (T)obj; + return JsonManager.GetObject(Worker.ResultData, key); } return default; } diff --git a/Api/Utility/Factory.cs b/Api/Utility/Factory.cs index 6d56b2c..876ffb9 100644 --- a/Api/Utility/Factory.cs +++ b/Api/Utility/Factory.cs @@ -73,7 +73,7 @@ namespace Milimoe.FunGame.Core.Api.Utility /// 房间状态 /// 房间密码 /// - public static Room GetRoom(long Id = 0, string Roomid = "-1", DateTime? CreateTime = null, User? RoomMaster = null, RoomType RoomType = RoomType.None, RoomState RoomState = RoomState.Created, string Password = "") + public static Room GetRoom(long Id = 0, string Roomid = "-1", DateTime? CreateTime = null, User? RoomMaster = null, RoomType RoomType = RoomType.All, RoomState RoomState = RoomState.Created, string Password = "") { return RoomFactory.Create(Id, Roomid, CreateTime, RoomMaster, RoomType, RoomState, Password); } diff --git a/Controller/RunTimeController.cs b/Controller/RunTimeController.cs index 25b1777..6c5e4cc 100644 --- a/Controller/RunTimeController.cs +++ b/Controller/RunTimeController.cs @@ -1,4 +1,5 @@ -using Milimoe.FunGame.Core.Library.Constant; +using Milimoe.FunGame.Core.Api.Transmittal; +using Milimoe.FunGame.Core.Library.Constant; namespace Milimoe.FunGame.Core.Controller { @@ -6,8 +7,6 @@ namespace Milimoe.FunGame.Core.Controller { public abstract bool Connected { get; } - public abstract Task GetServerConnection(); - public abstract Task Connect(); public abstract bool Disconnect(); @@ -19,5 +18,7 @@ namespace Milimoe.FunGame.Core.Controller public abstract Task AutoLogin(string Username, string Password, string AutoKey); public abstract void WritelnSystemInfo(string msg); + + public abstract DataRequest NewDataRequest(DataRequestType RequestType); } } diff --git a/Entity/System/Room.cs b/Entity/System/Room.cs index f9edc57..097ee1e 100644 --- a/Entity/System/Room.cs +++ b/Entity/System/Room.cs @@ -21,7 +21,7 @@ namespace Milimoe.FunGame.Core.Entity } - internal Room(long Id = 0, string Roomid = "-1", DateTime? CreateTime = null, User? RoomMaster = null, RoomType RoomType = RoomType.None, RoomState RoomState = RoomState.Created, string Password = "") + internal Room(long Id = 0, string Roomid = "-1", DateTime? CreateTime = null, User? RoomMaster = null, RoomType RoomType = RoomType.All, RoomState RoomState = RoomState.Created, string Password = "") { this.Id = Id; this.Roomid = Roomid; diff --git a/Library/Common/Event/RoomEventArgs.cs b/Library/Common/Event/RoomEventArgs.cs index c8b1445..4602d08 100644 --- a/Library/Common/Event/RoomEventArgs.cs +++ b/Library/Common/Event/RoomEventArgs.cs @@ -7,7 +7,7 @@ namespace Milimoe.FunGame.Core.Library.Common.Event { public string RoomID { get; set; } = ""; public long RoomMaster { get; set; } = 0; - public RoomType RoomType { get; set; } = RoomType.None; + public RoomType RoomType { get; set; } = RoomType.All; public RoomState RoomState { get; set; } = RoomState.Created; public bool HasPassword => Password.Trim() != ""; public string Password { get; set; } = ""; @@ -20,7 +20,7 @@ namespace Milimoe.FunGame.Core.Library.Common.Event GameMode.GameMode_Team => Constant.RoomType.Team, GameMode.GameMode_MixHasPass => Constant.RoomType.MixHasPass, GameMode.GameMode_TeamHasPass => Constant.RoomType.TeamHasPass, - _ => Constant.RoomType.None + _ => Constant.RoomType.All }; this.Password = Password; } diff --git a/Library/Common/Network/SocketObject.cs b/Library/Common/Network/SocketObject.cs index c7fcfc7..26fe9f8 100644 --- a/Library/Common/Network/SocketObject.cs +++ b/Library/Common/Network/SocketObject.cs @@ -14,8 +14,9 @@ namespace Milimoe.FunGame.Core.Library.Common.Network /// /// 从参数列表中获取指定索引的参数的Json字符串 - /// -- 此方法仅返回Json字符串,对象类型请使用反序列化方法GetParam() -- + /// -- 此索引器仅返回Json字符串,对象类型请使用反序列化方法GetParam() -- /// -- 当然也可以自己反序列化 -- + /// -- 基本类型可能有效,但仍建议使用反序列化方法 -- /// /// 索引 /// diff --git a/Library/Constant/ConstantSet.cs b/Library/Constant/ConstantSet.cs index e3c814a..30b9c85 100644 --- a/Library/Constant/ConstantSet.cs +++ b/Library/Constant/ConstantSet.cs @@ -71,9 +71,11 @@ public class GameMode { + public const string GameMode_All = "所有模式"; + public const string GameMode_AllHasPass = "带密码的所有模式"; public const string GameMode_Mix = "混战模式"; - public const string GameMode_Team = "团队模式"; public const string GameMode_MixHasPass = "带密码的混战模式"; + public const string GameMode_Team = "团队模式"; public const string GameMode_TeamHasPass = "带密码的团队模式"; } } diff --git a/Library/Constant/TypeEnum.cs b/Library/Constant/TypeEnum.cs index 30f67c5..136e86c 100644 --- a/Library/Constant/TypeEnum.cs +++ b/Library/Constant/TypeEnum.cs @@ -26,7 +26,7 @@ public enum RoomType { - None, + All, Mix, Team, MixHasPass, @@ -80,6 +80,12 @@ public enum DataRequestType { UnKnown, + GetNotice, + GetRegVerifyCode, + CreateRoom, + UpdateRoom, + GetRoomSettings, + GetRoomPlayerCount, GetFindPasswordVerifyCode } diff --git a/Model/FunGameConfig.cs b/Model/FunGameConfig.cs new file mode 100644 index 0000000..11b851b --- /dev/null +++ b/Model/FunGameConfig.cs @@ -0,0 +1,67 @@ +using Milimoe.FunGame.Core.Library.Constant; + +namespace Milimoe.FunGame.Core.Model +{ + public class FunGameConfig + { + /// + /// 是否自动连接服务器 + /// + public bool FunGame_isAutoConnect { get; set; } = true; + + /// + /// 是否自动登录 + /// + public bool FunGame_isAutoLogin { get; set; } = false; + + /// + /// 是否在匹配中 + /// + public bool FunGame_isMatching { get; set; } = false; + + /// + /// 是否连接上服务器 + /// + public bool FunGame_isConnected { get; set; } = false; + + /// + /// 是否正在重连 + /// + public bool FunGame_isRetrying { get; set; } = false; + + /// + /// 是否自动重连 + /// + public bool FunGame_isAutoRetry { get; set; } = true; + + /// + /// 当前游戏模式 + /// + public string FunGame_GameMode { get; set; } = GameMode.GameMode_Mix; + + /// + /// 服务器名称 + /// + public string FunGame_ServerName { get; set; } = ""; + + /// + /// 公告 + /// + public string FunGame_Notice { get; set; } = ""; + + /// + /// 自动登录的账号 + /// + public string FunGame_AutoLoginUser { get; set; } = ""; + + /// + /// 自动登录的密码 + /// + public string FunGame_AutoLoginPassword { get; set; } = ""; + + /// + /// 自动登录的秘钥 + /// + public string FunGame_AutoLoginKey { get; set; } = ""; + } +} diff --git a/Model/RunTime.cs b/Model/RunTime.cs index dd0537d..41ee52c 100644 --- a/Model/RunTime.cs +++ b/Model/RunTime.cs @@ -1,4 +1,5 @@ -using Milimoe.FunGame.Core.Api.Utility; +using Milimoe.FunGame.Core.Api.Transmittal; +using Milimoe.FunGame.Core.Api.Utility; using Milimoe.FunGame.Core.Controller; using Milimoe.FunGame.Core.Library.Common.Network; using Milimoe.FunGame.Core.Library.Constant; @@ -6,16 +7,46 @@ using Milimoe.FunGame.Core.Library.Exception; namespace Milimoe.FunGame.Core.Model { + /// + /// 此类实现服务器连接、断开连接、心跳检测、创建数据请求等功能 + /// -- 需要继承并实现部分方法 -- + /// public abstract class RunTime { - public abstract Socket? Socket { get; } + /// + /// 与服务器的连接套接字实例 + /// + public Socket? Socket => _Socket; + + /// + /// 套接字是否已经连接 + /// public bool Connected => _Socket != null && _Socket.Connected; + /// + /// 接收服务器信息的线程 + /// protected Task? _ReceivingTask; + + /// + /// 用于类内赋值 + /// protected Socket? _Socket; + + /// + /// 是否正在接收服务器信息 + /// protected bool _IsReceiving; + + /// + /// 是否拥有一个控制器 + /// protected RunTimeController? _Controller; + /// + /// 断开服务器连接 + /// + /// public bool Disconnect() { bool result = false; @@ -32,15 +63,43 @@ namespace Milimoe.FunGame.Core.Model return result; } - public void Disconnected() + /// + /// 获取服务器地址 + /// + /// string:IP地址;int:端口号 + /// + public (string, int) GetServerAddress() { - Disconnect(); + try + { + string? ipaddress = (string?)Implement.GetFunGameImplValue(InterfaceType.IClient, InterfaceMethod.RemoteServerIP); + if (ipaddress != null) + { + string[] s = ipaddress.Split(':'); + if (s != null && s.Length > 1) + { + return (s[0], Convert.ToInt32(s[1])); + } + } + throw new FindServerFailedException(); + } + catch (FindServerFailedException e) + { + _Controller?.WritelnSystemInfo(e.GetErrorInfo()); + return ("", 0); + } } - public abstract void GetServerConnection(); - + /// + /// 客户端需要自行实现连接服务器的事务 + /// + /// 支持异步 public abstract Task Connect(); + /// + /// 关闭所有连接 + /// + /// public bool Close() { try @@ -65,8 +124,32 @@ namespace Milimoe.FunGame.Core.Model return true; } + /// + /// 自定处理异常的方法 + /// -- 一般放在catch中 -- + /// + /// public abstract void Error(Exception e); + /// + /// 基于本地已连接的Socket创建新的数据请求 + /// + /// + /// + /// + public DataRequest NewDataRequest(DataRequestType RequestType) + { + if (_Socket != null) + { + DataRequest request = new(_Socket, RequestType); + return request; + } + throw new ConnectFailedException(); + } + + /// + /// 开始接收服务器信息 + /// protected void StartReceiving() { _ReceivingTask = Task.Factory.StartNew(() => @@ -81,6 +164,10 @@ namespace Milimoe.FunGame.Core.Model _Socket?.StartReceiving(_ReceivingTask); } + /// + /// 获取服务器已发送的信息为SocketObject数组 + /// + /// protected SocketObject[] GetServerMessage() { if (_Socket != null && _Socket.Connected) @@ -90,6 +177,10 @@ namespace Milimoe.FunGame.Core.Model return Array.Empty(); } + /// + /// 具体接收服务器信息以及处理信息的方法 + /// + /// protected SocketMessageType Receiving() { if (_Socket is null) return SocketMessageType.Unknown; @@ -114,7 +205,10 @@ namespace Milimoe.FunGame.Core.Model break; case SocketMessageType.HeartBeat: - SocketHandler_HeartBeat(ServerMessage); + if (_Socket != null && _Socket.Connected) + { + SocketHandler_HeartBeat(ServerMessage); + } break; case SocketMessageType.Unknown: @@ -131,10 +225,23 @@ namespace Milimoe.FunGame.Core.Model return result; } + /// + /// 连接服务器的处理方法 + /// + /// + /// protected abstract bool SocketHandler_Connect(SocketObject ServerMessage); + /// + /// 与服务器断开连接的处理方法 + /// + /// protected abstract void SocketHandler_Disconnect(SocketObject ServerMessage); + /// + /// 心跳检测处理方法 + /// + /// protected abstract void SocketHandler_HeartBeat(SocketObject ServerMessage); } } diff --git a/Model/Session.cs b/Model/Session.cs index faf863f..4da6e91 100644 --- a/Model/Session.cs +++ b/Model/Session.cs @@ -5,10 +5,39 @@ namespace Milimoe.FunGame.Core.Model { public class Session { - public Guid SocketToken { get; set; } = Guid.Empty; // SocketToken - public Guid LoginKey { get; set; } = Guid.Empty; // LoginKey - public User LoginUser { get; set; } = General.UnknownUserInstance; // 已登录的用户 - public string LoginUserName { get; set; } = ""; // 已登录用户名 - public Room InRoom { get; set; } = General.HallInstance; // 所处的房间 + /// + /// 服务器IP地址 + /// + public string Server_IP { get; set; } = ""; + + /// + /// 服务器端口号 + /// + public int Server_Port { get; set; } = 0; + + /// + /// SocketToken + /// + public Guid SocketToken { get; set; } = Guid.Empty; + + /// + /// LoginKey + /// + public Guid LoginKey { get; set; } = Guid.Empty; + + /// + /// 已登录的用户 + /// + public User LoginUser { get; set; } = General.UnknownUserInstance; + + /// + /// 已登录用户名 + /// + public string LoginUserName { get; set; } = ""; + + /// + /// 所处的房间 + /// + public Room InRoom { get; set; } = General.HallInstance; } } diff --git a/Service/JsonManager.cs b/Service/JsonManager.cs index 8b81766..c8c896f 100644 --- a/Service/JsonManager.cs +++ b/Service/JsonManager.cs @@ -1,4 +1,6 @@ -using System.Text.Json; +using System; +using System.Collections; +using System.Text.Json; using System.Text.Json.Serialization; using Milimoe.FunGame.Core.Library.Common.JsonConverter; using Milimoe.FunGame.Core.Library.Common.Network; @@ -58,19 +60,6 @@ namespace Milimoe.FunGame.Core.Service return JsonSerializer.Deserialize(json, GeneralOptions); } - /// - /// 反序列化多个Json对象 - /// 注意必须是相同的Json对象才可以使用此方法解析 - /// - /// - /// - /// - internal static List GetObjects(string json) - { - json = "[" + json.Replace("}{", "},{") + "]"; // 将Json字符串转换为数组 - return JsonSerializer.Deserialize>(json, GeneralOptions) ?? new List(); - } - /// /// 反序列化SocketObject中索引为index的Json对象 /// @@ -86,5 +75,39 @@ namespace Milimoe.FunGame.Core.Service T? result = element.Deserialize(GeneralOptions); return result; } + + /// + /// 反序列化Hashtable中Key对应的Json对象 + /// + /// + /// + /// + /// + internal static T? GetObject(Hashtable table, string key) + { + if (table.ContainsKey(key)) + { + JsonElement? element = (JsonElement?)table[key]; + if (element != null) + { + T? result = ((JsonElement)element).Deserialize(GeneralOptions); + return result; + } + } + return default; + } + + /// + /// 反序列化多个Json对象 + /// 注意必须是相同的Json对象才可以使用此方法解析 + /// + /// + /// + /// + internal static List GetObjects(string json) + { + json = "[" + json.Replace("}{", "},{") + "]"; // 将Json字符串转换为数组 + return JsonSerializer.Deserialize>(json, GeneralOptions) ?? new List(); + } } }