continuity/Continuity.AuthServer/PacketHandlers/CharacterCreationHandler.cs

177 lines
6.9 KiB
C#
Raw Permalink Normal View History

2023-11-21 20:37:50 +00:00
// Copyright (c) 2023 Timothy Schenk. Subject to the GNU AGPL Version 3 License.
2023-11-20 18:56:14 +00:00
using Continuity.AuthServer.DB;
using Continuity.AuthServer.DB.Documents;
using Continuity.AuthServer.Services;
2023-11-25 11:20:55 +00:00
using Microsoft.EntityFrameworkCore;
using NetCoreServer;
using Wonderking.Game.Data.Character;
2023-11-16 20:05:33 +00:00
using Wonderking.Game.Mapping;
using Wonderking.Packets.Incoming;
using Wonderking.Packets.Outgoing;
using Wonderking.Packets.Outgoing.Data;
namespace Continuity.AuthServer.PacketHandlers;
2023-11-25 11:20:55 +00:00
public class CharacterCreationHandler : IPacketHandler<CharacterCreationPacket>
{
private readonly CharacterStatsMappingConfiguration _characterStatsMapping;
2023-11-19 16:07:28 +00:00
private readonly ItemObjectPoolService _itemObjectPoolService;
private readonly WonderkingContext _wonderkingContext;
public CharacterCreationHandler(WonderkingContext wonderkingContext, ItemObjectPoolService itemObjectPoolService,
CharacterStatsMappingConfiguration characterStatsMappingConfiguration)
{
_wonderkingContext = wonderkingContext;
_itemObjectPoolService = itemObjectPoolService;
_characterStatsMapping = characterStatsMappingConfiguration;
}
2023-11-16 09:30:01 +00:00
public async Task HandleAsync(CharacterCreationPacket packet, TcpSession session)
{
if (session is not AuthSession authSession)
{
return;
}
2023-11-25 11:20:55 +00:00
var account = await _wonderkingContext.Accounts.FirstOrDefaultAsync(a => a.Id == authSession.AccountId);
if (account is null)
{
return;
}
2023-11-19 19:06:02 +00:00
var firstJobConfig = SelectFirstJobConfig(packet.FirstJob);
2023-11-19 19:17:14 +00:00
var items = CreateDefaultItems(packet, firstJobConfig);
2023-11-19 19:17:14 +00:00
var toBeAddedCharacter = CreateDefaultCharacter(packet, account, items, firstJobConfig);
account.Characters.Add(toBeAddedCharacter);
2023-11-21 20:36:05 +00:00
await _wonderkingContext.SaveChangesAsync();
2023-11-24 19:04:42 +00:00
var character =
new CharacterData
{
Name = toBeAddedCharacter.Name,
Job = toBeAddedCharacter.JobData,
Gender = toBeAddedCharacter.Gender,
Level = toBeAddedCharacter.Level,
Experience = toBeAddedCharacter.Experience,
Stats = toBeAddedCharacter.BaseStats,
Health = toBeAddedCharacter.Health,
Mana = toBeAddedCharacter.Mana,
EquippedItems =
GetItemIDsByInventoryTab(toBeAddedCharacter.InventoryItems
.Where(item => item.InventoryTab == InventoryTab.WornEquipment)
.Select(item => new Tuple<ushort, byte>(item.ItemId, item.Slot)).AsEnumerable()),
EquippedCashItems = GetItemIDsByInventoryTab(toBeAddedCharacter.InventoryItems
.Where(item => item.InventoryTab == InventoryTab.WornCashEquipment)
.Select(item => new Tuple<ushort, byte>(item.ItemId, item.Slot)).AsEnumerable())
};
2023-11-19 14:05:48 +00:00
await authSession.SendAsync(new CharacterCreationResponsePacket
2023-11-16 11:49:28 +00:00
{
Character = character,
Slot = packet.Slot,
2023-11-19 16:07:28 +00:00
isDuplicate = false
2023-11-21 20:36:05 +00:00
});
}
2023-11-19 19:17:14 +00:00
private InventoryItem[] CreateDefaultItems(CharacterCreationPacket packet, JobSpecificMapping firstJobConfig)
{
var mappedDefaultItems = _characterStatsMapping.DefaultCharacterMapping.Items
.Select(i => _itemObjectPoolService.GetBaseInventoryItem(i.Id, i.Quantity)).ToArray();
var mappedJobItems = firstJobConfig.Items
.Select(i => _itemObjectPoolService.GetBaseInventoryItem(i.Id, i.Quantity)).ToArray();
var defaultItems = CreateChosenItems(packet);
InventoryItem[] items =
[
.. mappedDefaultItems,
.. mappedJobItems,
.. defaultItems
];
return items;
}
private static Character CreateDefaultCharacter(CharacterCreationPacket packet, Account account,
2023-11-19 19:20:16 +00:00
InventoryItem[] items, JobSpecificMapping firstJobConfig)
{
return new Character()
2023-11-19 19:17:14 +00:00
{
Account = account,
MapId = 300,
Name = packet.Name,
LastXCoordinate = 113,
LastYCoordinate = 0,
PvPLevel = PvPLevel.None,
Gender = packet.Gender,
Experience = 0,
Level = 1,
InventoryItems = items,
BaseStats = firstJobConfig.BaseStats,
JobData = new JobData { FirstJob = packet.FirstJob, SecondJob = 0, ThirdJob = 0, FourthJob = 0 },
Health = CalculateCurrentHealth(1, firstJobConfig),
Mana = CalculateCurrentMana(1, firstJobConfig)
};
2023-11-19 19:20:16 +00:00
}
2023-11-19 19:17:14 +00:00
2023-11-19 19:20:16 +00:00
private JobSpecificMapping SelectFirstJobConfig(byte firstJob)
{
return firstJob switch
2023-11-19 19:06:02 +00:00
{
1 => _characterStatsMapping.Swordsman,
2 => _characterStatsMapping.Mage,
3 => _characterStatsMapping.Thief,
4 => _characterStatsMapping.Scout,
_ => _characterStatsMapping.Swordsman
};
2023-11-19 19:20:16 +00:00
}
2023-11-19 19:06:02 +00:00
2023-11-19 19:20:16 +00:00
private InventoryItem[] CreateChosenItems(CharacterCreationPacket packet)
{
return new[]
2023-11-19 19:06:02 +00:00
{
_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 +
2023-11-19 19:20:16 +00:00
packet.Pants + 58))
2023-11-19 19:06:02 +00:00
};
2023-11-19 19:20:16 +00:00
}
2023-11-19 19:06:02 +00:00
2023-11-19 19:20:16 +00:00
private static int CalculateCurrentHealth(ushort level, JobSpecificMapping firstJobConfig)
{
return (int)((level - 1) * firstJobConfig.DynamicStats.HealthPerLevel +
firstJobConfig.BaseStats.Vitality * firstJobConfig.DynamicStats.HealthPerVitality);
}
2023-11-19 19:20:16 +00:00
private static int CalculateCurrentMana(ushort level, JobSpecificMapping firstJobConfig)
{
return (int)((level - 1) * firstJobConfig.DynamicStats.ManaPerLevel +
firstJobConfig.BaseStats.Wisdom * firstJobConfig.DynamicStats.ManaPerWisdom);
}
2023-11-19 19:17:14 +00:00
private static ushort[] GetItemIDsByInventoryTab(IEnumerable<Tuple<ushort, byte>> items)
{
Span<ushort> ids = stackalloc ushort[20];
ids.Clear();
2023-11-19 19:17:14 +00:00
foreach (var item in items)
{
2023-11-19 19:17:14 +00:00
if (item.Item2 > 20)
{
continue;
}
ids[item.Item2] = item.Item1;
}
return ids.ToArray();
}
}