2023-10-27 17:47:17 +00:00
|
|
|
using Microsoft.EntityFrameworkCore;
|
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
|
|
|
|
|
|
|
using DB;
|
|
|
|
using Microsoft.Extensions.Configuration;
|
|
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
using NetCoreServer;
|
|
|
|
|
|
|
|
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-10-27 17:47:17 +00:00
|
|
|
this._configuration = configuration;
|
|
|
|
this._logger = logger;
|
|
|
|
this._wonderkingContext = wonderkingContext;
|
2023-08-14 20:22:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public ChannelSelectionHandler()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
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-16 13:27:17 +00:00
|
|
|
var account = await this._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",
|
|
|
|
Port = 12345,
|
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-13 20:12:12 +00:00
|
|
|
EquippedItems =
|
2023-11-15 19:00:08 +00:00
|
|
|
c.InventoryItems.Where(item => item.InventoryTab == InventoryTab.WornEquipment)
|
2023-11-14 19:07:45 +00:00
|
|
|
.Select(item => item.ItemId)
|
|
|
|
.ToArray(),
|
|
|
|
EquippedCashItems = c.InventoryItems
|
2023-11-15 19:00:08 +00:00
|
|
|
.Where(item => item.InventoryTab == InventoryTab.WornCashEquipment)
|
2023-11-13 20:12:12 +00:00
|
|
|
.Select(item => item.ItemId)
|
2023-11-16 09:25:10 +00:00
|
|
|
.ToArray()
|
2023-11-13 20:12:12 +00:00
|
|
|
})
|
2023-11-16 09:30:01 +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-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",
|
|
|
|
Port = 12345,
|
|
|
|
Characters = Array.Empty<CharacterData>()
|
2023-11-14 19:07:45 +00:00
|
|
|
};
|
2023-11-13 20:16:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
authSession.Send(responsePacket);
|
2023-11-16 09:25:10 +00:00
|
|
|
authSession.Send(guildNameResponsePacket);
|
2023-11-13 20:16:02 +00:00
|
|
|
}
|
2023-08-14 20:22:43 +00:00
|
|
|
}
|