yuanshen/Yuanshen.Gameserver/Controllers/CombatController.cs
wisedev fc2ac1050f First working version of the server
KCP gameserver with packet dispatch, scene streaming, combat, gacha and the
shop, plus a small HTTP server for the login flow and the wish history page.

Game data under assets is pulled out of the client instead of being typed by
hand. The excel tables come from the packed bin container and the spawn points
from the compiled scene scripts.
2026-09-04 02:10:50 +03:00

99 lines
4.3 KiB
C#

using Yuanshen.Gameserver.Controllers.Attributes;
using Yuanshen.Gameserver.Controllers.Result;
using Yuanshen.Gameserver.Game;
using Yuanshen.Gameserver.Network.Session;
using Yuanshen.Protocol;
using Google.Protobuf;
using Microsoft.Extensions.Logging;
namespace Yuanshen.Gameserver.Controllers;
[NetController]
internal sealed class CombatController : ControllerBase
{
[NetCommand(CmdType.UnionCmdNotify)]
public async ValueTask<IResult> OnUnionCmdNotify(GameState state, Player player, NetSession session, CombatManager combat, WorldSpawner worldSpawner, ILogger<CombatController> logger)
{
UnionCmdNotify notify = Packet!.DecodeBody<UnionCmdNotify>();
foreach (UnionCmd cmd in notify.CmdList)
{
if (cmd.MessageId == (uint)CmdType.CombatInvocationsNotify)
await ProcessCombatInvocationsAsync(CombatInvocationsNotify.Parser.ParseFrom(cmd.Body), state, player, combat, worldSpawner, logger, session);
else if (cmd.MessageId == (uint)CmdType.AbilityInvocationsNotify)
await AbilityController.ForwardAsync(session, AbilityInvocationsNotify.Parser.ParseFrom(cmd.Body).Invokes);
}
return Ok();
}
[NetCommand(CmdType.CombatInvocationsNotify)]
public async ValueTask<IResult> OnCombatInvocationsNotify(GameState state, Player player, CombatManager combat, WorldSpawner worldSpawner, ILogger<CombatController> logger, NetSession session)
{
await ProcessCombatInvocationsAsync(Packet!.DecodeBody<CombatInvocationsNotify>(), state, player, combat, worldSpawner, logger, session);
return Ok();
}
[NetCommand(CmdType.EvtBeingHitNotify)]
public async ValueTask<IResult> OnEvtBeingHitNotify(CombatManager combat)
{
EvtBeingHitNotify notify = Packet!.DecodeBody<EvtBeingHitNotify>();
if (notify.BeingHitInfo?.AttackResult is not null)
await combat.HandleAttackAsync(notify.BeingHitInfo.AttackResult);
return Ok();
}
[NetCommand(CmdType.EvtBeingHitsCombineNotify)]
public async ValueTask<IResult> OnEvtBeingHitsCombineNotify(CombatManager combat)
{
EvtBeingHitsCombineNotify notify = Packet!.DecodeBody<EvtBeingHitsCombineNotify>();
foreach (EvtBeingHitInfo hit in notify.EvtBeingHitInfoList)
{
if (hit.AttackResult is not null)
await combat.HandleAttackAsync(hit.AttackResult);
}
return Ok();
}
private static async ValueTask ProcessCombatInvocationsAsync(CombatInvocationsNotify notify, GameState state, Player player, CombatManager combat, WorldSpawner worldSpawner, ILogger logger, NetSession session)
{
uint activeEntityId = player.ActiveAvatar?.EntityId ?? 0;
CombatInvocationsNotify forward = new();
foreach (CombatInvokeEntry entry in notify.InvokeList)
{
switch (entry.ArgumentType)
{
case CombatTypeArgument.EntityMove:
EntityMoveInfo move = EntityMoveInfo.Parser.ParseFrom(entry.CombatData);
if (move.EntityId == activeEntityId && move.MotionInfo?.Pos is not null)
{
state.UpdatePosition(move.MotionInfo.Pos);
await worldSpawner.StreamAsync(move.MotionInfo.Pos);
}
break;
case CombatTypeArgument.CombatEvtBeingHit:
EvtBeingHitInfo hit = EvtBeingHitInfo.Parser.ParseFrom(entry.CombatData);
logger.LogInformation("HIT defense={d} damage={dmg} forward={f}",
hit.AttackResult?.DefenseId, hit.AttackResult?.Damage, entry.ForwardType);
if (hit.AttackResult is not null)
await combat.HandleAttackAsync(hit.AttackResult);
break;
default:
logger.LogDebug("combat arg={arg} fwd={f}", entry.ArgumentType, entry.ForwardType);
break;
}
if (entry.ForwardType is ForwardType.ForwardToAll or ForwardType.ForwardToHost or ForwardType.ForwardToPeer)
forward.InvokeList.Add(entry);
}
if (forward.InvokeList.Count != 0)
await session.NotifyAsync(CmdType.CombatInvocationsNotify, forward);
}
}