Skip to content

Commit

Permalink
Convert to API data types in DiscordIntegrationWorker
Browse files Browse the repository at this point in the history
This allows us to get the same results as the API (e.g. applying PSP path, cropped asset URLs, etc.) for free, avoiding code duplication.
  • Loading branch information
jvyden committed Jul 23, 2024
1 parent a1e35dd commit aa88b0c
Showing 1 changed file with 25 additions and 24 deletions.
49 changes: 25 additions & 24 deletions Refresh.GameServer/Workers/DiscordIntegrationWorker.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
using Bunkum.Core.Storage;
using Discord;
using Discord.Webhook;
using NotEnoughLogs;
using Refresh.GameServer.Authentication;
using Refresh.GameServer.Configuration;
using Refresh.GameServer.Database;
using Refresh.GameServer.Endpoints.ApiV3.DataTypes.Response.Levels;
using Refresh.GameServer.Endpoints.ApiV3.DataTypes.Response.Users;
using Refresh.GameServer.Endpoints.ApiV3.DataTypes.Response.Users.Photos;
using Refresh.GameServer.Types.Activity;
using Refresh.GameServer.Types.Levels;
using Refresh.GameServer.Types.Photos;
using Refresh.GameServer.Types.UserData;
using Refresh.GameServer.Types.UserData.Leaderboard;
using Refresh.GameServer.Types.Data;

namespace Refresh.GameServer.Workers;

Expand Down Expand Up @@ -44,14 +41,22 @@ private string GetAssetUrl(string hash)
return $"{this._externalUrl}/api/v3/assets/{hash}/image";
}

private Embed? GenerateEmbedFromEvent(Event @event, GameDatabaseContext database)
private Embed? GenerateEmbedFromEvent(Event @event, DataContext context)
{
EmbedBuilder embed = new();

GameLevel? level = @event.StoredDataType == EventDataType.Level ? database.GetLevelById(@event.StoredSequentialId!.Value) : null;
GameUser? user = @event.StoredDataType == EventDataType.User ? database.GetUserByObjectId(@event.StoredObjectId) : null;
GameSubmittedScore? score = @event.StoredDataType == EventDataType.Score ? database.GetScoreByObjectId(@event.StoredObjectId) : null;
GamePhoto? photo = @event.StoredDataType == EventDataType.Photo ? database.GetPhotoFromEvent(@event) : null;
ApiGameLevelResponse? level = @event.StoredDataType == EventDataType.Level ?
ApiGameLevelResponse.FromOld(context.Database.GetLevelById(@event.StoredSequentialId!.Value), context)
: null;
ApiGameUserResponse? user = @event.StoredDataType == EventDataType.User ?
ApiGameUserResponse.FromOld(context.Database.GetUserByObjectId(@event.StoredObjectId), context)
: null;
ApiGameScoreResponse? score = @event.StoredDataType == EventDataType.Score ?
ApiGameScoreResponse.FromOld(context.Database.GetScoreByObjectId(@event.StoredObjectId), context)
: null;
ApiGamePhotoResponse? photo = @event.StoredDataType == EventDataType.Photo ?
ApiGamePhotoResponse.FromOld(context.Database.GetPhotoFromEvent(@event), context)
: null;

if (photo != null)
level = photo.Level;
Expand Down Expand Up @@ -85,30 +90,26 @@ private string GetAssetUrl(string hash)
embed.WithDescription($"[{@event.User.Username}]({this._externalUrl}/u/{@event.User.UserId}) {description}");

if (photo != null)
{
embed.WithImageUrl(this.GetAssetUrl(photo.LargeAsset.IsPSP ? $"psp/{photo.LargeAsset.AssetHash}" : photo.LargeAsset.AssetHash));
} else if (level != null)
{
embed.WithThumbnailUrl(this.GetAssetUrl(level.GameVersion == TokenGame.LittleBigPlanetPSP ? $"psp/{level.IconHash}" : level.IconHash));
} else if (user != null)
{
embed.WithImageUrl(this.GetAssetUrl(photo.LargeHash));
else if (level != null)
embed.WithThumbnailUrl(this.GetAssetUrl(level.IconHash));
else if (user != null)
embed.WithThumbnailUrl(this.GetAssetUrl(user.IconHash));
}

embed.WithTimestamp(DateTimeOffset.FromUnixTimeMilliseconds(@event.Timestamp));
embed.WithAuthor(@event.User.Username, this.GetAssetUrl(@event.User.IconHash), $"{this._externalUrl}/u/{@event.UserId}");

return embed.Build();
}

public void DoWork(Logger logger, IDataStore dataStore, GameDatabaseContext database)
public void DoWork(DataContext context)
{
if (this._firstCycle)
{
this.DoFirstCycle();
}

DatabaseList<Event> activity = database.GetGlobalRecentActivity(new ActivityQueryParameters
DatabaseList<Event> activity = context.Database.GetGlobalRecentActivity(new ActivityQueryParameters
{
Timestamp = Now,
EndTimestamp = this._lastTimestamp,
Expand All @@ -123,7 +124,7 @@ public void DoWork(Logger logger, IDataStore dataStore, GameDatabaseContext data

IEnumerable<Embed> embeds = activity.Items
.Reverse() // events are descending
.Select(e => this.GenerateEmbedFromEvent(e, database))
.Select(e => this.GenerateEmbedFromEvent(e, context))
.Where(e => e != null)
.ToList()!;

Expand All @@ -132,6 +133,6 @@ public void DoWork(Logger logger, IDataStore dataStore, GameDatabaseContext data
ulong id = this._client.SendMessageAsync(embeds: embeds,
username: this._config.DiscordNickname, avatarUrl: this._config.DiscordAvatarUrl).Result;

logger.LogInfo(RefreshContext.Worker, $"Posted webhook containing {activity.Items.Count()} events with id {id}");
context.Logger.LogInfo(RefreshContext.Worker, $"Posted webhook containing {activity.Items.Count()} events with id {id}");
}
}

0 comments on commit aa88b0c

Please sign in to comment.