Skip to content

Commit

Permalink
Merge pull request #29 from YongAn404/master
Browse files Browse the repository at this point in the history
数据库修复和数据获取优化
  • Loading branch information
XLittleLeft authored Dec 13, 2024
2 parents e1d2138 + 798e8f3 commit 94f9527
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 14 deletions.
16 changes: 14 additions & 2 deletions HelpSense/API/API.cs
Original file line number Diff line number Diff line change
@@ -1,16 +1,28 @@
using HelpSense.API.Serialization;
using LiteDB;
using System.Collections.Generic;

namespace HelpSense.API
{
public static class API
{
public static List<string> TimerHidden { get; } = [];
public static Dictionary<string, PlayerLog> PlayerDataDic = [];

public static bool TryGetLog(string id, out PlayerLog log)
{
log = Plugin.Instance.Database.GetCollection<PlayerLog>("Players")?.FindById(id);
return log != null;
using LiteDatabase database = new(Plugin.Instance.Config.SavePath);
if (PlayerDataDic.TryGetValue(id,out log))
{
return true;
}
log = database.GetCollection<PlayerLog>("Players")?.FindById(id);
if (log != null)
{
PlayerDataDic.Add(id, log);
return true;
}
return false;
}
}
}
11 changes: 8 additions & 3 deletions HelpSense/API/InfoExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
namespace HelpSense.API
{
using HelpSense.API.Serialization;
using HelpSense.ConfigSystem;
using HelpSense.Helper;
using LiteDB;
using MEC;
using PluginAPI.Core;
using System;
Expand All @@ -29,7 +31,8 @@ public static PlayerLog GetLog(this Player ply)
RolePlayed = 0,
PlayerShot = 0,
};
Plugin.Instance.Database.GetCollection<PlayerLog>("Players").Insert(toInsert);
using LiteDatabase database = new(Plugin.Instance.Config.SavePath);
database.GetCollection<PlayerLog>("Players").Insert(toInsert);
}

if (log is null)
Expand Down Expand Up @@ -57,7 +60,8 @@ public static PlayerLog GetLog(this ReferenceHub ply)
RolePlayed = 0,
PlayerShot = 0,
};
Plugin.Instance.Database.GetCollection<PlayerLog>("Players").Insert(toInsert);
using LiteDatabase database = new(Plugin.Instance.Config.SavePath);
database.GetCollection<PlayerLog>("Players").Insert(toInsert);
}

if (log is null)
Expand All @@ -67,7 +71,8 @@ public static PlayerLog GetLog(this ReferenceHub ply)

public static void UpdateLog(this PlayerLog log)
{
Plugin.Instance.Database.GetCollection<PlayerLog>("Players").Update(log);
using LiteDatabase database = new(Plugin.Instance.Config.SavePath);
database.GetCollection<PlayerLog>("Players").Update(log);
}

public static IEnumerator<float> CollectInfo()
Expand Down
9 changes: 0 additions & 9 deletions HelpSense/Plugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,6 @@ public class Plugin
{
private Harmony _harmony = new("cn.xlittleleft.plugin");

public LiteDatabase Database;

[PluginConfig]
public Config Config;

Expand Down Expand Up @@ -104,11 +102,6 @@ private void LoadPlugin()
{
Instance = this;

if (Config.SavePlayersInfo)
{
Database = new LiteDatabase(Config.SavePath);
}

EventManager.RegisterEvents(this);

_harmony.PatchAll();
Expand Down Expand Up @@ -1495,8 +1488,6 @@ public void OnDisabled()

Instance = null;
_harmony = null;
Database.Dispose();
Database = null;
}
}
}

0 comments on commit 94f9527

Please sign in to comment.