From b05f86ed69a04f8518e5bafb8b747afa138b85fc Mon Sep 17 00:00:00 2001 From: milimoe Date: Thu, 2 Jan 2025 01:10:22 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E8=A7=92=E8=89=B2=E7=8A=B6?= =?UTF-8?q?=E6=80=81=E6=98=BE=E7=A4=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- OshimaCore/Controllers/FunGameController.cs | 122 +++++++++++++++----- OshimaCore/OshimaWebAPI.cs | 13 ++- OshimaCore/Utils/FunGameService.cs | 86 +++++++++++--- OshimaModules/Items/Consumable/经验书.cs | 20 +++- 4 files changed, 195 insertions(+), 46 deletions(-) diff --git a/OshimaCore/Controllers/FunGameController.cs b/OshimaCore/Controllers/FunGameController.cs index ea61a53..9cb445e 100644 --- a/OshimaCore/Controllers/FunGameController.cs +++ b/OshimaCore/Controllers/FunGameController.cs @@ -18,11 +18,12 @@ namespace Oshima.Core.Controllers [Route("[controller]")] public class FunGameController(ILogger logger) : ControllerBase { + public static ItemType[] ItemCanUsed => [ItemType.Consumable, ItemType.MagicCard, ItemType.SpecialItem, ItemType.GiftBox, ItemType.Others]; + private readonly ILogger _logger = logger; private const int drawCardReduce = 2000; private const int drawCardReduce_Material = 10; private const string noSaved = "你还没有创建存档!请发送【创建存档】创建。"; - private readonly ItemType[] itemCanUsed = [ItemType.Consumable, ItemType.MagicCard, ItemType.SpecialItem, ItemType.GiftBox, ItemType.Others]; [HttpGet("test")] public List GetTest([FromQuery] bool? isweb = null, [FromQuery] bool? isteam = null, [FromQuery] bool? showall = null) @@ -283,6 +284,25 @@ namespace Oshima.Core.Controllers } return NetworkUtility.JsonSerialize(""); } + + [HttpGet("iteminfoname")] + public string GetItemInfo_Name([FromQuery] string? name = null) + { + IEnumerable items = FunGameService.AllItems; + if (name != null) + { + List msg = []; + Item? i = items.Where(i => i.Name == name).FirstOrDefault()?.Copy(); + if (i != null) + { + i.SetLevel(1); + msg.Add(i.ToString(false, true)); + } + + return NetworkUtility.JsonSerialize(string.Join("\r\n\r\n", msg)); + } + return NetworkUtility.JsonSerialize(""); + } [HttpGet("newmagiccard")] public string GenerateMagicCard() @@ -746,7 +766,7 @@ namespace Oshima.Core.Controllers } str += $"物品序号:{itemsIndex}\r\n"; str += $"拥有数量:{objs.Count}(" + (first.IsEquipment ? $"可装备数量:{objs.Count(i => i.Character is null)}," : "") + - (itemCanUsed.Contains(first.ItemType) ? $"可使用数量:{objs.Count(i => i.RemainUseTimes > 0)}," : "") + + (ItemCanUsed.Contains(first.ItemType) ? $"可使用数量:{objs.Count(i => i.RemainUseTimes > 0)}," : "") + $"可出售数量:{objs.Count(i => i.IsSellable)},可交易数量:{objs.Count(i => i.IsTradable)})"; list.Add(str); } @@ -831,7 +851,7 @@ namespace Oshima.Core.Controllers } str += $"物品序号:{itemsIndex}\r\n"; str += $"拥有数量:{objs.Count}(" + (first.IsEquipment ? $"可装备数量:{objs.Count(i => i.Character is null)}," : "") + - (itemCanUsed.Contains(first.ItemType) ? $"可使用数量:{objs.Count(i => i.RemainUseTimes > 0)}," : "") + + (ItemCanUsed.Contains(first.ItemType) ? $"可使用数量:{objs.Count(i => i.RemainUseTimes > 0)}," : "") + $"可出售数量:{objs.Count(i => i.IsSellable)},可交易数量:{objs.Count(i => i.IsTradable)})"; list.Add(str); } @@ -1639,7 +1659,7 @@ namespace Oshima.Core.Controllers if (itemIndex > 0 && itemIndex <= user.Inventory.Items.Count) { item = user.Inventory.Items.ToList()[itemIndex - 1]; - if (itemCanUsed.Contains(item.ItemType)) + if (ItemCanUsed.Contains(item.ItemType)) { if (item.RemainUseTimes <= 0) { @@ -1656,7 +1676,7 @@ namespace Oshima.Core.Controllers } } - if (FunGameService.UseItem(item, user, [.. targets], out string msg)) + if (FunGameService.UseItem(item, user, targets, out string msg)) { user.LastTime = DateTime.Now; pc.Add("user", user); @@ -1712,7 +1732,6 @@ namespace Oshima.Core.Controllers { items = items.TakeLast(useCount); List msgs = []; - int successCount = 0; List targets = []; Character? character = null; @@ -1729,33 +1748,14 @@ namespace Oshima.Core.Controllers } } - foreach (Item item in items) - { - if (itemCanUsed.Contains(item.ItemType)) - { - if (item.RemainUseTimes <= 0) - { - msgs.Add("此物品剩余使用次数为0,无法使用!"); - } - - if (FunGameService.UseItem(item, user, [.. targets], out string msg)) - { - successCount++; - } - msgs.Add(msg); - } - else - { - msgs.Add($"这个物品无法使用!"); - } - } - if (successCount > 0) + // 一个失败全部失败 + if (FunGameService.UseItems(items, user, targets, msgs)) { user.LastTime = DateTime.Now; pc.Add("user", user); pc.SaveConfig(); } - return NetworkUtility.JsonSerialize($"使用完毕!使用 {useCount} 件物品,成功 {successCount} 件!\r\n" + string.Join("\r\n", msgs.Count > 30 ? msgs.Take(30) : msgs)); + return NetworkUtility.JsonSerialize($"成功使用 {useCount} 件物品!\r\n" + string.Join("\r\n", msgs.Count > 30 ? msgs.Take(30) : msgs)); } else { @@ -3507,6 +3507,72 @@ namespace Oshima.Core.Controllers } } + [HttpPost("settlequest")] + public string SettleQuest([FromQuery] long? qq = null) + { + long userid = qq ?? Convert.ToInt64("10" + Verification.CreateVerifyCode(VerifyCodeType.NumberVerifyCode, 11)); + + PluginConfig pc = new("saved", userid.ToString()); + pc.LoadConfig(); + + if (pc.Count > 0) + { + User user = FunGameService.GetUser(pc); + + EntityModuleConfig quests = new("quests", userid.ToString()); + quests.LoadConfig(); + if (quests.Count > 0 && FunGameService.SettleQuest(user, quests)) + { + quests.SaveConfig(); + user.LastTime = DateTime.Now; + pc.Add("user", user); + pc.SaveConfig(); + } + + return NetworkUtility.JsonSerialize("任务结算已完成,请查看你的任务列表!"); + } + else + { + return NetworkUtility.JsonSerialize(noSaved); + } + } + + [HttpPost("showmaincharacterorsquadstatus")] + public string ShowMainCharacterOrSquadStatus([FromQuery] long? qq = null, [FromQuery] bool? squad = null) + { + long userid = qq ?? Convert.ToInt64("10" + Verification.CreateVerifyCode(VerifyCodeType.NumberVerifyCode, 11)); + bool showSquad = squad ?? false; + + PluginConfig pc = new("saved", userid.ToString()); + pc.LoadConfig(); + + if (pc.Count > 0) + { + User user = FunGameService.GetUser(pc); + string msg = ""; + + if (showSquad) + { + Character[] characters = [.. user.Inventory.Characters.Where(c => user.Inventory.Squad.Contains(c.Id))]; + foreach (Character character in characters) + { + if (msg != "") msg += "\r\n"; + msg += character.GetSimpleInfo(true, false, false, true); + } + } + else + { + msg = user.Inventory.MainCharacter.GetSimpleInfo(true, false, false, true); + } + + return NetworkUtility.JsonSerialize(msg); + } + else + { + return NetworkUtility.JsonSerialize(noSaved); + } + } + [HttpPost("signin")] public string SignIn([FromQuery] long? qq = null) { diff --git a/OshimaCore/OshimaWebAPI.cs b/OshimaCore/OshimaWebAPI.cs index e21143d..0b5bc67 100644 --- a/OshimaCore/OshimaWebAPI.cs +++ b/OshimaCore/OshimaWebAPI.cs @@ -63,7 +63,7 @@ namespace Oshima.Core.WebAPI Controller.WriteLine("重置物品交易冷却时间"); _ = FunGameService.AllowSellAndTrade(); }); - TaskScheduler.Shared.AddRecurringTask("刷新存档缓存", TimeSpan.FromSeconds(20), () => + TaskScheduler.Shared.AddRecurringTask("刷新存档缓存", TimeSpan.FromMinutes(1), () => { string directoryPath = $@"{AppDomain.CurrentDomain.BaseDirectory}configs/saved"; if (Directory.Exists(directoryPath)) @@ -77,7 +77,18 @@ namespace Oshima.Core.WebAPI if (pc.Count > 0) { User user = FunGameService.GetUser(pc); + // 将用户名存入缓存 FunGameService.UserIdAndUsername[user.Id] = user.Username; + // 任务结算 + EntityModuleConfig quests = new("quests", user.Id.ToString()); + quests.LoadConfig(); + if (quests.Count > 0 && FunGameService.SettleQuest(user, quests)) + { + quests.SaveConfig(); + user.LastTime = DateTime.Now; + pc.Add("user", user); + pc.SaveConfig(); + } } } Controller.WriteLine("读取 FunGame 存档缓存"); diff --git a/OshimaCore/Utils/FunGameService.cs b/OshimaCore/Utils/FunGameService.cs index d9fae3c..16b74af 100644 --- a/OshimaCore/Utils/FunGameService.cs +++ b/OshimaCore/Utils/FunGameService.cs @@ -2,6 +2,7 @@ using System.Text; using Milimoe.FunGame.Core.Api.Utility; using Milimoe.FunGame.Core.Entity; using Milimoe.FunGame.Core.Library.Constant; +using Oshima.Core.Controllers; using Oshima.FunGame.OshimaModules; using Oshima.FunGame.OshimaModules.Characters; using Oshima.FunGame.OshimaModules.Effects.OpenEffects; @@ -553,17 +554,6 @@ namespace Oshima.Core.Utils } } - // 任务结算 - EntityModuleConfig quests = new("quests", user.Id.ToString()); - quests.LoadConfig(); - if (quests.Count > 0 && SettleQuest(user, quests)) - { - quests.SaveConfig(); - user.LastTime = DateTime.Now; - pc.Add("user", user); - pc.SaveConfig(); - } - return user; } @@ -1005,12 +995,12 @@ namespace Oshima.Core.Utils } } - public static bool UseItem(Item item, User user, Character[] targets, out string msg) + public static bool UseItem(Item item, User user, IEnumerable targets, out string msg) { msg = ""; Dictionary args = new() { - { "targets", targets } + { "targets", targets.ToArray() } }; bool result = item.UseItem(args); string key = args.Keys.FirstOrDefault(s => s.Equals("msg", StringComparison.CurrentCultureIgnoreCase)) ?? ""; @@ -1018,8 +1008,78 @@ namespace Oshima.Core.Utils { msg = str; } + if (msg.Trim() == "" && !result) + { + result = UseItemCustom(item, user, targets, out msg); + } return result; } + + public static bool UseItems(IEnumerable items, User user, IEnumerable targets, List msgs) + { + Dictionary args = new() + { + { "targets", targets.ToArray() }, + { "useCount", items.Count() } + }; + bool result = true; + foreach (Item item in items) + { + if (!result) + { + break; + } + if (FunGameController.ItemCanUsed.Contains(item.ItemType)) + { + if (item.RemainUseTimes <= 0) + { + msgs.Add($"{item.Name} 的剩余使用次数为 0,无法使用!"); + result = false; + } + bool tempResult = item.UseItem(args); + string tempStr = ""; + string key = args.Keys.FirstOrDefault(s => s.Equals("msg", StringComparison.CurrentCultureIgnoreCase)) ?? ""; + if (key != "" && args.TryGetValue(key, out object? value) && value is string str) + { + if (str != "") msgs.Add(str); + tempStr = str; + } + if (tempStr.Trim() == "" && !tempResult) + { + // 使用自定义使用方法 + tempResult = UseItemCustom(item, user, targets, out tempStr); + } + if (!tempResult) + { + result = false; + } + msgs.Add(tempStr); + // 这个参数会覆盖掉原消息 + key = args.Keys.FirstOrDefault(s => s.Equals("truemsg", StringComparison.CurrentCultureIgnoreCase)) ?? ""; + if (key != "" && args.TryGetValue(key, out value) && value is string truemsg) + { + msgs.Clear(); + msgs.Add(truemsg); + } + } + else + { + msgs.Add($"这个物品无法使用!"); + } + } + return result; + } + + public static bool UseItemCustom(Item item, User user, IEnumerable targets, out string msg) + { + msg = ""; + switch (item.Name) + { + default: + break; + } + return false; + } public static string GetLevelBreakNeedy(int levelBreak) { diff --git a/OshimaModules/Items/Consumable/经验书.cs b/OshimaModules/Items/Consumable/经验书.cs index bd7c53a..83d9ac9 100644 --- a/OshimaModules/Items/Consumable/经验书.cs +++ b/OshimaModules/Items/Consumable/经验书.cs @@ -40,12 +40,14 @@ namespace Oshima.FunGame.OshimaModules.Items { string msg = ""; bool result = false; + Character[] targets = []; string key = args.Keys.FirstOrDefault(s => s.Equals("targets", StringComparison.CurrentCultureIgnoreCase)) ?? ""; - if (key != "" && args.TryGetValue(key, out object? value) && value is Character[] targets) + if (key != "" && args.TryGetValue(key, out object? value) && value is Character[] temp) { - if (targets.Length > 0) + if (temp.Length > 0) { - msg = UseItem(item, targets[0]); + targets = [temp[0]]; + msg = UseItem(item, temp[0]); result = true; } else @@ -53,7 +55,17 @@ namespace Oshima.FunGame.OshimaModules.Items msg = $"使用物品失败,没有作用目标!"; } } - args.Add("msg", msg); + args["msg"] = msg; + key = args.Keys.FirstOrDefault(s => s.Equals("useCount", StringComparison.CurrentCultureIgnoreCase)) ?? ""; + if (key != "" && args.TryGetValue(key, out value) && value is int count && targets.Length > 0) + { + string truemsg = $"对角色 [ {targets[0]} ] 使用 {count} 个 [ {item.Name} ] 成功!"; + if (item is EXPBook expBook) + { + truemsg += $"获得了 {expBook.EXP * count} 点经验值!"; + } + args["truemsg"] = truemsg; + } return result; } }