From 231f28541f5e9781da55bd7766c8bd2e9d4b5a76 Mon Sep 17 00:00:00 2001 From: Mili Date: Wed, 15 Mar 2023 23:52:53 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9User=E6=9E=84=E9=80=A0?= =?UTF-8?q?=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- FunGame.Core/Api/Factory/UserFactory.cs | 16 ++- FunGame.Core/Api/Utility/Factory.cs | 105 ++---------------- FunGame.Core/Entity/User/User.cs | 32 ++++-- FunGame.Core/FunGame.Core.csproj | 4 + .../Library/Common/Network/JsonObject.cs | 10 +- .../Library/Common/Network/SocketObject.cs | 10 +- FunGame.Core/Library/Exception/Exception.cs | 5 + FunGame.Core/Library/SQLScript/Entity/User.cs | 1 - FunGame.Desktop/Model/LoginModel.cs | 9 +- 9 files changed, 63 insertions(+), 129 deletions(-) diff --git a/FunGame.Core/Api/Factory/UserFactory.cs b/FunGame.Core/Api/Factory/UserFactory.cs index bdaabc9..ce3aa03 100644 --- a/FunGame.Core/Api/Factory/UserFactory.cs +++ b/FunGame.Core/Api/Factory/UserFactory.cs @@ -1,20 +1,18 @@ +using System.Data; +using Milimoe.FunGame.Core.Entity; + namespace Milimoe.FunGame.Core.Api.Factory { internal class UserFactory { - internal static Milimoe.FunGame.Core.Entity.User GetInstance() + internal static User GetInstance() { - return new Milimoe.FunGame.Core.Entity.User(); + return new User(); } - internal static Milimoe.FunGame.Core.Entity.User GetInstance(string username) + internal static User GetInstance(DataSet? ds) { - return new Milimoe.FunGame.Core.Entity.User(username); - } - - internal static Milimoe.FunGame.Core.Entity.User GetInstance(string username, string password) - { - return new Milimoe.FunGame.Core.Entity.User(username, password); + return new User(ds); } } } diff --git a/FunGame.Core/Api/Utility/Factory.cs b/FunGame.Core/Api/Utility/Factory.cs index b037cc8..58076ea 100644 --- a/FunGame.Core/Api/Utility/Factory.cs +++ b/FunGame.Core/Api/Utility/Factory.cs @@ -1,4 +1,5 @@ -using Milimoe.FunGame.Core.Entity; +using System.Data; +using Milimoe.FunGame.Core.Entity; using Milimoe.FunGame.Core.Library.Constant; namespace Milimoe.FunGame.Core.Api.Utility @@ -7,43 +8,19 @@ namespace Milimoe.FunGame.Core.Api.Utility { /// /// 获取一个可能为NULL的实例 + /// Item默认返回PassiveItem + /// Skill默认返回PassiveSkill + /// 若无法找到T,返回唯一的空对象 /// /// Entity类 - /// 构造函数的参数 + /// 使用DataSet构造对象(真香) /// - public static T? GetInstance(params object[]? objs) - { - if (!IsEntity()) return default; - object? instance = null; - if (objs is null || objs.Length == 0) return (T?)instance; - if (typeof(T) == typeof(Entity.User)) - { - instance = Api.Factory.UserFactory.GetInstance("Mili"); - } - else if (typeof(T) == typeof(Skill)) - { - - } - return (T?)instance; - } - - /// - /// 获取一个不可能为NULL的实例 - /// Item默认返回PassiveItem - /// Skill默认返回PassiveSkill - /// 若无法找到T,返回唯一的空对象 - /// - /// Entity类 - /// 构造函数的参数 - /// - public static T New(params object[]? objs) + public static T GetInstance(DataSet? DataSet) { object instance = General.EntityInstance; - if (!IsEntity()) return (T)instance; - if (objs is null || objs.Length == 0) return (T)instance; - if (typeof(T) == typeof(Entity.User)) + if (typeof(T) == typeof(User)) { - instance = GetUser(objs); + instance = Api.Factory.UserFactory.GetInstance(DataSet); } else if (typeof(T) == typeof(Skill)) { @@ -51,69 +28,5 @@ namespace Milimoe.FunGame.Core.Api.Utility } return (T)instance; } - - /// - /// 获取一个不可能为NULL的单例 - /// Item默认返回PassiveItem - /// Skill默认返回PassiveSkill - /// 若无法找到T,返回唯一的空对象 - /// - /// Entity类 - /// 构造函数的参数 - /// - public static object NewSingle(params object[]? objs) - { - object instance = General.EntityInstance; - if (!IsEntity()) return instance; - if (objs is null || objs.Length == 0) return instance; - if (typeof(T) == typeof(Entity.User)) - { - instance = Api.Factory.UserFactory.GetInstance("Mili"); - } - else if (typeof(T) == typeof(Skill)) - { - - } - Singleton.Add(instance); - return instance; - } - - private static bool IsEntity() - { - if (typeof(T) == typeof(Entity.ActiveItem) || typeof(T) == typeof(ActiveSkill) - || typeof(T) == typeof(Entity.Character) || typeof(T) == typeof(Entity.CharacterStatistics) - || typeof(T) == typeof(Entity.GameStatistics) || typeof(T) == typeof(Inventory) - || typeof(T) == typeof(Entity.Item) || typeof(T) == typeof(Entity.PassiveItem) - || typeof(T) == typeof(PassiveSkill) || typeof(T) == typeof(Entity.Room) - || typeof(T) == typeof(Skill) || typeof(T) == typeof(Entity.User) - || typeof(T) == typeof(Entity.UserStatistics)) - return true; - return false; - } - - private static User GetUser(params object[] objs) - { - string username = "", password = ""; - object objTemp; - if (objs.Length > 0) - { - objTemp = objs[0]; - if (objTemp.GetType() != typeof(string)) - { - username = NetworkUtility.ConvertJsonObject(objTemp) ?? ""; - } - else username = (string)objs[0]; - } - if (objs.Length > 1) - { - objTemp = objs[1]; - if (objTemp.GetType() != typeof(string)) - { - password = NetworkUtility.ConvertJsonObject(objTemp) ?? ""; - } - else password = (string)objs[1]; - } - return Api.Factory.UserFactory.GetInstance(username, password); - } } } diff --git a/FunGame.Core/Entity/User/User.cs b/FunGame.Core/Entity/User/User.cs index 2e25667..df892d9 100644 --- a/FunGame.Core/Entity/User/User.cs +++ b/FunGame.Core/Entity/User/User.cs @@ -1,4 +1,6 @@ +using System.Data; using Milimoe.FunGame.Core.Interface.Entity; +using Milimoe.FunGame.Core.Library.SQLScript.Entity; namespace Milimoe.FunGame.Core.Entity { @@ -14,11 +16,10 @@ namespace Milimoe.FunGame.Core.Entity public bool IsAdmin { get; set; } = false; public bool IsOperator { get; set; } = false; public bool IsEnable { get; set; } = false; - public int OnlineState { get; set; } = 0; - public string Roomid { get; set; } = ""; public decimal Credits { get; set; } = 0; public decimal Materials { get; set; } = 0; public decimal GameTime { get; set; } = 0; + public string AutoKey { get; set; } = ""; public UserStatistics? Statistics { get; set; } = null; public Inventory? Stock { get; set; } = null; @@ -27,15 +28,26 @@ namespace Milimoe.FunGame.Core.Entity } - internal User(string username) + internal User(DataSet? ds) { - Username = username; - } - - internal User(string username, string password) - { - Username = username; - Password = password; + if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0) + { + DataRow row = ds.Tables[0].Rows[0]; + Id = (long)row[UserQuery.Column_UID]; + Username = (string)row[UserQuery.Column_Username]; + Password = (string)row[UserQuery.Column_Password]; + RegTime = (DateTime)row[UserQuery.Column_RegTime]; + LastTime = (DateTime)row[UserQuery.Column_LastTime]; + Email = (string)row[UserQuery.Column_Email]; + NickName = (string)row[UserQuery.Column_Nickname]; + IsAdmin = (int)row[UserQuery.Column_IsAdmin] == 1; + IsOperator = (int)row[UserQuery.Column_IsOperator] == 1; + IsEnable = (int)row[UserQuery.Column_IsEnable] == 1; + Credits = (decimal)row[UserQuery.Column_Credits]; + Materials = (decimal)row[UserQuery.Column_Materials]; + GameTime = (decimal)row[UserQuery.Column_GameTime]; + AutoKey = (string)row[UserQuery.Column_AutoKey]; + } } public override bool Equals(IBaseEntity? other) diff --git a/FunGame.Core/FunGame.Core.csproj b/FunGame.Core/FunGame.Core.csproj index 54151b5..adad91f 100644 --- a/FunGame.Core/FunGame.Core.csproj +++ b/FunGame.Core/FunGame.Core.csproj @@ -23,4 +23,8 @@ embedded + + + + diff --git a/FunGame.Core/Library/Common/Network/JsonObject.cs b/FunGame.Core/Library/Common/Network/JsonObject.cs index a42992e..fa26ef2 100644 --- a/FunGame.Core/Library/Common/Network/JsonObject.cs +++ b/FunGame.Core/Library/Common/Network/JsonObject.cs @@ -1,6 +1,5 @@ -using Milimoe.FunGame.Core.Library.Constant; -using System.Text.Json; -using System.Text.Json.Serialization; +using Newtonsoft.Json; +using Milimoe.FunGame.Core.Library.Constant; namespace Milimoe.FunGame.Core.Library.Common.Network { @@ -12,13 +11,12 @@ namespace Milimoe.FunGame.Core.Library.Common.Network public object[] Parameters { get; } public string JsonString { get; } - [JsonConstructor] public JsonObject(SocketMessageType MessageType, Guid Token, params object[] Parameters) { this.MessageType = MessageType; this.Token = Token; this.Parameters = Parameters; - this.JsonString = JsonSerializer.Serialize(this); + this.JsonString = JsonConvert.SerializeObject(this); } public static string GetString(SocketMessageType MessageType, Guid Token, params object[] Parameters) @@ -28,7 +26,7 @@ namespace Milimoe.FunGame.Core.Library.Common.Network public static JsonObject? GetObject(string JsonString) { - return JsonSerializer.Deserialize(JsonString); + return JsonConvert.DeserializeObject(JsonString); } } } diff --git a/FunGame.Core/Library/Common/Network/SocketObject.cs b/FunGame.Core/Library/Common/Network/SocketObject.cs index 35d1b00..334e27a 100644 --- a/FunGame.Core/Library/Common/Network/SocketObject.cs +++ b/FunGame.Core/Library/Common/Network/SocketObject.cs @@ -1,5 +1,4 @@ -using Milimoe.FunGame.Core.Api.Utility; -using Milimoe.FunGame.Core.Library.Constant; +using Milimoe.FunGame.Core.Library.Constant; namespace Milimoe.FunGame.Core.Library.Common.Network { @@ -29,7 +28,12 @@ namespace Milimoe.FunGame.Core.Library.Common.Network { if (index < Parameters.Length) { - return NetworkUtility.ConvertJsonObject(Parameters[index]); + if (typeof(T) == typeof(Guid)) + { + object param = Guid.Parse((string)Parameters[index]); + return (T)param; + } + return (T)Parameters[index]; } throw new IndexOutOfArrayLengthException(); } diff --git a/FunGame.Core/Library/Exception/Exception.cs b/FunGame.Core/Library/Exception/Exception.cs index d84449f..dace84e 100644 --- a/FunGame.Core/Library/Exception/Exception.cs +++ b/FunGame.Core/Library/Exception/Exception.cs @@ -149,4 +149,9 @@ { public override string Message => "退出房间失败 (#10030)"; } + + public class GetInstanceException : Exception + { + public override string Message => "构造对象实例遇到错误 (#10031)"; + } } diff --git a/FunGame.Core/Library/SQLScript/Entity/User.cs b/FunGame.Core/Library/SQLScript/Entity/User.cs index 94615a2..0189c90 100644 --- a/FunGame.Core/Library/SQLScript/Entity/User.cs +++ b/FunGame.Core/Library/SQLScript/Entity/User.cs @@ -14,7 +14,6 @@ public const string Column_IsAdmin = "IsAdmin"; public const string Column_IsOperator = "IsOperator"; public const string Column_IsEnable = "IsEnable"; - public const string Column_OnlineState = "OnlineState"; public const string Column_Credits = "Credits"; public const string Column_Materials = "Materials"; public const string Column_GameTime = "GameTime"; diff --git a/FunGame.Desktop/Model/LoginModel.cs b/FunGame.Desktop/Model/LoginModel.cs index 2ea3f91..f87d35b 100644 --- a/FunGame.Desktop/Model/LoginModel.cs +++ b/FunGame.Desktop/Model/LoginModel.cs @@ -1,6 +1,6 @@ -using Milimoe.FunGame.Core.Api.Utility; +using System.Data; +using Milimoe.FunGame.Core.Api.Utility; using Milimoe.FunGame.Core.Entity; -using Milimoe.FunGame.Core.Library.Common.Event; using Milimoe.FunGame.Core.Library.Common.Architecture; using Milimoe.FunGame.Core.Library.Common.Network; using Milimoe.FunGame.Core.Library.Constant; @@ -126,12 +126,13 @@ namespace Milimoe.FunGame.Desktop.Model private static void SocketHandler_CheckLogin(SocketObject SocketObject) { - // 返回的objs是该Login的User对象的各个属性 + // 返回构造User对象的DataTable object[] objs = SocketObject.Parameters; if (objs != null && objs.Length > 0) { + DataSet? Set = SocketObject.GetParam(0); // 创建User对象并返回到Main - RunTime.Main?.UpdateUI(MainInvokeType.SetUser, new object[] { Factory.New(objs) }); + RunTime.Main?.UpdateUI(MainInvokeType.SetUser, new object[] { Factory.GetInstance(Set) }); RunTime.Login?.OnSucceedLoginEvent(Login.EventArgs); RunTime.Login?.OnAfterLoginEvent(Login.EventArgs); return;