-
Notifications
You must be signed in to change notification settings - Fork 56
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Initial WebSocket implementation done
- Loading branch information
Showing
6 changed files
with
131 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>" | ||
); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
13
Fika.Core/WebSocket/Models/FikaMatchCreatedWebSocketModel.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
10
Fika.Core/WebSocket/Models/FikaMatchEndedWebSocketModel.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |