多线程访问存档优化

This commit is contained in:
milimoe 2025-07-21 00:52:37 +08:00
parent afe506793b
commit dee953ea7b
Signed by: milimoe
GPG Key ID: 9554D37E4B8991D0
5 changed files with 647 additions and 584 deletions

View File

@ -135,8 +135,7 @@ namespace Oshima.FunGame.OshimaServers
foreach (string filePath in filePaths) foreach (string filePath in filePaths)
{ {
string fileName = Path.GetFileNameWithoutExtension(filePath); string fileName = Path.GetFileNameWithoutExtension(filePath);
PluginConfig pc = new("saved", fileName); PluginConfig pc = FunGameService.GetUserConfig(fileName);
pc.LoadConfig();
if (pc.Count > 0) if (pc.Count > 0)
{ {
User user = FunGameService.GetUser(pc); User user = FunGameService.GetUser(pc);
@ -162,15 +161,14 @@ namespace Oshima.FunGame.OshimaServers
} }
if (updateQuest || updateExplore) if (updateQuest || updateExplore)
{ {
user.LastTime = DateTime.Now; FunGameService.SetUserConfig(user.Id, pc, user);
pc.Add("user", user);
pc.SaveConfig();
} }
if (FunGameConstant.UserLastVisitStore.TryGetValue(user.Id, out LastStoreModel? value) && value != null && (DateTime.Now - value.LastTime).TotalMinutes > 2) if (FunGameConstant.UserLastVisitStore.TryGetValue(user.Id, out LastStoreModel? value) && value != null && (DateTime.Now - value.LastTime).TotalMinutes > 2)
{ {
FunGameConstant.UserLastVisitStore.Remove(user.Id); FunGameConstant.UserLastVisitStore.Remove(user.Id);
} }
} }
FunGameService.ReleaseUserSemaphoreSlim(fileName);
} }
Controller.WriteLine("读取 FunGame 存档缓存", LogLevel.Debug); Controller.WriteLine("读取 FunGame 存档缓存", LogLevel.Debug);
} }
@ -207,12 +205,12 @@ namespace Oshima.FunGame.OshimaServers
foreach (string filePath in filePaths) foreach (string filePath in filePaths)
{ {
string fileName = Path.GetFileNameWithoutExtension(filePath); string fileName = Path.GetFileNameWithoutExtension(filePath);
PluginConfig pc = new("saved", fileName); PluginConfig pc = FunGameService.GetUserConfig(fileName);
pc.LoadConfig();
pc.Add("signed", false); pc.Add("signed", false);
pc.Add("logon", false); pc.Add("logon", false);
pc.Add("exploreTimes", FunGameConstant.MaxExploreTimes); pc.Add("exploreTimes", FunGameConstant.MaxExploreTimes);
pc.SaveConfig(); pc.SaveConfig();
FunGameService.ReleaseUserSemaphoreSlim(fileName);
} }
Controller.WriteLine("刷新签到"); Controller.WriteLine("刷新签到");
} }

View File

@ -1,4 +1,5 @@
using Milimoe.FunGame.Core.Entity; using System.Collections.Concurrent;
using Milimoe.FunGame.Core.Entity;
using Milimoe.FunGame.Core.Library.Constant; using Milimoe.FunGame.Core.Library.Constant;
using Oshima.Core.Constant; using Oshima.Core.Constant;
using Oshima.FunGame.OshimaModules.Effects.OpenEffects; using Oshima.FunGame.OshimaModules.Effects.OpenEffects;
@ -34,6 +35,7 @@ namespace Oshima.FunGame.OshimaServers.Service
public static Dictionary<long, User> UserIdAndUsername { get; } = []; public static Dictionary<long, User> UserIdAndUsername { get; } = [];
public static Dictionary<long, Item> MarketItemIdAndItem { get; } = []; public static Dictionary<long, Item> MarketItemIdAndItem { get; } = [];
public static Dictionary<long, LastStoreModel> UserLastVisitStore { get; } = []; public static Dictionary<long, LastStoreModel> UserLastVisitStore { get; } = [];
public static ConcurrentDictionary<string, SemaphoreSlim> UserSemaphoreSlims { get; } = [];
public static ItemType[] ItemCanUsed => [ItemType.Consumable, ItemType.MagicCard, ItemType.SpecialItem, ItemType.GiftBox, ItemType.Others]; public static ItemType[] ItemCanUsed => [ItemType.Consumable, ItemType.MagicCard, ItemType.SpecialItem, ItemType.GiftBox, ItemType.Others];
public static ItemType[] ItemCanNotDrawCard => [ItemType.Collectible, ItemType.QuestItem, ItemType.GiftBox, ItemType.Others]; public static ItemType[] ItemCanNotDrawCard => [ItemType.Collectible, ItemType.QuestItem, ItemType.GiftBox, ItemType.Others];

View File

@ -965,8 +965,7 @@ namespace Oshima.FunGame.OshimaServers.Service
tasks.Add(Task.Run(() => tasks.Add(Task.Run(() =>
{ {
string fileNameWithoutExtension = Path.GetFileNameWithoutExtension(file); string fileNameWithoutExtension = Path.GetFileNameWithoutExtension(file);
PluginConfig pc = new("saved", fileNameWithoutExtension); PluginConfig pc = GetUserConfig(fileNameWithoutExtension);
pc.LoadConfig();
if (pc.Count > 0) if (pc.Count > 0)
{ {
User user = GetUser(pc); User user = GetUser(pc);
@ -983,8 +982,11 @@ namespace Oshima.FunGame.OshimaServers.Service
item.IsTradable = true; item.IsTradable = true;
} }
} }
pc.Add("user", user); SetUserConfig(user.Id, pc, user, false);
pc.SaveConfig(); }
else
{
ReleaseUserSemaphoreSlim(fileNameWithoutExtension);
} }
})); }));
} }
@ -2114,7 +2116,7 @@ namespace Oshima.FunGame.OshimaServers.Service
} }
else else
{ {
return NetworkUtility.JsonSerialize($"你的{General.GameplayEquilibriumConstant.InGameCurrency}不足 {reduce} 呢,无法购买【{goods.Name}】!"); return $"你的{General.GameplayEquilibriumConstant.InGameCurrency}不足 {reduce} 呢,无法购买【{goods.Name}】!";
} }
} }
else if (needy == General.GameplayEquilibriumConstant.InGameMaterial) else if (needy == General.GameplayEquilibriumConstant.InGameMaterial)
@ -2126,7 +2128,7 @@ namespace Oshima.FunGame.OshimaServers.Service
} }
else else
{ {
return NetworkUtility.JsonSerialize($"你的{General.GameplayEquilibriumConstant.InGameMaterial}不足 {reduce} 呢,无法购买【{goods.Name}】!"); return $"你的{General.GameplayEquilibriumConstant.InGameMaterial}不足 {reduce} 呢,无法购买【{goods.Name}】!";
} }
} }
} }
@ -3280,13 +3282,8 @@ namespace Oshima.FunGame.OshimaServers.Service
itemsTradeRecord.Add("offeree", offereeItems); itemsTradeRecord.Add("offeree", offereeItems);
itemsTradeRecord.SaveConfig(); itemsTradeRecord.SaveConfig();
user.LastTime = DateTime.Now; SetUserConfig(user.Id, pc, user);
pc.Add("user", user); SetUserConfig(user2.Id, pc2, user2);
pc.SaveConfig();
user2.LastTime = DateTime.Now;
pc2.Add("user", user2);
pc2.SaveConfig();
AddNotice(offer.Offeror, $"报价编号 {offerId} 已交易完成,请通过【查报价{offerId}】查询报价记录。"); AddNotice(offer.Offeror, $"报价编号 {offerId} 已交易完成,请通过【查报价{offerId}】查询报价记录。");
@ -3960,5 +3957,58 @@ namespace Oshima.FunGame.OshimaServers.Service
return msg; return msg;
} }
public static void ReleaseUserSemaphoreSlim(string key)
{
if (FunGameConstant.UserSemaphoreSlims.TryGetValue(key, out SemaphoreSlim? obj) && obj != null)
{
obj.Release();
}
}
public static void ReleaseUserSemaphoreSlim(long uid) => ReleaseUserSemaphoreSlim(uid.ToString());
public static void SetUserConfig(string key, PluginConfig pc, User user, bool updateLastTime = true)
{
if (updateLastTime) user.LastTime = DateTime.Now;
pc.Add("user", user);
pc.SaveConfig();
if (FunGameConstant.UserSemaphoreSlims.TryGetValue(key, out SemaphoreSlim? obj) && obj != null)
{
obj.Release();
}
}
public static void SetUserConfig(long uid, PluginConfig pc, User user, bool updateLastTime = true) => SetUserConfig(uid.ToString(), pc, user, updateLastTime);
public static PluginConfig GetUserConfig(string key)
{
if (FunGameConstant.UserSemaphoreSlims.TryGetValue(key, out SemaphoreSlim? obj) && obj != null)
{
obj.Wait();
}
else
{
obj = new(1, 1);
obj.Wait();
FunGameConstant.UserSemaphoreSlims[key] = obj;
}
PluginConfig pc = new("saved", key);
pc.LoadConfig();
return pc;
}
public static PluginConfig GetUserConfig(long uid) => GetUserConfig(uid.ToString());
public static bool CheckSemaphoreSlim(string key)
{
if (FunGameConstant.UserSemaphoreSlims.TryGetValue(key, out SemaphoreSlim? obj) && obj != null)
{
return obj.CurrentCount == 0;
}
return false;
}
public static bool CheckSemaphoreSlim(long uid) => CheckSemaphoreSlim(uid.ToString());
} }
} }

File diff suppressed because it is too large Load Diff

View File

@ -114,6 +114,19 @@ namespace Oshima.FunGame.WebAPI.Services
} }
} }
if (e.Detail == "重置状态")
{
FunGameService.ReleaseUserSemaphoreSlim(uid);
await SendAsync(e, "筽祀牻", "Done");
return result;
}
if (FunGameService.CheckSemaphoreSlim(uid))
{
await SendAsync(e, "筽祀牻", "检测到上一条指令尚未完成,若出现异常情况,请等待其执行完成,或者使用【重置状态】指令重置当前的指令执行状态。", msgSeq: 999);
return result;
}
//if (QQOpenID.QQAndOpenID.TryGetValue(openid, out long temp_qq)) //if (QQOpenID.QQAndOpenID.TryGetValue(openid, out long temp_qq))
//{ //{
// qq = temp_qq; // qq = temp_qq;
@ -2282,7 +2295,7 @@ namespace Oshima.FunGame.WebAPI.Services
} }
if (cindexs.Count > 1 && cindexs.Count <= 5) if (cindexs.Count > 1 && cindexs.Count <= 5)
{ {
(msg, eid) = await Controller.ExploreRegion(uid, cindexs[0], [.. cindexs.Skip(1).Select(id => (long)id)]); (msg, eid) = Controller.ExploreRegion(uid, cindexs[0], [.. cindexs.Skip(1).Select(id => (long)id)]);
if (msg.Trim() != "") if (msg.Trim() != "")
{ {
await SendAsync(e, "探索", msg); await SendAsync(e, "探索", msg);