From 9235c6c5f1c280ca5cb252487758fd5a819ab906 Mon Sep 17 00:00:00 2001 From: milimoe Date: Tue, 14 Jan 2025 00:28:41 +0800 Subject: [PATCH] =?UTF-8?q?=E5=B0=86=E6=9C=8D=E5=8A=A1=E7=B1=BB=E8=BD=AC?= =?UTF-8?q?=E7=A7=BB=E5=88=B0=20Servers=20=E9=A1=B9=E7=9B=AE=EF=BC=8C?= =?UTF-8?q?=E6=96=B9=E4=BE=BF=E5=85=BC=E5=AE=B9=20WebSocket=20=E6=A8=A1?= =?UTF-8?q?=E5=BC=8F=EF=BC=9B=E6=B7=BB=E5=8A=A0=E5=8C=BF=E5=90=8D=E6=9C=8D?= =?UTF-8?q?=E5=8A=A1=E5=99=A8=E6=A8=A1=E7=BB=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- OshimaCore/Controllers/FunGameController.cs | 11 +- OshimaCore/OshimaCore.csproj | 1 + OshimaCore/OshimaWebAPI.cs | 5 +- OshimaMaps/AnonymousMap.cs | 24 ++++ OshimaModules/Modules/Constant.cs | 2 + OshimaServers/AnonymousServer.cs | 122 ++++++++++++++++++ .../Service}/FunGameActionQueue.cs | 2 +- .../Service}/FunGameService.cs | 8 +- .../Service}/FunGameSimulation.cs | 2 +- 9 files changed, 163 insertions(+), 14 deletions(-) create mode 100644 OshimaMaps/AnonymousMap.cs create mode 100644 OshimaServers/AnonymousServer.cs rename {OshimaCore/Utils => OshimaServers/Service}/FunGameActionQueue.cs (99%) rename {OshimaCore/Utils => OshimaServers/Service}/FunGameService.cs (99%) rename {OshimaCore/Utils => OshimaServers/Service}/FunGameSimulation.cs (99%) diff --git a/OshimaCore/Controllers/FunGameController.cs b/OshimaCore/Controllers/FunGameController.cs index bdffeec..f5d6660 100644 --- a/OshimaCore/Controllers/FunGameController.cs +++ b/OshimaCore/Controllers/FunGameController.cs @@ -1,4 +1,3 @@ -using System.Diagnostics.Metrics; using System.Text; using System.Text.RegularExpressions; using Microsoft.AspNetCore.Authorization; @@ -9,9 +8,9 @@ using Milimoe.FunGame.Core.Entity; using Milimoe.FunGame.Core.Library.Constant; using Oshima.Core.Configs; using Oshima.Core.Models; -using Oshima.Core.Utils; using Oshima.FunGame.OshimaModules.Characters; using Oshima.FunGame.OshimaModules.Items; +using Oshima.FunGame.OshimaServers.Service; namespace Oshima.Core.Controllers { @@ -20,8 +19,6 @@ 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; @@ -881,7 +878,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)}," : "") + + (FunGameService.ItemCanUsed.Contains(first.ItemType) ? $"可使用数量:{objs.Count(i => i.RemainUseTimes > 0)}," : "") + $"可出售数量:{objs.Count(i => i.IsSellable)},可交易数量:{objs.Count(i => i.IsTradable)})"; list.Add(str); } @@ -966,7 +963,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)}," : "") + + (FunGameService.ItemCanUsed.Contains(first.ItemType) ? $"可使用数量:{objs.Count(i => i.RemainUseTimes > 0)}," : "") + $"可出售数量:{objs.Count(i => i.IsSellable)},可交易数量:{objs.Count(i => i.IsTradable)})"; list.Add(str); } @@ -1780,7 +1777,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 (FunGameService.ItemCanUsed.Contains(item.ItemType)) { if (item.RemainUseTimes <= 0) { diff --git a/OshimaCore/OshimaCore.csproj b/OshimaCore/OshimaCore.csproj index 65de335..4ac21a5 100644 --- a/OshimaCore/OshimaCore.csproj +++ b/OshimaCore/OshimaCore.csproj @@ -25,6 +25,7 @@ + diff --git a/OshimaCore/OshimaWebAPI.cs b/OshimaCore/OshimaWebAPI.cs index c32565d..d428de3 100644 --- a/OshimaCore/OshimaWebAPI.cs +++ b/OshimaCore/OshimaWebAPI.cs @@ -1,10 +1,11 @@ using Milimoe.FunGame.Core.Api.Utility; using Milimoe.FunGame.Core.Entity; using Milimoe.FunGame.Core.Library.Common.Addon; +using Milimoe.FunGame.Core.Library.Constant; using Oshima.Core.Configs; using Oshima.Core.Constant; -using Oshima.Core.Utils; using Oshima.FunGame.OshimaModules; +using Oshima.FunGame.OshimaServers.Service; using TaskScheduler = Milimoe.FunGame.Core.Api.Utility.TaskScheduler; namespace Oshima.Core.WebAPI @@ -91,7 +92,7 @@ namespace Oshima.Core.WebAPI } } } - Controller.WriteLine("读取 FunGame 存档缓存"); + Controller.WriteLine("读取 FunGame 存档缓存", LogLevel.Debug); } }, true); TaskScheduler.Shared.AddTask("刷新每日任务", new TimeSpan(4, 0, 0), () => diff --git a/OshimaMaps/AnonymousMap.cs b/OshimaMaps/AnonymousMap.cs new file mode 100644 index 0000000..d8a7029 --- /dev/null +++ b/OshimaMaps/AnonymousMap.cs @@ -0,0 +1,24 @@ +using Milimoe.FunGame.Core.Library.Common.Addon; +using Oshima.FunGame.OshimaModules; + +namespace Oshima.FunGame.OshimaMaps +{ + public class AnonymousMap : GameMap + { + public override string Name => OshimaGameModuleConstant.AnonymousMap; + + public override string Description => OshimaGameModuleConstant.Description; + + public override string Version => OshimaGameModuleConstant.Version; + + public override string Author => OshimaGameModuleConstant.Author; + + public override float Length => 6; + + public override float Width => 6; + + public override float Height => 3; + + public override float Size => 6; + } +} diff --git a/OshimaModules/Modules/Constant.cs b/OshimaModules/Modules/Constant.cs index b98cf32..070adaa 100644 --- a/OshimaModules/Modules/Constant.cs +++ b/OshimaModules/Modules/Constant.cs @@ -14,6 +14,8 @@ namespace Oshima.FunGame.OshimaModules public const string Version = "1.0.0"; public const string Author = "Oshima Studios"; public const string FastAutoMap = "oshima.fungame.fastauto.map"; + public const string Anonymous = "oshima.fungame.anonymous"; + public const string AnonymousMap = "oshima.fungame.anonymous.map"; private static readonly string[] Maps = [FastAutoMap]; private static readonly string[] Characters = [Character]; diff --git a/OshimaServers/AnonymousServer.cs b/OshimaServers/AnonymousServer.cs new file mode 100644 index 0000000..cfd1fe7 --- /dev/null +++ b/OshimaServers/AnonymousServer.cs @@ -0,0 +1,122 @@ +using Milimoe.FunGame.Core.Entity; +using Milimoe.FunGame.Core.Interface.Base; +using Milimoe.FunGame.Core.Library.Common.Addon; +using Milimoe.FunGame.Core.Library.Constant; +using Oshima.FunGame.OshimaModules; + +namespace Oshima.FunGame.OshimaServers +{ + public class AnonymousServer : GameModuleServer + { + public override string Name => OshimaGameModuleConstant.Anonymous; + + public override string Description => OshimaGameModuleConstant.Description; + + public override string Version => OshimaGameModuleConstant.Version; + + public override string Author => OshimaGameModuleConstant.Author; + + public override string DefaultMap => OshimaGameModuleConstant.AnonymousMap; + + public override GameModuleDepend GameModuleDepend => OshimaGameModuleConstant.GameModuleDepend; + + public override bool IsAnonymous => true; + + public static HashSet Instances { get; } = []; + + /// + /// 向客户端推送事件 + /// + /// + public static async Task PushMessageToClients(long qq, string msg) + { + AnonymousServer[] servers = [.. Instances]; + foreach (AnonymousServer anonymous in servers) + { + try + { + await anonymous.PushMessage(qq, msg); + } + catch (Exception e) + { + anonymous.Controller.Error(e); + Instances.Remove(anonymous); + } + } + } + + protected IServerModel? _clientModel; + + /// + /// 启动匿名服务器 + /// + /// + /// + public override bool StartAnonymousServer(IServerModel model) + { + // 添加当前单例 + Instances.Add(this); + Controller.WriteLine($"{model.GetClientName()} 连接至匿名服务器", LogLevel.Info); + // 接收连接匿名服务器的客户端 + _clientModel = model; + return true; + } + + /// + /// 关闭匿名服务器 + /// + /// + public override void CloseAnonymousServer(IServerModel model) + { + // 移除当前单例 + Instances.Remove(this); + Controller.WriteLine($"{model.GetClientName()} 从匿名服务器断开", LogLevel.Info); + } + + /// + /// 向客户端推送事件 + /// + /// + public async Task PushMessage(long qq, string msg) + { + if (_clientModel != null) + { + Dictionary data = []; + data.Add(nameof(qq), qq); + data.Add(nameof(msg), msg); + Controller.WriteLine("向客户端推送事件", LogLevel.Debug); + await SendAnonymousGameServerMessage([_clientModel], data); + } + } + + /// + /// 接收并处理匿名服务器消息 + /// + /// + /// + public override async Task> AnonymousGameServerHandler(Dictionary data) + { + Dictionary result = []; + Controller.WriteLine("接收匿名服务器消息", LogLevel.Debug); + + // 根据服务器和客户端的数据传输约定,自行处理 data,并返回。 + if (data.Count > 0) + { + await Task.Delay(1); + } + result.Add("msg", "匿名服务器已经收到消息了"); + + return result; + } + + public override Task> GamingMessageHandler(string username, GamingType type, Dictionary data) + { + throw new NotImplementedException(); + } + + public override bool StartServer(string GameModule, Room Room, List Users, IServerModel RoomMasterServerModel, Dictionary ServerModels, params object[] Args) + { + throw new NotImplementedException(); + } + } +} diff --git a/OshimaCore/Utils/FunGameActionQueue.cs b/OshimaServers/Service/FunGameActionQueue.cs similarity index 99% rename from OshimaCore/Utils/FunGameActionQueue.cs rename to OshimaServers/Service/FunGameActionQueue.cs index fccf4b8..a7deaf2 100644 --- a/OshimaCore/Utils/FunGameActionQueue.cs +++ b/OshimaServers/Service/FunGameActionQueue.cs @@ -4,7 +4,7 @@ using Milimoe.FunGame.Core.Entity; using Milimoe.FunGame.Core.Model; using Oshima.FunGame.OshimaModules.Effects.OpenEffects; -namespace Oshima.Core.Utils +namespace Oshima.FunGame.OshimaServers.Service { public class FunGameActionQueue { diff --git a/OshimaCore/Utils/FunGameService.cs b/OshimaServers/Service/FunGameService.cs similarity index 99% rename from OshimaCore/Utils/FunGameService.cs rename to OshimaServers/Service/FunGameService.cs index 3ce1110..de4d2d1 100644 --- a/OshimaCore/Utils/FunGameService.cs +++ b/OshimaServers/Service/FunGameService.cs @@ -2,14 +2,13 @@ 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; using Oshima.FunGame.OshimaModules.Items; using Oshima.FunGame.OshimaModules.Skills; -namespace Oshima.Core.Utils +namespace Oshima.FunGame.OshimaServers.Service { public class FunGameService { @@ -25,6 +24,7 @@ namespace Oshima.Core.Utils public static List AllSkills { get; } = []; public static Dictionary UserIdAndUsername { get; } = []; public static Dictionary Bosses { get; } = []; + public static ItemType[] ItemCanUsed => [ItemType.Consumable, ItemType.MagicCard, ItemType.SpecialItem, ItemType.GiftBox, ItemType.Others]; public static void InitFunGame() { @@ -1051,7 +1051,7 @@ namespace Oshima.Core.Utils { break; } - if (FunGameController.ItemCanUsed.Contains(item.ItemType)) + if (ItemCanUsed.Contains(item.ItemType)) { if (item.RemainUseTimes <= 0) { @@ -2018,6 +2018,8 @@ namespace Oshima.Core.Utils user.Inventory.Items.Add(newItem); } } + TaskUtility.NewTask(async () => await AnonymousServer.PushMessageToClients(user.Id, $"FunGame Web API 推送:你的任务【{quest.Name}】已结算," + + $"获得奖励:【{string.Join(",", quest.Awards.Select(kv => kv.Key + " * " + kv.Value))}】!")); result = true; } return result; diff --git a/OshimaCore/Utils/FunGameSimulation.cs b/OshimaServers/Service/FunGameSimulation.cs similarity index 99% rename from OshimaCore/Utils/FunGameSimulation.cs rename to OshimaServers/Service/FunGameSimulation.cs index 812c850..af5628e 100644 --- a/OshimaCore/Utils/FunGameSimulation.cs +++ b/OshimaServers/Service/FunGameSimulation.cs @@ -5,7 +5,7 @@ using Milimoe.FunGame.Core.Library.Constant; using Milimoe.FunGame.Core.Model; using Oshima.FunGame.OshimaModules.Effects.OpenEffects; -namespace Oshima.Core.Utils +namespace Oshima.FunGame.OshimaServers.Service { public class FunGameSimulation {