Skip to content

Commit

Permalink
Serversync and toornament as internal modules.
Browse files Browse the repository at this point in the history
  • Loading branch information
AtomicLiquid committed Sep 29, 2024
1 parent 02fecce commit b59e66b
Show file tree
Hide file tree
Showing 76 changed files with 4,538 additions and 2 deletions.
14 changes: 14 additions & 0 deletions EvoSC.sln
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,10 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SpectatorCamModeModule", "s
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SpectatorCamModeModule.Tests", "tests\Modules\SpectatorCamModeModule.Tests\SpectatorCamModeModule.Tests.csproj", "{09A88256-8008-4085-A8E6-CA6DEFAC63E3}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ServerSyncModule", "src\Modules\ServerSyncModule\ServerSyncModule.csproj", "{AB39B49E-729D-42EF-93DE-98AD6329AAEC}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ToornamentModule", "src\Modules\ToornamentModule\ToornamentModule.csproj", "{6F12A74A-BC4B-426D-B094-638FCB266FC6}"
EndProject



Expand Down Expand Up @@ -447,6 +451,14 @@ Global
{09A88256-8008-4085-A8E6-CA6DEFAC63E3}.Debug|Any CPU.Build.0 = Debug|Any CPU
{09A88256-8008-4085-A8E6-CA6DEFAC63E3}.Release|Any CPU.ActiveCfg = Release|Any CPU
{09A88256-8008-4085-A8E6-CA6DEFAC63E3}.Release|Any CPU.Build.0 = Release|Any CPU
{AB39B49E-729D-42EF-93DE-98AD6329AAEC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{AB39B49E-729D-42EF-93DE-98AD6329AAEC}.Debug|Any CPU.Build.0 = Debug|Any CPU
{AB39B49E-729D-42EF-93DE-98AD6329AAEC}.Release|Any CPU.ActiveCfg = Release|Any CPU
{AB39B49E-729D-42EF-93DE-98AD6329AAEC}.Release|Any CPU.Build.0 = Release|Any CPU
{6F12A74A-BC4B-426D-B094-638FCB266FC6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{6F12A74A-BC4B-426D-B094-638FCB266FC6}.Debug|Any CPU.Build.0 = Debug|Any CPU
{6F12A74A-BC4B-426D-B094-638FCB266FC6}.Release|Any CPU.ActiveCfg = Release|Any CPU
{6F12A74A-BC4B-426D-B094-638FCB266FC6}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down Expand Up @@ -517,5 +529,7 @@ Global
{E4BF17BE-A517-4D3C-8DCA-DA99A100EBFE} = {6D75D6A2-6ECD-4DE4-96C5-CAD7D134407A}
{E9806703-6E24-4F05-A728-A04F7EB31749} = {DC47658A-F421-4BA4-B617-090A7DFB3900}
{09A88256-8008-4085-A8E6-CA6DEFAC63E3} = {6D75D6A2-6ECD-4DE4-96C5-CAD7D134407A}
{AB39B49E-729D-42EF-93DE-98AD6329AAEC} = {DC47658A-F421-4BA4-B617-090A7DFB3900}
{6F12A74A-BC4B-426D-B094-638FCB266FC6} = {DC47658A-F421-4BA4-B617-090A7DFB3900}
EndGlobalSection
EndGlobal
4 changes: 4 additions & 0 deletions src/EvoSC/EvoSC.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,14 @@
<PackageReference Include="linq2db" Version="5.3.2" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging" Version="8.0.0" />
<PackageReference Include="NATS.Client" Version="1.1.6" />
<PackageReference Include="SonarAnalyzer.CSharp" Version="9.15.0.81779">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="System.CommandLine" Version="2.0.0-beta4.22272.1" />
<PackageReference Include="System.CommandLine.Rendering" Version="0.4.0-alpha.22272.1" />
<PackageReference Include="toornamentapi" Version="1.2.0" />
</ItemGroup>

<ItemGroup>
Expand Down Expand Up @@ -49,12 +51,14 @@
<ProjectReference Include="..\Modules\MatchManagerModule\MatchManagerModule.csproj" />
<ProjectReference Include="..\Modules\ASayModule\ASayModule.csproj" />
<ProjectReference Include="..\Modules\ServerManagementModule\ServerManagementModule.csproj" />
<ProjectReference Include="..\Modules\ServerSyncModule\ServerSyncModule.csproj" />
<ProjectReference Include="..\Modules\SpectatorCamModeModule\SpectatorCamModeModule.csproj" />
<ProjectReference Include="..\Modules\SpectatorTargetInfoModule\SpectatorTargetInfoModule.csproj" />
<ProjectReference Include="..\Modules\TeamChatModule\TeamChatModule.csproj" />
<ProjectReference Include="..\Modules\TeamInfoModule\TeamInfoModule.csproj" />
<ProjectReference Include="..\Modules\TeamSettingsModule\TeamSettingsModule.csproj" />
<ProjectReference Include="..\Modules\GameModeUiModule\GameModeUiModule.csproj" />
<ProjectReference Include="..\Modules\ToornamentModule\ToornamentModule.csproj" />
<ProjectReference Include="..\Modules\WorldRecordModule\WorldRecordModule.csproj" />
<ProjectReference Include="..\Modules\MotdModule\MotdModule.csproj" />
<ProjectReference Include="..\Modules\Player\Player.csproj" />
Expand Down
6 changes: 5 additions & 1 deletion src/EvoSC/InternalModules.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using EvoSC.Common.Interfaces;
using EvoSC.Modules.EvoEsports.ServerSyncModule;
using EvoSC.Modules.EvoEsports.ToornamentModule;
using EvoSC.Modules.Interfaces;
using EvoSC.Modules.Official.ASayModule;
using EvoSC.Modules.Official.CurrentMapModule;
Expand Down Expand Up @@ -65,7 +67,9 @@ public static class InternalModules
typeof(TeamSettingsModule),
typeof(ServerManagementModule),
typeof(TeamInfoModule),
typeof(TeamChatModule)
typeof(TeamChatModule),
typeof(ServerSyncModule),
typeof(ToornamentModule)
];

/// <summary>
Expand Down
5 changes: 4 additions & 1 deletion src/EvoSC/Properties/launchSettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@
"profiles": {
"EvoSC": {
"commandName": "Project",
"commandLineArgs": "run"
"commandLineArgs": "run",
"environmentVariables": {
"EVOSC_SERVERSYNCMODULE_NATSSETTINGS_HOST": "116.202.26.112"
}
}
}
}
71 changes: 71 additions & 0 deletions src/Modules/ServerSyncModule/Controllers/ServerSyncController.cs
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);
}
}
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; }
}
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
{

}
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; }
}
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; }
}
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; }
}
19 changes: 19 additions & 0 deletions src/Modules/ServerSyncModule/Events/ServerSyncEvents.cs
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 src/Modules/ServerSyncModule/Interfaces/IKeyValueStoreService.cs
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 src/Modules/ServerSyncModule/Interfaces/INatsConnectionService.cs
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;
}
9 changes: 9 additions & 0 deletions src/Modules/ServerSyncModule/Interfaces/IServerState.cs
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; }
}
Loading

0 comments on commit b59e66b

Please sign in to comment.