refactor: early return + itemids as span

This commit is contained in:
Timothy Schenk 2023-11-25 14:21:55 +01:00
parent c4905b8cba
commit ec155ecab1

View file

@ -29,19 +29,23 @@ public class CharacterCreationHandler : IPacketHandler<CharacterCreationPacket>
public async Task HandleAsync(CharacterCreationPacket packet, TcpSession session) public async Task HandleAsync(CharacterCreationPacket packet, TcpSession session)
{ {
var authSession = session as AuthSession; if (session is not AuthSession authSession)
if (authSession is null)
{ {
return; return;
} }
var account = await _wonderkingContext.Accounts.FirstOrDefaultAsync(a => a.Id == authSession.AccountId); var account = await _wonderkingContext.Accounts.FirstOrDefaultAsync(a => a.Id == authSession.AccountId);
if (account is null)
{
return;
}
var firstJobConfig = SelectFirstJobConfig(packet.FirstJob); var firstJobConfig = SelectFirstJobConfig(packet.FirstJob);
var items = CreateDefaultItems(packet, firstJobConfig); var items = CreateDefaultItems(packet, firstJobConfig);
var toBeAddedCharacter = CreateDefaultCharacter(packet, account, items, firstJobConfig); var toBeAddedCharacter = CreateDefaultCharacter(packet, account, items, firstJobConfig);
account?.Characters.Add(toBeAddedCharacter); account.Characters.Add(toBeAddedCharacter);
await _wonderkingContext.SaveChangesAsync(); await _wonderkingContext.SaveChangesAsync();
var character = var character =
@ -92,7 +96,7 @@ public class CharacterCreationHandler : IPacketHandler<CharacterCreationPacket>
private static Character CreateDefaultCharacter(CharacterCreationPacket packet, Account account, private static Character CreateDefaultCharacter(CharacterCreationPacket packet, Account account,
InventoryItem[] items, JobSpecificMapping firstJobConfig) InventoryItem[] items, JobSpecificMapping firstJobConfig)
{ {
return new Character return new Character()
{ {
Account = account, Account = account,
MapId = 300, MapId = 300,
@ -154,8 +158,8 @@ public class CharacterCreationHandler : IPacketHandler<CharacterCreationPacket>
private static ushort[] GetItemIDsByInventoryTab(IEnumerable<Tuple<ushort, byte>> items) private static ushort[] GetItemIDsByInventoryTab(IEnumerable<Tuple<ushort, byte>> items)
{ {
var ids = new ushort[20]; Span<ushort> ids = stackalloc ushort[20];
ids.AsSpan().Clear(); ids.Clear();
foreach (var item in items) foreach (var item in items)
{ {
@ -167,6 +171,6 @@ public class CharacterCreationHandler : IPacketHandler<CharacterCreationPacket>
ids[item.Item2] = item.Item1; ids[item.Item2] = item.Item1;
} }
return ids; return ids.ToArray();
} }
} }