mirror of
https://github.com/project-redbud/FunGame-Core.git
synced 2025-04-21 11:39:35 +08:00
更新自定义转换器和实体工厂方法 (#32)
This commit is contained in:
parent
d25dd8d2e3
commit
42d0ca61c5
@ -1,5 +1,6 @@
|
||||
using Milimoe.FunGame.Core.Entity;
|
||||
using Milimoe.FunGame.Core.Interface.Base;
|
||||
using Milimoe.FunGame.Core.Library.Constant;
|
||||
|
||||
namespace Milimoe.FunGame.Core.Api.Factory
|
||||
{
|
||||
@ -9,7 +10,12 @@ namespace Milimoe.FunGame.Core.Api.Factory
|
||||
|
||||
public User Create()
|
||||
{
|
||||
return new User();
|
||||
return General.UnknownUserInstance;
|
||||
}
|
||||
|
||||
public static User Create(long Id = 0, string Username = "", string Password = "", DateTime? RegTime = null, DateTime? LastTime = null, string Email = "", string NickName = "", bool IsAdmin = false, bool IsOperator = false, bool IsEnable = true, decimal Credits = 0, decimal Materials = 0, decimal GameTime = 0, string AutoKey = "")
|
||||
{
|
||||
return new User(Id, Username, Password, RegTime, LastTime, Email, NickName, IsAdmin, IsOperator, IsEnable, Credits, Materials, GameTime, AutoKey);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,6 +1,9 @@
|
||||
using Milimoe.FunGame.Core.Api.Factory;
|
||||
using System;
|
||||
using System.Data;
|
||||
using Milimoe.FunGame.Core.Api.Factory;
|
||||
using Milimoe.FunGame.Core.Entity;
|
||||
using Milimoe.FunGame.Core.Library.Constant;
|
||||
using Milimoe.FunGame.Core.Library.SQLScript.Entity;
|
||||
|
||||
namespace Milimoe.FunGame.Core.Api.Utility
|
||||
{
|
||||
@ -75,6 +78,37 @@ namespace Milimoe.FunGame.Core.Api.Utility
|
||||
return RoomFactory.Create(Id, Roomid, CreateTime, RoomMaster, RoomType, RoomState, Password);
|
||||
}
|
||||
|
||||
public static List<Room> GetRooms(DataSet DsRoom, DataSet DsUser)
|
||||
{
|
||||
List<Room> list = new()
|
||||
{
|
||||
General.HallInstance
|
||||
};
|
||||
if (DsRoom != null && DsRoom.Tables.Count > 0)
|
||||
{
|
||||
foreach (DataRow DrRoom in DsRoom.Tables[0].Rows)
|
||||
{
|
||||
long Id = (long)DrRoom[RoomQuery.Column_ID];
|
||||
string Roomid = (string)DrRoom[RoomQuery.Column_RoomID];
|
||||
DateTime CreateTime = (DateTime)DrRoom[RoomQuery.Column_CreateTime];
|
||||
User RoomMaster = General.UnknownUserInstance;
|
||||
if (DsUser != null && DsUser.Tables.Count > 0)
|
||||
{
|
||||
DataRow[] rows = DsUser.Tables[0].Select($"{UserQuery.Column_UID} = {(long)DrRoom[RoomQuery.Column_RoomMaster]}");
|
||||
if (rows.Length > 0)
|
||||
{
|
||||
RoomMaster = GetUser(rows[0]);
|
||||
}
|
||||
}
|
||||
RoomType RoomType = (RoomType)Convert.ToInt32(DrRoom[RoomQuery.Column_RoomType]);
|
||||
RoomState RoomState = (RoomState)Convert.ToInt32(DrRoom[RoomQuery.Column_RoomState]);
|
||||
string Password = (string)DrRoom[RoomQuery.Column_Password];
|
||||
list.Add(GetRoom(Id, Roomid, CreateTime, RoomMaster, RoomType, RoomState, Password));
|
||||
}
|
||||
}
|
||||
return list;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取大厅(-1号房)
|
||||
/// </summary>
|
||||
@ -120,5 +154,70 @@ namespace Milimoe.FunGame.Core.Api.Utility
|
||||
{
|
||||
return UserFactory.Create();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取用户实例
|
||||
/// </summary>
|
||||
/// <param name="Id"></param>
|
||||
/// <param name="Username"></param>
|
||||
/// <param name="Password"></param>
|
||||
/// <param name="RegTime"></param>
|
||||
/// <param name="LastTime"></param>
|
||||
/// <param name="Email"></param>
|
||||
/// <param name="NickName"></param>
|
||||
/// <param name="IsAdmin"></param>
|
||||
/// <param name="IsOperator"></param>
|
||||
/// <param name="IsEnable"></param>
|
||||
/// <param name="Credits"></param>
|
||||
/// <param name="Materials"></param>
|
||||
/// <param name="GameTime"></param>
|
||||
/// <param name="AutoKey"></param>
|
||||
/// <returns></returns>
|
||||
public static User GetUser(long Id = 0, string Username = "", string Password = "", DateTime? RegTime = null, DateTime? LastTime = null, string Email = "", string NickName = "", bool IsAdmin = false, bool IsOperator = false, bool IsEnable = true, decimal Credits = 0, decimal Materials = 0, decimal GameTime = 0, string AutoKey = "")
|
||||
{
|
||||
return UserFactory.Create(Id, Username, Password, RegTime, LastTime, Email, NickName, IsAdmin, IsOperator, IsEnable, Credits, Materials, GameTime, AutoKey);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取用户实例
|
||||
/// </summary>
|
||||
/// <param name="ds"></param>
|
||||
/// <returns></returns>
|
||||
public static User GetUser(DataSet ds)
|
||||
{
|
||||
if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
|
||||
{
|
||||
return GetUser(ds.Tables[0].Rows[0]);
|
||||
}
|
||||
return UserFactory.Create();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取用户实例
|
||||
/// </summary>
|
||||
/// <param name="ds"></param>
|
||||
/// <returns></returns>
|
||||
public static User GetUser(DataRow dr)
|
||||
{
|
||||
if (dr != null)
|
||||
{
|
||||
long Id = (long)dr[UserQuery.Column_UID];
|
||||
string Username = (string)dr[UserQuery.Column_Username];
|
||||
string Password = (string)dr[UserQuery.Column_Password];
|
||||
DateTime RegTime = (DateTime)dr[UserQuery.Column_RegTime];
|
||||
DateTime LastTime = (DateTime)dr[UserQuery.Column_LastTime];
|
||||
string Email = (string)dr[UserQuery.Column_Email];
|
||||
string NickName = (string)dr[UserQuery.Column_Nickname];
|
||||
bool IsAdmin = Convert.ToInt32(dr[UserQuery.Column_IsAdmin]) == 1;
|
||||
bool IsOperator = Convert.ToInt32(dr[UserQuery.Column_IsOperator]) == 1;
|
||||
bool IsEnable = Convert.ToInt32(dr[UserQuery.Column_IsEnable]) == 1;
|
||||
decimal Credits = Convert.ToDecimal(dr[UserQuery.Column_Credits]);
|
||||
decimal Materials = Convert.ToDecimal(dr[UserQuery.Column_Materials]);
|
||||
decimal GameTime = Convert.ToDecimal(dr[UserQuery.Column_GameTime]);
|
||||
string AutoKey = (string)dr[UserQuery.Column_AutoKey];
|
||||
return UserFactory.Create(Id, Username, Password, RegTime, LastTime, Email, NickName, IsAdmin, IsOperator, IsEnable, Credits, Materials, GameTime, AutoKey);
|
||||
}
|
||||
return UserFactory.Create();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -53,7 +53,7 @@ namespace Milimoe.FunGame.Core.Entity
|
||||
public Hashtable Skills { get; set; } = new();
|
||||
public Hashtable Items { get; set; } = new();
|
||||
|
||||
public Character()
|
||||
internal Character()
|
||||
{
|
||||
|
||||
}
|
||||
|
@ -6,8 +6,8 @@ namespace Milimoe.FunGame.Core.Entity
|
||||
public class Room : BaseEntity
|
||||
{
|
||||
public override long Id { get => base.Id ; set => base.Id = value; }
|
||||
public string Roomid { get; set; } = "";
|
||||
public DateTime CreateTime { get; set; } = DateTime.Now;
|
||||
public string Roomid { get; set; } = "-1";
|
||||
public DateTime CreateTime { get; set; } = General.DefaultTime;
|
||||
public Dictionary<string, User> Players { get; set; } = new();
|
||||
public User? RoomMaster { get; set; }
|
||||
public RoomType RoomType { get; set; }
|
||||
@ -16,12 +16,17 @@ namespace Milimoe.FunGame.Core.Entity
|
||||
public string Password { get; set; } = "";
|
||||
public GameStatistics? Statistics { get; set; } = null;
|
||||
|
||||
internal Room()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
internal Room(long Id = 0, string Roomid = "-1", DateTime? CreateTime = null, User? RoomMaster = null, RoomType RoomType = RoomType.None, RoomState RoomState = RoomState.Created, string Password = "")
|
||||
{
|
||||
this.Id = Id;
|
||||
this.Roomid = Roomid;
|
||||
if (CreateTime != null) this.CreateTime = (DateTime)CreateTime;
|
||||
if (RoomMaster != null) this.RoomMaster = RoomMaster;
|
||||
this.CreateTime = CreateTime ?? General.DefaultTime;
|
||||
this.RoomMaster = RoomMaster;
|
||||
this.RoomType = RoomType;
|
||||
this.RoomState = RoomState;
|
||||
this.Password = Password;
|
||||
|
@ -1,11 +1,13 @@
|
||||
using System.Data;
|
||||
using Milimoe.FunGame.Core.Interface.Entity;
|
||||
using Milimoe.FunGame.Core.Library.Constant;
|
||||
using Milimoe.FunGame.Core.Library.SQLScript.Entity;
|
||||
|
||||
namespace Milimoe.FunGame.Core.Entity
|
||||
{
|
||||
public class User : BaseEntity
|
||||
{
|
||||
public override Guid Guid { get; set; } = Guid.NewGuid();
|
||||
public override long Id { get; set; }
|
||||
public string Username { get; set; } = "";
|
||||
public string Password { get; set; } = "";
|
||||
@ -15,7 +17,7 @@ namespace Milimoe.FunGame.Core.Entity
|
||||
public string NickName { get; set; } = "";
|
||||
public bool IsAdmin { get; set; } = false;
|
||||
public bool IsOperator { get; set; } = false;
|
||||
public bool IsEnable { get; set; } = false;
|
||||
public bool IsEnable { get; set; } = true;
|
||||
public decimal Credits { get; set; } = 0;
|
||||
public decimal Materials { get; set; } = 0;
|
||||
public decimal GameTime { get; set; } = 0;
|
||||
@ -28,26 +30,22 @@ namespace Milimoe.FunGame.Core.Entity
|
||||
|
||||
}
|
||||
|
||||
internal User(DataSet? DataSet, int Index = 0)
|
||||
internal User(long Id = 0, string Username = "", string Password = "", DateTime? RegTime = null, DateTime? LastTime = null, string Email = "", string NickName = "", bool IsAdmin = false, bool IsOperator = false, bool IsEnable = true, decimal Credits = 0, decimal Materials = 0, decimal GameTime = 0, string AutoKey = "")
|
||||
{
|
||||
if (DataSet != null && DataSet.Tables[0].Rows.Count > 0)
|
||||
{
|
||||
DataRow DataRow = DataSet.Tables[0].Rows[Index];
|
||||
Id = (long)DataRow[UserQuery.Column_UID];
|
||||
Username = (string)DataRow[UserQuery.Column_Username];
|
||||
Password = (string)DataRow[UserQuery.Column_Password];
|
||||
RegTime = (DateTime)DataRow[UserQuery.Column_RegTime];
|
||||
LastTime = (DateTime)DataRow[UserQuery.Column_LastTime];
|
||||
Email = (string)DataRow[UserQuery.Column_Email];
|
||||
NickName = (string)DataRow[UserQuery.Column_Nickname];
|
||||
IsAdmin = Convert.ToInt32(DataRow[UserQuery.Column_IsAdmin]) == 1;
|
||||
IsOperator = Convert.ToInt32(DataRow[UserQuery.Column_IsOperator]) == 1;
|
||||
IsEnable = Convert.ToInt32(DataRow[UserQuery.Column_IsEnable]) == 1;
|
||||
Credits = Convert.ToDecimal(DataRow[UserQuery.Column_Credits]);
|
||||
Materials = Convert.ToDecimal(DataRow[UserQuery.Column_Materials]);
|
||||
GameTime = Convert.ToDecimal(DataRow[UserQuery.Column_GameTime]);
|
||||
AutoKey = (string)DataRow[UserQuery.Column_AutoKey];
|
||||
}
|
||||
this.Id = Id;
|
||||
this.Username = Username;
|
||||
this.Password = Password;
|
||||
this.RegTime = RegTime ?? General.DefaultTime;
|
||||
this.LastTime = LastTime ?? General.DefaultTime;
|
||||
this.Email = Email;
|
||||
this.NickName = NickName;
|
||||
this.IsAdmin = IsAdmin;
|
||||
this.IsOperator = IsOperator;
|
||||
this.IsEnable = IsEnable;
|
||||
this.Credits = Credits;
|
||||
this.Materials = Materials;
|
||||
this.GameTime = GameTime;
|
||||
this.AutoKey = AutoKey;
|
||||
}
|
||||
|
||||
public override bool Equals(IBaseEntity? other)
|
||||
|
9
Interface/Base/IEntityConverter.cs
Normal file
9
Interface/Base/IEntityConverter.cs
Normal file
@ -0,0 +1,9 @@
|
||||
using System.Text.Json;
|
||||
|
||||
namespace Milimoe.FunGame.Core.Interface.Base
|
||||
{
|
||||
internal interface IEntityConverter<T>
|
||||
{
|
||||
public void ReadPropertyName(ref Utf8JsonReader reader, string propertyName, ref T? result);
|
||||
}
|
||||
}
|
29
Library/Common/Architecture/BaseEntityConverter.cs
Normal file
29
Library/Common/Architecture/BaseEntityConverter.cs
Normal file
@ -0,0 +1,29 @@
|
||||
using System.Text.Json;
|
||||
using System.Text.Json.Serialization;
|
||||
using Milimoe.FunGame.Core.Interface.Base;
|
||||
|
||||
namespace Milimoe.FunGame.Core.Library.Common.Architecture
|
||||
{
|
||||
public abstract class BaseEntityConverter<T> : JsonConverter<T>, IEntityConverter<T>
|
||||
{
|
||||
public override T? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
|
||||
{
|
||||
T? result = default;
|
||||
|
||||
while (reader.Read())
|
||||
{
|
||||
if (reader.TokenType == JsonTokenType.EndObject) break;
|
||||
|
||||
if (reader.TokenType == JsonTokenType.PropertyName)
|
||||
{
|
||||
string propertyName = reader.GetString() ?? "";
|
||||
ReadPropertyName(ref reader, propertyName, ref result);
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
public abstract void ReadPropertyName(ref Utf8JsonReader reader, string propertyName, ref T? result);
|
||||
}
|
||||
}
|
@ -93,7 +93,7 @@ namespace Milimoe.FunGame.Core.Library.Common.JsonConverter
|
||||
{
|
||||
values[index] = result;
|
||||
}
|
||||
else values[index] = DateTime.MinValue;
|
||||
else values[index] = General.DefaultTime;
|
||||
break;
|
||||
|
||||
case "System.Decimal":
|
||||
|
@ -9,59 +9,60 @@ namespace Milimoe.FunGame.Core.Library.Common.JsonConverter
|
||||
{
|
||||
public class RoomConverter : JsonConverter<Room>
|
||||
{
|
||||
public override bool CanConvert(Type objectType)
|
||||
{
|
||||
return objectType == typeof(Room);
|
||||
}
|
||||
|
||||
public override Room Read(ref Utf8JsonReader reader, Type type, JsonSerializerOptions options)
|
||||
{
|
||||
Room room = Factory.GetRoom();
|
||||
|
||||
while (reader.Read())
|
||||
{
|
||||
if (reader.TokenType == JsonTokenType.EndObject) break;
|
||||
|
||||
if (reader.TokenType == JsonTokenType.PropertyName)
|
||||
{
|
||||
string property = reader.GetString() ?? "";
|
||||
|
||||
reader.Read();
|
||||
switch (property)
|
||||
{
|
||||
case RoomQuery.Column_ID:
|
||||
reader.Read();
|
||||
room.Id = reader.GetInt64();
|
||||
break;
|
||||
|
||||
case RoomQuery.Column_RoomID:
|
||||
reader.Read();
|
||||
room.Roomid = reader.GetString() ?? "";
|
||||
break;
|
||||
|
||||
case RoomQuery.Column_CreateTime:
|
||||
reader.Read();
|
||||
string dateString = reader.GetString() ?? "";
|
||||
if (DateTime.TryParseExact(dateString, General.GeneralDateTimeFormat, null, System.Globalization.DateTimeStyles.None, out DateTime result))
|
||||
{
|
||||
room.CreateTime = result;
|
||||
}
|
||||
else room.CreateTime = DateTime.MinValue;
|
||||
else room.CreateTime = General.DefaultTime;
|
||||
break;
|
||||
|
||||
case RoomQuery.Column_RoomMaster:
|
||||
reader.Read();
|
||||
string master = reader.GetString() ?? "";
|
||||
room.RoomMaster = JsonSerializer.Deserialize<User>(master, options);
|
||||
break;
|
||||
|
||||
case RoomQuery.Column_RoomType:
|
||||
reader.Read();
|
||||
room.RoomType = (RoomType)reader.GetInt64();
|
||||
break;
|
||||
|
||||
case RoomQuery.Column_RoomState:
|
||||
reader.Read();
|
||||
room.RoomState = (RoomState)reader.GetInt64();
|
||||
break;
|
||||
|
||||
case RoomQuery.Column_Password:
|
||||
reader.Read();
|
||||
room.Password = reader.GetString() ?? "";
|
||||
break;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -20,61 +20,64 @@ namespace Milimoe.FunGame.Core.Library.Common.JsonConverter
|
||||
|
||||
while (reader.Read())
|
||||
{
|
||||
if (reader.TokenType == JsonTokenType.EndObject) break;
|
||||
|
||||
if (reader.TokenType == JsonTokenType.PropertyName)
|
||||
{
|
||||
string propertyName = reader.GetString() ?? "";
|
||||
reader.Read();
|
||||
switch (propertyName)
|
||||
{
|
||||
case "Id":
|
||||
case UserQuery.Column_UID:
|
||||
user.Id = reader.GetInt64();
|
||||
break;
|
||||
case "Username":
|
||||
case UserQuery.Column_Username:
|
||||
user.Username = reader.GetString() ?? "";
|
||||
break;
|
||||
case "Password":
|
||||
case UserQuery.Column_Password:
|
||||
user.Password = reader.GetString() ?? "";
|
||||
break;
|
||||
case "RegTime":
|
||||
case UserQuery.Column_RegTime:
|
||||
string regTime = reader.GetString() ?? "";
|
||||
if (DateTime.TryParseExact(regTime, General.GeneralDateTimeFormat, null, System.Globalization.DateTimeStyles.None, out DateTime RegTime))
|
||||
{
|
||||
user.RegTime = RegTime;
|
||||
}
|
||||
else user.RegTime = DateTime.MinValue;
|
||||
else user.RegTime = General.DefaultTime;
|
||||
break;
|
||||
case "LastTime":
|
||||
case UserQuery.Column_LastTime:
|
||||
string lastTime = reader.GetString() ?? "";
|
||||
if (DateTime.TryParseExact(lastTime, General.GeneralDateTimeFormat, null, System.Globalization.DateTimeStyles.None, out DateTime LastTime))
|
||||
{
|
||||
user.LastTime = LastTime;
|
||||
}
|
||||
else user.LastTime = DateTime.MinValue;
|
||||
else user.LastTime = General.DefaultTime;
|
||||
break;
|
||||
case "Email":
|
||||
case UserQuery.Column_Email:
|
||||
user.Email = reader.GetString() ?? "";
|
||||
break;
|
||||
case "NickName":
|
||||
case UserQuery.Column_Nickname:
|
||||
user.NickName = reader.GetString() ?? "";
|
||||
break;
|
||||
case "IsAdmin":
|
||||
case UserQuery.Column_IsAdmin:
|
||||
user.IsAdmin = reader.GetBoolean();
|
||||
break;
|
||||
case "IsOperator":
|
||||
case UserQuery.Column_IsOperator:
|
||||
user.IsOperator = reader.GetBoolean();
|
||||
break;
|
||||
case "IsEnable":
|
||||
case UserQuery.Column_IsEnable:
|
||||
user.IsEnable = reader.GetBoolean();
|
||||
break;
|
||||
case "Credits":
|
||||
case UserQuery.Column_Credits:
|
||||
user.Credits = reader.GetDecimal();
|
||||
break;
|
||||
case "Materials":
|
||||
case UserQuery.Column_Materials:
|
||||
user.Materials = reader.GetDecimal();
|
||||
break;
|
||||
case "GameTime":
|
||||
case UserQuery.Column_GameTime:
|
||||
user.GameTime = reader.GetDecimal();
|
||||
break;
|
||||
case "AutoKey":
|
||||
case UserQuery.Column_AutoKey:
|
||||
user.AutoKey = reader.GetString() ?? "";
|
||||
break;
|
||||
}
|
||||
|
@ -6,12 +6,12 @@ namespace Milimoe.FunGame.Core.Library.Constant
|
||||
public class General
|
||||
{
|
||||
// Static Variable
|
||||
public static Empty EntityInstance { get; } = new();
|
||||
public static User UnknownUserInstance { get; } = new();
|
||||
public static Room HallInstance { get; } = Api.Utility.Factory.GetHall();
|
||||
public static Empty EntityInstance => new();
|
||||
public static User UnknownUserInstance => new();
|
||||
public static Room HallInstance => new();
|
||||
public static Encoding DefaultEncoding => Encoding.Unicode;
|
||||
public static string GeneralDateTimeFormat => "yyyy-MM-dd HH:mm:ss.fff";
|
||||
public static DateTime DefaultTime { get; } = new(1970,1,1,8,0,0);
|
||||
public static DateTime DefaultTime => new(1970, 1, 1, 8, 0, 0);
|
||||
|
||||
// Const
|
||||
public const int MaxRetryTimes = 20;
|
||||
|
@ -14,7 +14,7 @@ namespace Milimoe.FunGame.Core.Service
|
||||
{
|
||||
WriteIndented = true,
|
||||
ReferenceHandler = ReferenceHandler.IgnoreCycles,
|
||||
Converters = { new DateTimeConverter(), new DataTableConverter(), new DataSetConverter() }
|
||||
Converters = { new DateTimeConverter(), new DataTableConverter(), new DataSetConverter(), new UserConverter(), new RoomConverter() }
|
||||
};
|
||||
|
||||
/// <summary>
|
||||
|
Loading…
x
Reference in New Issue
Block a user