Skip to content

Commit

Permalink
Display all the shlopas
Browse files Browse the repository at this point in the history
  • Loading branch information
stalengd committed Sep 18, 2024
1 parent ae42787 commit b958091
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 13 deletions.
30 changes: 19 additions & 11 deletions Content.Server/GameTicking/GameTicker.RoundFlow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -429,23 +429,31 @@ public void ShowRoundEndScoreboard(string text = "")
Connected = connected
};
listOfPlayerInfo.Add(playerEndRoundInfo);
// SS220 Round End Titles begin
var tiers = contentPlayerData?.SponsorInfo?.Tiers;
if (tiers is { Length: > 0 })
{
listOfSponsors.Add(new()
{
PlayerOOCName = playerEndRoundInfo.PlayerOOCName,
Tiers = tiers,
});
}
// SS220 Round End Titles end
}

// This ordering mechanism isn't great (no ordering of minds) but functions
var listOfPlayerInfoFinal = listOfPlayerInfo.OrderBy(pi => pi.PlayerOOCName).ToArray();
var sound = RoundEndSoundCollection == null ? null : _audio.GetSound(new SoundCollectionSpecifier(RoundEndSoundCollection));

// SS220 Round End Titles begin
var discordManager = IoCManager.Resolve<SS220.Discord.DiscordPlayerManager>();
void AddSponsorsTierFrom(Shared.SS220.Discord.SponsorTier tier, IReadOnlyList<string> names)
{
foreach (var sponsor in names)
{
listOfSponsors.Add(new(sponsor, [tier]));
}
}
if (discordManager.CachedSponsorUsers is { } sponsorUsers)
{
AddSponsorsTierFrom(Shared.SS220.Discord.SponsorTier.CriticalMassShlopa, sponsorUsers.CriticalMassShlopas);
AddSponsorsTierFrom(Shared.SS220.Discord.SponsorTier.GoldenShlopa, sponsorUsers.GoldenShlopas);
AddSponsorsTierFrom(Shared.SS220.Discord.SponsorTier.HugeShlopa, sponsorUsers.HugeShlopas);
AddSponsorsTierFrom(Shared.SS220.Discord.SponsorTier.BigShlopa, sponsorUsers.BigShlopas);
AddSponsorsTierFrom(Shared.SS220.Discord.SponsorTier.Shlopa, sponsorUsers.Shlopas);
}
// SS220 Round End Titles end

var roundEndMessageEvent = new RoundEndMessageEvent(
gamemodeTitle,
roundEndText,
Expand Down
22 changes: 21 additions & 1 deletion Content.Server/SS220/Discord/DiscordPlayerManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using System.Security.Cryptography;
using System.Text.Json;
using System.Text.Json.Serialization;
using System.Threading;
using System.Threading.Tasks;
using Content.Server.Database;
using Content.Shared.Corvax.CCCVars;
Expand All @@ -20,15 +21,19 @@

namespace Content.Server.SS220.Discord;

public sealed class DiscordPlayerManager : IPostInjectInit
public sealed class DiscordPlayerManager : IPostInjectInit, IDisposable
{
internal SponsorUsers? CachedSponsorUsers => _cachedSponsorUsers;

[Dependency] private readonly IServerDbManager _db = default!;
[Dependency] private readonly IPlayerManager _playerManager = default!;
[Dependency] private readonly IServerNetManager _netMgr = default!;
[Dependency] private readonly IConfigurationManager _cfg = default!;


private ISawmill _sawmill = default!;
private Timer? _statusRefreshTimer; // We should keep reference or else evil GC will kill our timer
private volatile SponsorUsers? _cachedSponsorUsers;
private readonly HttpClient _httpClient = new();

private string _apiUrl = string.Empty;
Expand All @@ -45,13 +50,28 @@ public void Initialize()
_httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", v);
},
true);

_statusRefreshTimer = new Timer(async _ =>
{
_cachedSponsorUsers = await GetSponsorUsers();
},
state: null,
dueTime: TimeSpan.FromSeconds(_cfg.GetCVar(CCCVars.DiscordSponsorsCacheLoadDelaySeconds)),
period: TimeSpan.FromSeconds(_cfg.GetCVar(CCCVars.DiscordSponsorsCacheRefreshIntervalSeconds))
);
}

void IPostInjectInit.PostInject()
{
_playerManager.PlayerStatusChanged += PlayerManager_PlayerStatusChanged;
}

public void Dispose()
{
_statusRefreshTimer?.Dispose();
_httpClient.Dispose();
}

private async void PlayerManager_PlayerStatusChanged(object? sender, SessionStatusEventArgs e)
{
if (e.NewStatus == SessionStatus.InGame)
Expand Down
18 changes: 17 additions & 1 deletion Content.Shared/Corvax/CCCVars/CCCVars.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using Robust.Shared.Configuration;
using Robust.Shared.Configuration;

namespace Content.Shared.Corvax.CCCVars;

Expand Down Expand Up @@ -140,4 +140,20 @@ public static readonly CVarDef<bool>
/// </summary>
public static readonly CVarDef<string> DiscordAuthApiKey =
CVarDef.Create("discord_auth.api_key", "", CVar.SERVERONLY | CVar.CONFIDENTIAL);

/*
* Discord Sponsors Cache
*/

/// <summary>
/// Delay in seconds before first load of the discord sponsors data.
/// </summary>
public static readonly CVarDef<float> DiscordSponsorsCacheLoadDelaySeconds =
CVarDef.Create("discord_sponsors_cache.load_delay_seconds", 10f, CVar.SERVERONLY);

/// <summary>
/// Interval in seconds between refreshes of the discord sponsors data.
/// </summary>
public static readonly CVarDef<float> DiscordSponsorsCacheRefreshIntervalSeconds =
CVarDef.Create("discord_sponsors_cache.refresh_interval_seconds", 60f * 60f * 4f, CVar.SERVERONLY);
}
6 changes: 6 additions & 0 deletions Content.Shared/GameTicking/SharedGameTicker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,12 @@ public partial struct RoundEndSponsorInfo
[DataField]
public string PlayerOOCName;
public SS220.Discord.SponsorTier[] Tiers;

public RoundEndSponsorInfo(string playerOOCName, SS220.Discord.SponsorTier[] tiers)
{
PlayerOOCName = playerOOCName;
Tiers = tiers;
}
}
// SS220 Round End Titles end

Expand Down

0 comments on commit b958091

Please sign in to comment.