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:58:30 +00:00
|
|
|
|
2023-11-21 21:56:26 +00:00
|
|
|
using DotNext.Collections.Generic;
|
2023-11-19 16:07:28 +00:00
|
|
|
using NetCoreServer;
|
|
|
|
using Server.DB;
|
2023-11-13 20:12:12 +00:00
|
|
|
using Server.DB.Documents;
|
2023-10-27 17:47:17 +00:00
|
|
|
using Wonderking.Packets.Incoming;
|
2023-11-13 20:12:12 +00:00
|
|
|
using Wonderking.Packets.Outgoing;
|
2023-11-14 20:25:28 +00:00
|
|
|
using Wonderking.Packets.Outgoing.Data;
|
2023-10-27 17:47:17 +00:00
|
|
|
|
2023-10-12 07:15:34 +00:00
|
|
|
namespace Server.PacketHandlers;
|
2023-08-14 20:22:43 +00:00
|
|
|
|
2023-11-22 13:51:46 +00:00
|
|
|
public partial class ChannelSelectionHandler : IPacketHandler<ChannelSelectionPacket>
|
2023-08-14 20:22:43 +00:00
|
|
|
{
|
2023-10-27 17:47:17 +00:00
|
|
|
private readonly WonderkingContext _wonderkingContext;
|
2023-08-14 20:22:43 +00:00
|
|
|
|
2023-11-19 19:17:14 +00:00
|
|
|
public ChannelSelectionHandler(WonderkingContext wonderkingContext)
|
2023-08-14 20:22:43 +00:00
|
|
|
{
|
2023-11-19 16:07:28 +00:00
|
|
|
_wonderkingContext = wonderkingContext;
|
2023-08-14 20:22:43 +00:00
|
|
|
}
|
|
|
|
|
2023-11-16 09:30:01 +00:00
|
|
|
public async Task HandleAsync(ChannelSelectionPacket packet, TcpSession session)
|
2023-08-14 20:22:43 +00:00
|
|
|
{
|
|
|
|
var authSession = (AuthSession)session;
|
2023-11-13 20:12:12 +00:00
|
|
|
ChannelSelectionResponsePacket responsePacket;
|
2023-11-16 09:25:10 +00:00
|
|
|
var guildNameResponsePacket = new CharacterSelectionSetGuildNamePacket { GuildNames = Array.Empty<string>() };
|
2023-11-13 20:12:12 +00:00
|
|
|
|
2023-11-22 06:35:25 +00:00
|
|
|
var accountExists = await _accountExists(_wonderkingContext, authSession.AccountId);
|
2023-11-21 21:56:26 +00:00
|
|
|
|
2023-11-22 06:35:25 +00:00
|
|
|
var amountOfCharacter = await _getAmountOfCharacters(_wonderkingContext, authSession.AccountId);
|
2023-11-21 21:56:26 +00:00
|
|
|
|
2023-11-22 06:35:25 +00:00
|
|
|
if (!accountExists)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (amountOfCharacter > 0)
|
2023-11-13 20:12:12 +00:00
|
|
|
{
|
|
|
|
responsePacket = new ChannelSelectionResponsePacket
|
|
|
|
{
|
|
|
|
ChannelIsFullFlag = 0,
|
|
|
|
Endpoint = "127.0.0.1",
|
2023-11-17 10:14:42 +00:00
|
|
|
Port = 2000,
|
2023-11-22 06:35:25 +00:00
|
|
|
Characters = await GetCharacterDataAsync(authSession.AccountId).ToArrayAsync()
|
2023-11-13 20:12:12 +00:00
|
|
|
};
|
2023-11-14 19:07:45 +00:00
|
|
|
|
2023-11-22 06:35:25 +00:00
|
|
|
guildNameResponsePacket.GuildNames =
|
|
|
|
await _getGuildNames(_wonderkingContext, authSession.AccountId).ToArrayAsync();
|
2023-11-13 20:12:12 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2023-11-16 09:25:10 +00:00
|
|
|
responsePacket = new ChannelSelectionResponsePacket
|
2023-11-14 19:07:45 +00:00
|
|
|
{
|
2023-11-16 09:25:10 +00:00
|
|
|
ChannelIsFullFlag = 0,
|
|
|
|
Endpoint = "127.0.0.1",
|
2023-11-17 07:25:29 +00:00
|
|
|
Port = 2000,
|
2023-11-16 09:25:10 +00:00
|
|
|
Characters = Array.Empty<CharacterData>()
|
2023-11-14 19:07:45 +00:00
|
|
|
};
|
2023-11-13 20:16:02 +00:00
|
|
|
}
|
|
|
|
|
2023-11-21 20:36:05 +00:00
|
|
|
await authSession.SendAsync(responsePacket);
|
2023-11-17 07:25:29 +00:00
|
|
|
if (guildNameResponsePacket.GuildNames.Length > 0 &&
|
|
|
|
guildNameResponsePacket.GuildNames.Select(n => n != string.Empty).Any())
|
|
|
|
{
|
2023-11-21 20:36:05 +00:00
|
|
|
await authSession.SendAsync(guildNameResponsePacket);
|
2023-11-17 07:25:29 +00:00
|
|
|
}
|
2023-11-13 20:16:02 +00:00
|
|
|
}
|
2023-11-17 10:14:42 +00:00
|
|
|
|
2023-11-21 21:56:26 +00:00
|
|
|
private static ushort[] GetItemIDsByInventoryTab(IEnumerable<InventoryItemProjection> items)
|
2023-11-17 10:14:42 +00:00
|
|
|
{
|
|
|
|
var ids = new ushort[20];
|
2023-11-19 19:17:14 +00:00
|
|
|
ids.AsSpan().Clear();
|
2023-11-17 10:14:42 +00:00
|
|
|
|
2023-11-19 19:17:14 +00:00
|
|
|
foreach (var item in items)
|
2023-11-17 10:14:42 +00:00
|
|
|
{
|
2023-11-21 21:56:26 +00:00
|
|
|
if (item.Slot > 20)
|
2023-11-19 19:17:14 +00:00
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2023-11-21 21:56:26 +00:00
|
|
|
ids[item.Slot] = item.ItemId;
|
2023-11-17 10:14:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return ids;
|
|
|
|
}
|
2023-11-21 21:56:26 +00:00
|
|
|
|
|
|
|
private async IAsyncEnumerable<CharacterData> GetCharacterDataAsync(Guid accountId)
|
|
|
|
{
|
|
|
|
await foreach (var c in _getCharacters(_wonderkingContext, accountId))
|
|
|
|
{
|
|
|
|
yield return new CharacterData
|
|
|
|
{
|
|
|
|
Name = c.Name,
|
|
|
|
Job = c.JobData,
|
|
|
|
Gender = c.Gender,
|
|
|
|
Level = c.Level,
|
|
|
|
// TODO: Calculate instead of clamping based on max experience for level
|
|
|
|
Experience = Math.Clamp(c.Experience, 0, 100),
|
|
|
|
Stats = c.BaseStats,
|
|
|
|
Health = c.Health,
|
|
|
|
Mana = c.Mana,
|
|
|
|
EquippedItems =
|
|
|
|
GetItemIDsByInventoryTab(c.InventoryItems.Where(i =>
|
|
|
|
i.InventoryTab == InventoryTab.WornEquipment)),
|
|
|
|
EquippedCashItems = GetItemIDsByInventoryTab(c.InventoryItems.Where(i =>
|
|
|
|
i.InventoryTab == InventoryTab.WornCashEquipment))
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-14 20:22:43 +00:00
|
|
|
}
|