mirror of
https://github.com/oshima-studios/OshimaGameModule.git
synced 2025-04-23 04:19:36 +08:00
将服务类转移到 Servers 项目,方便兼容 WebSocket 模式;添加匿名服务器模组
This commit is contained in:
parent
1b8d547a0c
commit
9235c6c5f1
@ -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<FunGameController> logger) : ControllerBase
|
||||
{
|
||||
public static ItemType[] ItemCanUsed => [ItemType.Consumable, ItemType.MagicCard, ItemType.SpecialItem, ItemType.GiftBox, ItemType.Others];
|
||||
|
||||
private readonly ILogger<FunGameController> _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)
|
||||
{
|
||||
|
@ -25,6 +25,7 @@
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\OshimaModules\OshimaModules.csproj" />
|
||||
<ProjectReference Include="..\OshimaServers\OshimaServers.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
|
@ -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), () =>
|
||||
|
24
OshimaMaps/AnonymousMap.cs
Normal file
24
OshimaMaps/AnonymousMap.cs
Normal file
@ -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;
|
||||
}
|
||||
}
|
@ -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];
|
||||
|
122
OshimaServers/AnonymousServer.cs
Normal file
122
OshimaServers/AnonymousServer.cs
Normal file
@ -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<AnonymousServer> Instances { get; } = [];
|
||||
|
||||
/// <summary>
|
||||
/// 向客户端推送事件
|
||||
/// </summary>
|
||||
/// <param name="msg"></param>
|
||||
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;
|
||||
|
||||
/// <summary>
|
||||
/// 启动匿名服务器
|
||||
/// </summary>
|
||||
/// <param name="model"></param>
|
||||
/// <returns></returns>
|
||||
public override bool StartAnonymousServer(IServerModel model)
|
||||
{
|
||||
// 添加当前单例
|
||||
Instances.Add(this);
|
||||
Controller.WriteLine($"{model.GetClientName()} 连接至匿名服务器", LogLevel.Info);
|
||||
// 接收连接匿名服务器的客户端
|
||||
_clientModel = model;
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 关闭匿名服务器
|
||||
/// </summary>
|
||||
/// <param name="model"></param>
|
||||
public override void CloseAnonymousServer(IServerModel model)
|
||||
{
|
||||
// 移除当前单例
|
||||
Instances.Remove(this);
|
||||
Controller.WriteLine($"{model.GetClientName()} 从匿名服务器断开", LogLevel.Info);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 向客户端推送事件
|
||||
/// </summary>
|
||||
/// <param name="msg"></param>
|
||||
public async Task PushMessage(long qq, string msg)
|
||||
{
|
||||
if (_clientModel != null)
|
||||
{
|
||||
Dictionary<string, object> data = [];
|
||||
data.Add(nameof(qq), qq);
|
||||
data.Add(nameof(msg), msg);
|
||||
Controller.WriteLine("向客户端推送事件", LogLevel.Debug);
|
||||
await SendAnonymousGameServerMessage([_clientModel], data);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 接收并处理匿名服务器消息
|
||||
/// </summary>
|
||||
/// <param name="data"></param>
|
||||
/// <returns></returns>
|
||||
public override async Task<Dictionary<string, object>> AnonymousGameServerHandler(Dictionary<string, object> data)
|
||||
{
|
||||
Dictionary<string, object> result = [];
|
||||
Controller.WriteLine("接收匿名服务器消息", LogLevel.Debug);
|
||||
|
||||
// 根据服务器和客户端的数据传输约定,自行处理 data,并返回。
|
||||
if (data.Count > 0)
|
||||
{
|
||||
await Task.Delay(1);
|
||||
}
|
||||
result.Add("msg", "匿名服务器已经收到消息了");
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
public override Task<Dictionary<string, object>> GamingMessageHandler(string username, GamingType type, Dictionary<string, object> data)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public override bool StartServer(string GameModule, Room Room, List<User> Users, IServerModel RoomMasterServerModel, Dictionary<string, IServerModel> ServerModels, params object[] Args)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
}
|
||||
}
|
@ -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
|
||||
{
|
@ -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<Skill> AllSkills { get; } = [];
|
||||
public static Dictionary<long, User> UserIdAndUsername { get; } = [];
|
||||
public static Dictionary<int, Character> 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;
|
@ -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
|
||||
{
|
Loading…
x
Reference in New Issue
Block a user