continuity/Server/PacketHandlers/CharacterCreationHandler..cs

89 lines
3.4 KiB
C#
Raw Normal View History

using Microsoft.EntityFrameworkCore;
using NetCoreServer;
using Server.DB;
using Server.DB.Documents;
using Server.Services;
using Wonderking.Game.Data.Character;
using Wonderking.Packets.Incoming;
using Wonderking.Packets.Outgoing;
using Wonderking.Packets.Outgoing.Data;
namespace Server.PacketHandlers;
public class CharacterCreationHandler : IPacketHandler<CharacterCreationPacket>
{
private readonly WonderkingContext _wonderkingContext;
private readonly ItemObjectPoolService _itemObjectPoolService;
public CharacterCreationHandler(WonderkingContext wonderkingContext, ItemObjectPoolService itemObjectPoolService)
{
_wonderkingContext = wonderkingContext;
_itemObjectPoolService = itemObjectPoolService;
}
2023-11-16 09:30:01 +00:00
public async Task HandleAsync(CharacterCreationPacket packet, TcpSession session)
{
var authSession = session as AuthSession;
var account =
_wonderkingContext.Accounts.FirstOrDefault(a => authSession != null && a.Id == authSession.AccountId);
var items = new List<InventoryItem>
{
_itemObjectPoolService.GetBaseInventoryItem(25),
_itemObjectPoolService.GetBaseInventoryItem(764),
_itemObjectPoolService.GetBaseInventoryItem(766),
_itemObjectPoolService.GetBaseInventoryItem(763),
_itemObjectPoolService.GetBaseInventoryItem(767)
};
account?.Characters.Add(new Character
{
Account = account,
MapId = 300,
Name = packet.Name,
LastXCoordinate = 113,
LastYCoordinate = 0,
PvPLevel = PvPLevel.None,
Gender = Gender.None,
Experience = 0,
Level = 1,
InventoryItems = items,
BaseStats = new BaseStats
{
Strength = 5,
Dexterity = 5,
Intelligence = 5,
Vitality = 5,
Luck = 5,
Wisdom = 5
},
JobData = new JobData { FirstJob = packet.FirstJob, SecondJob = 0, ThirdJob = 0, FourthJob = 0 },
Health = 250,
Mana = 250,
});
2023-11-16 09:30:01 +00:00
await _wonderkingContext.SaveChangesAsync().ConfigureAwait(false);
2023-11-16 09:30:01 +00:00
var character = await _wonderkingContext.Characters.AsNoTracking()
.Where(c => authSession != null && c.Account.Id == authSession.AccountId && c.Name == packet.Name)
.Select(c =>
new CharacterData
{
Name = c.Name,
Job = c.JobData,
Gender = c.Gender,
Level = c.Level,
Experience = 0,
Stats = c.BaseStats,
Health = c.Health,
Mana = c.Mana,
EquippedItems =
c.InventoryItems.Where(item => item.InventoryTab == InventoryTab.WornEquipment)
.Select(item => item.ItemId)
.ToArray(),
EquippedCashItems = c.InventoryItems
.Where(item => item.InventoryTab == InventoryTab.WornCashEquipment)
.Select(item => item.ItemId)
.ToArray(),
2023-11-16 09:30:01 +00:00
}).FirstOrDefaultAsync().ConfigureAwait(true);
authSession?.Send(new CharacterCreationResponsePacket { Character = character });
}
}