continuity/Server/PacketHandlers/CharacterCreationHandler..cs
Timothy Schenk 5abe7f7564
All checks were successful
Build, Package and Push Images / preprocess (push) Successful in 2s
Build, Package and Push Images / build (push) Successful in 27s
Build, Package and Push Images / sonarqube (push) Has been skipped
Build, Package and Push Images / docs (push) Successful in 37s
Build, Package and Push Images / sbom-scan (push) Successful in 55s
Build, Package and Push Images / build-docs-container (push) Successful in 1m21s
Build, Package and Push Images / container-build (push) Successful in 1m29s
Build, Package and Push Images / deploy-wiki (push) Successful in 5s
Build, Package and Push Images / container-sbom-scan (push) Successful in 51s
refactor: make HandleAsync async
2023-11-16 10:30:01 +01:00

88 lines
3.4 KiB
C#

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;
}
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,
});
await _wonderkingContext.SaveChangesAsync().ConfigureAwait(false);
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(),
}).FirstOrDefaultAsync().ConfigureAwait(true);
authSession?.Send(new CharacterCreationResponsePacket { Character = character });
}
}