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

Add command /setteampoints to set both map and match points with a si… #290

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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 @@ -84,6 +84,19 @@ public async Task SetRoundPointsAsync(int team, int points)

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

[ChatCommand("setteampoints", "Set the round, map and match points of a team.", MatchControlPermissions.SetTeamPoints)]
public async Task SetPointsAsync(int team, int points)
{
var playerTeam = team == 0 ? PlayerTeam.Team1 : PlayerTeam.Team2;
await matchControl.SetTeamPointsAsync(playerTeam, points);

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

await server.SuccessMessageAsync(Context.Player, $"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)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using EvoSC.Common.Controllers;
using EvoSC.Common.Controllers.Attributes;
using EvoSC.Common.Events.Attributes;
using EvoSC.Common.Interfaces.Controllers;
using EvoSC.Common.Remote;
using EvoSC.Common.Remote.EventArgsModels;
using EvoSC.Modules.Official.MatchManagerModule.Interfaces;

namespace EvoSC.Modules.Official.MatchManagerModule.Controllers;

[Controller]
public class MatchManagerEventController(IMatchControlService matchControlService)
: EvoScController<IEventControllerContext>
{
[Subscribe(ModeScriptEvent.Scores)]
public async Task OnScoresAsync(object sender, ScoresEventArgs args)
{
var team1 = args.Teams.First();
var team2 = args.Teams.Skip(1).First();

await matchControlService.UpdateTeamPointsAsync(
team1?.MapPoints ?? 0,
team1?.MatchPoints ?? 0,
team2?.MapPoints ?? 0,
team2?.MatchPoints ?? 0
);
}
}
3 changes: 3 additions & 0 deletions src/Modules/MatchManagerModule/Events/AuditEvents.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ public enum AuditEvents
[Identifier(Name = "MatchManager:ScriptSettingsSet")]
ScriptSettingsModified,

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

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

Expand Down
24 changes: 24 additions & 0 deletions src/Modules/MatchManagerModule/Interfaces/IMatchControlService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,14 @@ public interface IMatchControlService
/// <returns></returns>
public Task SkipMapAsync();

/// <summary>
/// Sets the round, map and match 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 SetTeamPointsAsync(PlayerTeam team, int points);

/// <summary>
/// Sets the round points for a team.
/// </summary>
Expand Down Expand Up @@ -61,4 +69,20 @@ public interface IMatchControlService
/// </summary>
/// <returns></returns>
public Task UnpauseMatchAsync();

/// <summary>
/// Request the scores from the current match.
/// </summary>
/// <returns></returns>
public Task RequestScoresAsync();

/// <summary>
/// Updates the cached team points.
/// </summary>
/// <param name="team1MapPoints"></param>
/// <param name="team1MatchPoints"></param>
/// <param name="team2MapPoints"></param>
/// <param name="team2MatchPoints"></param>
/// <returns></returns>
public Task UpdateTeamPointsAsync(int team1MapPoints, int team1MatchPoints, int team2MapPoints, int team2MatchPoints);
}
9 changes: 8 additions & 1 deletion src/Modules/MatchManagerModule/MatchManagerModule.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
using EvoSC.Modules.Attributes;
using EvoSC.Modules.Interfaces;
using EvoSC.Modules.Official.MatchManagerModule.Interfaces;

namespace EvoSC.Modules.Official.MatchManagerModule;

[Module(IsInternal = true)]
public class MatchManagerModule : EvoScModule
public class MatchManagerModule(IMatchControlService matchControlService) : EvoScModule, IToggleable
{
public Task EnableAsync()
=> matchControlService.RequestScoresAsync();

public Task DisableAsync()
=> Task.CompletedTask;
}
54 changes: 46 additions & 8 deletions src/Modules/MatchManagerModule/Services/MatchControlService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,12 @@

namespace EvoSC.Modules.Official.MatchManagerModule.Services;

[Service(LifeStyle = ServiceLifeStyle.Transient)]
[Service(LifeStyle = ServiceLifeStyle.Singleton)]
public class MatchControlService(IServerClient server, IEventManager events) : IMatchControlService
{
private readonly Dictionary<PlayerTeam, int> _matchPoints = [];
private readonly Dictionary<PlayerTeam, int> _mapPoints = [];

public async Task StartMatchAsync()
{
await RestartMatchAsync();
Expand All @@ -33,29 +36,64 @@ public async Task EndRoundAsync()
public async Task RestartMatchAsync()
{
await server.Remote.RestartMapAsync();

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

public async Task SkipMapAsync()
{
await server.Remote.NextMapAsync();

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

public async Task SetTeamPointsAsync(PlayerTeam team, int points)
{
await server.Remote.TriggerModeScriptEventArrayAsync("Trackmania.SetTeamPoints", ((int)team).ToString(),
points.ToString(), points.ToString(), points.ToString());

_mapPoints[team] = points;
_matchPoints[team] = points;
}


public Task SetTeamRoundPointsAsync(PlayerTeam team, int points) =>
server.Remote.TriggerModeScriptEventArrayAsync("Trackmania.SetTeamPoints", ((int)team).ToString(), points.ToString(), "", "");
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 async Task SetTeamMapPointsAsync(PlayerTeam team, int points)
{
await server.Remote.TriggerModeScriptEventArrayAsync("Trackmania.SetTeamPoints", ((int)team).ToString(), "",
points.ToString(), _matchPoints.GetValueOrDefault(team).ToString());

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

public async Task SetTeamMatchPointsAsync(PlayerTeam team, int points)
{
await server.Remote.TriggerModeScriptEventArrayAsync("Trackmania.SetTeamPoints", ((int)team).ToString(), "",
_mapPoints.GetValueOrDefault(team).ToString(), points.ToString());

_matchPoints[team] = points;
}

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

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

public Task RequestScoresAsync() =>
server.Remote.TriggerModeScriptEventArrayAsync("Trackmania.GetScores");

public Task UpdateTeamPointsAsync(int team1MapPoints, int team1MatchPoints, int team2MapPoints,
int team2MatchPoints)
{
_mapPoints[PlayerTeam.Team1] = team1MapPoints;
_matchPoints[PlayerTeam.Team1] = team1MatchPoints;
_mapPoints[PlayerTeam.Team2] = team2MapPoints;
_matchPoints[PlayerTeam.Team2] = team2MatchPoints;

return Task.CompletedTask;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ public async Task OnScoresAsync(object sender, ScoresEventArgs eventArgs)
if (teamInfoService.ShouldUpdateTeamPoints(eventArgs.Section))
{
await teamInfoService.UpdatePointsAsync(
eventArgs.Teams.FirstOrDefault()?.MatchPoints ?? 0,
eventArgs.Teams.Skip(1).FirstOrDefault()?.MatchPoints ?? 0,
eventArgs.Teams.FirstOrDefault()?.MapPoints ?? 0,
eventArgs.Teams.Skip(1).FirstOrDefault()?.MapPoints ?? 0,
teamInfoService.ShouldExecuteManiaScript(eventArgs.Section)
);
}
Expand Down
Loading