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
|
|
|
|
2024-01-29 07:39:18 +00:00
|
|
|
using Continuity.AuthServer.DB;
|
|
|
|
using Continuity.AuthServer.DB.Documents;
|
2023-11-21 21:56:26 +00:00
|
|
|
using DotNext.Collections.Generic;
|
2023-11-25 11:20:55 +00:00
|
|
|
using Microsoft.EntityFrameworkCore;
|
2023-11-19 16:07:28 +00:00
|
|
|
using NetCoreServer;
|
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
|
|
|
|
2024-01-29 07:39:18 +00:00
|
|
|
namespace Continuity.AuthServer.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
|
|
|
{
|
2023-11-25 11:20:55 +00:00
|
|
|
if (session is not AuthSession authSession)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
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-25 11:20:55 +00:00
|
|
|
var accountExists =
|
|
|
|
await _wonderkingContext.Accounts.AsNoTracking().AnyAsync(a => a.Id == authSession.AccountId);
|
2023-11-21 21:56:26 +00:00
|
|
|
|
2023-11-25 11:20:55 +00:00
|
|
|
var amountOfCharacter = await _wonderkingContext.Characters.AsNoTracking().Include(c => c.Account)
|
|
|
|
.Where(c => c.Account.Id == authSession.AccountId).Take(3)
|
|
|
|
.CountAsync();
|
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 =
|
2023-11-25 11:20:55 +00:00
|
|
|
await _wonderkingContext.Characters.AsNoTracking().Include(c => c.Account).Include(c => c.GuildMember)
|
|
|
|
.ThenInclude(gm => gm.Guild)
|
|
|
|
.Where(c => c.Account.Id == authSession.AccountId && c.GuildMember.Guild != null)
|
|
|
|
.Select(c => c.GuildMember.Guild.Name).Take(3).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)
|
|
|
|
{
|
2023-11-25 11:20:55 +00:00
|
|
|
var characterDataProjections = _wonderkingContext.Characters.AsNoTracking().AsSplitQuery()
|
|
|
|
.Include(c => c.InventoryItems).Include(c => c.Account)
|
|
|
|
.Where(c => c.Account.Id == accountId)
|
|
|
|
.Select(c => new CharacterDataProjection(
|
|
|
|
c.Name,
|
|
|
|
c.JobData,
|
|
|
|
c.Gender,
|
|
|
|
c.Level,
|
|
|
|
c.Experience,
|
|
|
|
c.BaseStats,
|
|
|
|
c.Health,
|
|
|
|
c.Mana,
|
|
|
|
c.InventoryItems.Select(i =>
|
|
|
|
new InventoryItemProjection(i.ItemId, i.Slot, i.InventoryTab))
|
|
|
|
)).Take(3);
|
|
|
|
|
|
|
|
await foreach (var c in characterDataProjections)
|
2023-11-21 21:56:26 +00:00
|
|
|
{
|
|
|
|
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
|
|
|
}
|