Skip to content

Commit

Permalink
7TV integration
Browse files Browse the repository at this point in the history
  • Loading branch information
Marakusa committed Aug 26, 2023
1 parent 6370042 commit aa52a6c
Show file tree
Hide file tree
Showing 14 changed files with 347 additions and 80 deletions.
3 changes: 2 additions & 1 deletion CutOverlay/CutOverlay/App/OAuthOverlayApp.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public abstract class OAuthOverlayApp : OverlayApp
public abstract string CallbackAddress { get; }
public abstract string AuthorizationAddress { get; }
public abstract string Scopes { get; }

private string RegenerateStateToken()
{
_stateToken = new string(Enumerable.Repeat(Chars, 32).Select(s => s[Random.Next(s.Length)]).ToArray());
Expand Down Expand Up @@ -141,6 +141,7 @@ public void AuthCallback(string code, string state)
throw new Exception("Failed to verify the request");

AccessToken = code;

_ = UpdateAuthorizationAsync();
}

Expand Down
35 changes: 11 additions & 24 deletions CutOverlay/CutOverlay/Controllers/ConfigurationController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Security.Cryptography;
using System.Text;
using System.Text.Json;
using CutOverlay.Services;
using Microsoft.AspNetCore.Mvc;

namespace CutOverlay.Controllers;
Expand All @@ -12,9 +13,15 @@ public class ConfigurationController : ControllerBase
{
public static Dictionary<string, string?>? Configurations;
private readonly IConfiguration _configuration;
private readonly Spotify _spotify;
private readonly Twitch _twitch;
private readonly Pulsoid _pulsoid;

public ConfigurationController(IConfiguration configuration)
public ConfigurationController(IConfiguration configuration, Spotify spotify, Twitch twitch, Pulsoid pulsoid)
{
_spotify = spotify;
_twitch = twitch;
_pulsoid = pulsoid;
_configuration = configuration;
Configurations = new Dictionary<string, string?>();
DecryptAndReadConfig();
Expand Down Expand Up @@ -74,32 +81,12 @@ public async Task<IActionResult> SaveConfig([FromBody] Dictionary<string, string
{
try
{
bool spotifySettingsRefresh = false;

if (Configurations != null && config.ContainsKey("spotifyClientId") &&
Configurations.TryGetValue("spotifyClientId", out string? configuration))
spotifySettingsRefresh = config["spotifyClientId"] != configuration;

if (!spotifySettingsRefresh)
if (Configurations != null && config.ContainsKey("spotifyClientSecret") &&
Configurations.TryGetValue("spotifyClientSecret", out string? configuration1))
spotifySettingsRefresh = config["spotifyClientSecret"] != configuration1;

bool twitchSettingsRefresh = false;

if (Configurations != null && config.ContainsKey("twitchUsername") &&
Configurations.TryGetValue("twitchUsername", out string? configuration2))
twitchSettingsRefresh = config["twitchUsername"] != configuration2;

bool pulsoidSettingsRefresh = false;

if (Configurations != null && config.ContainsKey("pulsoid") &&
Configurations.TryGetValue("pulsoidAccessToken", out string? configuration3))
pulsoidSettingsRefresh = config["pulsoidAccessToken"] != configuration3;

EncryptAndSaveConfig(config);
DecryptAndReadConfig();

await _spotify.RefreshConfigurationsAsync();
await _twitch.RefreshConfigurationsAsync();
await _pulsoid.RefreshConfigurationsAsync();
/*if (spotifySettingsRefresh)
{
Spotify.Instance?.Unload();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using Newtonsoft.Json;

namespace CutOverlay.Models.Twitch;
namespace CutOverlay.Models.Twitch.SevenTv;

public class Badge
{
Expand Down
150 changes: 150 additions & 0 deletions CutOverlay/CutOverlay/Models/Twitch/SevenTv/SevenTvEmotes.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
using Newtonsoft.Json;

namespace CutOverlay.Models.Twitch.SevenTv;

public class Data
{
[JsonProperty("id")]
public string Id { get; set; }

[JsonProperty("name")]
public string Name { get; set; }

[JsonProperty("flags")]
public int Flags { get; set; }

[JsonProperty("lifecycle")]
public int Lifecycle { get; set; }

[JsonProperty("state")]
public List<string> State { get; set; }

[JsonProperty("listed")]
public bool Listed { get; set; }

[JsonProperty("animated")]
public bool Animated { get; set; }

[JsonProperty("owner")]
public Owner Owner { get; set; }

[JsonProperty("host")]
public Host Host { get; set; }

[JsonProperty("tags")]
public List<string> Tags { get; set; }
}

public class Emote
{
[JsonProperty("id")]
public string Id { get; set; }

[JsonProperty("name")]
public string Name { get; set; }

[JsonProperty("flags")]
public int Flags { get; set; }

[JsonProperty("timestamp")]
public object Timestamp { get; set; }

[JsonProperty("actor_id")]
public string ActorId { get; set; }

[JsonProperty("data")]
public Data Data { get; set; }
}

public class File
{
[JsonProperty("name")]
public string Name { get; set; }

[JsonProperty("static_name")]
public string StaticName { get; set; }

[JsonProperty("width")]
public int Width { get; set; }

[JsonProperty("height")]
public int Height { get; set; }

[JsonProperty("frame_count")]
public int FrameCount { get; set; }

[JsonProperty("size")]
public int Size { get; set; }

[JsonProperty("format")]
public string Format { get; set; }
}

public class Host
{
[JsonProperty("url")]
public string Url { get; set; }

[JsonProperty("files")]
public List<File> Files { get; set; }
}

public class Owner
{
[JsonProperty("id")]
public string Id { get; set; }

[JsonProperty("username")]
public string Username { get; set; }

[JsonProperty("display_name")]
public string DisplayName { get; set; }

[JsonProperty("avatar_url")]
public string AvatarUrl { get; set; }

[JsonProperty("style")]
public Style Style { get; set; }

[JsonProperty("roles")]
public List<string> Roles { get; set; }
}

public class SevenTvEmotes
{
[JsonProperty("id")]
public string Id { get; set; }

[JsonProperty("name")]
public string Name { get; set; }

[JsonProperty("flags")]
public int Flags { get; set; }

[JsonProperty("tags")]
public List<object> Tags { get; set; }

[JsonProperty("immutable")]
public bool Immutable { get; set; }

[JsonProperty("privileged")]
public bool Privileged { get; set; }

[JsonProperty("emotes")]
public List<Emote> Emotes { get; set; }

[JsonProperty("emote_count")]
public int EmoteCount { get; set; }

[JsonProperty("capacity")]
public int Capacity { get; set; }

[JsonProperty("owner")]
public Owner Owner { get; set; }
}

public class Style
{
[JsonProperty("color")]
public int? Color { get; set; }
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using Newtonsoft.Json;

namespace CutOverlay.Models.Twitch;
namespace CutOverlay.Models.Twitch.SevenTv;

public class Role
{
Expand Down
5 changes: 4 additions & 1 deletion CutOverlay/CutOverlay/Models/Twitch/UserChatMessage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public class UserChatMessage
public string? Message { get; set; }

[JsonProperty("messageEmotes")]
public List<EmoteData>? MessageEmotes { get; set; }
public List<EmoteData> MessageEmotes { get; set; }

[JsonProperty("userColor")]
public string? UserColor { get; set; }
Expand Down Expand Up @@ -42,6 +42,9 @@ public class EmoteData

[JsonProperty("endIndex")]
public int EndIndex { get; set; }

[JsonProperty("overlay")]
public bool Overlay { get; set; }
}

public class Flags
Expand Down
4 changes: 2 additions & 2 deletions CutOverlay/CutOverlay/Pages/Index.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
Id = "use7TV",
Value = "true"
})
@await Html.PartialAsync("_Checkbox", new InputModel
<!--@await Html.PartialAsync("_Checkbox", new InputModel
{
Label = "BetterTTV Integration",
Id = "userBetterTTV",
Expand All @@ -44,7 +44,7 @@
Label = "FrankerFaceZ Integration",
Id = "useFrankerFaceZ",
Value = "true"
})
})-->

<hr/>

Expand Down
2 changes: 1 addition & 1 deletion CutOverlay/CutOverlay/Pages/Index.cshtml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public IndexModel(ILogger<IndexModel> logger)
_logger = logger;
}

public string CopyrightString => "CUT Overlay (c) 2023 Markus Kannisto";
public string CopyrightString => "CUT Flags (c) 2023 Markus Kannisto";

public string? Version => Assembly.GetExecutingAssembly().GetName().Version?.ToString();

Expand Down
4 changes: 2 additions & 2 deletions CutOverlay/CutOverlay/Services/BeatSaberPlus.cs
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ private async Task ReceiveMessage(WebSocket webSocket, CancellationTokenSource c
await _overlayStatus.SaveStateAsync<BeatSaberPlus>(playbackState, 10);
break;
case "score":
BeatSaberPlusWebHookScoreEvent? scoreEvent = JsonConvert.DeserializeObject<BeatSaberPlusWebHookScoreEvent>(message);
/*BeatSaberPlusWebHookScoreEvent? scoreEvent = JsonConvert.DeserializeObject<BeatSaberPlusWebHookScoreEvent>(message);
BeatSaberAppScoreData? scoreData = null;
if (scoreEvent is { ScoreEvent: not null })
scoreData = new BeatSaberAppScoreData
Expand All @@ -186,7 +186,7 @@ private async Task ReceiveMessage(WebSocket webSocket, CancellationTokenSource c
Bpm = 0,
Pp = 0
};
await _overlayStatus.SaveScoreStateAsync(scoreData);
await _overlayStatus.SaveScoreStateAsync(scoreData);*/
break;
}

Expand Down
15 changes: 13 additions & 2 deletions CutOverlay/CutOverlay/Services/Pulsoid.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Net.WebSockets;
using System.Net.Sockets;
using System.Net.WebSockets;
using System.Text;
using CutOverlay.App;
using CutOverlay.Models;
Expand All @@ -12,19 +13,29 @@ public class Pulsoid : OverlayApp
private int _reconnectInterval = 1000;
private ClientWebSocket? _socket;
private string? _heartBeat;
private readonly ConfigurationService _configurationService;

public Pulsoid(ConfigurationService configurationService)
{
HttpClient = new HttpClient();
_configurationService = configurationService;

_socket = null;

_ = Task.Run(async () =>
{
await Start(await configurationService.FetchConfigurationsAsync());
await Start(await _configurationService.FetchConfigurationsAsync());
});
}

public async Task RefreshConfigurationsAsync()
{
await _socket?.CloseAsync(WebSocketCloseStatus.NormalClosure, "Restarting the service", CancellationToken.None)!;
_socket.Dispose();
_socket = null;
await Start(await _configurationService.FetchConfigurationsAsync());
}

public virtual Task Start(Dictionary<string, string?>? configurations)
{
Console.WriteLine("Pulsoid app starting...");
Expand Down
12 changes: 11 additions & 1 deletion CutOverlay/CutOverlay/Services/Spotify.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,25 +10,35 @@ public class Spotify : OAuthOverlayApp
{
private Timer? _statusTimer;
private readonly OverlayStatusService _overlayStatus;
private readonly ConfigurationService _configurationService;

public Spotify(OverlayStatusService overlayStatus, ConfigurationService configurationService)
{
HttpClient = new HttpClient();
_configurationService = configurationService;
_overlayStatus = overlayStatus;

AuthorizationTimer = null;
_statusTimer = null;

_ = Task.Run(async () =>
{
await Start(await configurationService.FetchConfigurationsAsync());
await Start(await _configurationService.FetchConfigurationsAsync());
});
}

public override string AuthApiUri => "https://accounts.spotify.com/api/token";
public override string CallbackAddress => $"http://localhost:{Globals.Port}/spotify/callback";
public override string AuthorizationAddress => "https://accounts.spotify.com/authorize";
public override string Scopes => "user-read-playback-state user-read-currently-playing user-modify-playback-state";

public async Task RefreshConfigurationsAsync()
{
AuthorizationTimer?.Stop();
_statusTimer?.Stop();

await Start(await _configurationService.FetchConfigurationsAsync());
}

public Task Start(Dictionary<string, string?>? configurations)
{
Expand Down
Loading

0 comments on commit aa52a6c

Please sign in to comment.