Skip to content

Commit

Permalink
refactor guild lobby building
Browse files Browse the repository at this point in the history
  • Loading branch information
toobeeh committed Jan 23, 2025
1 parent ff2dce4 commit efef4bf
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 17 deletions.
2 changes: 1 addition & 1 deletion tobeh.Avallone.Server/Classes/Dto/GuildLobbyDto.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@
namespace tobeh.Avallone.Server.Classes.Dto;

[TranspilationSource]
public record GuildLobbyDto(string UserName, int CurrentPlayers, string Language, string Invite, bool Private);
public record GuildLobbyDto(string UserName, int CurrentPlayers, string Language, string LobbyId, bool Private);
4 changes: 2 additions & 2 deletions tobeh.Avallone.Server/Hubs/LobbyHubDrops.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public async Task<DropClaimResultDto> ClaimDrop(DropClaimDto dropClaim)
LeagueMode = leagueMode
});
var resultNotification = new DropClaimResultDto(username, claimResult.FirstClaim, claimResult.ClearedDrop,
claimResult.CatchMs, claimResult.LeagueWeight, dropClaim.DropId, leagueMode);
claimResult.CatchMs, claimResult.LeagueWeight, dropClaim.DropId, claimResult.LeagueMode);

/* log drop */
dropsClient.LogDropClaimAsync(new LogDropMessage
Expand All @@ -49,7 +49,7 @@ public async Task<DropClaimResultDto> ClaimDrop(DropClaimDto dropClaim)
DiscordId = discordId,
DropId = dropClaim.DropId,
EventDropId = claimResult.EventDropId,
LobbyKey = lobbyContext.OwnerClaim.LobbyId,
LobbyKey = lobbyContext.OwnerClaim.LobbyId
});

/* reward drop only if not in league mode */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,38 +13,76 @@ namespace tobeh.Avallone.Server.Quartz.GuildLobbyUpdater;
public class GuildLobbiesUpdaterJob(
ILogger<GuildLobbiesUpdaterJob> logger,
Lobbies.LobbiesClient lobbiesClient,
Members.MembersClient membersClient,
GuildLobbiesStore guildLobbiesStore,
IHubContext<GuildLobbiesHub, IGuildLobbiesReceiver> guildLobbiesHubContext
) : IJob
{
public async Task Execute(IJobExecutionContext context)
{
logger.LogTrace("Execute({context})", context);

/* get all lobbies with online members */
var memberLobbies = await lobbiesClient
.GetOnlineLobbyPlayers(new GetOnlinePlayersRequest())
.ToListAsync();

/* get all members that are online */
var memberLogins = memberLobbies.SelectMany(lobby => lobby.Members.Select(member => member.Login));
var members =await membersClient
.GetMembersByLogin(new GetMembersByLoginMessage { Logins = { memberLogins } })
.ToDictionaryAsync(member => member.Login);

/* get all skribbl lobby details */
var lobbyIds = memberLobbies.Select(lobby => lobby.LobbyId);
var lobbies = await lobbiesClient
.GetLobbiesById(new GetLobbiesByIdRequest { LobbyIds = { lobbyIds } })
.ToDictionaryAsync(lobby => lobby.SkribblState.LobbyId);

/* for each lobby member, get the lobby details and their connected guilds and add to guild lobbies */
var guildLobbies = new Dictionary<long, List<GuildLobbyDto>>();
foreach (var lobby in memberLobbies)
{
var lobbyDetails = lobbies[lobby.LobbyId];
foreach (var lobbyMember in lobby.Members)
{
var member = members[lobbyMember.Login];
var lobbyPlayer = lobbyDetails.SkribblState.Players
.FirstOrDefault(p => p.PlayerId == lobbyMember.LobbyPlayerId);
if (lobbyPlayer is null) continue;

/* get all guild lobbies links */
var lobbies = await lobbiesClient.GetLobbyLinks(new Empty()).ToListAsync();
var guildLobbies =
lobbies.GroupBy(lobby => lobby.GuildId)
.Select(group => new
var guildLobby = new GuildLobbyDto(
lobbyPlayer.Name,
lobbyDetails.SkribblState.Players.Count,
lobbyDetails.SkribblState.Settings.Language,
lobbyDetails.SkribblState.LobbyId,
lobbyDetails.SkribblState.OwnerId is not null
);

foreach (var server in member.ServerConnections)
{
Id = group.Key.ToString(),
Lobbies = group.Select(lobby => new GuildLobbyDto(lobby.Username, 1, "", lobby.Link, false))
.ToList() // TODO refactor valmar to get actual details for lobby
});

if(guildLobbies.TryGetValue(server, out var value)) value.Add(guildLobby);
else guildLobbies[server] = [guildLobby];
}
}
}

/* save updates to store and push to clients */
guildLobbiesStore.BeginReset();
var updates = guildLobbies.Select(async guild =>
{
var changes = guildLobbiesStore.SetLobbiesForGuild(guild.Id, guild.Lobbies);
var guildId = guild.Key.ToString();
var changes = guildLobbiesStore.SetLobbiesForGuild(guildId, guild.Value);
if (changes)
{
await guildLobbiesHubContext.Clients.Group(guild.Id)
.GuildLobbiesUpdated(new GuildLobbiesUpdatedDto(guild.Id, guild.Lobbies));
logger.LogDebug("Updated guild lobbies of {id}", guild.Id);
await guildLobbiesHubContext.Clients.Group(guildId)
.GuildLobbiesUpdated(new GuildLobbiesUpdatedDto(guildId, guild.Value));

logger.LogDebug("Updated guild lobbies of {id}", guildId);
}
}).ToList();

await Task.WhenAll(updates);
guildLobbiesStore.ResetUnchanged();
}
Expand Down

0 comments on commit efef4bf

Please sign in to comment.