2023-11-15 19:00:08 +00:00
|
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
using NetCoreServer;
|
|
|
|
using Server.DB;
|
|
|
|
using Server.DB.Documents;
|
|
|
|
using Server.Services;
|
|
|
|
using Wonderking.Game.Data.Character;
|
2023-11-16 20:05:33 +00:00
|
|
|
using Wonderking.Game.Mapping;
|
2023-11-15 19:00:08 +00:00
|
|
|
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;
|
2023-11-16 20:01:55 +00:00
|
|
|
private readonly CharacterStatsMappingConfiguration _characterStatsMapping;
|
2023-11-15 19:00:08 +00:00
|
|
|
|
2023-11-16 20:01:55 +00:00
|
|
|
public CharacterCreationHandler(WonderkingContext wonderkingContext, ItemObjectPoolService itemObjectPoolService,
|
|
|
|
CharacterStatsMappingConfiguration characterStatsMappingConfiguration)
|
2023-11-15 19:00:08 +00:00
|
|
|
{
|
|
|
|
_wonderkingContext = wonderkingContext;
|
|
|
|
_itemObjectPoolService = itemObjectPoolService;
|
2023-11-16 20:01:55 +00:00
|
|
|
_characterStatsMapping = characterStatsMappingConfiguration;
|
2023-11-15 19:00:08 +00:00
|
|
|
}
|
|
|
|
|
2023-11-16 09:30:01 +00:00
|
|
|
public async Task HandleAsync(CharacterCreationPacket packet, TcpSession session)
|
2023-11-15 19:00:08 +00:00
|
|
|
{
|
|
|
|
var authSession = session as AuthSession;
|
|
|
|
var account =
|
|
|
|
_wonderkingContext.Accounts.FirstOrDefault(a => authSession != null && a.Id == authSession.AccountId);
|
2023-11-16 20:01:55 +00:00
|
|
|
var mappedDefaultItems = _characterStatsMapping.DefaultCharacterMapping.Items
|
|
|
|
.Select(i => _itemObjectPoolService.GetBaseInventoryItem(i.Id, i.Quantity)).ToArray();
|
|
|
|
|
|
|
|
var firstJobConfig = packet.FirstJob switch
|
2023-11-15 19:00:08 +00:00
|
|
|
{
|
2023-11-16 20:01:55 +00:00
|
|
|
1 => _characterStatsMapping.Swordsman,
|
|
|
|
2 => _characterStatsMapping.Mage,
|
|
|
|
3 => _characterStatsMapping.Thief,
|
|
|
|
4 => _characterStatsMapping.Scout,
|
|
|
|
_ => _characterStatsMapping.Swordsman
|
2023-11-15 19:00:08 +00:00
|
|
|
};
|
2023-11-16 20:01:55 +00:00
|
|
|
|
|
|
|
var mappedJobItems = firstJobConfig.Items
|
|
|
|
.Select(i => _itemObjectPoolService.GetBaseInventoryItem(i.Id, i.Quantity)).ToArray();
|
|
|
|
InventoryItem[] items =
|
|
|
|
[
|
|
|
|
.. mappedDefaultItems,
|
|
|
|
.. mappedJobItems,
|
|
|
|
_itemObjectPoolService.GetBaseInventoryItem((ushort)((packet.FirstJob - 1) * 6 +
|
|
|
|
((byte)packet.Gender - 1) * 3 +
|
|
|
|
packet.Hair + 1)),
|
|
|
|
_itemObjectPoolService.GetBaseInventoryItem((ushort)((packet.FirstJob - 1) * 6 +
|
|
|
|
((byte)packet.Gender - 1) * 3 +
|
|
|
|
packet.Eyes + 25)),
|
|
|
|
_itemObjectPoolService.GetBaseInventoryItem((ushort)(((byte)packet.Gender - 1) * 3 +
|
|
|
|
packet.Shirt + 49)),
|
|
|
|
_itemObjectPoolService.GetBaseInventoryItem((ushort)(((byte)packet.Gender - 1) * 3 +
|
|
|
|
packet.Pants + 58)),
|
|
|
|
];
|
|
|
|
|
|
|
|
var calculateCurrentMana = CalculateCurrentMana(1, firstJobConfig);
|
|
|
|
var calculateCurrentHealth = CalculateCurrentHealth(1, firstJobConfig);
|
2023-11-16 22:51:30 +00:00
|
|
|
var toBeAddedCharacter = new Character
|
2023-11-15 19:00:08 +00:00
|
|
|
{
|
|
|
|
Account = account,
|
|
|
|
MapId = 300,
|
|
|
|
Name = packet.Name,
|
|
|
|
LastXCoordinate = 113,
|
|
|
|
LastYCoordinate = 0,
|
|
|
|
PvPLevel = PvPLevel.None,
|
2023-11-16 20:01:55 +00:00
|
|
|
Gender = packet.Gender,
|
2023-11-15 19:00:08 +00:00
|
|
|
Experience = 0,
|
|
|
|
Level = 1,
|
|
|
|
InventoryItems = items,
|
2023-11-16 20:01:55 +00:00
|
|
|
BaseStats = firstJobConfig.BaseStats,
|
2023-11-15 19:00:08 +00:00
|
|
|
JobData = new JobData { FirstJob = packet.FirstJob, SecondJob = 0, ThirdJob = 0, FourthJob = 0 },
|
2023-11-16 20:01:55 +00:00
|
|
|
Health = calculateCurrentHealth,
|
|
|
|
Mana = calculateCurrentMana
|
2023-11-16 22:51:30 +00:00
|
|
|
};
|
|
|
|
account?.Characters.Add(toBeAddedCharacter);
|
2023-11-16 11:23:22 +00:00
|
|
|
await _wonderkingContext.SaveChangesAsync().ConfigureAwait(true);
|
2023-11-15 19:00:08 +00:00
|
|
|
|
2023-11-16 11:49:28 +00:00
|
|
|
var amountOfCharacters = await _wonderkingContext.Characters.AsNoTrackingWithIdentityResolution()
|
|
|
|
.CountAsync(c => authSession != null && c.Account.Id == authSession.AccountId).ConfigureAwait(true);
|
|
|
|
|
2023-11-16 11:23:22 +00:00
|
|
|
var character = await _wonderkingContext.Characters.AsNoTrackingWithIdentityResolution()
|
2023-11-15 19:00:08 +00:00
|
|
|
.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 11:23:22 +00:00
|
|
|
}).FirstAsync().ConfigureAwait(true);
|
2023-11-16 11:49:28 +00:00
|
|
|
authSession?.Send(new CharacterCreationResponsePacket
|
|
|
|
{
|
|
|
|
Character = character,
|
|
|
|
Slot = amountOfCharacters - 1,
|
|
|
|
isDuplicate = false,
|
|
|
|
});
|
2023-11-15 19:00:08 +00:00
|
|
|
}
|
2023-11-16 20:01:55 +00:00
|
|
|
|
|
|
|
private static int CalculateCurrentHealth(ushort level, JobSpecificMapping firstJobConfig)
|
|
|
|
{
|
|
|
|
return (int)((level - 1) * firstJobConfig.DynamicStats.HealthPerLevel +
|
|
|
|
firstJobConfig.BaseStats.Vitality * firstJobConfig.DynamicStats.HealthPerVitality);
|
|
|
|
}
|
|
|
|
|
|
|
|
private static int CalculateCurrentMana(ushort level, JobSpecificMapping firstJobConfig)
|
|
|
|
{
|
|
|
|
return (int)((level - 1) * firstJobConfig.DynamicStats.ManaPerLevel +
|
|
|
|
firstJobConfig.BaseStats.Wisdom * firstJobConfig.DynamicStats.ManaPerWisdom);
|
|
|
|
}
|
2023-11-15 19:00:08 +00:00
|
|
|
}
|