using FunGame.Server.Contracts.Gaming; using FunGame.Server.Contracts.Networking; using FunGame.Server.Contracts.Payloads; using FunGame.Server.Contracts.Plugins; using Microsoft.AspNetCore.Http; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; namespace FunGame.Server.Plugins.Example; /// /// 示例服务器插件(合并 v2 的 WebAPIPlugin 与 GameModuleServer) /// - HTTP 端点:GET /plugin/example/hello /// - WS 消息处理器:example.echo /// - 对局模式:HandleGamingMessage(example.cheer) /// public class ExamplePlugin : IServerPlugin { private ILogger? _logger; public string Name => "example"; public string Version => "1.2.0"; public string Author => "FunGame.Server"; public string Description => "示例服务器插件:演示 HTTP 端点、WS 消息处理器、对局模式与热重载"; public void OnLoad(IServiceProvider services) { _logger = services.GetService()?.CreateLogger("Plugin.example"); _logger?.LogInformation("ExamplePlugin v{Version} 已加载", Version); } public void OnUnload() { _logger?.LogInformation("ExamplePlugin 即将卸载(端点与消息处理器随后注销)"); } public void ConfigureEndpoints(IPluginHttpHandlerRegistry http, IMessageHandlerRegistry messages) { // GET /plugin/example/hello http.Register("GET", "hello", this, async context => { await context.Response.WriteAsJsonAsync(ApiResponse.Success(new { message = "来自示例插件的问候(热重载后 v1.2.0)", plugin = Name, version = Version, time = DateTime.Now })); }); // GET /plugin/example/info http.Register("GET", "info", this, async context => { await context.Response.WriteAsJsonAsync(ApiResponse.Success(new { name = Name, version = Version, author = Author, description = Description })); }); // WS:example.echo —— 原样回显负载 messages.Register("example.echo", this, async (client, message) => { return MessageEnvelope.Reply(message, message.Data); }); } public Task?> HandleGamingMessage(IGameSession session, string type, Dictionary data) { if (type == "example.cheer") { return Task.FromResult?>(new Dictionary { ["message"] = $"插件 {Name} 收到对局消息(对局 {session.Id},回合 {session.Queue.TotalRound})" }); } return Task.FromResult?>(null); } }