Skip to content

Commit

Permalink
sponsor info sync with bot
Browse files Browse the repository at this point in the history
  • Loading branch information
Werzet committed Jan 2, 2024
1 parent d2fd725 commit fd01cf3
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 29 deletions.
6 changes: 4 additions & 2 deletions Content.Client/SS220/Discord/DiscordPlayerInfoManager.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// © SS220, An EULA/CLA with a hosting restriction, full text: https://raw.githubusercontent.com/SerbiaStrong-220/space-station-14/master/CLA.txt

using Content.Shared.SS220.Discord;
using Robust.Shared.Network;

Expand All @@ -23,9 +25,9 @@ private void UpdateSponsorStatus(MsgUpdatePlayerDiscordStatus message)
SponsorStatusChanged?.Invoke();
}

public SponsorTier GetSponsorTier()
public SponsorTier[] GetSponsorTier()
{
return _info?.Tier ?? SponsorTier.None;
return _info?.Tiers ?? Array.Empty<SponsorTier>();
}
}
}
46 changes: 32 additions & 14 deletions Content.Server/SS220/Discord/DiscordPlayerManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
using System.Net.Http.Headers;
using System.Net.Http.Json;
using System.Security.Cryptography;
using System.Text.Json;
using System.Text.Json.Serialization;
using System.Threading.Tasks;
using Content.Server.Database;
using Content.Shared.Corvax.CCCVars;
Expand Down Expand Up @@ -78,27 +80,43 @@ private async Task UpdateUserDiscordRolesStatus(SessionStatusEventArgs e)
return null;
}

var url = $"{_apiUrl}/sponsors/{userId}";
var response = await _httpClient.GetAsync(url);
try
{
var url = $"{_apiUrl}/userinfo/{userId.UserId}";
var response = await _httpClient.GetAsync(url);

if (response.StatusCode != HttpStatusCode.OK)
{
var errorText = await response.Content.ReadAsStringAsync();

if (response.StatusCode == HttpStatusCode.NotFound)
_sawmill.Error(
"Failed to get player sponsor info: [{StatusCode}] {Response}",
response.StatusCode,
errorText);

return null;
}

return await response.Content.ReadFromJsonAsync<DiscordSponsorInfo>(GetJsonSerializerOptions());
}
catch (Exception exc)
{
return null;
_sawmill.Error(exc.Message);
}

if (response.StatusCode != HttpStatusCode.OK)
{
var errorText = await response.Content.ReadAsStringAsync();
return null;
}

_sawmill.Error(
"Failed to get player sponsor info: [{StatusCode}] {Response}",
response.StatusCode,
errorText);
private static JsonSerializerOptions GetJsonSerializerOptions()
{
var opt = new JsonSerializerOptions
{
PropertyNameCaseInsensitive = true
};

return null;
}
opt.Converters.Add(new JsonStringEnumConverter());

return await response.Content.ReadFromJsonAsync<DiscordSponsorInfo>();
return opt;
}

/// <summary>
Expand Down
5 changes: 4 additions & 1 deletion Content.Shared/SS220/Discord/DiscordSponsorInfo.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
// © SS220, An EULA/CLA with a hosting restriction, full text: https://raw.githubusercontent.com/SerbiaStrong-220/space-station-14/master/CLA.txt

using Robust.Shared.Serialization;

namespace Content.Shared.SS220.Discord;

[Serializable, NetSerializable]
public sealed class DiscordSponsorInfo
{
public SponsorTier Tier { get; set; }
public SponsorTier[] Tiers { get; set; } = Array.Empty<SponsorTier>();
}
23 changes: 17 additions & 6 deletions Content.Shared/SS220/Discord/MsgUpdatePlayerDiscordStatus.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
// © SS220, An EULA/CLA with a hosting restriction, full text: https://raw.githubusercontent.com/SerbiaStrong-220/space-station-14/master/CLA.txt

using System.IO;
using Lidgren.Network;
using Robust.Shared.Network;
using Robust.Shared.Serialization;
using Robust.Shared.Utility;

namespace Content.Shared.SS220.Discord;

Expand All @@ -14,22 +18,29 @@ public override void ReadFromBuffer(NetIncomingMessage buffer, IRobustSerializer
{
if (buffer.ReadBoolean())
{
Info = new DiscordSponsorInfo
{
Tier = (SponsorTier) buffer.ReadUInt32()
};
buffer.ReadPadBits();
var length = buffer.ReadVariableInt32();
using var stream = new MemoryStream();
buffer.ReadAlignedMemory(stream, length);
serializer.DeserializeDirect<DiscordSponsorInfo>(stream, out var info);

Info = info;
}
}

public override void WriteToBuffer(NetOutgoingMessage buffer, IRobustSerializer serializer)
{
buffer.Write(Info is not null);
buffer.WritePadBits();

if (Info == null)
if (Info is null)
{
return;
}

buffer.Write((uint) Info.Tier);
var stream = new MemoryStream();
serializer.SerializeDirect(stream, Info);
buffer.WriteVariableInt32((int) stream.Length);
buffer.Write(stream.AsSpan());
}
}
17 changes: 11 additions & 6 deletions Content.Shared/SS220/Discord/SponsorTier.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
// © SS220, An EULA/CLA with a hosting restriction, full text: https://raw.githubusercontent.com/SerbiaStrong-220/space-station-14/master/CLA.txt

using Robust.Shared.Serialization;

namespace Content.Shared.SS220.Discord;

[Serializable, NetSerializable]
public enum SponsorTier
{
None = 0,
FirstTier = 1,
SecondTier = 2,
ThirdTier = 3,
FourthTier = 4,
FifthTier = 5
None,
Shlopa,
BigShlopa,
HugeShlopa,
GoldenShlopa,
CriticalMassShlopa
}

0 comments on commit fd01cf3

Please sign in to comment.