Snowflake/RPG.Services.Gameserver/Network/Command/GameserverCommandHandler.cs

69 lines
2.5 KiB
C#

using Microsoft.Extensions.Logging;
using RPG.Network.Proto;
using RPG.Services.Core.Network;
using RPG.Services.Core.Network.Attributes;
using RPG.Services.Core.Network.Command;
using RPG.Services.Core.Session;
using RPG.Services.Gameserver.Session;
namespace RPG.Services.Gameserver.Network.Command;
internal class GameserverCommandHandler : ServiceCommandHandler
{
private readonly SessionManager _sessionManager;
public GameserverCommandHandler(ILogger<ServiceCommandHandler> logger, ServiceBox services, SessionManager sessionManager) : base(logger, services)
{
_sessionManager = sessionManager;
}
[ServiceCommand(ServiceCommandType.BindContainer)]
public Task OnCmdBindContainer(ServiceCommand command)
{
CmdBindContainer cmdBindContainer = CmdBindContainer.Parser.ParseFrom(command.Body.Span);
PlayerSession? session = _sessionManager.Create<PlayerSession>(cmdBindContainer.SessionId);
if (session == null)
{
Send(ServiceCommandType.BindContainerResult, new CmdBindContainerResult
{
Retcode = 1,
SessionId = cmdBindContainer.SessionId,
ServiceType = RPGServiceType.Gameserver
}, command.SenderType);
return Task.CompletedTask;
}
session.PlayerUid = cmdBindContainer.PlayerUid;
Send(ServiceCommandType.BindContainerResult, new CmdBindContainerResult
{
Retcode = 0,
SessionId = cmdBindContainer.SessionId,
ServiceType = RPGServiceType.Gameserver
}, command.SenderType);
return Task.CompletedTask;
}
[ServiceCommand(ServiceCommandType.UnbindContainer)]
public Task OnCmdUnbindContainer(ServiceCommand command)
{
CmdUnbindContainer cmdUnbindContainer = CmdUnbindContainer.Parser.ParseFrom(command.Body.Span);
if (_sessionManager.TryGet(cmdUnbindContainer.SessionId, out PlayerSession? session))
{
_sessionManager.Remove(session);
}
return Task.CompletedTask;
}
[ServiceCommand(ServiceCommandType.ForwardGameMessage)]
public async Task OnCmdForwardGameMessage(ServiceCommand command)
{
CmdForwardGameMessage cmd = CmdForwardGameMessage.Parser.ParseFrom(command.Body.Span);
if (_sessionManager.TryGet(cmd.SessionId, out PlayerSession? session))
{
await session.HandleGameCommand((ushort)cmd.CmdType, cmd.Payload.Memory);
}
}
}