diff --git a/Api/Transmittal/SQLHelper.cs b/Api/Transmittal/SQLHelper.cs index 7f8efe2..063cc0e 100644 --- a/Api/Transmittal/SQLHelper.cs +++ b/Api/Transmittal/SQLHelper.cs @@ -18,6 +18,7 @@ namespace Milimoe.FunGame.Core.Api.Transmittal public abstract SQLServerInfo ServerInfo { get; } public abstract int UpdateRows { get; } public abstract DataSet DataSet { get; } + public abstract Dictionary Parameters { get; } public bool Success => Result == SQLResult.Success; /// diff --git a/Api/Utility/TaskScheduler.cs b/Api/Utility/TaskScheduler.cs index f73a0f8..f0daa42 100644 --- a/Api/Utility/TaskScheduler.cs +++ b/Api/Utility/TaskScheduler.cs @@ -12,7 +12,6 @@ namespace Milimoe.FunGame.Core.Api.Utility private readonly List _tasks = []; private readonly List _recurringTasks = []; - private readonly Timer _timer; private readonly Lock _lock = new(); /// @@ -20,8 +19,14 @@ namespace Milimoe.FunGame.Core.Api.Utility /// public TaskScheduler() { - _timer = new Timer(CheckAndRunTasks, null, TimeSpan.Zero, TimeSpan.FromSeconds(1)); - _timer.Change(TimeSpan.Zero, TimeSpan.FromSeconds(1)); + Task.Factory.StartNew(async () => + { + while (true) + { + CheckAndRunTasks(); + await Task.Delay(1000); + } + }, TaskCreationOptions.LongRunning); } /// @@ -158,8 +163,7 @@ namespace Milimoe.FunGame.Core.Api.Utility /// /// 执行任务 /// - /// - private void CheckAndRunTasks(object? state) + private void CheckAndRunTasks() { lock (_lock) { @@ -172,7 +176,7 @@ namespace Milimoe.FunGame.Core.Api.Utility if (now.TimeOfDay >= task.TimeOfDay && now.TimeOfDay < task.TimeOfDay.Add(TimeSpan.FromSeconds(10))) { task.LastRun = now; - ThreadPool.QueueUserWorkItem(_ => + Task.Run(() => { try { @@ -193,7 +197,7 @@ namespace Milimoe.FunGame.Core.Api.Utility { recurringTask.LastRun = now; recurringTask.NextRun = recurringTask.NextRun.Add(recurringTask.Interval); - ThreadPool.QueueUserWorkItem(_ => + Task.Run(() => { try { diff --git a/Controller/RunTimeController.cs b/Controller/RunTimeController.cs index 268b392..420d9c0 100644 --- a/Controller/RunTimeController.cs +++ b/Controller/RunTimeController.cs @@ -17,6 +17,11 @@ namespace Milimoe.FunGame.Core.Controller /// 与服务器的连接套接字实例 /// public Socket? Socket => _Socket; + + /// + /// 与服务器的连接套接字实例(WebSocket) + /// + public HTTPClient? WebSocket => _WebSocket; /// /// 套接字是否已经连接 @@ -32,6 +37,11 @@ namespace Milimoe.FunGame.Core.Controller /// 用于类内赋值 /// protected Socket? _Socket; + + /// + /// 用于类内赋值 + /// + protected HTTPClient? _WebSocket; /// /// 是否正在接收服务器信息 @@ -79,88 +89,196 @@ namespace Milimoe.FunGame.Core.Controller } /// - /// 连接服务器 + /// 连接服务器 [ 可选参数需要根据连接方式提供 ] + /// 建议使用异步版,此方法为兼容性处理 /// - /// + /// + /// /// + /// + /// /// - public ConnectResult Connect(string addr, int port) + public ConnectResult Connect(TransmittalType type, string address, int port, bool ssl = false, string subUrl = "") { - ArrayList ConnectArgs = []; - if (!BeforeConnect(ref addr, ref port, ConnectArgs)) + return Task.Run(() => ConnectAsync(type, address, port, ssl, subUrl)).Result; + } + + /// + /// 连接服务器 [ 异步版,可选参数需要根据连接方式提供 ] + /// + /// + /// + /// + /// + /// + /// + public async Task ConnectAsync(TransmittalType type, string address, int port = 0, bool ssl = false, string subUrl = "") + { + ArrayList connectArgs = []; + if (!BeforeConnect(ref address, ref port, connectArgs)) { return ConnectResult.ConnectFailed; } ConnectResult result = ConnectResult.Success; - string msg = ""; - string servername = ""; + string msg; + string serverName = ""; string notice = ""; - // 检查服务器IP地址和端口是否正确 - if (addr == "" || port <= 0) + // 检查服务器地址和端口是否正确 + if (address == "" || (type == TransmittalType.Socket && port <= 0) || (type == TransmittalType.WebSocket && port < 0)) { result = ConnectResult.FindServerFailed; } if (result == ConnectResult.Success) { // 与服务器建立连接 - _Socket?.Close(); - _Socket = Socket.Connect(addr, port); - if (_Socket != null && _Socket.Connected) + if (type == TransmittalType.Socket) { - if (_Socket.Send(SocketMessageType.Connect, ConnectArgs.Cast().ToArray()) == SocketResult.Success) - { - SocketObject[] objs = _Socket.Receive(); - foreach (SocketObject obj in objs) - { - if (obj.SocketType == SocketMessageType.Connect) - { - bool success = obj.GetParam(0); - msg = obj.GetParam(1) ?? ""; - result = success ? ConnectResult.Success : ConnectResult.ConnectFailed; - if (success) - { - _Socket.Token = obj.GetParam(2); - servername = obj.GetParam(3) ?? ""; - notice = obj.GetParam(4) ?? ""; - StartReceiving(); - Task.Run(() => - { - while (true) - { - if (_IsReceiving) - { - break; - } - } - }); - } - } - } - } - else result = ConnectResult.ConnectFailed; + connectArgs = await Connect_Socket(connectArgs, address, port); + } + else if (type == TransmittalType.WebSocket) + { + connectArgs = await Connect_WebSocket(connectArgs, address, ssl, port, subUrl); + } + else + { + result = ConnectResult.FindServerFailed; + msg = "连接服务器失败,未指定连接方式。"; + connectArgs = [result, msg, serverName, notice]; } - else _Socket?.Close(); } - ConnectArgs.Clear(); - ConnectArgs = [result, msg, servername, notice]; - AfterConnect(ConnectArgs); + AfterConnect(connectArgs); // 允许修改数组中的result,强行改变连接的结果 - if (ConnectArgs.Count > 0) + if (connectArgs.Count > 0) { - result = (ConnectResult?)ConnectArgs[0] ?? result; + result = (ConnectResult?)connectArgs[0] ?? result; } return result; } + /// + /// 使用 Socket 方式连接服务器 + /// + /// + /// + /// + /// + private async Task Connect_Socket(ArrayList connectArgs, string address, int port) + { + ConnectResult result = ConnectResult.Success; + string msg = ""; + string serverName = ""; + string notice = ""; + + _Socket?.Close(); + _Socket = Socket.Connect(address, port); + if (_Socket != null && _Socket.Connected) + { + if (_Socket.Send(SocketMessageType.Connect, connectArgs.Cast().ToArray()) == SocketResult.Success) + { + SocketObject[] objs = _Socket.Receive(); + foreach (SocketObject obj in objs) + { + if (obj.SocketType == SocketMessageType.Connect) + { + bool success = obj.GetParam(0); + msg = obj.GetParam(1) ?? ""; + result = success ? ConnectResult.Success : ConnectResult.ConnectFailed; + if (success) + { + _Socket.Token = obj.GetParam(2); + serverName = obj.GetParam(3) ?? ""; + notice = obj.GetParam(4) ?? ""; + StartReceiving(); + await Task.Run(() => + { + while (true) + { + if (_IsReceiving) + { + break; + } + } + }); + } + } + } + } + else result = ConnectResult.ConnectFailed; + } + else _Socket?.Close(); + + return [result, msg, serverName, notice]; + } + + /// + /// 使用 WebSocket 方式连接服务器 + /// + /// + /// + /// + /// + /// + /// + private async Task Connect_WebSocket(ArrayList connectArgs, string address, bool ssl, int port, string subUrl) + { + ConnectResult result = ConnectResult.Success; + string msg = ""; + string serverName = ""; + string notice = ""; + + _WebSocket?.Close(); + _WebSocket = await HTTPClient.Connect(address, ssl, port, subUrl, connectArgs.Cast().ToArray()); + if (_WebSocket.Connected) + { + bool webSocketConnected = false; + _WebSocket.AddSocketObjectHandler(obj => + { + try + { + if (obj.SocketType == SocketMessageType.Connect) + { + bool success = obj.GetParam(0); + msg = obj.GetParam(1) ?? ""; + result = success ? ConnectResult.Success : ConnectResult.ConnectFailed; + if (success) + { + _WebSocket.Token = obj.GetParam(2); + serverName = obj.GetParam(3) ?? ""; + notice = obj.GetParam(4) ?? ""; + } + webSocketConnected = true; + return; + } + HandleSocketMessage(TransmittalType.WebSocket, obj); + } + catch (Exception e) + { + Error(e); + } + }); + while (!webSocketConnected) + { + await Task.Delay(100); + } + } + else + { + _WebSocket?.Close(); + result = ConnectResult.ConnectFailed; + } + + return [result, msg, serverName, notice]; + } + /// /// 获取服务器地址 /// - /// string:IP地址;int:端口号 + /// string:服务器地址;int:端口号 /// public (string, int) GetServerAddress() { @@ -188,11 +306,11 @@ namespace Milimoe.FunGame.Core.Controller /// 此方法将在连接服务器前触发 /// 客户端可以重写此方法 /// - /// 服务器IP + /// 服务器地址 /// 服务器端口 /// 重写时可以提供额外的连接参数 /// false:中止连接 - public virtual bool BeforeConnect(ref string ip, ref int port, ArrayList args) + public virtual bool BeforeConnect(ref string address, ref int port, ArrayList args) { return true; } @@ -211,13 +329,16 @@ namespace Milimoe.FunGame.Core.Controller /// /// 客户端需要自行实现自动登录的事务 /// - public abstract void AutoLogin(string Username, string Password, string AutoKey); + public virtual void AutoLogin(string Username, string Password, string AutoKey) + { + + } /// - /// 关闭所有连接 + /// 关闭 Socket 连接 /// /// - public bool Close() + public bool Close_Socket() { try { @@ -240,6 +361,28 @@ namespace Milimoe.FunGame.Core.Controller } return true; } + + /// + /// 关闭 WebSocket 连接 + /// + /// + public bool Close_WebSocket() + { + try + { + if (_WebSocket != null) + { + _WebSocket.Close(); + _WebSocket = null; + } + } + catch (Exception e) + { + WritelnSystemInfo(e.GetErrorInfo()); + return false; + } + return true; + } /// /// 输出消息 @@ -267,6 +410,11 @@ namespace Milimoe.FunGame.Core.Controller DataRequest request = new(_Socket, RequestType); return request; } + else if (_WebSocket != null) + { + DataRequest request = new(_WebSocket, RequestType); + return request; + } throw new ConnectFailedException(); } @@ -283,6 +431,11 @@ namespace Milimoe.FunGame.Core.Controller DataRequest request = new(_Socket, RequestType, true); return request; } + else if (_WebSocket != null) + { + DataRequest request = new(_WebSocket, RequestType, true); + return request; + } throw new ConnectFailedException(); } @@ -300,6 +453,11 @@ namespace Milimoe.FunGame.Core.Controller DataRequest request = new(_Socket, RequestType, false, SocketRuntimeType.Addon); return request; } + else if (_WebSocket != null) + { + DataRequest request = new(_WebSocket, RequestType, false, SocketRuntimeType.Addon); + return request; + } throw new ConnectFailedException(); } @@ -317,6 +475,11 @@ namespace Milimoe.FunGame.Core.Controller DataRequest request = new(_Socket, RequestType, true, SocketRuntimeType.Addon); return request; } + else if (_WebSocket != null) + { + DataRequest request = new(_WebSocket, RequestType, true, SocketRuntimeType.Addon); + return request; + } throw new ConnectFailedException(); } @@ -334,6 +497,11 @@ namespace Milimoe.FunGame.Core.Controller DataRequest request = new(_Socket, GamingType, false, SocketRuntimeType.Addon); return request; } + else if (_WebSocket != null) + { + DataRequest request = new(_WebSocket, GamingType, false, SocketRuntimeType.Addon); + return request; + } throw new ConnectFailedException(); } @@ -351,11 +519,16 @@ namespace Milimoe.FunGame.Core.Controller DataRequest request = new(_Socket, GamingType, true, SocketRuntimeType.Addon); return request; } + else if (_WebSocket != null) + { + DataRequest request = new(_WebSocket, GamingType, true, SocketRuntimeType.Addon); + return request; + } throw new ConnectFailedException(); } /// - /// 开始接收服务器信息 + /// 开始接收服务器信息 [ Socket Only ] /// protected void StartReceiving() { @@ -372,7 +545,7 @@ namespace Milimoe.FunGame.Core.Controller } /// - /// 获取服务器已发送的信息为SocketObject数组 + /// 获取服务器已发送的信息为SocketObject数组 [ Socket Only ] /// /// protected SocketObject[] GetServerMessage() @@ -385,7 +558,7 @@ namespace Milimoe.FunGame.Core.Controller } /// - /// 具体接收服务器信息以及处理信息的方法 + /// 具体接收服务器信息以及处理信息的方法 [ Socket Only ] /// /// protected SocketMessageType Receiving() @@ -394,60 +567,11 @@ namespace Milimoe.FunGame.Core.Controller SocketMessageType result = SocketMessageType.Unknown; try { - SocketObject[] ServerMessages = GetServerMessage(); + SocketObject[] messages = GetServerMessage(); - foreach (SocketObject ServerMessage in ServerMessages) + foreach (SocketObject obj in messages) { - SocketMessageType type = ServerMessage.SocketType; - object[] objs = ServerMessage.Parameters; - result = type; - switch (type) - { - case SocketMessageType.Disconnect: - Close(); - SocketHandler_Disconnect(ServerMessage); - break; - - case SocketMessageType.System: - SocketHandler_System(ServerMessage); - break; - - case SocketMessageType.HeartBeat: - SocketHandler_HeartBeat(ServerMessage); - break; - - case SocketMessageType.ForceLogout: - SocketHandler_ForceLogout(ServerMessage); - break; - - case SocketMessageType.Chat: - SocketHandler_Chat(ServerMessage); - break; - - case SocketMessageType.UpdateRoomMaster: - SocketHandler_UpdateRoomMaster(ServerMessage); - break; - - case SocketMessageType.MatchRoom: - SocketHandler_MatchRoom(ServerMessage); - break; - - case SocketMessageType.StartGame: - SocketHandler_StartGame(ServerMessage); - break; - - case SocketMessageType.EndGame: - SocketHandler_EndGame(ServerMessage); - break; - - case SocketMessageType.Gaming: - SocketHandler_Gaming(ServerMessage); - break; - - case SocketMessageType.Unknown: - default: - break; - } + result = HandleSocketMessage(TransmittalType.Socket, obj); } } catch (Exception e) @@ -458,6 +582,73 @@ namespace Milimoe.FunGame.Core.Controller return result; } + /// + /// 处理接收到的信息 + /// + /// + /// + /// + protected SocketMessageType HandleSocketMessage(TransmittalType transmittalType, SocketObject obj) + { + SocketMessageType type = obj.SocketType; + SocketMessageType result = type; + switch (type) + { + case SocketMessageType.Disconnect: + if (transmittalType == TransmittalType.Socket) + { + Close_Socket(); + } + else if (transmittalType == TransmittalType.WebSocket) + { + Close_WebSocket(); + } + SocketHandler_Disconnect(obj); + break; + + case SocketMessageType.System: + SocketHandler_System(obj); + break; + + case SocketMessageType.HeartBeat: + SocketHandler_HeartBeat(obj); + break; + + case SocketMessageType.ForceLogout: + SocketHandler_ForceLogout(obj); + break; + + case SocketMessageType.Chat: + SocketHandler_Chat(obj); + break; + + case SocketMessageType.UpdateRoomMaster: + SocketHandler_UpdateRoomMaster(obj); + break; + + case SocketMessageType.MatchRoom: + SocketHandler_MatchRoom(obj); + break; + + case SocketMessageType.StartGame: + SocketHandler_StartGame(obj); + break; + + case SocketMessageType.EndGame: + SocketHandler_EndGame(obj); + break; + + case SocketMessageType.Gaming: + SocketHandler_Gaming(obj); + break; + + case SocketMessageType.Unknown: + default: + break; + } + return result; + } + /// /// 客户端接收服务器断开连接的通知 /// @@ -468,54 +659,81 @@ namespace Milimoe.FunGame.Core.Controller /// 客户端接收并处理服务器系统消息 /// /// - protected abstract void SocketHandler_System(SocketObject ServerMessage); + protected virtual void SocketHandler_System(SocketObject ServerMessage) + { + + } /// /// 客户端接收并处理服务器心跳 /// /// - protected abstract void SocketHandler_HeartBeat(SocketObject ServerMessage); + protected virtual void SocketHandler_HeartBeat(SocketObject ServerMessage) + { + + } /// /// 客户端接收强制退出登录的通知 /// /// - protected abstract void SocketHandler_ForceLogout(SocketObject ServerMessage); + protected virtual void SocketHandler_ForceLogout(SocketObject ServerMessage) + { + + } /// /// 客户端接收并处理聊天信息 /// /// - protected abstract void SocketHandler_Chat(SocketObject ServerMessage); + protected virtual void SocketHandler_Chat(SocketObject ServerMessage) + { + + } /// /// 客户端接收并处理更换房主信息 /// /// - protected abstract void SocketHandler_UpdateRoomMaster(SocketObject ServerMessage); + protected virtual void SocketHandler_UpdateRoomMaster(SocketObject ServerMessage) + { + + } /// /// 客户端接收并处理匹配房间成功信息 /// /// - protected abstract void SocketHandler_MatchRoom(SocketObject ServerMessage); + protected virtual void SocketHandler_MatchRoom(SocketObject ServerMessage) + { + + } /// /// 客户端接收并处理开始游戏信息 /// /// - protected abstract void SocketHandler_StartGame(SocketObject ServerMessage); + protected virtual void SocketHandler_StartGame(SocketObject ServerMessage) + { + + } /// /// 客户端接收并处理游戏结束信息 /// /// - protected abstract void SocketHandler_EndGame(SocketObject ServerMessage); + protected virtual void SocketHandler_EndGame(SocketObject ServerMessage) + { + + } /// /// 客户端接收并处理局内消息 /// /// - protected abstract void SocketHandler_Gaming(SocketObject ServerMessage); + protected virtual void SocketHandler_Gaming(SocketObject ServerMessage) + { + + } } } diff --git a/Entity/Character/CharacterBuilder.cs b/Entity/Character/CharacterBuilder.cs index 8ebba27..d2e86e2 100644 --- a/Entity/Character/CharacterBuilder.cs +++ b/Entity/Character/CharacterBuilder.cs @@ -104,9 +104,8 @@ namespace Milimoe.FunGame.Core.Entity /// /// /// - /// /// 构建的新角色 - public Character Build(IEnumerable skills, IEnumerable items, bool newItemGuid = true, EquipSlot? equips = null, Inventory? inventory = null, IEnumerable? itemsDefined = null, IEnumerable? skillsDefined = null, bool recovery = true) + public Character Build(IEnumerable skills, IEnumerable items, bool newItemGuid = true, EquipSlot? equips = null, Inventory? inventory = null, IEnumerable? itemsDefined = null, IEnumerable? skillsDefined = null) { Character character = Factory.GetCharacter(); character.Id = Id; @@ -227,7 +226,7 @@ namespace Milimoe.FunGame.Core.Entity } } } - if (recovery) character.Recovery(); + character.Recovery(); return character; } @@ -245,7 +244,7 @@ namespace Milimoe.FunGame.Core.Entity /// 构建的新角色 public static Character Build(Character reference, bool newItemGuid = true, bool copyLevel = true, Inventory? inventory = null, IEnumerable? itemsDefined = null, IEnumerable? skillsDefined = null, bool recovery = true) { - Character character = new CharacterBuilder(reference).Build(reference.Skills, reference.Items, newItemGuid, reference.EquipSlot, inventory, itemsDefined, skillsDefined, recovery); + Character character = new CharacterBuilder(reference).Build(reference.Skills, reference.Items, newItemGuid, reference.EquipSlot, inventory, itemsDefined, skillsDefined); if (copyLevel) { character.Level = reference.Level; @@ -255,6 +254,11 @@ namespace Milimoe.FunGame.Core.Entity character.NormalAttack.Level = reference.NormalAttack.Level; character.NormalAttack.HardnessTime = reference.NormalAttack.HardnessTime; character.NormalAttack.SetMagicType(reference.NormalAttack.IsMagic, reference.NormalAttack.MagicType); + if (!recovery) + { + character.HP = reference.HP; + character.MP = reference.MP; + } return character; } } diff --git a/Entity/System/Inventory.cs b/Entity/System/Inventory.cs index 2b0c281..56a0236 100644 --- a/Entity/System/Inventory.cs +++ b/Entity/System/Inventory.cs @@ -1,4 +1,5 @@ using System.Text; +using Milimoe.FunGame.Core.Api.Utility; using Milimoe.FunGame.Core.Library.Constant; using Milimoe.FunGame.Core.Model; @@ -57,7 +58,7 @@ namespace Milimoe.FunGame.Core.Entity _character = Characters.First(); return _character; } - throw new GetInstanceException(); + return Factory.GetCharacter(); } set { diff --git a/Interface/Base/IEntityConverter.cs b/Interface/Base/IEntityConverter.cs index a69655f..47a85e3 100644 --- a/Interface/Base/IEntityConverter.cs +++ b/Interface/Base/IEntityConverter.cs @@ -6,6 +6,8 @@ namespace Milimoe.FunGame.Core.Interface.Base { public T NewInstance(); - public void ReadPropertyName(ref Utf8JsonReader reader, string propertyName, JsonSerializerOptions options, ref T result); + public void ReadPropertyName(ref Utf8JsonReader reader, string propertyName, JsonSerializerOptions options, ref T result, Dictionary convertingContext); + + public void AfterConvert(ref T result, Dictionary convertingContext); } } diff --git a/Library/Common/Architecture/Authenticator.cs b/Library/Common/Architecture/Authenticator.cs index b7cbe70..7fd73e4 100644 --- a/Library/Common/Architecture/Authenticator.cs +++ b/Library/Common/Architecture/Authenticator.cs @@ -57,7 +57,7 @@ namespace Milimoe.FunGame.Core.Library.Common.Architecture public bool Authenticate(string username, string password) { if (!BeforeAuthenticator(AuthenticationType.Username, username, password)) return false; - SQLHelper.ExecuteDataSet(UserQuery.Select_Users_LoginQuery(username, password)); + SQLHelper.ExecuteDataSet(UserQuery.Select_Users_LoginQuery(SQLHelper, username, password)); if (SQLHelper.Success) { DataSet ds = SQLHelper.DataSet; diff --git a/Library/Common/Architecture/BaseEntityConverter.cs b/Library/Common/Architecture/BaseEntityConverter.cs index 143e102..c650892 100644 --- a/Library/Common/Architecture/BaseEntityConverter.cs +++ b/Library/Common/Architecture/BaseEntityConverter.cs @@ -9,6 +9,7 @@ namespace Milimoe.FunGame.Core.Library.Common.Architecture public override T? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) { T? result = default; + Dictionary convertingContext = []; while (reader.Read()) { @@ -19,15 +20,25 @@ namespace Milimoe.FunGame.Core.Library.Common.Architecture result ??= NewInstance(); string propertyName = reader.GetString() ?? ""; reader.Read(); - ReadPropertyName(ref reader, propertyName, options, ref result); + ReadPropertyName(ref reader, propertyName, options, ref result, convertingContext); } } + if (result != null) + { + AfterConvert(ref result, convertingContext); + } + return result; } public abstract T NewInstance(); - public abstract void ReadPropertyName(ref Utf8JsonReader reader, string propertyName, JsonSerializerOptions options, ref T result); + public abstract void ReadPropertyName(ref Utf8JsonReader reader, string propertyName, JsonSerializerOptions options, ref T result, Dictionary convertingContext); + + public virtual void AfterConvert(ref T result, Dictionary convertingContext) + { + // do nothing + } } } diff --git a/Library/Common/JsonConverter/CharacterConverter.cs b/Library/Common/JsonConverter/CharacterConverter.cs index d253a81..c878181 100644 --- a/Library/Common/JsonConverter/CharacterConverter.cs +++ b/Library/Common/JsonConverter/CharacterConverter.cs @@ -13,7 +13,7 @@ namespace Milimoe.FunGame.Core.Library.Common.JsonConverter return Factory.GetCharacter(); } - public override void ReadPropertyName(ref Utf8JsonReader reader, string propertyName, JsonSerializerOptions options, ref Character result) + public override void ReadPropertyName(ref Utf8JsonReader reader, string propertyName, JsonSerializerOptions options, ref Character result, Dictionary convertingContext) { switch (propertyName) { @@ -87,13 +87,13 @@ namespace Milimoe.FunGame.Core.Library.Common.JsonConverter result.ExMPPercentage = reader.GetDouble(); break; case nameof(Character.HP): - result.HP = reader.GetDouble(); + convertingContext[nameof(Character.HP)] = reader.GetDouble(); break; case nameof(Character.MP): - result.MP = reader.GetDouble(); + convertingContext[nameof(Character.MP)] = reader.GetDouble(); break; case nameof(Character.EP): - result.EP = reader.GetDouble(); + convertingContext[nameof(Character.EP)] = reader.GetDouble(); break; case nameof(Character.InitialATK): result.InitialATK = reader.GetDouble(); @@ -303,5 +303,20 @@ namespace Milimoe.FunGame.Core.Library.Common.JsonConverter writer.WriteEndObject(); } + public override void AfterConvert(ref Character result, Dictionary convertingContext) + { + if (convertingContext.TryGetValue(nameof(Character.HP), out object? value) && value is double hp) + { + result.HP = hp; + } + if (convertingContext.TryGetValue(nameof(Character.MP), out value) && value is double mp) + { + result.MP = mp; + } + if (convertingContext.TryGetValue(nameof(Character.EP), out value) && value is double ep) + { + result.EP = ep; + } + } } } diff --git a/Library/Common/JsonConverter/ClubConverter.cs b/Library/Common/JsonConverter/ClubConverter.cs index 9d04bcf..31869eb 100644 --- a/Library/Common/JsonConverter/ClubConverter.cs +++ b/Library/Common/JsonConverter/ClubConverter.cs @@ -12,7 +12,7 @@ namespace Milimoe.FunGame.Core.Library.Common.JsonConverter return new(); } - public override void ReadPropertyName(ref Utf8JsonReader reader, string propertyName, JsonSerializerOptions options, ref Club result) + public override void ReadPropertyName(ref Utf8JsonReader reader, string propertyName, JsonSerializerOptions options, ref Club result, Dictionary convertingContext) { switch (propertyName) { diff --git a/Library/Common/JsonConverter/EffectConverter.cs b/Library/Common/JsonConverter/EffectConverter.cs index b1a25d4..2cb13da 100644 --- a/Library/Common/JsonConverter/EffectConverter.cs +++ b/Library/Common/JsonConverter/EffectConverter.cs @@ -11,7 +11,7 @@ namespace Milimoe.FunGame.Core.Library.Common.JsonConverter return new(); } - public override void ReadPropertyName(ref Utf8JsonReader reader, string propertyName, JsonSerializerOptions options, ref Effect result) + public override void ReadPropertyName(ref Utf8JsonReader reader, string propertyName, JsonSerializerOptions options, ref Effect result, Dictionary convertingContext) { switch (propertyName) { diff --git a/Library/Common/JsonConverter/EquipSlotConverter.cs b/Library/Common/JsonConverter/EquipSlotConverter.cs index fe011cb..0cc7011 100644 --- a/Library/Common/JsonConverter/EquipSlotConverter.cs +++ b/Library/Common/JsonConverter/EquipSlotConverter.cs @@ -13,7 +13,7 @@ namespace Milimoe.FunGame.Core.Library.Common.JsonConverter return new(); } - public override void ReadPropertyName(ref Utf8JsonReader reader, string propertyName, JsonSerializerOptions options, ref EquipSlot result) + public override void ReadPropertyName(ref Utf8JsonReader reader, string propertyName, JsonSerializerOptions options, ref EquipSlot result, Dictionary convertingContext) { Item temp; switch (propertyName) diff --git a/Library/Common/JsonConverter/InventoryConverter.cs b/Library/Common/JsonConverter/InventoryConverter.cs index fa20a7c..464b5f4 100644 --- a/Library/Common/JsonConverter/InventoryConverter.cs +++ b/Library/Common/JsonConverter/InventoryConverter.cs @@ -12,7 +12,7 @@ namespace Milimoe.FunGame.Core.Library.Common.JsonConverter return Factory.GetInventory(); } - public override void ReadPropertyName(ref Utf8JsonReader reader, string propertyName, JsonSerializerOptions options, ref Inventory result) + public override void ReadPropertyName(ref Utf8JsonReader reader, string propertyName, JsonSerializerOptions options, ref Inventory result, Dictionary convertingContext) { switch (propertyName) { diff --git a/Library/Common/JsonConverter/ItemConverter.cs b/Library/Common/JsonConverter/ItemConverter.cs index 5e5facd..aa94c3c 100644 --- a/Library/Common/JsonConverter/ItemConverter.cs +++ b/Library/Common/JsonConverter/ItemConverter.cs @@ -13,7 +13,7 @@ namespace Milimoe.FunGame.Core.Library.Common.JsonConverter return new(); } - public override void ReadPropertyName(ref Utf8JsonReader reader, string propertyName, JsonSerializerOptions options, ref Item result) + public override void ReadPropertyName(ref Utf8JsonReader reader, string propertyName, JsonSerializerOptions options, ref Item result, Dictionary convertingContext) { switch (propertyName) { diff --git a/Library/Common/JsonConverter/MagicResistanceConverter.cs b/Library/Common/JsonConverter/MagicResistanceConverter.cs index 689dd3e..b0d3a3d 100644 --- a/Library/Common/JsonConverter/MagicResistanceConverter.cs +++ b/Library/Common/JsonConverter/MagicResistanceConverter.cs @@ -11,7 +11,7 @@ namespace Milimoe.FunGame.Core.Library.Common.JsonConverter return new(); } - public override void ReadPropertyName(ref Utf8JsonReader reader, string propertyName, JsonSerializerOptions options, ref MagicResistance result) + public override void ReadPropertyName(ref Utf8JsonReader reader, string propertyName, JsonSerializerOptions options, ref MagicResistance result, Dictionary convertingContext) { switch (propertyName) { diff --git a/Library/Common/JsonConverter/NormalAttackConverter.cs b/Library/Common/JsonConverter/NormalAttackConverter.cs index 8af273c..e94eb1c 100644 --- a/Library/Common/JsonConverter/NormalAttackConverter.cs +++ b/Library/Common/JsonConverter/NormalAttackConverter.cs @@ -13,7 +13,7 @@ namespace Milimoe.FunGame.Core.Library.Common.JsonConverter return new NormalAttack(Factory.GetCharacter()); } - public override void ReadPropertyName(ref Utf8JsonReader reader, string propertyName, JsonSerializerOptions options, ref NormalAttack result) + public override void ReadPropertyName(ref Utf8JsonReader reader, string propertyName, JsonSerializerOptions options, ref NormalAttack result, Dictionary convertingContext) { switch (propertyName) { diff --git a/Library/Common/JsonConverter/RoomConverter.cs b/Library/Common/JsonConverter/RoomConverter.cs index ba33de8..1534f3c 100644 --- a/Library/Common/JsonConverter/RoomConverter.cs +++ b/Library/Common/JsonConverter/RoomConverter.cs @@ -14,7 +14,7 @@ namespace Milimoe.FunGame.Core.Library.Common.JsonConverter return Factory.GetRoom(); } - public override void ReadPropertyName(ref Utf8JsonReader reader, string propertyName, JsonSerializerOptions options, ref Room result) + public override void ReadPropertyName(ref Utf8JsonReader reader, string propertyName, JsonSerializerOptions options, ref Room result, Dictionary convertingContext) { switch (propertyName) { diff --git a/Library/Common/JsonConverter/SkillConverter.cs b/Library/Common/JsonConverter/SkillConverter.cs index c3b44e8..de76903 100644 --- a/Library/Common/JsonConverter/SkillConverter.cs +++ b/Library/Common/JsonConverter/SkillConverter.cs @@ -13,7 +13,7 @@ namespace Milimoe.FunGame.Core.Library.Common.JsonConverter return new OpenSkill(0, "", []); } - public override void ReadPropertyName(ref Utf8JsonReader reader, string propertyName, JsonSerializerOptions options, ref Skill result) + public override void ReadPropertyName(ref Utf8JsonReader reader, string propertyName, JsonSerializerOptions options, ref Skill result, Dictionary convertingContext) { switch (propertyName) { diff --git a/Library/Common/JsonConverter/UserConverter.cs b/Library/Common/JsonConverter/UserConverter.cs index 313318f..024e485 100644 --- a/Library/Common/JsonConverter/UserConverter.cs +++ b/Library/Common/JsonConverter/UserConverter.cs @@ -15,7 +15,7 @@ namespace Milimoe.FunGame.Core.Library.Common.JsonConverter return Factory.GetUser(); } - public override void ReadPropertyName(ref Utf8JsonReader reader, string propertyName, JsonSerializerOptions options, ref User result) + public override void ReadPropertyName(ref Utf8JsonReader reader, string propertyName, JsonSerializerOptions options, ref User result, Dictionary convertingContext) { switch (propertyName) { diff --git a/Library/Common/Network/HTTPClient.cs b/Library/Common/Network/HTTPClient.cs index d847be0..9cc9511 100644 --- a/Library/Common/Network/HTTPClient.cs +++ b/Library/Common/Network/HTTPClient.cs @@ -23,7 +23,7 @@ namespace Milimoe.FunGame.Core.Library.Common.Network private bool _receiving = false; private readonly HashSet> _boundEvents = []; - private HTTPClient(System.Net.WebSockets.ClientWebSocket instance, string serverAddress, int serverPort, params object[] args) + private HTTPClient(ClientWebSocket instance, string serverAddress, int serverPort, params object[] args) { Instance = instance; ServerAddress = serverAddress; @@ -33,11 +33,10 @@ namespace Milimoe.FunGame.Core.Library.Common.Network Task.Factory.StartNew(async () => await StartListening(args)); } - public static async Task Connect(string serverAddress, int serverPort, bool ssl, string subUrl = "", params object[] args) + public static async Task Connect(string serverAddress, bool ssl, int serverPort = 0, string subUrl = "", params object[] args) { - string ServerIP = Api.Utility.NetworkUtility.GetIPAddress(serverAddress); - Uri uri = new((ssl ? "wss://" : "ws://") + ServerIP + ":" + serverPort + "/" + subUrl.Trim('/') + "/"); - System.Net.WebSockets.ClientWebSocket? socket = await HTTPManager.Connect(uri); + Uri uri = new((ssl ? "wss://" : "ws://") + serverAddress + ":" + (serverPort != 0 ? serverPort : "") + "/" + subUrl.Trim('/') + "/"); + ClientWebSocket? socket = await HTTPManager.Connect(uri); if (socket != null && socket.State == WebSocketState.Open) { HTTPClient client = new(socket, serverAddress, serverPort, args); diff --git a/Library/SQLScript/Common/Common.cs b/Library/SQLScript/Common/Common.cs index 1c13d51..d3c7dc7 100644 --- a/Library/SQLScript/Common/Common.cs +++ b/Library/SQLScript/Common/Common.cs @@ -1,4 +1,6 @@ -namespace Milimoe.FunGame.Core.Library.SQLScript +using Milimoe.FunGame.Core.Api.Transmittal; + +namespace Milimoe.FunGame.Core.Library.SQLScript { public class Constant { @@ -37,9 +39,12 @@ namespace Milimoe.FunGame.Core.Library.SQLScript.Common public const string Column_LoginTime = "LoginTime"; public const string Column_LastTime = "LastTime"; - public static string Insert_ServerLoginLogs(string ServerName, string ServerKey) + public static string Insert_ServerLoginLogs(SQLHelper SQLHelper, string ServerName, string ServerKey) { - return $"{Command_Insert} {Command_Into} {TableName} ({Column_ServerName}, {Column_ServerKey}, {Column_LoginTime}) {Command_Values} ('{ServerName}', '{ServerKey}', '{DateTime.Now}')"; + SQLHelper.Parameters["@ServerName"] = ServerName; + SQLHelper.Parameters["@ServerKey"] = ServerKey; + SQLHelper.Parameters["@LoginTime"] = DateTime.Now; + return $"{Command_Insert} {Command_Into} {TableName} ({Column_ServerName}, {Column_ServerKey}, {Column_LoginTime}) {Command_Values} (@ServerName, @ServerKey, @LoginTime)"; } public static string Select_GetLastLoginTime() diff --git a/Library/SQLScript/Common/VerifyCodes.cs b/Library/SQLScript/Common/VerifyCodes.cs index f1c5011..67f5682 100644 --- a/Library/SQLScript/Common/VerifyCodes.cs +++ b/Library/SQLScript/Common/VerifyCodes.cs @@ -1,4 +1,6 @@ -namespace Milimoe.FunGame.Core.Library.SQLScript.Common +using Milimoe.FunGame.Core.Api.Transmittal; + +namespace Milimoe.FunGame.Core.Library.SQLScript.Common { public class RegVerifyCodes : Constant { @@ -8,24 +10,35 @@ public const string Column_RegVerifyCode = "RegVerifyCode"; public const string Column_RegTime = "RegTime"; - public static string Insert_RegVerifyCode(string Username, string Email, string RegVerifyCodes) + public static string Insert_RegVerifyCode(SQLHelper SQLHelper, string Username, string Email, string RegVerifyCodes) { - return $"{Command_Insert} {Command_Into} {TableName} ({Column_Username}, {Column_Email}, {Column_RegVerifyCode}, {Column_RegTime}) {Command_Values} ('{Username}', '{Email}', '{RegVerifyCodes}', '{DateTime.Now}')"; + SQLHelper.Parameters["@Username"] = Username; + SQLHelper.Parameters["@Email"] = Email; + SQLHelper.Parameters["@RegVerifyCodes"] = RegVerifyCodes; + SQLHelper.Parameters["@RegTime"] = DateTime.Now; + return $"{Command_Insert} {Command_Into} {TableName} ({Column_Username}, {Column_Email}, {Column_RegVerifyCode}, {Column_RegTime}) {Command_Values} (@Username, @Email, @RegVerifyCodes, @RegTime)"; } - public static string Select_RegVerifyCode(string Username, string Email, string RegVerifyCode) + public static string Select_RegVerifyCode(SQLHelper SQLHelper, string Username, string Email, string RegVerifyCode) { - return $"{Command_Select} {Command_All} {Command_From} {TableName} {Command_Where} {Column_Username} = '{Username}' and {Column_Email} = '{Email}' and {Column_RegVerifyCode} = '{RegVerifyCode}'"; + SQLHelper.Parameters["@Username"] = Username; + SQLHelper.Parameters["@Email"] = Email; + SQLHelper.Parameters["@RegVerifyCode"] = RegVerifyCode; + return $"{Command_Select} {Command_All} {Command_From} {TableName} {Command_Where} {Column_Username} = @Username and {Column_Email} = @Email and {Column_RegVerifyCode} = @RegVerifyCode"; } - public static string Select_HasSentRegVerifyCode(string Username, string Email) + public static string Select_HasSentRegVerifyCode(SQLHelper SQLHelper, string Username, string Email) { - return $"{Command_Select} {Command_All} {Command_From} {TableName} {Command_Where} {Column_Username} = '{Username}' and {Column_Email} = '{Email}'"; + SQLHelper.Parameters["@Username"] = Username; + SQLHelper.Parameters["@Email"] = Email; + return $"{Command_Select} {Command_All} {Command_From} {TableName} {Command_Where} {Column_Username} = @Username and {Column_Email} = @Email"; } - public static string Delete_RegVerifyCode(string Username, string Email) + public static string Delete_RegVerifyCode(SQLHelper SQLHelper, string Username, string Email) { - return $"{Command_Delete} {Command_From} {TableName} {Command_Where} {Column_Username} = '{Username}' and {Column_Email} = '{Email}'"; + SQLHelper.Parameters["@Username"] = Username; + SQLHelper.Parameters["@Email"] = Email; + return $"{Command_Delete} {Command_From} {TableName} {Command_Where} {Column_Username} = @Username and {Column_Email} = @Email"; } } @@ -37,24 +50,35 @@ public const string Column_ForgetVerifyCode = "ForgetVerifyCode"; public const string Column_SendTime = "SendTime"; - public static string Insert_ForgetVerifyCode(string Username, string Email, string ForgetVerifyCodes) + public static string Insert_ForgetVerifyCode(SQLHelper SQLHelper, string Username, string Email, string ForgetVerifyCodes) { - return $"{Command_Insert} {Command_Into} {TableName} ({Column_Username}, {Column_Email}, {Column_ForgetVerifyCode}, {Column_SendTime}) {Command_Values} ('{Username}', '{Email}', '{ForgetVerifyCodes}', '{DateTime.Now}')"; + SQLHelper.Parameters["@Username"] = Username; + SQLHelper.Parameters["@Email"] = Email; + SQLHelper.Parameters["@ForgetVerifyCodes"] = ForgetVerifyCodes; + SQLHelper.Parameters["@SendTime"] = DateTime.Now; + return $"{Command_Insert} {Command_Into} {TableName} ({Column_Username}, {Column_Email}, {Column_ForgetVerifyCode}, {Column_SendTime}) {Command_Values} (@Username, @Email, @ForgetVerifyCodes, @SendTime)"; } - public static string Select_ForgetVerifyCode(string Username, string Email, string ForgetVerifyCode) + public static string Select_ForgetVerifyCode(SQLHelper SQLHelper, string Username, string Email, string ForgetVerifyCode) { - return $"{Command_Select} {Command_All} {Command_From} {TableName} {Command_Where} {Column_Username} = '{Username}' and {Column_Email} = '{Email}' and {Column_ForgetVerifyCode} = '{ForgetVerifyCode}'"; + SQLHelper.Parameters["@Username"] = Username; + SQLHelper.Parameters["@Email"] = Email; + SQLHelper.Parameters["@ForgetVerifyCode"] = ForgetVerifyCode; + return $"{Command_Select} {Command_All} {Command_From} {TableName} {Command_Where} {Column_Username} = @Username and {Column_Email} = @Email and {Column_ForgetVerifyCode} = @ForgetVerifyCode"; } - public static string Select_HasSentForgetVerifyCode(string Username, string Email) + public static string Select_HasSentForgetVerifyCode(SQLHelper SQLHelper, string Username, string Email) { - return $"{Command_Select} {Command_All} {Command_From} {TableName} {Command_Where} {Column_Username} = '{Username}' and {Column_Email} = '{Email}'"; + SQLHelper.Parameters["@Username"] = Username; + SQLHelper.Parameters["@Email"] = Email; + return $"{Command_Select} {Command_All} {Command_From} {TableName} {Command_Where} {Column_Username} = @Username and {Column_Email} = @Email"; } - public static string Delete_ForgetVerifyCode(string Username, string Email) + public static string Delete_ForgetVerifyCode(SQLHelper SQLHelper, string Username, string Email) { - return $"{Command_Delete} {Command_From} {TableName} {Command_Where} {Column_Username} = '{Username}' and {Column_Email} = '{Email}'"; + SQLHelper.Parameters["@Username"] = Username; + SQLHelper.Parameters["@Email"] = Email; + return $"{Command_Delete} {Command_From} {TableName} {Command_Where} {Column_Username} = @Username and {Column_Email} = @Email"; } } } diff --git a/Library/SQLScript/Entity/RoomQuery.cs b/Library/SQLScript/Entity/RoomQuery.cs index 543238e..c813b1c 100644 --- a/Library/SQLScript/Entity/RoomQuery.cs +++ b/Library/SQLScript/Entity/RoomQuery.cs @@ -1,4 +1,6 @@ -namespace Milimoe.FunGame.Core.Library.SQLScript.Entity +using Milimoe.FunGame.Core.Api.Transmittal; + +namespace Milimoe.FunGame.Core.Library.SQLScript.Entity { public class RoomQuery : Constant { @@ -19,38 +21,58 @@ public const string Select_Rooms = $"{Command_Select} {TableName}.{Command_All}, {UserQuery.TableName}.{UserQuery.Column_Username} {Command_As} {Column_RoomMasterName} " + $"{Command_From} {TableName} {Command_LeftJoin} {UserQuery.TableName} {Command_On} {UserQuery.TableName}.{UserQuery.Column_UID} = {TableName}.{Column_RoomMaster}"; - public static string Insert_CreateRoom(string roomid, long roomMaster, Library.Constant.RoomType roomType, string gameModule, string gameMap, bool isRank, string password, int maxUsers) + public static string Insert_CreateRoom(SQLHelper SQLHelper, string roomid, long roomMaster, Library.Constant.RoomType roomType, string gameModule, string gameMap, bool isRank, string password, int maxUsers) { Library.Constant.RoomState RoomState = Library.Constant.RoomState.Created; DateTime NowTime = DateTime.Now; bool HasPass = password.Trim() != ""; + + SQLHelper.Parameters["@roomid"] = roomid; + SQLHelper.Parameters["@CreateTime"] = NowTime; + SQLHelper.Parameters["@roomMaster"] = roomMaster; + SQLHelper.Parameters["@roomType"] = (int)roomType; + SQLHelper.Parameters["@gameModule"] = gameModule; + SQLHelper.Parameters["@gameMap"] = gameMap; + SQLHelper.Parameters["@RoomState"] = (int)RoomState; + SQLHelper.Parameters["@isRank"] = isRank ? 1 : 0; + SQLHelper.Parameters["@HasPass"] = HasPass ? 1 : 0; + SQLHelper.Parameters["@password"] = password; + SQLHelper.Parameters["@maxUsers"] = maxUsers; + return $"{Command_Insert} {Command_Into} {TableName} ({Column_RoomID}, {Column_CreateTime}, {Column_RoomMaster}, {Column_RoomType}, {Column_GameModule}, {Column_GameMap}, {Column_RoomState}, {Column_IsRank}, {Column_HasPass}, {Column_Password}, {Column_MaxUsers})" + - $" {Command_Values} ('{roomid}', '{NowTime}', {roomMaster}, {(int)roomType}, '{gameModule}', '{gameMap}', {(int)RoomState}, {(isRank ? 1 : 0)}, {(HasPass ? 1 : 0)}, '{password}', {maxUsers})"; + $" {Command_Values} (@roomid, @CreateTime, @roomMaster, @roomType, @gameModule, @gameMap, @RoomState, @isRank, @HasPass, @password, @maxUsers)"; } - public static string Delete_Rooms(params string[] roomids) + public static string Delete_Rooms(SQLHelper SQLHelper, params string[] roomids) { if (roomids.Length != 0) { string where = string.Join("', '", roomids); - return $"{Command_Delete} {Command_From} {TableName} {Command_Where} {Column_RoomID} {Command_In} ('{where}')"; + SQLHelper.Parameters["@roomids"] = where; + return $"{Command_Delete} {Command_From} {TableName} {Command_Where} {Column_RoomID} {Command_In} (@roomids)"; } return $"{Command_Delete} {Command_From} {TableName}"; } - public static string Delete_QuitRoom(string roomID, long roomMaster) + public static string Delete_QuitRoom(SQLHelper SQLHelper, string roomID, long roomMaster) { - return $"{Command_Delete} {Command_From} {TableName} {Command_Where} {Column_RoomID} = '{roomID}' {Command_And} {Column_RoomMaster} = {roomMaster}"; + SQLHelper.Parameters["@roomID"] = roomID; + SQLHelper.Parameters["@roomMaster"] = roomMaster; + return $"{Command_Delete} {Command_From} {TableName} {Command_Where} {Column_RoomID} = @roomID {Command_And} {Column_RoomMaster} = @roomMaster"; } - public static string Update_QuitRoom(string roomid, long oldRoomMaster, long newRoomMaster) + public static string Update_QuitRoom(SQLHelper SQLHelper, string roomid, long oldRoomMaster, long newRoomMaster) { - return $"{Command_Update} {TableName} {Command_Set} {Column_RoomMaster} = {newRoomMaster} {Command_Where} {Column_RoomID} = '{roomid}' {Command_And} {Column_RoomMaster} = {oldRoomMaster}"; + SQLHelper.Parameters["@roomid"] = roomid; + SQLHelper.Parameters["@oldRoomMaster"] = oldRoomMaster; + SQLHelper.Parameters["@newRoomMaster"] = newRoomMaster; + return $"{Command_Update} {TableName} {Command_Set} {Column_RoomMaster} = @newRoomMaster {Command_Where} {Column_RoomID} = @roomid {Command_And} {Column_RoomMaster} = @oldRoomMaster"; } - public static string Select_IsExistRoom(string roomid) + public static string Select_IsExistRoom(SQLHelper SQLHelper, string roomid) { - return $"{Command_Select} {Command_All} {Command_From} {TableName} {Command_Where} {Column_RoomID} = '{roomid}'"; + SQLHelper.Parameters["@roomid"] = roomid; + return $"{Command_Select} {Command_All} {Command_From} {TableName} {Command_Where} {Column_RoomID} = @roomid"; } } } diff --git a/Library/SQLScript/Entity/UserQuery.cs b/Library/SQLScript/Entity/UserQuery.cs index 8ff64e0..92bb10e 100644 --- a/Library/SQLScript/Entity/UserQuery.cs +++ b/Library/SQLScript/Entity/UserQuery.cs @@ -1,4 +1,6 @@ -namespace Milimoe.FunGame.Core.Library.SQLScript.Entity +using Milimoe.FunGame.Core.Api.Transmittal; + +namespace Milimoe.FunGame.Core.Library.SQLScript.Entity { public class UserQuery : Constant { @@ -18,55 +20,77 @@ public const string Column_AutoKey = "AutoKey"; public const string Select_Users = $"{Command_Select} {Command_All} {Command_From} {TableName}"; - public static string Select_Users_LoginQuery(string Username, string Password) + public static string Select_Users_LoginQuery(SQLHelper SQLHelper, string Username, string Password) { - return $"{Select_Users} {Command_Where} {Column_Username} = '{Username}' and {Column_Password} = '{Password}'"; + SQLHelper.Parameters["@Username"] = Username; + SQLHelper.Parameters["@Password"] = Password; + return $"{Select_Users} {Command_Where} {Column_Username} = @Username and {Column_Password} = @Password"; } - public static string Select_IsExistEmail(string Email) + public static string Select_IsExistEmail(SQLHelper SQLHelper, string Email) { - return $"{Select_Users} {Command_Where} {Column_Email} = '{Email}'"; + SQLHelper.Parameters["@Email"] = Email; + return $"{Select_Users} {Command_Where} {Column_Email} = @Email"; } - public static string Select_IsExistUsername(string Username) + public static string Select_IsExistUsername(SQLHelper SQLHelper, string Username) { - return $"{Select_Users} {Command_Where} {Column_Username} = '{Username}'"; + SQLHelper.Parameters["@Username"] = Username; + return $"{Select_Users} {Command_Where} {Column_Username} = @Username"; } - public static string Select_CheckEmailWithUsername(string Username, string email) + public static string Select_CheckEmailWithUsername(SQLHelper SQLHelper, string Username, string email) { - return $"{Select_Users} {Command_Where} {Column_Username} = '{Username}' and {Column_Email} = '{email}'"; + SQLHelper.Parameters["@Username"] = Username; + SQLHelper.Parameters["@Email"] = email; + return $"{Select_Users} {Command_Where} {Column_Username} = @Username and {Column_Email} = @Email"; } - public static string Select_Users_Where(string Where) + public static string Select_Users_Where(SQLHelper SQLHelper, string Where) { - return $"{Select_Users} {Command_Where} {Where}"; + SQLHelper.Parameters["@Where"] = Where; + return $"{Select_Users} {Command_Where} @Where"; } - public static string Select_CheckAutoKey(string Username, string AutoKey) + public static string Select_CheckAutoKey(SQLHelper SQLHelper, string Username, string AutoKey) { - return $"{Select_Users} {Command_Where} {Column_Username} = '{Username}' and {Column_AutoKey} = '{AutoKey}'"; + SQLHelper.Parameters["@Username"] = Username; + SQLHelper.Parameters["@AutoKey"] = AutoKey; + return $"{Select_Users} {Command_Where} {Column_Username} = @Username and {Column_AutoKey} = @AutoKey"; } - public static string Update_CheckLogin(string Username, string IP) + public static string Update_CheckLogin(SQLHelper SQLHelper, string Username, string IP) { - return $"{Command_Update} {TableName} {Command_Set} {Column_LastTime} = '{DateTime.Now}', {Column_LastIP} = '{IP}' {Command_Where} {Column_Username} = '{Username}'"; + SQLHelper.Parameters["@Username"] = Username; + SQLHelper.Parameters["@LastTime"] = DateTime.Now; + SQLHelper.Parameters["@LastIP"] = IP; + return $"{Command_Update} {TableName} {Command_Set} {Column_LastTime} = @LastTime, {Column_LastIP} = @LastIP {Command_Where} {Column_Username} = @Username"; } - public static string Update_Password(string Username, string Password) + public static string Update_Password(SQLHelper SQLHelper, string Username, string Password) { - return $"{Command_Update} {TableName} {Command_Set} {Column_Password} = '{Password}' {Command_Where} {Column_Username} = '{Username}'"; + SQLHelper.Parameters["@Username"] = Username; + SQLHelper.Parameters["@Password"] = Password; + return $"{Command_Update} {TableName} {Command_Set} {Column_Password} = @Password {Command_Where} {Column_Username} = @Username"; } - public static string Update_GameTime(string Username, int GameTimeMinutes) + public static string Update_GameTime(SQLHelper SQLHelper, string Username, int GameTimeMinutes) { - return $"{Command_Update} {TableName} {Command_Set} {Column_GameTime} = {Column_GameTime} + {GameTimeMinutes} {Command_Where} {Column_Username} = '{Username}'"; + SQLHelper.Parameters["@Username"] = Username; + SQLHelper.Parameters["@GameTimeMinutes"] = GameTimeMinutes; + return $"{Command_Update} {TableName} {Command_Set} {Column_GameTime} = {Column_GameTime} + @GameTimeMinutes {Command_Where} {Column_Username} = @Username"; } - public static string Insert_Register(string Username, string Password, string Email, string IP) + public static string Insert_Register(SQLHelper SQLHelper, string Username, string Password, string Email, string IP) { DateTime Now = DateTime.Now; - return $"{Command_Insert} {Command_Into} {TableName} ({Column_Username}, {Column_Password}, {Column_Email}, {Column_RegTime}, {Column_LastTime}, {Column_LastIP}) {Command_Values} ('{Username}', '{Password}', '{Email}', '{Now}', '{Now}', '{IP}')"; + SQLHelper.Parameters["@Username"] = Username; + SQLHelper.Parameters["@Password"] = Password; + SQLHelper.Parameters["@Email"] = Email; + SQLHelper.Parameters["@RegTime"] = Now; + SQLHelper.Parameters["@LastTime"] = Now; + SQLHelper.Parameters["@LastIP"] = IP; + return $"{Command_Insert} {Command_Into} {TableName} ({Column_Username}, {Column_Password}, {Column_Email}, {Column_RegTime}, {Column_LastTime}, {Column_LastIP}) {Command_Values} (@Username, @Password, @Email, @RegTime, @LastTime, @LastIP)"; } } }