Skip to content

Commit

Permalink
Initial WebSocket implementation done
Browse files Browse the repository at this point in the history
  • Loading branch information
nexus4880 committed May 23, 2024
1 parent 54392c8 commit 73a6829
Show file tree
Hide file tree
Showing 6 changed files with 131 additions and 0 deletions.
2 changes: 2 additions & 0 deletions Fika.Core/FikaPlugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
using Fika.Core.UI.Models;
using Fika.Core.UI.Patches;
using Fika.Core.Utils;
using Fika.Core.WebSocket;
using System;
using System.Collections;
using System.Collections.Generic;
Expand Down Expand Up @@ -190,6 +191,7 @@ protected void Awake()

SetupConfig();

new WebSocketMessageReceivedPatch().Enable();
new FikaVersionLabel_Patch().Enable();
new DisableReadyButton_Patch().Enable();
new DisableInsuranceReadyButton_Patch().Enable();
Expand Down
45 changes: 45 additions & 0 deletions Fika.Core/WebSocket/FikaWebSocketReceiver.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
using Fika.Core.WebSocket.Models;
using Newtonsoft.Json.Linq;

namespace Fika.Core.WebSocket
{
internal class FikaWebSocketReceiver : IWebSocketNotificationReceiver
{
public bool OnWebSocketNotificationReceived(string type, JObject payload)
{
switch (type)
{
case "fika_match_created":
{
HandleFikaMatchCreated(payload);

return true;
}
case "fika_match_ended":
{
HandleFikaMatchEnded(payload);

return true;
}
}

return false;
}

private void HandleFikaMatchCreated(JObject payload)
{
var matchCreatedData = payload.ToObject<FikaMatchCreatedWebSocketModel>();
NotificationManagerClass.DisplayMessageNotification(
$"<color=green>{matchCreatedData.HostUsername}</color> is hosting on <color=yellow>{matchCreatedData.LocationId.Localized()}</color>"
);
}

private void HandleFikaMatchEnded(JObject payload)
{
var matchEndedData = payload.ToObject<FikaMatchEndedWebSocketModel>();
NotificationManagerClass.DisplayMessageNotification(
$"<color=#FF6A00>{matchEndedData.MatchId} has ended</color>"
);
}
}
}
12 changes: 12 additions & 0 deletions Fika.Core/WebSocket/IWebSocketNotificationReceiver.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using Newtonsoft.Json.Linq;

namespace Fika.Core.WebSocket
{
public interface IWebSocketNotificationReceiver
{
/// <param name="type"></param>
/// <param name="payload"></param>
/// <returns>whether or not you handled the payload</returns>
bool OnWebSocketNotificationReceived(string type, JObject payload);
}
}
13 changes: 13 additions & 0 deletions Fika.Core/WebSocket/Models/FikaMatchCreatedWebSocketModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using Newtonsoft.Json;

namespace Fika.Core.WebSocket.Models
{
public struct FikaMatchCreatedWebSocketModel
{
[JsonProperty("hostUsername")]
public string HostUsername { get; set; }

[JsonProperty("locationId")]
public string LocationId { get; set; }
}
}
10 changes: 10 additions & 0 deletions Fika.Core/WebSocket/Models/FikaMatchEndedWebSocketModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using Newtonsoft.Json;

namespace Fika.Core.WebSocket.Models
{
public struct FikaMatchEndedWebSocketModel
{
[JsonProperty("matchId")]
public string MatchId { get; set; }
}
}
49 changes: 49 additions & 0 deletions Fika.Core/WebSocket/WebSocketMessageReceivedPatch.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
using Aki.Reflection.Patching;
using Newtonsoft.Json.Linq;
using System.Collections.Generic;
using System.Reflection;
using System.Text;

namespace Fika.Core.WebSocket
{
public class WebSocketMessageReceivedPatch : ModulePatch
{
public static HashSet<IWebSocketNotificationReceiver> Receivers { get; } = [];

static WebSocketMessageReceivedPatch()
{
Receivers.Add(new FikaWebSocketReceiver());
}

protected override MethodBase GetTargetMethod()
{
return typeof(NotificationManagerClass).GetMethod(nameof(NotificationManagerClass.method_6));
}

[PatchPrefix]
private static bool Prefix(byte[] bytes)
{
var text = Encoding.UTF8.GetString(bytes);
var jobject = JObject.Parse(text);
if (!jobject.TryGetValue("type", out var typeToken) || !jobject.TryGetValue("data", out var dataToken))
{
return true;
}

var typeValue = typeToken.Value<string>();
var payload = dataToken.Value<JObject>();
var clonedReceivers = new List<IWebSocketNotificationReceiver>(Receivers);
var wasHandled = false;
foreach (var receiver in clonedReceivers)
{
var thisHandled = receiver.OnWebSocketNotificationReceived(typeValue, payload);
if (thisHandled)
{
wasHandled = true;
}
}

return !wasHandled;
}
}
}

0 comments on commit 73a6829

Please sign in to comment.