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
|
|
|
|
2024-01-29 07:39:18 +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;
|
2023-11-15 19:00:08 +00:00
|
|
|
using NetCoreServer;
|
|
|
|
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;
|
|
|
|
|
2024-01-29 07:39:18 +00:00
|
|
|
namespace Continuity.AuthServer.PacketHandlers;
|
2023-11-15 19:00:08 +00:00
|
|
|
|
2023-11-25 11:20:55 +00:00
|
|
|
public class CharacterCreationHandler : IPacketHandler<CharacterCreationPacket>
|
2023-11-15 19:00:08 +00:00
|
|
|
{
|
2023-11-16 20:01:55 +00:00
|
|
|
private readonly CharacterStatsMappingConfiguration _characterStatsMapping;
|
2023-11-19 16:07:28 +00:00
|
|
|
private readonly ItemObjectPoolService _itemObjectPoolService;
|
|
|
|
private readonly WonderkingContext _wonderkingContext;
|
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
|
|
|
{
|
2023-11-25 13:21:55 +00:00
|
|
|
if (session is not AuthSession authSession)
|
2023-11-19 14:07:43 +00:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-11-25 11:20:55 +00:00
|
|
|
var account = await _wonderkingContext.Accounts.FirstOrDefaultAsync(a => a.Id == authSession.AccountId);
|
2023-11-16 20:01:55 +00:00
|
|
|
|
2023-11-25 13:21:55 +00:00
|
|
|
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-16 20:01:55 +00:00
|
|
|
|
2023-11-19 19:17:14 +00:00
|
|
|
var toBeAddedCharacter = CreateDefaultCharacter(packet, account, items, firstJobConfig);
|
2023-11-25 13:21:55 +00:00
|
|
|
account.Characters.Add(toBeAddedCharacter);
|
2023-11-21 20:36:05 +00:00
|
|
|
await _wonderkingContext.SaveChangesAsync();
|
2023-11-15 19:00:08 +00:00
|
|
|
|
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,
|
2023-11-19 14:09:04 +00:00
|
|
|
Slot = packet.Slot,
|
2023-11-19 16:07:28 +00:00
|
|
|
isDuplicate = false
|
2023-11-21 20:36:05 +00:00
|
|
|
});
|
2023-11-15 19:00:08 +00:00
|
|
|
}
|
2023-11-16 20:01:55 +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)
|
|
|
|
{
|
2023-11-25 13:21:55 +00:00
|
|
|
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-16 20:01:55 +00:00
|
|
|
|
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 14:07:43 +00:00
|
|
|
|
2023-11-19 19:17:14 +00:00
|
|
|
private static ushort[] GetItemIDsByInventoryTab(IEnumerable<Tuple<ushort, byte>> items)
|
2023-11-19 14:07:43 +00:00
|
|
|
{
|
2023-11-25 13:21:55 +00:00
|
|
|
Span<ushort> ids = stackalloc ushort[20];
|
|
|
|
ids.Clear();
|
2023-11-19 14:07:43 +00:00
|
|
|
|
2023-11-19 19:17:14 +00:00
|
|
|
foreach (var item in items)
|
2023-11-19 14:07:43 +00:00
|
|
|
{
|
2023-11-19 19:17:14 +00:00
|
|
|
if (item.Item2 > 20)
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
ids[item.Item2] = item.Item1;
|
2023-11-19 14:07:43 +00:00
|
|
|
}
|
|
|
|
|
2023-11-25 13:21:55 +00:00
|
|
|
return ids.ToArray();
|
2023-11-19 14:07:43 +00:00
|
|
|
}
|
2023-11-15 19:00:08 +00:00
|
|
|
}
|