2023-10-27 17:47:17 +00:00
|
|
|
using Microsoft.EntityFrameworkCore;
|
2023-11-19 16:07:28 +00:00
|
|
|
using Microsoft.Extensions.Configuration;
|
|
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
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
|
|
|
|
|
|
|
public class ChannelSelectionHandler : IPacketHandler<ChannelSelectionPacket>
|
|
|
|
{
|
2023-10-27 17:47:17 +00:00
|
|
|
private readonly IConfiguration _configuration;
|
|
|
|
private readonly ILogger<ChannelSelectionHandler> _logger;
|
|
|
|
private readonly WonderkingContext _wonderkingContext;
|
2023-08-14 20:22:43 +00:00
|
|
|
|
|
|
|
public ChannelSelectionHandler(IConfiguration configuration, ILogger<ChannelSelectionHandler> logger,
|
|
|
|
WonderkingContext wonderkingContext)
|
|
|
|
{
|
2023-11-19 16:07:28 +00:00
|
|
|
_configuration = configuration;
|
|
|
|
_logger = logger;
|
|
|
|
_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-19 16:07:28 +00:00
|
|
|
var account = await _wonderkingContext.Accounts
|
2023-11-16 09:30:01 +00:00
|
|
|
.FirstOrDefaultAsync(a => a.Id == authSession.AccountId).ConfigureAwait(true);
|
|
|
|
if (account != null && account.Characters.Count > 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-16 09:30:01 +00:00
|
|
|
Characters = await _wonderkingContext.Characters.AsNoTracking()
|
2023-11-16 09:25:10 +00:00
|
|
|
.Where(c => c.Account.Id == authSession.AccountId)
|
2023-11-14 19:07:45 +00:00
|
|
|
.Select(c =>
|
|
|
|
new CharacterData
|
2023-11-13 20:12:12 +00:00
|
|
|
{
|
2023-11-14 19:07:45 +00:00
|
|
|
Name = c.Name,
|
|
|
|
Job = c.JobData,
|
|
|
|
Gender = c.Gender,
|
|
|
|
Level = c.Level,
|
2023-11-16 09:25:10 +00:00
|
|
|
// TODO: Calculate instead of clamping based on max experience for level
|
|
|
|
Experience = Math.Clamp(c.Experience, 0, 100),
|
2023-11-14 19:07:45 +00:00
|
|
|
Stats = c.BaseStats,
|
|
|
|
Health = c.Health,
|
|
|
|
Mana = c.Mana,
|
2023-11-17 10:14:42 +00:00
|
|
|
EquippedItems = GetItemIDsByInventoryTab(c.InventoryItems
|
|
|
|
.Where(item => item.InventoryTab == InventoryTab.WornEquipment)
|
|
|
|
.Select(item => new Tuple<ushort, byte>(item.ItemId, item.Slot)).ToArray()),
|
|
|
|
EquippedCashItems = GetItemIDsByInventoryTab(c.InventoryItems
|
2023-11-15 19:00:08 +00:00
|
|
|
.Where(item => item.InventoryTab == InventoryTab.WornCashEquipment)
|
2023-11-19 16:07:28 +00:00
|
|
|
.Select(item => new Tuple<ushort, byte>(item.ItemId, item.Slot)).ToArray())
|
2023-11-13 20:12:12 +00:00
|
|
|
})
|
2023-11-19 16:07:28 +00:00
|
|
|
.ToArrayAsync().ConfigureAwait(true)
|
2023-11-13 20:12:12 +00:00
|
|
|
};
|
2023-11-14 19:07:45 +00:00
|
|
|
|
2023-11-16 09:30:01 +00:00
|
|
|
guildNameResponsePacket.GuildNames = await _wonderkingContext.Characters
|
2023-11-16 09:25:10 +00:00
|
|
|
.Where(c => c.Account.Id == authSession.AccountId)
|
2023-11-17 10:14:42 +00:00
|
|
|
.Where(c => c.Guild != null)
|
2023-11-16 09:30:01 +00:00
|
|
|
.Select(character => character.Guild.Name).ToArrayAsync().ConfigureAwait(true);
|
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-19 14:05:48 +00:00
|
|
|
await authSession.SendAsync(responsePacket).ConfigureAwait(false);
|
2023-11-17 07:25:29 +00:00
|
|
|
if (guildNameResponsePacket.GuildNames.Length > 0 &&
|
|
|
|
guildNameResponsePacket.GuildNames.Select(n => n != string.Empty).Any())
|
|
|
|
{
|
2023-11-19 14:05:48 +00:00
|
|
|
await authSession.SendAsync(guildNameResponsePacket).ConfigureAwait(false);
|
2023-11-17 07:25:29 +00:00
|
|
|
}
|
2023-11-13 20:16:02 +00:00
|
|
|
}
|
2023-11-17 10:14:42 +00:00
|
|
|
|
|
|
|
private static ushort[] GetItemIDsByInventoryTab(Tuple<ushort, byte>[] items)
|
|
|
|
{
|
|
|
|
var ids = new ushort[20];
|
|
|
|
|
|
|
|
for (var i = 0; i < 20; i++)
|
|
|
|
{
|
|
|
|
ids[i] = items.FirstOrDefault(item => item.Item2 == i)?.Item1 ?? 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ids;
|
|
|
|
}
|
2023-08-14 20:22:43 +00:00
|
|
|
}
|