continuity/Continuity.AuthServer/PacketHandlers/ChannelSelectionHandler.cs

138 lines
4.7 KiB
C#
Raw Normal View History

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
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;
chore: .net8 support and extended support for pipeline jobs fix: incorrect command fix: incorrect command fix: incorrect command fix: export tools path chore: test dir fix: test build script fix: missing semicolon fix: missing semicolon fix: missing buildscript fix: missing buildscript chore: update build to .net7 ci: set root dir ci: set root dir revert: .net 8 support for benchmarks ci: disable docker for ci ci: disable docker for ci ci: disable docker for ci ci: trigger ci: i stg ci: let's hope its the correct one ci: i stg ci: further sonarscanner setup ci: add tools ci: rearrange ci: verbose output ci: hardcoded project key ci: test env ci: test more env ci: env test again ci: test env ci: shit ci: final setup ci: final setup ci: final setup ci: adjust buildscript ci: nuke ci: install java ci: install java refactor: loggermessages to own namespace chore: switch to dotnet foundation editorconfig preset ci: switch to basic gitea ci ci: steps ci: add missing runs-on ci: remove unnecessary actions ci: test attempt? ci: add missing name for step ci: fix missing project name reference ci: lets try again ci: again ci: again.. ci: idk at this point ci: append path prematurely ci: add path to bash cli: I really don't know ci: again.... ci: idk ci: again.... ci: another one ci: fix incorrect path add-path ci: add dependency track support ci: fix upload ci: forgot to adjust data for action ci: incorrect path? ci: add version tag ci: idk ci: fix incorrect path for bom ci: fix version tag ci: disable github license resolution for now ci: does this work ci: another one bites the dust chore: .net 8 and extended pipeline support ci: again chore: dockerignore added chore(deps): update dependency benchmarkdotnet to v0.13.10 Signed-off-by: noreply@rainote.dev ci: dependency track can run on any branch ci: first attempt docker image ci: again ci: some fixes ci: idk ci: does this help ci: idk ci: another one ci: forgot ci: downgrade qemu setup ci: downgrade.. ci: v1 includes docker ci: rearrange ci: idk what to do with this ci: let's try cat ci: alt ci: depressing ci: yikes ci: ah come on ci: let's try new version again ci: again ci: another one ci: another one ci: confusion ci: try single ci: aaaa ci: one more time ci: again main (#69) Reviewed-on: https://forge.rainote.dev/wonderking/continuity/pulls/69 Co-authored-by: Timothy Schenk <admin@rainote.dev> Co-committed-by: Timothy Schenk <admin@rainote.dev> chore: ci jobs expanded & .net 8 support ci: branch name sanitization for docker ci: another attempt ci: forgot actor chore: remove nuke remnants chore(deps): update dependency dotnet-sdk to v7.0.403 Signed-off-by: noreply@rainote.dev refactor: shared data into library refactor: rewrite for packethandler/id map chore: logging chore: Sonar Warnings chore: upgrade to .net8 image chore: fetch all required information for characters chore: upgrade to .net 8 ci: install .net 7.0 alongside 8.0 for sbom fix: incorrect job ci: let's try arm64 again ci: adjust project for .net 8 support chore: adjustments to composefile chore: analyzer setup chore: analyzer and project settings updated chore: ci jobs expanded & .net 8 support
2023-10-27 17:47:17 +00:00
using Wonderking.Packets.Incoming;
2023-11-13 20:12:12 +00:00
using Wonderking.Packets.Outgoing;
using Wonderking.Packets.Outgoing.Data;
chore: .net8 support and extended support for pipeline jobs fix: incorrect command fix: incorrect command fix: incorrect command fix: export tools path chore: test dir fix: test build script fix: missing semicolon fix: missing semicolon fix: missing buildscript fix: missing buildscript chore: update build to .net7 ci: set root dir ci: set root dir revert: .net 8 support for benchmarks ci: disable docker for ci ci: disable docker for ci ci: disable docker for ci ci: trigger ci: i stg ci: let's hope its the correct one ci: i stg ci: further sonarscanner setup ci: add tools ci: rearrange ci: verbose output ci: hardcoded project key ci: test env ci: test more env ci: env test again ci: test env ci: shit ci: final setup ci: final setup ci: final setup ci: adjust buildscript ci: nuke ci: install java ci: install java refactor: loggermessages to own namespace chore: switch to dotnet foundation editorconfig preset ci: switch to basic gitea ci ci: steps ci: add missing runs-on ci: remove unnecessary actions ci: test attempt? ci: add missing name for step ci: fix missing project name reference ci: lets try again ci: again ci: again.. ci: idk at this point ci: append path prematurely ci: add path to bash cli: I really don't know ci: again.... ci: idk ci: again.... ci: another one ci: fix incorrect path add-path ci: add dependency track support ci: fix upload ci: forgot to adjust data for action ci: incorrect path? ci: add version tag ci: idk ci: fix incorrect path for bom ci: fix version tag ci: disable github license resolution for now ci: does this work ci: another one bites the dust chore: .net 8 and extended pipeline support ci: again chore: dockerignore added chore(deps): update dependency benchmarkdotnet to v0.13.10 Signed-off-by: noreply@rainote.dev ci: dependency track can run on any branch ci: first attempt docker image ci: again ci: some fixes ci: idk ci: does this help ci: idk ci: another one ci: forgot ci: downgrade qemu setup ci: downgrade.. ci: v1 includes docker ci: rearrange ci: idk what to do with this ci: let's try cat ci: alt ci: depressing ci: yikes ci: ah come on ci: let's try new version again ci: again ci: another one ci: another one ci: confusion ci: try single ci: aaaa ci: one more time ci: again main (#69) Reviewed-on: https://forge.rainote.dev/wonderking/continuity/pulls/69 Co-authored-by: Timothy Schenk <admin@rainote.dev> Co-committed-by: Timothy Schenk <admin@rainote.dev> chore: ci jobs expanded & .net 8 support ci: branch name sanitization for docker ci: another attempt ci: forgot actor chore: remove nuke remnants chore(deps): update dependency dotnet-sdk to v7.0.403 Signed-off-by: noreply@rainote.dev refactor: shared data into library refactor: rewrite for packethandler/id map chore: logging chore: Sonar Warnings chore: upgrade to .net8 image chore: fetch all required information for characters chore: upgrade to .net 8 ci: install .net 7.0 alongside 8.0 for sbom fix: incorrect job ci: let's try arm64 again ci: adjust project for .net 8 support chore: adjustments to composefile chore: analyzer setup chore: analyzer and project settings updated chore: ci jobs expanded & .net 8 support
2023-10-27 17:47:17 +00:00
namespace Continuity.AuthServer.PacketHandlers;
2023-08-14 20:22:43 +00:00
public partial class ChannelSelectionHandler : IPacketHandler<ChannelSelectionPacket>
2023-08-14 20:22:43 +00:00
{
chore: .net8 support and extended support for pipeline jobs fix: incorrect command fix: incorrect command fix: incorrect command fix: export tools path chore: test dir fix: test build script fix: missing semicolon fix: missing semicolon fix: missing buildscript fix: missing buildscript chore: update build to .net7 ci: set root dir ci: set root dir revert: .net 8 support for benchmarks ci: disable docker for ci ci: disable docker for ci ci: disable docker for ci ci: trigger ci: i stg ci: let's hope its the correct one ci: i stg ci: further sonarscanner setup ci: add tools ci: rearrange ci: verbose output ci: hardcoded project key ci: test env ci: test more env ci: env test again ci: test env ci: shit ci: final setup ci: final setup ci: final setup ci: adjust buildscript ci: nuke ci: install java ci: install java refactor: loggermessages to own namespace chore: switch to dotnet foundation editorconfig preset ci: switch to basic gitea ci ci: steps ci: add missing runs-on ci: remove unnecessary actions ci: test attempt? ci: add missing name for step ci: fix missing project name reference ci: lets try again ci: again ci: again.. ci: idk at this point ci: append path prematurely ci: add path to bash cli: I really don't know ci: again.... ci: idk ci: again.... ci: another one ci: fix incorrect path add-path ci: add dependency track support ci: fix upload ci: forgot to adjust data for action ci: incorrect path? ci: add version tag ci: idk ci: fix incorrect path for bom ci: fix version tag ci: disable github license resolution for now ci: does this work ci: another one bites the dust chore: .net 8 and extended pipeline support ci: again chore: dockerignore added chore(deps): update dependency benchmarkdotnet to v0.13.10 Signed-off-by: noreply@rainote.dev ci: dependency track can run on any branch ci: first attempt docker image ci: again ci: some fixes ci: idk ci: does this help ci: idk ci: another one ci: forgot ci: downgrade qemu setup ci: downgrade.. ci: v1 includes docker ci: rearrange ci: idk what to do with this ci: let's try cat ci: alt ci: depressing ci: yikes ci: ah come on ci: let's try new version again ci: again ci: another one ci: another one ci: confusion ci: try single ci: aaaa ci: one more time ci: again main (#69) Reviewed-on: https://forge.rainote.dev/wonderking/continuity/pulls/69 Co-authored-by: Timothy Schenk <admin@rainote.dev> Co-committed-by: Timothy Schenk <admin@rainote.dev> chore: ci jobs expanded & .net 8 support ci: branch name sanitization for docker ci: another attempt ci: forgot actor chore: remove nuke remnants chore(deps): update dependency dotnet-sdk to v7.0.403 Signed-off-by: noreply@rainote.dev refactor: shared data into library refactor: rewrite for packethandler/id map chore: logging chore: Sonar Warnings chore: upgrade to .net8 image chore: fetch all required information for characters chore: upgrade to .net 8 ci: install .net 7.0 alongside 8.0 for sbom fix: incorrect job ci: let's try arm64 again ci: adjust project for .net 8 support chore: adjustments to composefile chore: analyzer setup chore: analyzer and project settings updated chore: ci jobs expanded & .net 8 support
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",
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-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-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-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-21 21:56:26 +00:00
private static ushort[] GetItemIDsByInventoryTab(IEnumerable<InventoryItemProjection> items)
{
var ids = new ushort[20];
2023-11-19 19:17:14 +00:00
ids.AsSpan().Clear();
2023-11-19 19:17:14 +00:00
foreach (var item in items)
{
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;
}
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
}