-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Serversync and toornament as internal modules.
- Loading branch information
1 parent
02fecce
commit b59e66b
Showing
76 changed files
with
4,538 additions
and
2 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
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
71 changes: 71 additions & 0 deletions
71
src/Modules/ServerSyncModule/Controllers/ServerSyncController.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,71 @@ | ||
using EvoSC.Common.Controllers; | ||
using EvoSC.Common.Controllers.Attributes; | ||
using EvoSC.Common.Events.Attributes; | ||
using EvoSC.Common.Interfaces; | ||
using EvoSC.Common.Interfaces.Controllers; | ||
using EvoSC.Common.Interfaces.Services; | ||
using EvoSC.Common.Remote; | ||
using EvoSC.Common.Remote.EventArgsModels; | ||
using EvoSC.Common.Util; | ||
using EvoSC.Modules.EvoEsports.ServerSyncModule.Events; | ||
using EvoSC.Modules.EvoEsports.ServerSyncModule.Events.Args; | ||
using EvoSC.Modules.EvoEsports.ServerSyncModule.Interfaces; | ||
using GbxRemoteNet.Events; | ||
|
||
namespace EvoSC.Modules.EvoEsports.ServerSyncModule.Controllers; | ||
|
||
[Controller] | ||
public class ServerSyncController : EvoScController<IEventControllerContext> | ||
{ | ||
private readonly IServerClient _server; | ||
private readonly ISyncService _sync; | ||
private readonly IPlayerManagerService _players; | ||
|
||
public ServerSyncController(IServerClient server, ISyncService sync, IPlayerManagerService players) | ||
{ | ||
_server = server; | ||
_sync = sync; | ||
_players = players; | ||
} | ||
|
||
[Subscribe(ServerSyncEvents.ChatMessage)] | ||
public async Task OnRemoteChatMessageAsync(object sender, ChatStateMessageEventArgs args) | ||
{ | ||
var player = await _players.GetOnlinePlayerAsync(args.ChatMessage.AccountId); | ||
var chatMessage = FormattingUtils.FormatPlayerChatMessage(player, args.ChatMessage.Message, false); | ||
await _server.Chat.SendChatMessageAsync(chatMessage); | ||
} | ||
|
||
[Subscribe(GbxRemoteEvent.PlayerChat)] | ||
public async Task OnLocalChatMessageAsync(object sender, PlayerChatGbxEventArgs args) | ||
{ | ||
var player = await _players.GetOnlinePlayerAsync(PlayerUtils.ConvertLoginToAccountId(args.Login)); | ||
await _sync.PublishChatMessageAsync(player, args.Text); | ||
} | ||
|
||
[Subscribe(GbxRemoteEvent.EndMap)] | ||
public Task OnEndMapAsync(object sender, MapGbxEventArgs args) => | ||
_sync.PublishMapFinishedAsync(); | ||
|
||
[Subscribe(ModeScriptEvent.EndRoundStart)] | ||
public Task OnEndRoundAsync(object sender, RoundEventArgs args) => | ||
_sync.PublishEndRoundAsync(); | ||
|
||
[Subscribe(GbxRemoteEvent.EndMatch)] | ||
public Task OnEndMatchAsync(object sender, EndMatchGbxEventArgs args) => | ||
_sync.PublishEndMatchAsync(); | ||
|
||
[Subscribe(ModeScriptEvent.WayPoint)] | ||
public async Task OnWayPointAsync(object sender, WayPointEventArgs args) | ||
{ | ||
var player = await _players.GetOnlinePlayerAsync(PlayerUtils.ConvertLoginToAccountId(args.Login)); | ||
await _sync.PublishWayPointAsync(player, args.RaceTime, args.CheckpointInRace, args.CurrentRaceCheckpoints, | ||
args.IsEndRace, args.Speed); | ||
} | ||
|
||
[Subscribe(ModeScriptEvent.Scores)] | ||
public async Task OnScoresAsync(object sender, ScoresEventArgs args) | ||
{ | ||
await _sync.PublishScoresAsync(args.Players, args.Teams, args.WinnerTeam, args.WinnerPlayer, args.Section, args.UseTeams); | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
src/Modules/ServerSyncModule/Events/Args/ChatStateMessageEventArgs.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,9 @@ | ||
using EvoSC.Modules.EvoEsports.ServerSyncModule.Interfaces.StateMessages; | ||
using EvoSC.Modules.EvoEsports.ServerSyncModule.Models.StateMessages; | ||
|
||
namespace EvoSC.Modules.EvoEsports.ServerSyncModule.Events.Args; | ||
|
||
public class ChatStateMessageEventArgs : EventArgs | ||
{ | ||
public required IChatStateStateMessage ChatMessage { get; init; } | ||
} |
6 changes: 6 additions & 0 deletions
6
src/Modules/ServerSyncModule/Events/Args/MapFinishedStateEventArgs.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,6 @@ | ||
namespace EvoSC.Modules.EvoEsports.ServerSyncModule.Events.Args; | ||
|
||
public class MapFinishedStateEventArgs : EventArgs | ||
{ | ||
|
||
} |
13 changes: 13 additions & 0 deletions
13
src/Modules/ServerSyncModule/Events/Args/Nats/NatsMessageEventArgs.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 NATS.Client; | ||
|
||
namespace EvoSC.Modules.EvoEsports.ServerSyncModule.Events.Args.Nats; | ||
|
||
public class NatsMessageEventArgs : EventArgs | ||
{ | ||
public required Msg Message { get; init; } | ||
} | ||
|
||
public class NatsMessageEventArgs<T> : NatsMessageEventArgs | ||
{ | ||
public T? Data { get; init; } | ||
} |
8 changes: 8 additions & 0 deletions
8
src/Modules/ServerSyncModule/Events/Args/PlayerStateUpdateEventArgs.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,8 @@ | ||
using EvoSC.Modules.EvoEsports.ServerSyncModule.Interfaces.StateMessages; | ||
|
||
namespace EvoSC.Modules.EvoEsports.ServerSyncModule.Events.Args; | ||
|
||
public class PlayerStateUpdateEventArgs : EventArgs | ||
{ | ||
public required IPlayerStateUpdateMessage PlayerState { get; set; } | ||
} |
8 changes: 8 additions & 0 deletions
8
src/Modules/ServerSyncModule/Events/Args/WaypointUpdateEventArgs.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,8 @@ | ||
using EvoSC.Modules.EvoEsports.ServerSyncModule.Interfaces.StateMessages; | ||
|
||
namespace EvoSC.Modules.EvoEsports.ServerSyncModule.Events.Args; | ||
|
||
public class WaypointUpdateEventArgs : EventArgs | ||
{ | ||
public required IWaypointStateMessage WaypointState { 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,19 @@ | ||
namespace EvoSC.Modules.EvoEsports.ServerSyncModule.Events; | ||
|
||
public enum ServerSyncEvents | ||
{ | ||
/// <summary> | ||
/// Always triggered whenever a message is received. | ||
/// </summary> | ||
PlayerStateUpdate, | ||
|
||
/// <summary> | ||
/// When a new chat message has been sent from one of the other servers. | ||
/// </summary> | ||
ChatMessage, | ||
|
||
/// <summary> | ||
/// When the map was finished on a server. | ||
/// </summary> | ||
MapFinished | ||
} |
37 changes: 37 additions & 0 deletions
37
src/Modules/ServerSyncModule/Interfaces/IKeyValueStoreService.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,37 @@ | ||
using NATS.Client.KeyValue; | ||
|
||
namespace EvoSC.Modules.EvoEsports.ServerSyncModule.Interfaces; | ||
|
||
public interface IKeyValueStoreService | ||
{ | ||
/// <summary> | ||
/// Creates a new key value entry. | ||
/// </summary> | ||
/// <param name="key"></param> | ||
/// <param name="value"></param> | ||
/// <returns>Revision number of the key</returns> | ||
ulong CreateEntry(string key, byte[] value); | ||
|
||
/// <summary> | ||
/// Updates an existing key value entry. | ||
/// </summary> | ||
/// <param name="key"></param> | ||
/// <param name="value"></param> | ||
/// <param name="revision"></param> | ||
void UpdateEntry(string key, byte[] value, ulong revision); | ||
|
||
/// <summary> | ||
/// Gets an existing key value entry. | ||
/// </summary> | ||
/// <param name="key"></param> | ||
/// <param name="revision"></param> | ||
/// <returns>Key and value of the entry</returns> | ||
KeyValueEntry GetEntry(string key, ulong revision); | ||
|
||
/// <summary> | ||
/// Deletes an existing key value entry. | ||
/// </summary> | ||
/// <param name="key"></param> | ||
/// <param name="revision"></param> | ||
void DeleteEntry(string key, ulong revision); | ||
} |
74 changes: 74 additions & 0 deletions
74
src/Modules/ServerSyncModule/Interfaces/INatsConnectionService.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,74 @@ | ||
using EvoSC.Modules.EvoEsports.ServerSyncModule.Events.Args; | ||
using EvoSC.Modules.EvoEsports.ServerSyncModule.Events.Args.Nats; | ||
using EvoSC.Modules.EvoEsports.ServerSyncModule.Interfaces.StateMessages; | ||
using EvoSC.Modules.EvoEsports.ServerSyncModule.Services; | ||
using NATS.Client; | ||
using NATS.Client.JetStream; | ||
using NATS.Client.KeyValue; | ||
|
||
namespace EvoSC.Modules.EvoEsports.ServerSyncModule.Interfaces; | ||
|
||
public interface INatsConnectionService | ||
{ | ||
/// <summary> | ||
/// Connection to the NATS server. | ||
/// </summary> | ||
public IConnection Connection { get; } | ||
|
||
/// <summary> | ||
/// The stream context. | ||
/// </summary> | ||
public IJetStream JetStream { get; } | ||
|
||
/// <summary> | ||
/// The key value context. | ||
/// </summary> | ||
public IKeyValue KeyValue { get; } | ||
|
||
/// <summary> | ||
/// The ID/Name of the current client. | ||
/// </summary> | ||
public string ClientId { get; } | ||
|
||
/// <summary> | ||
/// Establish a connection to NATS. | ||
/// </summary> | ||
/// <returns></returns> | ||
internal Task ConnectAsync(); | ||
|
||
/// <summary> | ||
/// Disconnect from NATS. | ||
/// </summary> | ||
/// <returns></returns> | ||
internal Task DisconnectAsync(); | ||
|
||
/// <summary> | ||
/// Publish a state message to the NATS stream. | ||
/// </summary> | ||
/// <param name="subject">The subject to publish to.</param> | ||
/// <param name="message">The message to publish.</param> | ||
/// <typeparam name="TStateMsg"></typeparam> | ||
/// <returns></returns> | ||
public Task PublishStateAsync<TStateMsg>(string subject, TStateMsg message) where TStateMsg : IStateMessage; | ||
|
||
/// <summary> | ||
/// Publish a state message to the NATS stream. | ||
/// </summary> | ||
/// <param name="subject">The subject to publish to.</param> | ||
/// <param name="message">The message to publish.</param> | ||
/// <typeparam name="TStateMsg"></typeparam> | ||
/// <returns></returns> | ||
public Task PublishStateAsync<TStateMsg>(Enum subject, TStateMsg message) where TStateMsg : IStateMessage; | ||
|
||
/// <summary> | ||
/// When a player state update message has been received. | ||
/// </summary> | ||
public event EventHandler<NatsMessageEventArgs<IPlayerStateUpdateMessage>> PlayerStateUpdated; | ||
|
||
/// <summary> | ||
/// When a chat message has been received. | ||
/// </summary> | ||
public event EventHandler<NatsMessageEventArgs<IChatStateStateMessage>> ChatMessageReceived; | ||
|
||
public event EventHandler<NatsMessageEventArgs<IStateMessage>> MapFinishedReceived; | ||
} |
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,9 @@ | ||
namespace EvoSC.Modules.EvoEsports.ServerSyncModule.Interfaces; | ||
|
||
public interface IServerState | ||
{ | ||
/// <summary> | ||
/// The time at which this state change occurred. | ||
/// </summary> | ||
public DateTime Timestamp { get; set; } | ||
} |
Oops, something went wrong.