Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

260 commands to set points #270

Merged
merged 3 commits into from
Aug 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using EvoSC.Common.Controllers.Attributes;
using EvoSC.Common.Interfaces;
using EvoSC.Common.Interfaces.Localization;
using EvoSC.Common.Interfaces.Models;
using EvoSC.Modules.Official.MatchManagerModule.Events;
using EvoSC.Modules.Official.MatchManagerModule.Interfaces;
using EvoSC.Modules.Official.MatchManagerModule.Permissions;
Expand Down Expand Up @@ -70,4 +71,57 @@ public async Task SkipMapAsync()
.WithEventName(AuditEvents.SkipMap)
.Comment(_locale.Audit_MapSkipped);
}

[ChatCommand("setteamroundpoints", "Set the round points of a team.", MatchControlPermissions.SetTeamPoints)]
public async Task SetRoundPointsAsync(int team, int points)
{
var playerTeam = team == 0 ? PlayerTeam.Team1 : PlayerTeam.Team2;
await matchControl.SetTeamRoundPointsAsync(playerTeam, points);

Context.AuditEvent.Success()
.WithEventName(AuditEvents.TeamRoundPointsSet)
.HavingProperties(new { Points = points, Team = playerTeam });

await server.SuccessMessageAsync(Context.Player, $"Round points for team {playerTeam} was set to {points}.");
}

[ChatCommand("setteammappoints", "Set the map points of a team.", MatchControlPermissions.SetTeamPoints)]
public async Task SetMapPointsAsync(int team, int points)
{
var playerTeam = team == 0 ? PlayerTeam.Team1 : PlayerTeam.Team2;
await matchControl.SetTeamMapPointsAsync(playerTeam, points);

Context.AuditEvent.Success()
.WithEventName(AuditEvents.TeamMapPointsSet)
.HavingProperties(new { Points = points, Team = playerTeam });

await server.SuccessMessageAsync(Context.Player, $"Map points for team {playerTeam} was set to {points}.");
}

[ChatCommand("setteammatchpoints", "Set the match points of a team.", MatchControlPermissions.SetTeamPoints)]
public async Task SetMatchPointsAsync(int team, int points)
{
var playerTeam = team == 0 ? PlayerTeam.Team1 : PlayerTeam.Team2;
await matchControl.SetTeamMatchPointsAsync(playerTeam, points);

Context.AuditEvent.Success()
.WithEventName(AuditEvents.TeamMatchPointsSet)
.HavingProperties(new { Points = points, Team = playerTeam });

await server.SuccessMessageAsync(Context.Player, $"Match points for team {playerTeam} was set to {points}.");
}

[ChatCommand("pause", "Pause the current match.", MatchControlPermissions.PauseMatch)]
public async Task PauseMatchAsync()
{
await matchControl.PauseMatchAsync();
Context.AuditEvent.Success().WithEventName(AuditEvents.MatchPaused);
}

[ChatCommand("unpause", "Unpause the current match.", MatchControlPermissions.PauseMatch)]
public async Task UnpauseMatchAsync()
{
await matchControl.UnpauseMatchAsync();
Context.AuditEvent.Success().WithEventName(AuditEvents.MatchUnpaused);
}
}
17 changes: 16 additions & 1 deletion src/Modules/MatchManagerModule/Events/AuditEvents.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,20 @@ public enum AuditEvents
MatchSettingsLoaded,

[Identifier(Name = "MatchManager:ScriptSettingsSet")]
ScriptSettingsModified
ScriptSettingsModified,

[Identifier(Name = "MatchManager:TeamRoundPointsSet")]
TeamRoundPointsSet,

[Identifier(Name = "MatchManager:TeamRoundMapSet")]
TeamMapPointsSet,

[Identifier(Name = "MatchManager:TeamRoundMatchSet")]
TeamMatchPointsSet,

[Identifier(Name = "MatchManager:MatchPaused")]
MatchPaused,

[Identifier(Name = "MatchManager:MatchUnpaused")]
MatchUnpaused,
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
namespace EvoSC.Modules.Official.MatchManagerModule.Interfaces;
using EvoSC.Common.Interfaces.Models;

namespace EvoSC.Modules.Official.MatchManagerModule.Interfaces;

public interface IMatchControlService
{
Expand All @@ -23,4 +25,40 @@ public interface IMatchControlService
/// </summary>
/// <returns></returns>
public Task SkipMapAsync();

/// <summary>
/// Sets the round points for a team.
/// </summary>
/// <param name="team">Team to set the round points for.</param>
/// <param name="points">Points to set.</param>
/// <returns></returns>
public Task SetTeamRoundPointsAsync(PlayerTeam team, int points);

/// <summary>
/// Sets the map points for a team.
/// </summary>
/// <param name="team">Team to set the map points for.</param>
/// <param name="points">Points to set.</param>
/// <returns></returns>
public Task SetTeamMapPointsAsync(PlayerTeam team, int points);

/// <summary>
/// Sets the match points for a team.
/// </summary>
/// <param name="team">Team to set the match points for.</param>
/// <param name="points">Points to set.</param>
/// <returns></returns>
public Task SetTeamMatchPointsAsync(PlayerTeam team, int points);

/// <summary>
/// Pause the current match. Only works on round-based modes.
/// </summary>
/// <returns></returns>
public Task PauseMatchAsync();

/// <summary>
/// Unpause the current match. Only works on round-based modes.
/// </summary>
/// <returns></returns>
public Task UnpauseMatchAsync();
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,15 @@ public enum MatchControlPermissions
[Description("[Permission.SkipMap]")]
SkipMap,

[Description("[Permission.StartMatch]")]
StartMatch,

EndMatch
[Description("[Permission.EndMatch]")]
EndMatch,

[Description("[Permission.SetTeamPoints]")]
SetTeamPoints,

[Description("[Permission.PauseMatch]")]
PauseMatch
}
16 changes: 16 additions & 0 deletions src/Modules/MatchManagerModule/Services/MatchControlService.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using EvoSC.Common.Interfaces;
using EvoSC.Common.Interfaces.Models;
using EvoSC.Common.Services.Attributes;
using EvoSC.Common.Services.Models;
using EvoSC.Modules.Official.MatchManagerModule.Events;
Expand Down Expand Up @@ -42,4 +43,19 @@ public async Task SkipMapAsync()

await events.RaiseAsync(FlowControlEvent.MapSkipped, EventArgs.Empty);
}

public Task SetTeamRoundPointsAsync(PlayerTeam team, int points) =>
server.Remote.TriggerModeScriptEventArrayAsync("Trackmania.SetTeamPoints", ((int)team).ToString(), points.ToString(), "", "");

public Task SetTeamMapPointsAsync(PlayerTeam team, int points) =>
server.Remote.TriggerModeScriptEventArrayAsync("Trackmania.SetTeamPoints", ((int)team).ToString(), "", points.ToString(), "");

public Task SetTeamMatchPointsAsync(PlayerTeam team, int points) =>
server.Remote.TriggerModeScriptEventArrayAsync("Trackmania.SetTeamPoints", ((int)team).ToString(), "", "", points.ToString());

public Task PauseMatchAsync() =>
server.Remote.TriggerModeScriptEventArrayAsync("Maniaplanet.Pause.SetActive", "true");

public Task UnpauseMatchAsync() =>
server.Remote.TriggerModeScriptEventArrayAsync("Maniaplanet.Pause.SetActive", "false");
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using EvoSC.Common.Interfaces.Localization;
using EvoSC.Common.Interfaces.Models;
using EvoSC.Modules.Official.MatchManagerModule.Controllers;
using EvoSC.Modules.Official.MatchManagerModule.Events;
using EvoSC.Modules.Official.MatchManagerModule.Interfaces;
using EvoSC.Testing;
using EvoSC.Testing.Controllers;
Expand Down Expand Up @@ -73,4 +74,60 @@ public async Task Map_Is_Skipped_And_Audited()
_matchControl.Verify(m => m.SkipMapAsync(), Times.Once);
AuditEventBuilder.Verify(m => m.Success(), Times.Once);
}

[Theory]
[InlineData(0, PlayerTeam.Team1)]
[InlineData(1, PlayerTeam.Team2)]
public async Task Round_Points_Set_And_Audited(int team, PlayerTeam expectedTeam)
{
await Controller.SetRoundPointsAsync(team, 1337);

_matchControl.Verify(m => m.SetTeamRoundPointsAsync(expectedTeam, 1337));
AuditEventBuilder.Verify(m => m.Success());
AuditEventBuilder.Verify(m => m.WithEventName(AuditEvents.TeamRoundPointsSet));
}

[Theory]
[InlineData(0, PlayerTeam.Team1)]
[InlineData(1, PlayerTeam.Team2)]
public async Task Round_Map_Set_And_Audited(int team, PlayerTeam expectedTeam)
{
await Controller.SetMapPointsAsync(team, 1337);

_matchControl.Verify(m => m.SetTeamMapPointsAsync(expectedTeam, 1337));
AuditEventBuilder.Verify(m => m.Success());
AuditEventBuilder.Verify(m => m.WithEventName(AuditEvents.TeamMapPointsSet));
}

[Theory]
[InlineData(0, PlayerTeam.Team1)]
[InlineData(1, PlayerTeam.Team2)]
public async Task Round_Match_Set_And_Audited(int team, PlayerTeam expectedTeam)
{
await Controller.SetMatchPointsAsync(team, 1337);

_matchControl.Verify(m => m.SetTeamMatchPointsAsync(expectedTeam, 1337));
AuditEventBuilder.Verify(m => m.Success());
AuditEventBuilder.Verify(m => m.WithEventName(AuditEvents.TeamMatchPointsSet));
}

[Fact]
public async Task Pause_Match_Pauses_Match_And_Audits()
{
await Controller.PauseMatchAsync();

_matchControl.Verify(m => m.PauseMatchAsync());
AuditEventBuilder.Verify(m => m.Success());
AuditEventBuilder.Verify(m => m.WithEventName(AuditEvents.MatchPaused));
}

[Fact]
public async Task Unpause_Match_Unpauses_Match_And_Audits()
{
await Controller.UnpauseMatchAsync();

_matchControl.Verify(m => m.UnpauseMatchAsync());
AuditEventBuilder.Verify(m => m.Success());
AuditEventBuilder.Verify(m => m.WithEventName(AuditEvents.MatchUnpaused));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
using EvoSC.Common.Interfaces;
using EvoSC.Common.Interfaces.Models;
using EvoSC.Modules.Official.MatchManagerModule.Interfaces;
using EvoSC.Modules.Official.MatchManagerModule.Services;
using EvoSC.Testing;
using GbxRemoteNet.Interfaces;
using Moq;

namespace MatchManagerModule.Tests.Services;

public class MatchControlServiceTests
{
private (
IMatchControlService MatchControlService,
(Mock<IServerClient> Client, Mock<IGbxRemoteClient> Remote) Server,
Mock<IEventManager> EventManager
) NewMatchControlServiceMock()
{
var server = Mocking.NewServerClientMock();
var events = new Mock<IEventManager>();

var service = new MatchControlService(server.Client.Object, events.Object);

return (
service,
server,
events
);
}

[Theory]
[InlineData(PlayerTeam.Team1, "0")]
[InlineData(PlayerTeam.Team2, "1")]
public async Task SetTeamRoundPoints_Triggers_Correct_ModeScript_Callback(PlayerTeam team, string expectedTeam)
{
var mock = NewMatchControlServiceMock();

await mock.MatchControlService.SetTeamRoundPointsAsync(team, 123);

mock.Server.Remote.Verify(m => m.TriggerModeScriptEventArrayAsync("Trackmania.SetTeamPoints", expectedTeam, "123", "", ""));
}

[Theory]
[InlineData(PlayerTeam.Team1, "0")]
[InlineData(PlayerTeam.Team2, "1")]
public async Task SetTeamMapPoints_Triggers_Correct_ModeScript_Callback(PlayerTeam team, string expectedTeam)
{
var mock = NewMatchControlServiceMock();

await mock.MatchControlService.SetTeamMapPointsAsync(team, 123);

mock.Server.Remote.Verify(m => m.TriggerModeScriptEventArrayAsync("Trackmania.SetTeamPoints", expectedTeam, "", "123", ""));
}

[Theory]
[InlineData(PlayerTeam.Team1, "0")]
[InlineData(PlayerTeam.Team2, "1")]
public async Task SetTeamMatchPoints_Triggers_Correct_ModeScript_Callback(PlayerTeam team, string expectedTeam)
{
var mock = NewMatchControlServiceMock();

await mock.MatchControlService.SetTeamMatchPointsAsync(team, 123);

mock.Server.Remote.Verify(m => m.TriggerModeScriptEventArrayAsync("Trackmania.SetTeamPoints", expectedTeam, "", "", "123"));
}

[Fact]
public async Task PauseMatch_Triggers_Pause_ModeScript_Method()
{
var mock = NewMatchControlServiceMock();

await mock.MatchControlService.PauseMatchAsync();

mock.Server.Remote.Verify(m => m.TriggerModeScriptEventArrayAsync("Maniaplanet.Pause.SetActive", "true"));
}

[Fact]
public async Task UnpauseMatch_Triggers_Unpause_ModeScript_Method()
{
var mock = NewMatchControlServiceMock();

await mock.MatchControlService.UnpauseMatchAsync();

mock.Server.Remote.Verify(m => m.TriggerModeScriptEventArrayAsync("Maniaplanet.Pause.SetActive", "false"));
}
}
Loading