Skip to content

Commit

Permalink
Fix bug in login, add support for AS categories
Browse files Browse the repository at this point in the history
  • Loading branch information
Blackburn29 committed Jul 9, 2024
1 parent 2fac9cb commit d657272
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,22 @@
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using Fragment.NetSlum.Networking.Attributes;
using Fragment.NetSlum.Networking.Constants;
using Fragment.NetSlum.Networking.Objects;
using Fragment.NetSlum.Networking.Packets.Response.ChatLobby;
using Fragment.NetSlum.Networking.Sessions;
using Fragment.NetSlum.Persistence;
using Fragment.NetSlum.Persistence.Entities;
using Fragment.NetSlum.TcpServer.Extensions;
using Microsoft.EntityFrameworkCore;

namespace Fragment.NetSlum.Networking.Packets.Request.ChatLobby;

[FragmentPacket(MessageType.Data, OpCodes.DataLobbyGetServersGetList)]
public class GetLobbyServerListRequest : BaseRequest
public partial class GetLobbyServerListRequest : BaseRequest
{
private readonly FragmentContext _database;

Expand All @@ -37,9 +39,10 @@ public override ValueTask<ICollection<FragmentMessage>> GetResponse(FragmentTcpS
.AsNoTracking()
.FirstOrDefault(c => c.Id == categoryId);


var areaServers = session.Server.Sessions
.Cast<FragmentTcpSession>()
.Where(s => s.IsAreaServer)
.Where(s => s.IsAreaServer && CategoryFilter(category, s.AreaServerInfo!.ServerName))
.ToArray();

var responses = new List<FragmentMessage>();
Expand All @@ -62,7 +65,7 @@ public override ValueTask<ICollection<FragmentMessage>> GetResponse(FragmentTcpS
.SetExternalAddress((clientIpMatchesPrivate ? server.AreaServerInfo!.PrivateConnectionEndpoint : server.AreaServerInfo!.PublicConnectionEndpoint)!)
.SetDetails(server.AreaServerInfo.Detail)
.SetPlayerCount(server.AreaServerInfo.CurrentPlayerCount)
.SetServerName(server.AreaServerInfo.ServerName)
.SetServerName(FormatServerName(server.AreaServerInfo.ServerName))
.Build());
}

Expand All @@ -87,4 +90,32 @@ private ICollection<FragmentMessage> HandleCategories()

return responses;
}

[GeneratedRegex(@"^(.*)\|(.*)$", RegexOptions.Compiled, 1000)]
private static partial Regex CategorySeparatorRegex();

private static string FormatServerName(string serverName)
{
var match = CategorySeparatorRegex().Match(serverName);

return match.Success ? match.Groups[2].Value : serverName;
}

private static bool CategoryFilter(AreaServerCategory? category, string serverName)
{
if (category == null)
{
return false;
}

var match = CategorySeparatorRegex().Match(serverName);

// If its the main category and theres no category specified, include it as well
if (category.Id == 1 && (!match.Success || match.Groups[1].Value.Equals(category.CategoryName)))
{
return true;
}

return match.Success && match.Groups[1].Value.Equals(category.CategoryName);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,18 @@ public RegisterCharacterCommandHandler(FragmentContext database)

PlayerAccount? playerAccount = await _database.PlayerAccounts.FirstOrDefaultAsync(p => p.SaveId == characterInfo.SaveId, cancellationToken);

character = CharacterInfoMapper.MapOrCreate(characterInfo, character);

// If a player account was found when looking up the save ID, use that player account instead of persisting a new one
if (playerAccount != null)
// If the player account is missing, attempt to create it now.
if (playerAccount == null)
{
character.PlayerAccount = playerAccount;
playerAccount = new PlayerAccount
{
SaveId = characterInfo.SaveId,
};
}

character = CharacterInfoMapper.MapOrCreate(characterInfo, character);

character.PlayerAccount = playerAccount;
character.LastLoginAt = DateTime.UtcNow;

_database.Update(character);
Expand Down

0 comments on commit d657272

Please sign in to comment.