Skip to content

Commit

Permalink
Force team module (#246)
Browse files Browse the repository at this point in the history
  • Loading branch information
snixtho authored Jul 26, 2024
1 parent fd29f91 commit 663d43d
Show file tree
Hide file tree
Showing 39 changed files with 730 additions and 13 deletions.
16 changes: 15 additions & 1 deletion EvoSC.sln
Original file line number Diff line number Diff line change
Expand Up @@ -116,10 +116,14 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "LocalRecordsModule", "src/M
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "LocalRecordsModule.Tests", "tests\Modules\LocalRecordsModule.Tests\LocalRecordsModule.Tests.csproj", "{7401429B-B842-4316-B7A2-B77E9AD966CB}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ForceTeamModule", "src/Modules/ForceTeamModule/ForceTeamModule.csproj", "{ACFF2B23-0518-493C-93A0-08CF9FD4F8E6}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TeamSettingsModule", "src\Modules\TeamSettingsModule\TeamSettingsModule.csproj", "{C10A11E5-4AD8-4229-81F1-57D7DC364E27}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TeamSettingsModule.Tests", "tests\Modules\TeamSettingsModule.Tests\TeamSettingsModule.Tests.csproj", "{B1745099-0081-4443-BF79-26A888765E16}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ForceTeamModule.Tests", "tests\Modules\ForceTeamModule.Tests\ForceTeamModule.Tests.csproj", "{DE9532E8-F0ED-400B-9592-AF8F74BC83EB}"
EndProject



Expand Down Expand Up @@ -223,7 +227,7 @@ Global
{B684B864-31BF-407E-9ED9-A95D32BC4FE2}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{B684B864-31BF-407E-9ED9-A95D32BC4FE2}.Debug|Any CPU.Build.0 = Debug|Any CPU
{B684B864-31BF-407E-9ED9-A95D32BC4FE2}.Release|Any CPU.ActiveCfg = Release|Any CPU
{B684B864-31BF-407E-9ED9-A95D32BC4FE2}.Release|Any CPU.Build.0 = Release|Any CPU
{B684B864-31BF-407E-9ED9-A95D32BC4FE2}.Release|Any CPU.Build.0 = Release|Any CPU
{BA828B35-E9B7-4F18-B139-7F000B267D4C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{BA828B35-E9B7-4F18-B139-7F000B267D4C}.Debug|Any CPU.Build.0 = Debug|Any CPU
{BA828B35-E9B7-4F18-B139-7F000B267D4C}.Release|Any CPU.ActiveCfg = Release|Any CPU
Expand Down Expand Up @@ -346,6 +350,10 @@ Global
{7401429B-B842-4316-B7A2-B77E9AD966CB}.Debug|Any CPU.Build.0 = Debug|Any CPU
{7401429B-B842-4316-B7A2-B77E9AD966CB}.Release|Any CPU.ActiveCfg = Release|Any CPU
{7401429B-B842-4316-B7A2-B77E9AD966CB}.Release|Any CPU.Build.0 = Release|Any CPU
{ACFF2B23-0518-493C-93A0-08CF9FD4F8E6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{ACFF2B23-0518-493C-93A0-08CF9FD4F8E6}.Debug|Any CPU.Build.0 = Debug|Any CPU
{ACFF2B23-0518-493C-93A0-08CF9FD4F8E6}.Release|Any CPU.ActiveCfg = Release|Any CPU
{ACFF2B23-0518-493C-93A0-08CF9FD4F8E6}.Release|Any CPU.Build.0 = Release|Any CPU
{C10A11E5-4AD8-4229-81F1-57D7DC364E27}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{C10A11E5-4AD8-4229-81F1-57D7DC364E27}.Debug|Any CPU.Build.0 = Debug|Any CPU
{C10A11E5-4AD8-4229-81F1-57D7DC364E27}.Release|Any CPU.ActiveCfg = Release|Any CPU
Expand All @@ -354,6 +362,10 @@ Global
{B1745099-0081-4443-BF79-26A888765E16}.Debug|Any CPU.Build.0 = Debug|Any CPU
{B1745099-0081-4443-BF79-26A888765E16}.Release|Any CPU.ActiveCfg = Release|Any CPU
{B1745099-0081-4443-BF79-26A888765E16}.Release|Any CPU.Build.0 = Release|Any CPU
{DE9532E8-F0ED-400B-9592-AF8F74BC83EB}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{DE9532E8-F0ED-400B-9592-AF8F74BC83EB}.Debug|Any CPU.Build.0 = Debug|Any CPU
{DE9532E8-F0ED-400B-9592-AF8F74BC83EB}.Release|Any CPU.ActiveCfg = Release|Any CPU
{DE9532E8-F0ED-400B-9592-AF8F74BC83EB}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down Expand Up @@ -408,7 +420,9 @@ Global
{098D1F9B-054D-4158-BB6C-AC908C4595F6} = {6D75D6A2-6ECD-4DE4-96C5-CAD7D134407A}
{1D8DBFC8-EC21-4DDA-9D88-DE7CA04C8449} = {DC47658A-F421-4BA4-B617-090A7DFB3900}
{7401429B-B842-4316-B7A2-B77E9AD966CB} = {6D75D6A2-6ECD-4DE4-96C5-CAD7D134407A}
{ACFF2B23-0518-493C-93A0-08CF9FD4F8E6} = {DC47658A-F421-4BA4-B617-090A7DFB3900}
{C10A11E5-4AD8-4229-81F1-57D7DC364E27} = {DC47658A-F421-4BA4-B617-090A7DFB3900}
{B1745099-0081-4443-BF79-26A888765E16} = {6D75D6A2-6ECD-4DE4-96C5-CAD7D134407A}
{DE9532E8-F0ED-400B-9592-AF8F74BC83EB} = {6D75D6A2-6ECD-4DE4-96C5-CAD7D134407A}
EndGlobalSection
EndGlobal
3 changes: 2 additions & 1 deletion src/EvoSC.Commands/CommandInteractionContext.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
using EvoSC.Commands.Interfaces;
using EvoSC.Common.Controllers.Context;
using EvoSC.Common.Interfaces;
using EvoSC.Common.Interfaces.Controllers;
using EvoSC.Common.Interfaces.Models;

namespace EvoSC.Commands;

public class CommandInteractionContext
(IOnlinePlayer player, IControllerContext context) : PlayerInteractionContext(player, context),
(IOnlinePlayer player, IServerClient server, IControllerContext context) : PlayerInteractionContext(player, server, context),
ICommandInteractionContext
{
public required IChatCommand CommandExecuted { get; init; }
Expand Down
10 changes: 9 additions & 1 deletion src/EvoSC.Commands/Middleware/CommandsMiddleware.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using EvoSC.Common.Remote.ChatRouter;
using EvoSC.Common.Util;
using EvoSC.Common.Util.ServerUtils;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;

namespace EvoSC.Commands.Middleware;
Expand Down Expand Up @@ -47,7 +48,14 @@ private async Task ExecuteCommandAsync(IChatCommand cmd, object[] args, ChatRout
{
var (controller, context) = controllers.CreateInstance(cmd.ControllerType);

var playerInteractionContext = new CommandInteractionContext((IOnlinePlayer)routerContext.Player, context)
if (context.ServiceScope.Container == null)
{
throw new InvalidOperationException("Failed to instantiate controller context as the service container is null");
}

var contextServerClient = context.ServiceScope.Container.GetRequiredService<IServerClient>();

var playerInteractionContext = new CommandInteractionContext((IOnlinePlayer)routerContext.Player, contextServerClient, context)
{
CommandExecuted = cmd
};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using EvoSC.Common.Interfaces.Controllers;
using EvoSC.Common.Interfaces;
using EvoSC.Common.Interfaces.Controllers;
using EvoSC.Common.Interfaces.Models;

namespace EvoSC.Common.Controllers.Context;
Expand All @@ -14,9 +15,12 @@ public class PlayerInteractionContext : GenericControllerContext, IPlayerInterac
/// </summary>
public IOnlinePlayer Player { get; }

public PlayerInteractionContext(IOnlinePlayer player, IControllerContext context) : base(context)
public IServerClient Server { get; }

public PlayerInteractionContext(IOnlinePlayer player, IServerClient server, IControllerContext context) : base(context)
{
Player = player;
Server = server;
AuditEvent.CausedBy(player);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ namespace EvoSC.Common.Interfaces.Controllers;
public interface IPlayerInteractionContext : IGenericControllerContext
{
public IOnlinePlayer Player { get; }
public IServerClient Server { get; }
}
5 changes: 5 additions & 0 deletions src/EvoSC.Common/Interfaces/Models/IOnlinePlayer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,9 @@ public interface IOnlinePlayer : IPlayer
/// Flags assigned to the player.
/// </summary>
public IPlayerFlags Flags { get; }

/// <summary>
/// The team the player is part of.
/// </summary>
public PlayerTeam Team { get; }
}
25 changes: 25 additions & 0 deletions src/EvoSC.Common/Interfaces/Models/PlayerTeam.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using EvoSC.Common.Util.EnumIdentifier;

namespace EvoSC.Common.Interfaces.Models;

public enum PlayerTeam
{
/// <summary>
/// Player is in an unknown team, for example
/// in a mode that doesn't support teams.
/// </summary>
[Identifier(Name = "-1")]
Unknown = -1,

/// <summary>
/// The first/blue/left team.
/// </summary>
[Identifier(Name = "0")]
Team1 = 0,

/// <summary>
/// The second/red/right team.
/// </summary>
[Identifier(Name = "1")]
Team2 = 1
}
1 change: 1 addition & 0 deletions src/EvoSC.Common/Models/Players/OnlinePlayer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ public class OnlinePlayer : IOnlinePlayer
public IPlayerSettings Settings { get; set; }
public required PlayerState State { get; set; }
public IPlayerFlags Flags { get; set; }
public PlayerTeam Team { get; set; }

public OnlinePlayer(){}

Expand Down
10 changes: 6 additions & 4 deletions src/EvoSC.Common/Services/PlayerCacheService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -174,10 +174,11 @@ private async Task ForceUpdatePlayerInternalAsync(string accountId)
throw new PlayerNotFoundException(accountId, "Failed to fetch or create player in the database.");
}

return new OnlinePlayer(player)
return new OnlinePlayer(player)
{
State = onlinePlayerDetails.GetState(),
Flags = onlinePlayerInfo.GetFlags()
Flags = onlinePlayerInfo.GetFlags(),
Team = onlinePlayerDetails.TeamId == 0 ? PlayerTeam.Team1 : PlayerTeam.Team2
};
}

Expand Down Expand Up @@ -240,11 +241,12 @@ public async Task UpdatePlayerListAsync()
{
throw new PlayerNotFoundException(accountId, "Failed to fetch or create player in the database.");
}

var onlinePlayer = new OnlinePlayer(player)
{
State = onlinePlayerDetails.GetState(),
Flags = onlinePlayerInfo.GetFlags()
Flags = onlinePlayerInfo.GetFlags(),
Team = onlinePlayerDetails.TeamId == 0 ? PlayerTeam.Team1 : PlayerTeam.Team2
};

lock (_onlinePlayersMutex)
Expand Down
16 changes: 16 additions & 0 deletions src/EvoSC.Common/Util/ColorUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,22 @@ public string ToMlColor(string hexColor)

return $"<{r}, {g}, {b}>";
}

/// <summary>
/// Renders a color code in ManiaScript.
/// </summary>
/// <param name="hexColor">Color to render.</param>
/// <returns></returns>
public string ToTextColor(string hexColor)
{
var color = new Hex(hexColor).ToRgb();

var r = ((int)Math.Round(color.R / 255 * 15)).ToString("X");
var g = ((int)Math.Round(color.G / 255 * 15)).ToString("X");
var b = ((int)Math.Round(color.B / 255 * 15)).ToString("X");

return $"${r}{g}{b}";
}

/// <summary>
/// Set the opacity of a color using the alpha channel (4th byte).
Expand Down
14 changes: 14 additions & 0 deletions src/EvoSC.Common/Util/PlayerUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -201,4 +201,18 @@ public static IPlayerFlags GetFlags(this TmPlayerInfo player)
/// <returns></returns>
public static string GetLogin(this IPlayer player) =>
ConvertAccountIdToLogin(player.AccountId);

/// <summary>
/// Returns true if the player is part of team 1.
/// </summary>
/// <param name="player"></param>
/// <returns></returns>
public static bool IsTeam1(this IOnlinePlayer player) => player.Team == PlayerTeam.Team1;

/// <summary>
/// Returns true if player is part of team 2.
/// </summary>
/// <param name="player"></param>
/// <returns></returns>
public static bool IsTeam2(this IOnlinePlayer player) => player.Team == PlayerTeam.Team2;
}
3 changes: 2 additions & 1 deletion src/EvoSC.Manialinks/ManialinkInteractionContext.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using EvoSC.Common.Controllers.Context;
using EvoSC.Common.Interfaces;
using EvoSC.Common.Interfaces.Controllers;
using EvoSC.Common.Interfaces.Models;
using EvoSC.Manialinks.Interfaces;
Expand All @@ -7,7 +8,7 @@
namespace EvoSC.Manialinks;

public class ManialinkInteractionContext
(IOnlinePlayer player, IControllerContext context) : PlayerInteractionContext(player, context),
(IOnlinePlayer player, IServerClient server, IControllerContext context) : PlayerInteractionContext(player, server, context),
IManialinkInteractionContext
{
public required IManialinkActionContext ManialinkAction { get; init; }
Expand Down
3 changes: 2 additions & 1 deletion src/EvoSC.Manialinks/ManialinkInteractionHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,12 +81,13 @@ private async Task HandleManialinkPageAnswerAsync(object? sender, ManiaLinkPageA

var player = await GetPlayerAsync(args.Login);
var manialinkManager = context.ServiceScope.Container.GetRequiredService<IManialinkManager>();
var serverClient = context.ServiceScope.Container.GetRequiredService<IServerClient>();

var (actionParams, entryModel, validationResults) =
await ConvertRequestParametersAsync(action.FirstParameter, path, args.Entries,
context.ServiceScope.Container);

var manialinkInteractionContext = new ManialinkInteractionContext(player, context)
var manialinkInteractionContext = new ManialinkInteractionContext(player, serverClient, context)
{
ManialinkAction = new ManialinkActionContext {Action = action, EntryModel = entryModel},
ManialinkManager = manialinkManager
Expand Down
40 changes: 40 additions & 0 deletions src/EvoSC.Manialinks/Templates/Style/UIStyle.mt
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,46 @@
textsize="3"
/>

<style
class="text-xs"
textsize="{{ Theme.UI_FontSize*0.8 }}"
/>

<style
class="text-normal"
textsize="{{ Theme.UI_FontSize }}"
/>

<style
class="text-md"
textsize="{{ Theme.UI_FontSize*1.2 }}"
/>

<style
class="text-lg"
textsize="{{ Theme.UI_FontSize*1.5 }}"
/>

<style
class="text-xl"
textsize="{{ Theme.UI_FontSize*2 }}"
/>

<style
class="text-2xl"
textsize="{{ Theme.UI_FontSize*4 }}"
/>

<style
class="text-3xl"
textsize="{{ Theme.UI_FontSize*6 }}"
/>

<style
class="text-4xl"
textsize="{{ Theme.UI_FontSize*8 }}"
/>

<!-- Background -->
<style
class="bg-header"
Expand Down
1 change: 1 addition & 0 deletions src/EvoSC/EvoSC.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
<ProjectReference Include="..\EvoSC.Manialinks\EvoSC.Manialinks.csproj" />
<ProjectReference Include="..\EvoSC.Modules\EvoSC.Modules.csproj" />
<ProjectReference Include="..\Modules\ExampleModule\ExampleModule.csproj" />
<ProjectReference Include="..\Modules\ForceTeamModule\ForceTeamModule.csproj" />
<ProjectReference Include="..\Modules\LiveRankingModule\LiveRankingModule.csproj" />
<ProjectReference Include="..\Modules\LocalRecordsModule\LocalRecordsModule.csproj" />
<ProjectReference Include="..\Modules\MapListModule\MapListModule.csproj" />
Expand Down
2 changes: 2 additions & 0 deletions src/EvoSC/InternalModules.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using EvoSC.Modules.Official.CurrentMapModule;
using EvoSC.Modules.Official.ExampleModule;
using EvoSC.Modules.Official.FastestCpModule;
using EvoSC.Modules.Official.ForceTeamModule;
using EvoSC.Modules.Official.LiveRankingModule;
using EvoSC.Modules.Official.LocalRecordsModule;
using EvoSC.Modules.Official.MapListModule;
Expand Down Expand Up @@ -53,6 +54,7 @@ public static class InternalModules
typeof(MapQueueModule),
typeof(MapListModule),
typeof(LocalRecordsModule),
typeof(ForceTeamModule),
typeof(TeamSettingsModule)
};

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using EvoSC.Commands.Attributes;
using EvoSC.Commands.Interfaces;
using EvoSC.Common.Controllers;
using EvoSC.Common.Controllers.Attributes;
using EvoSC.Modules.Official.ForceTeamModule.Interfaces;
using EvoSC.Modules.Official.ForceTeamModule.Permissions;

namespace EvoSC.Modules.Official.ForceTeamModule.Controllers;

[Controller]
public class ForceTeamCommandController(IForceTeamService forceTeamService) : EvoScController<ICommandInteractionContext>
{
[ChatCommand("forceteam", "Force players to teams.", ForceTeamPermissions.ForcePlayerTeam)]
[CommandAlias("/ft", hide: true)]
public Task ForceTeamAsync() => forceTeamService.ShowWindowAsync(Context.Player);

[ChatCommand("autobalance", "Auto balance players into the teams.", ForceTeamPermissions.AutoTeamBalance)]
public Task AutoBalanceAsync() => forceTeamService.BalanceTeamsAsync();
}
Loading

0 comments on commit 663d43d

Please sign in to comment.