continuity/Server/PacketHandlers/ChannelSelectionHandler.cs
Timothy Schenk 64ba76e41a
All checks were successful
Build, Package and Push Images / preprocess (push) Successful in 2s
Build, Package and Push Images / build (push) Successful in 33s
Build, Package and Push Images / sonarqube (push) Has been skipped
Build, Package and Push Images / sbom-scan (push) Successful in 39s
Build, Package and Push Images / container-build (push) Successful in 1m20s
Build, Package and Push Images / container-sbom-scan (push) Successful in 36s
fix: add default empty response packet
2023-11-13 21:17:44 +01:00

124 lines
4.6 KiB
C#

using Microsoft.EntityFrameworkCore;
using Server.DB.Documents;
using Wonderking.Game.Data.Character;
using Wonderking.Packets.Incoming;
using Wonderking.Packets.Outgoing;
namespace Server.PacketHandlers;
using DB;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
using NetCoreServer;
public class ChannelSelectionHandler : IPacketHandler<ChannelSelectionPacket>
{
private readonly IConfiguration _configuration;
private readonly ILogger<ChannelSelectionHandler> _logger;
private readonly WonderkingContext _wonderkingContext;
public ChannelSelectionHandler(IConfiguration configuration, ILogger<ChannelSelectionHandler> logger,
WonderkingContext wonderkingContext)
{
this._configuration = configuration;
this._logger = logger;
this._wonderkingContext = wonderkingContext;
}
public ChannelSelectionHandler()
{
}
public Task HandleAsync(ChannelSelectionPacket packet, TcpSession session)
{
var authSession = (AuthSession)session;
var charactersOfAccount = this._wonderkingContext.Accounts.Include(account => account.Characters)
.ThenInclude(character => character.InventoryItems).Include(account => account.Characters)
.ThenInclude(character => character.JobData).Include(account => account.Characters)
.ThenInclude(character => character.BaseStats)
.FirstOrDefault(a => a.Id == authSession.AccountId)
?.Characters;
ChannelSelectionResponsePacket responsePacket;
var testingChars = true;
if (charactersOfAccount != null && testingChars)
{
responsePacket = new ChannelSelectionResponsePacket
{
ChannelIsFullFlag = 0,
Endpoint = "127.0.0.1",
Port = 12345,
Characters = charactersOfAccount.Select((character,
_) => new CharacterData
{
Name = character.Name,
Job = character.JobData,
Gender = character.Gender,
Level = character.Level,
Experience = 0,
Stats = character.BaseStats,
Health = character.Health,
Mana = character.Mana,
EquippedItems =
character.InventoryItems.Where(item => item.ItemType == ItemType.WornEquipment)
.Select(item => item.ItemId)
.ToArray(),
EquippedCashItems = character.InventoryItems
.Where(item => item.ItemType == ItemType.WornCashEquipment)
.Select(item => item.ItemId)
.ToArray(),
})
.ToArray(),
};
}
else
{
responsePacket = testingChars
? CreateTestChannelSelectionResponsePacket()
: new ChannelSelectionResponsePacket
{
ChannelIsFullFlag = 0,
Endpoint = "127.0.0.1",
Port = 12345,
Characters = Array.Empty<CharacterData>()
};
}
authSession.Send(responsePacket);
return Task.CompletedTask;
}
private static ChannelSelectionResponsePacket CreateTestChannelSelectionResponsePacket()
{
return new ChannelSelectionResponsePacket
{
ChannelIsFullFlag = 0,
Endpoint = "127.0.0.1",
Port = 12345,
Characters = new[]
{
new CharacterData
{
Name = "Test243",
Job = new JobData { FirstJob = 1, SecondJob = 0, ThirdJob = 0, FourthJob = 0 },
Gender = Gender.None,
Level = ushort.MaxValue - 1,
Experience = 255,
Stats = new BaseStats
{
Strength = 5,
Dexterity = 5,
Intelligence = 5,
Vitality = 5,
Luck = 5,
Wisdom = 5
},
Health = int.MaxValue - 1,
Mana = int.MaxValue - 1,
EquippedItems = Enumerable.Repeat((ushort)25, 20).ToArray(),
EquippedCashItems = Enumerable.Repeat((ushort)25, 20).ToArray()
},
},
};
}
}