mirror of
https://gitee.com/cssfw/EasyTools.git
synced 2026-03-28 12:01:36 +08:00
feat: 玩家数据储存系统(序列化和反序列化部分)
This commit is contained in:
19
Configs/DataBaseConfig.cs
Normal file
19
Configs/DataBaseConfig.cs
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
using LabApi.Loader.Features.Paths;
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.ComponentModel;
|
||||||
|
using System.IO;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace EasyTools.Configs
|
||||||
|
{
|
||||||
|
public class DataBaseConfig
|
||||||
|
{
|
||||||
|
[Description("是否启用玩家数据储存系统")]
|
||||||
|
public bool database_enable { get; set; } = true;
|
||||||
|
[Description("数据库存储路径")]
|
||||||
|
public string database_path { get; set; } = Path.Combine(PathManager.Configs.FullName ?? Environment.CurrentDirectory, @"EasyTools.db");
|
||||||
|
}
|
||||||
|
}
|
||||||
34
DataBase/DataAPI.cs
Normal file
34
DataBase/DataAPI.cs
Normal file
@@ -0,0 +1,34 @@
|
|||||||
|
using EasyTools.DataBase.Serialization;
|
||||||
|
using EasyTools.Events;
|
||||||
|
using LiteDB;
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using UnityEngine.LowLevel;
|
||||||
|
|
||||||
|
namespace EasyTools.DataBase
|
||||||
|
{
|
||||||
|
public static class DataAPI
|
||||||
|
{
|
||||||
|
public static List<string> TimerHidden { get; } = [];
|
||||||
|
public static Dictionary<string, PlayerData> PlayerDataDic = [];
|
||||||
|
|
||||||
|
public static bool TryGetData(string id, out PlayerData data)
|
||||||
|
{
|
||||||
|
if (PlayerDataDic.TryGetValue(id, out data))
|
||||||
|
{
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
using LiteDatabase database = new(CustomEventHandler.DataBaseConfig.database_path);
|
||||||
|
|
||||||
|
if ((data = database.GetCollection<PlayerData>("Players")?.FindById(id)) != null)
|
||||||
|
{
|
||||||
|
PlayerDataDic.Add(id, data);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
106
DataBase/InfoExtension.cs
Normal file
106
DataBase/InfoExtension.cs
Normal file
@@ -0,0 +1,106 @@
|
|||||||
|
using System.Collections.Generic;
|
||||||
|
using EasyTools.DataBase.Serialization;
|
||||||
|
using EasyTools.Events;
|
||||||
|
using LabApi.Features.Wrappers;
|
||||||
|
using LiteDB;
|
||||||
|
using MEC;
|
||||||
|
using System;
|
||||||
|
|
||||||
|
namespace EasyTools.DataBase
|
||||||
|
{
|
||||||
|
public static class InfoExtension
|
||||||
|
{
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 玩家列表
|
||||||
|
/// 不用 Player.ReadyList 是因为它包含Dummy
|
||||||
|
/// </summary>
|
||||||
|
public static List<Player> PlayerList = [];
|
||||||
|
|
||||||
|
public static PlayerData GetData(this Player ply)
|
||||||
|
{
|
||||||
|
PlayerData toInsert = null;
|
||||||
|
if (!DataAPI.TryGetData(ply.UserId, out PlayerData data))
|
||||||
|
{
|
||||||
|
toInsert = new PlayerData()
|
||||||
|
{
|
||||||
|
ID = ply.UserId,
|
||||||
|
NickName = "",
|
||||||
|
LastJoinedTime = DateTime.Now,
|
||||||
|
LastLeftTime = DateTime.Now,
|
||||||
|
PlayedTimes = 0,
|
||||||
|
PlayerKills = 0,
|
||||||
|
PlayerDeath = 0,
|
||||||
|
PlayerSCPKills = 0,
|
||||||
|
PlayerDamage = 0,
|
||||||
|
RolePlayed = 0,
|
||||||
|
PlayerShot = 0,
|
||||||
|
};
|
||||||
|
using LiteDatabase database = new(CustomEventHandler.DataBaseConfig.database_path);
|
||||||
|
database.GetCollection<PlayerData>("Players").Insert(toInsert);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (data is null)
|
||||||
|
return toInsert;
|
||||||
|
return data;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static PlayerData GetData(this ReferenceHub ply)
|
||||||
|
{
|
||||||
|
PlayerData toInsert = null;
|
||||||
|
if (string.IsNullOrWhiteSpace(ply.authManager.UserId))
|
||||||
|
throw new ArgumentNullException(nameof(ply));
|
||||||
|
if (!DataAPI.TryGetData(ply.authManager.UserId, out PlayerData data))
|
||||||
|
{
|
||||||
|
toInsert = new PlayerData()
|
||||||
|
{
|
||||||
|
ID = ply.authManager.UserId,
|
||||||
|
NickName = "",
|
||||||
|
LastJoinedTime = DateTime.Now,
|
||||||
|
LastLeftTime = DateTime.Now,
|
||||||
|
PlayedTimes = 0,
|
||||||
|
PlayerKills = 0,
|
||||||
|
PlayerDeath = 0,
|
||||||
|
PlayerSCPKills = 0,
|
||||||
|
PlayerDamage = 0,
|
||||||
|
RolePlayed = 0,
|
||||||
|
PlayerShot = 0,
|
||||||
|
};
|
||||||
|
using LiteDatabase database = new(CustomEventHandler.DataBaseConfig.database_path);
|
||||||
|
database.GetCollection<PlayerData>("Players").Insert(toInsert);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (data is null)
|
||||||
|
return toInsert;
|
||||||
|
return data;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void UpdateData(this PlayerData data)
|
||||||
|
{
|
||||||
|
using LiteDatabase database = new(CustomEventHandler.DataBaseConfig.database_path);
|
||||||
|
database.GetCollection<PlayerData>("Players").Update(data);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static IEnumerator<float> CollectInfo()
|
||||||
|
{
|
||||||
|
while (true)
|
||||||
|
{
|
||||||
|
yield return Timing.WaitForSeconds(60f);
|
||||||
|
|
||||||
|
foreach (Player Player in PlayerList)
|
||||||
|
{
|
||||||
|
if (Player != null && !Player.DoNotTrack)
|
||||||
|
{
|
||||||
|
var pLog = Player.GetData();
|
||||||
|
pLog.PlayedTimes += 60;
|
||||||
|
pLog.UpdateData();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (Round.IsRoundEnded)
|
||||||
|
{
|
||||||
|
yield break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
22
DataBase/Serialization/PlayerData.cs
Normal file
22
DataBase/Serialization/PlayerData.cs
Normal file
@@ -0,0 +1,22 @@
|
|||||||
|
using LiteDB;
|
||||||
|
using System;
|
||||||
|
|
||||||
|
namespace EasyTools.DataBase.Serialization
|
||||||
|
{
|
||||||
|
[Serializable]
|
||||||
|
public class PlayerData
|
||||||
|
{
|
||||||
|
public string NickName { get; set; }
|
||||||
|
public DateTime LastJoinedTime { get; set; }
|
||||||
|
public DateTime LastLeftTime { get; set; }
|
||||||
|
public int PlayedTimes { get; set; }
|
||||||
|
public int PlayerKills { get; set; }
|
||||||
|
public int PlayerDeath { get; set; }
|
||||||
|
public int PlayerSCPKills { get; set; }
|
||||||
|
public float PlayerDamage { get; set; }
|
||||||
|
public int RolePlayed { get; set; }
|
||||||
|
public int PlayerShot { get; set; }
|
||||||
|
[BsonId]
|
||||||
|
public string ID { get; set; }
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -124,6 +124,7 @@
|
|||||||
<Compile Include="Configs\Config.cs" />
|
<Compile Include="Configs\Config.cs" />
|
||||||
<Compile Include="Configs\CustomRoleConfig.cs" />
|
<Compile Include="Configs\CustomRoleConfig.cs" />
|
||||||
<Compile Include="Configs\TranslateConfig.cs" />
|
<Compile Include="Configs\TranslateConfig.cs" />
|
||||||
|
<Compile Include="DataBase\Serialization\PlayerData.cs" />
|
||||||
<Compile Include="Events\CustomEventHandler.cs" />
|
<Compile Include="Events\CustomEventHandler.cs" />
|
||||||
<Compile Include="Plugins.cs" />
|
<Compile Include="Plugins.cs" />
|
||||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||||
|
|||||||
@@ -40,6 +40,8 @@ namespace EasyTools.Events
|
|||||||
|
|
||||||
public static CustomRoleConfig CustomRoleConfig;
|
public static CustomRoleConfig CustomRoleConfig;
|
||||||
|
|
||||||
|
public static DataBaseConfig DataBaseConfig;
|
||||||
|
|
||||||
public static CoroutineHandle Badge_Coroutine;
|
public static CoroutineHandle Badge_Coroutine;
|
||||||
|
|
||||||
public override void OnServerWaitingForPlayers()
|
public override void OnServerWaitingForPlayers()
|
||||||
|
|||||||
@@ -26,6 +26,7 @@ namespace EasyTools
|
|||||||
CustomEventHandler.TranslateConfig = this.LoadConfig<TranslateConfig>("translateConfig.yml");
|
CustomEventHandler.TranslateConfig = this.LoadConfig<TranslateConfig>("translateConfig.yml");
|
||||||
CustomEventHandler.BadgeConfig = this.LoadConfig<BadgeConfig>("badgeConfig.yml");
|
CustomEventHandler.BadgeConfig = this.LoadConfig<BadgeConfig>("badgeConfig.yml");
|
||||||
CustomEventHandler.CustomRoleConfig = this.LoadConfig<CustomRoleConfig>("customRoleConfig.yml");
|
CustomEventHandler.CustomRoleConfig = this.LoadConfig<CustomRoleConfig>("customRoleConfig.yml");
|
||||||
|
CustomEventHandler.DataBaseConfig = this.LoadConfig<DataBaseConfig>("dataBaseConfig.yml");
|
||||||
|
|
||||||
if (!Directory.Exists(CustomEventHandler.BadgeConfig.Pach))
|
if (!Directory.Exists(CustomEventHandler.BadgeConfig.Pach))
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in New Issue
Block a user