修改User构造方法

This commit is contained in:
Mili 2023-03-15 23:52:53 +08:00
parent 70036f2f3d
commit 231f28541f
9 changed files with 63 additions and 129 deletions

View File

@ -1,20 +1,18 @@
using System.Data;
using Milimoe.FunGame.Core.Entity;
namespace Milimoe.FunGame.Core.Api.Factory namespace Milimoe.FunGame.Core.Api.Factory
{ {
internal class UserFactory 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); return new User(ds);
}
internal static Milimoe.FunGame.Core.Entity.User GetInstance(string username, string password)
{
return new Milimoe.FunGame.Core.Entity.User(username, password);
} }
} }
} }

View File

@ -1,4 +1,5 @@
using Milimoe.FunGame.Core.Entity; using System.Data;
using Milimoe.FunGame.Core.Entity;
using Milimoe.FunGame.Core.Library.Constant; using Milimoe.FunGame.Core.Library.Constant;
namespace Milimoe.FunGame.Core.Api.Utility namespace Milimoe.FunGame.Core.Api.Utility
@ -7,43 +8,19 @@ namespace Milimoe.FunGame.Core.Api.Utility
{ {
/// <summary> /// <summary>
/// 获取一个可能为NULL的实例 /// 获取一个可能为NULL的实例
/// <para>Item默认返回PassiveItem</para>
/// <para>Skill默认返回PassiveSkill</para>
/// <para>若无法找到T返回唯一的空对象</para>
/// </summary> /// </summary>
/// <typeparam name="T">Entity类</typeparam> /// <typeparam name="T">Entity类</typeparam>
/// <param name="objs">构造函数的参数</param> /// <param name="DataSet">使用DataSet构造对象真香</param>
/// <returns></returns> /// <returns></returns>
public static T? GetInstance<T>(params object[]? objs) public static T GetInstance<T>(DataSet? DataSet)
{
if (!IsEntity<T>()) 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;
}
/// <summary>
/// 获取一个不可能为NULL的实例
/// Item默认返回PassiveItem
/// Skill默认返回PassiveSkill
/// 若无法找到T返回唯一的空对象
/// </summary>
/// <typeparam name="T">Entity类</typeparam>
/// <param name="objs">构造函数的参数</param>
/// <returns></returns>
public static T New<T>(params object[]? objs)
{ {
object instance = General.EntityInstance; object instance = General.EntityInstance;
if (!IsEntity<T>()) return (T)instance; if (typeof(T) == typeof(User))
if (objs is null || objs.Length == 0) return (T)instance;
if (typeof(T) == typeof(Entity.User))
{ {
instance = GetUser(objs); instance = Api.Factory.UserFactory.GetInstance(DataSet);
} }
else if (typeof(T) == typeof(Skill)) else if (typeof(T) == typeof(Skill))
{ {
@ -51,69 +28,5 @@ namespace Milimoe.FunGame.Core.Api.Utility
} }
return (T)instance; return (T)instance;
} }
/// <summary>
/// 获取一个不可能为NULL的单例
/// Item默认返回PassiveItem
/// Skill默认返回PassiveSkill
/// 若无法找到T返回唯一的空对象
/// </summary>
/// <typeparam name="T">Entity类</typeparam>
/// <param name="objs">构造函数的参数</param>
/// <returns></returns>
public static object NewSingle<T>(params object[]? objs)
{
object instance = General.EntityInstance;
if (!IsEntity<T>()) 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<T>()
{
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<string>(objTemp) ?? "";
}
else username = (string)objs[0];
}
if (objs.Length > 1)
{
objTemp = objs[1];
if (objTemp.GetType() != typeof(string))
{
password = NetworkUtility.ConvertJsonObject<string>(objTemp) ?? "";
}
else password = (string)objs[1];
}
return Api.Factory.UserFactory.GetInstance(username, password);
}
} }
} }

View File

@ -1,4 +1,6 @@
using System.Data;
using Milimoe.FunGame.Core.Interface.Entity; using Milimoe.FunGame.Core.Interface.Entity;
using Milimoe.FunGame.Core.Library.SQLScript.Entity;
namespace Milimoe.FunGame.Core.Entity namespace Milimoe.FunGame.Core.Entity
{ {
@ -14,11 +16,10 @@ namespace Milimoe.FunGame.Core.Entity
public bool IsAdmin { get; set; } = false; public bool IsAdmin { get; set; } = false;
public bool IsOperator { get; set; } = false; public bool IsOperator { get; set; } = false;
public bool IsEnable { 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 Credits { get; set; } = 0;
public decimal Materials { get; set; } = 0; public decimal Materials { get; set; } = 0;
public decimal GameTime { get; set; } = 0; public decimal GameTime { get; set; } = 0;
public string AutoKey { get; set; } = "";
public UserStatistics? Statistics { get; set; } = null; public UserStatistics? Statistics { get; set; } = null;
public Inventory? Stock { 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; if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
} {
DataRow row = ds.Tables[0].Rows[0];
internal User(string username, string password) Id = (long)row[UserQuery.Column_UID];
{ Username = (string)row[UserQuery.Column_Username];
Username = username; Password = (string)row[UserQuery.Column_Password];
Password = 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) public override bool Equals(IBaseEntity? other)

View File

@ -23,4 +23,8 @@
<DebugType>embedded</DebugType> <DebugType>embedded</DebugType>
</PropertyGroup> </PropertyGroup>
<ItemGroup>
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
</ItemGroup>
</Project> </Project>

View File

@ -1,6 +1,5 @@
using Milimoe.FunGame.Core.Library.Constant; using Newtonsoft.Json;
using System.Text.Json; using Milimoe.FunGame.Core.Library.Constant;
using System.Text.Json.Serialization;
namespace Milimoe.FunGame.Core.Library.Common.Network namespace Milimoe.FunGame.Core.Library.Common.Network
{ {
@ -12,13 +11,12 @@ namespace Milimoe.FunGame.Core.Library.Common.Network
public object[] Parameters { get; } public object[] Parameters { get; }
public string JsonString { get; } public string JsonString { get; }
[JsonConstructor]
public JsonObject(SocketMessageType MessageType, Guid Token, params object[] Parameters) public JsonObject(SocketMessageType MessageType, Guid Token, params object[] Parameters)
{ {
this.MessageType = MessageType; this.MessageType = MessageType;
this.Token = Token; this.Token = Token;
this.Parameters = Parameters; this.Parameters = Parameters;
this.JsonString = JsonSerializer.Serialize(this); this.JsonString = JsonConvert.SerializeObject(this);
} }
public static string GetString(SocketMessageType MessageType, Guid Token, params object[] Parameters) 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) public static JsonObject? GetObject(string JsonString)
{ {
return JsonSerializer.Deserialize<JsonObject>(JsonString); return JsonConvert.DeserializeObject<JsonObject>(JsonString);
} }
} }
} }

View File

@ -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 namespace Milimoe.FunGame.Core.Library.Common.Network
{ {
@ -29,7 +28,12 @@ namespace Milimoe.FunGame.Core.Library.Common.Network
{ {
if (index < Parameters.Length) if (index < Parameters.Length)
{ {
return NetworkUtility.ConvertJsonObject<T>(Parameters[index]); if (typeof(T) == typeof(Guid))
{
object param = Guid.Parse((string)Parameters[index]);
return (T)param;
}
return (T)Parameters[index];
} }
throw new IndexOutOfArrayLengthException(); throw new IndexOutOfArrayLengthException();
} }

View File

@ -149,4 +149,9 @@
{ {
public override string Message => "退出房间失败 (#10030)"; public override string Message => "退出房间失败 (#10030)";
} }
public class GetInstanceException : Exception
{
public override string Message => "构造对象实例遇到错误 (#10031)";
}
} }

View File

@ -14,7 +14,6 @@
public const string Column_IsAdmin = "IsAdmin"; public const string Column_IsAdmin = "IsAdmin";
public const string Column_IsOperator = "IsOperator"; public const string Column_IsOperator = "IsOperator";
public const string Column_IsEnable = "IsEnable"; public const string Column_IsEnable = "IsEnable";
public const string Column_OnlineState = "OnlineState";
public const string Column_Credits = "Credits"; public const string Column_Credits = "Credits";
public const string Column_Materials = "Materials"; public const string Column_Materials = "Materials";
public const string Column_GameTime = "GameTime"; public const string Column_GameTime = "GameTime";

View File

@ -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.Entity;
using Milimoe.FunGame.Core.Library.Common.Event;
using Milimoe.FunGame.Core.Library.Common.Architecture; using Milimoe.FunGame.Core.Library.Common.Architecture;
using Milimoe.FunGame.Core.Library.Common.Network; using Milimoe.FunGame.Core.Library.Common.Network;
using Milimoe.FunGame.Core.Library.Constant; using Milimoe.FunGame.Core.Library.Constant;
@ -126,12 +126,13 @@ namespace Milimoe.FunGame.Desktop.Model
private static void SocketHandler_CheckLogin(SocketObject SocketObject) private static void SocketHandler_CheckLogin(SocketObject SocketObject)
{ {
// 返回的objs是该Login的User对象的各个属性 // 返回构造User对象的DataTable
object[] objs = SocketObject.Parameters; object[] objs = SocketObject.Parameters;
if (objs != null && objs.Length > 0) if (objs != null && objs.Length > 0)
{ {
DataSet? Set = SocketObject.GetParam<DataSet>(0);
// 创建User对象并返回到Main // 创建User对象并返回到Main
RunTime.Main?.UpdateUI(MainInvokeType.SetUser, new object[] { Factory.New<User>(objs) }); RunTime.Main?.UpdateUI(MainInvokeType.SetUser, new object[] { Factory.GetInstance<User>(Set) });
RunTime.Login?.OnSucceedLoginEvent(Login.EventArgs); RunTime.Login?.OnSucceedLoginEvent(Login.EventArgs);
RunTime.Login?.OnAfterLoginEvent(Login.EventArgs); RunTime.Login?.OnAfterLoginEvent(Login.EventArgs);
return; return;