WutheringWaves/GameServer/Controllers/InventoryController.cs

137 lines
4.8 KiB
C#
Raw Normal View History

using Core.Config;
using GameServer.Controllers.Attributes;
2024-02-21 21:14:56 +00:00
using GameServer.Extensions.Logic;
using GameServer.Models;
2024-02-09 22:15:05 +00:00
using GameServer.Network;
using GameServer.Systems.Entity;
using GameServer.Systems.Entity.Component;
using GameServer.Systems.Event;
2024-02-09 22:15:05 +00:00
using Protocol;
namespace GameServer.Controllers;
internal class InventoryController : Controller
{
public InventoryController(PlayerSession session) : base(session)
{
// InventoryController.
2024-02-09 22:15:05 +00:00
}
[NetEvent(MessageId.NormalItemRequest)]
public RpcResult OnNormalItemRequest() => Response(MessageId.NormalItemResponse, new NormalItemResponse());
2024-02-09 22:15:05 +00:00
[NetEvent(MessageId.WeaponItemRequest)]
public RpcResult OnWeaponItemRequest(ModelManager modelManager) => Response(MessageId.WeaponItemResponse, new WeaponItemResponse
{
WeaponItemList =
{
modelManager.Inventory.WeaponList
}
});
2024-02-09 22:15:05 +00:00
[NetEvent(MessageId.PhantomItemRequest)]
public RpcResult OnPhantomItemRequest() => Response(MessageId.PhantomItemResponse, new PhantomItemResponse());
2024-02-09 22:15:05 +00:00
[NetEvent(MessageId.ItemExchangeInfoRequest)]
public RpcResult OnItemExchangeInfoRequest() => Response(MessageId.ItemExchangeInfoResponse, new ItemExchangeInfoResponse());
[NetEvent(MessageId.EquipTakeOnRequest)]
2024-02-21 21:14:56 +00:00
public async Task<RpcResult> OnEquipTakeOnRequest(EquipTakeOnRequest request, ModelManager modelManager, CreatureController creatureController, ConfigManager configManager)
{
WeaponItem? weapon = modelManager.Inventory.GetWeaponById(request.Data.EquipIncId);
if (weapon == null) return Response(MessageId.EquipTakeOnResponse, new EquipTakeOnResponse
{
ErrorCode = (int)ErrorCode.ErrItemIdInvaild
});
2024-02-21 21:14:56 +00:00
WeaponConfig weaponConf = configManager.GetConfig<WeaponConfig>(weapon.Id)!;
roleInfo? role = modelManager.Roles.GetRoleById(request.Data.RoleId);
if (role == null) return Response(MessageId.EquipTakeOnResponse, new EquipTakeOnResponse
{
ErrorCode = (int)ErrorCode.NotValidRole
});
// Take off previous weapon
WeaponItem? prevWeapon = modelManager.Inventory.WeaponList.SingleOrDefault(weapon => weapon.RoleId == role.RoleId);
if (prevWeapon != null) prevWeapon.RoleId = 0;
// Set new weapon
weapon.RoleId = role.RoleId;
role.ApplyWeaponProperties(weaponConf);
// Update role prop data on client
await Session.Push(MessageId.PbRolePropsNotify, new PbRolePropsNotify
{
RoleId = role.RoleId,
BaseProp = { role.BaseProp },
AddProp = { role.AddProp }
});
PlayerEntity? entity = creatureController.GetPlayerEntityByRoleId(request.Data.RoleId);
2024-02-21 21:14:56 +00:00
if (entity != null)
{
2024-02-21 21:14:56 +00:00
// Update entity equipment
EntityEquipComponent equipComponent = entity.ComponentSystem.Get<EntityEquipComponent>();
equipComponent.WeaponId = weapon.Id;
await Session.Push(MessageId.EntityEquipChangeNotify, new EntityEquipChangeNotify
{
EntityId = entity.Id,
EquipComponent = equipComponent.Pb.EquipComponent
});
2024-02-21 21:14:56 +00:00
// Update entity gameplay attributes
EntityAttributeComponent attrComponent = entity.ComponentSystem.Get<EntityAttributeComponent>();
attrComponent.SetAll(role.GetAttributeList());
await Session.Push(MessageId.AttributeChangedNotify, new AttributeChangedNotify
{
Id = entity.Id,
Attributes = { attrComponent.Pb.AttributeComponent.GameAttributes }
});
}
2024-02-21 22:00:48 +00:00
// Response
EquipTakeOnResponse response = new()
{
DataList =
{
new RoleLoadEquipData
{
RoleId = request.Data.RoleId,
Pos = request.Data.Pos,
EquipIncId = request.Data.EquipIncId
}
}
2024-02-21 22:00:48 +00:00
};
if (prevWeapon != null)
{
response.DataList.Add(new RoleLoadEquipData
{
EquipIncId = prevWeapon.IncrId
});
}
return Response(MessageId.EquipTakeOnResponse, response);
}
[GameEvent(GameEventType.EnterGame)]
public async Task OnEnterGame()
{
await Session.Push(MessageId.ItemPkgOpenNotify, new ItemPkgOpenNotify
{
OpenPkg = { 0, 2, 1, 3, 4, 5, 6, 7 }
});
}
[GameEvent(GameEventType.DebugUnlockAllWeapons)]
public void DebugUnlockAllWeapons(ConfigManager configManager, ModelManager modelManager)
{
foreach (WeaponConfig weaponConf in configManager.Enumerate<WeaponConfig>())
{
modelManager.Inventory.AddNewWeapon(weaponConf.ItemId);
}
}
2024-02-09 22:15:05 +00:00
}