continuity/Server/PacketHandlers/ChannelSelectionHandler.cs

102 lines
3.9 KiB
C#

// Copyright (c) 2023 Timothy Schenk. Subject to the GNU AGPL Version 3 License.
using Microsoft.EntityFrameworkCore;
using NetCoreServer;
using Server.DB;
using Server.DB.Documents;
using Wonderking.Packets.Incoming;
using Wonderking.Packets.Outgoing;
using Wonderking.Packets.Outgoing.Data;
namespace Server.PacketHandlers;
public class ChannelSelectionHandler : IPacketHandler<ChannelSelectionPacket>
{
private readonly WonderkingContext _wonderkingContext;
public ChannelSelectionHandler(WonderkingContext wonderkingContext)
{
_wonderkingContext = wonderkingContext;
}
public async Task HandleAsync(ChannelSelectionPacket packet, TcpSession session)
{
var authSession = (AuthSession)session;
ChannelSelectionResponsePacket responsePacket;
var guildNameResponsePacket = new CharacterSelectionSetGuildNamePacket { GuildNames = Array.Empty<string>() };
var account = await _wonderkingContext.Accounts
.FirstOrDefaultAsync(a => a.Id == authSession.AccountId);
if (account != null && account.Characters.Count > 0)
{
responsePacket = new ChannelSelectionResponsePacket
{
ChannelIsFullFlag = 0,
Endpoint = "127.0.0.1",
Port = 2000,
Characters = await _wonderkingContext.Characters.AsNoTracking()
.Where(c => c.Account.Id == authSession.AccountId)
.Select(c =>
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(item => item.InventoryTab == InventoryTab.WornEquipment)
.Select(item => new Tuple<ushort, byte>(item.ItemId, item.Slot)).AsEnumerable()),
EquippedCashItems = GetItemIDsByInventoryTab(c.InventoryItems
.Where(item => item.InventoryTab == InventoryTab.WornCashEquipment)
.Select(item => new Tuple<ushort, byte>(item.ItemId, item.Slot)).AsEnumerable())
})
.ToArrayAsync()
};
guildNameResponsePacket.GuildNames = await _wonderkingContext.Characters
.Where(c => c.Account.Id == authSession.AccountId)
.Where(c => c.Guild != null)
.Select(character => character.Guild.Name).ToArrayAsync();
}
else
{
responsePacket = new ChannelSelectionResponsePacket
{
ChannelIsFullFlag = 0,
Endpoint = "127.0.0.1",
Port = 2000,
Characters = Array.Empty<CharacterData>()
};
}
await authSession.SendAsync(responsePacket);
if (guildNameResponsePacket.GuildNames.Length > 0 &&
guildNameResponsePacket.GuildNames.Select(n => n != string.Empty).Any())
{
await authSession.SendAsync(guildNameResponsePacket);
}
}
private static ushort[] GetItemIDsByInventoryTab(IEnumerable<Tuple<ushort, byte>> items)
{
var ids = new ushort[20];
ids.AsSpan().Clear();
foreach (var item in items)
{
if (item.Item2 > 20)
{
continue;
}
ids[item.Item2] = item.Item1;
}
return ids;
}
}