Skip to content

Commit

Permalink
Get max player for scoreboard dynamically
Browse files Browse the repository at this point in the history
- Use GameModeUiModule to hide default scoreboard
- Simplify code
  • Loading branch information
araszka committed Sep 30, 2024
1 parent 6b39129 commit 4cf4ef3
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 64 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,25 +10,24 @@
namespace EvoSC.Modules.Official.ScoreboardModule.Controllers;

[Controller]
public class ScoreboardEventController(IScoreboardNicknamesService nicknamesService)
public class ScoreboardEventController(
IScoreboardService scoreboardService,
IScoreboardNicknamesService nicknamesService
)
: EvoScController<IEventControllerContext>
{
[Subscribe(GbxRemoteEvent.PlayerConnect)]
public Task OnPlayerConnectAsync(object sender, PlayerGbxEventArgs args) =>
nicknamesService.AddNicknameByLoginAsync(args.Login);

[Subscribe(ModeScriptEvent.EndMapEnd)]
public async Task OnEndMapAsync(object sender, MapEventArgs args)
{
await nicknamesService.ClearNicknamesAsync();
}
public Task OnEndMapAsync(object sender, MapEventArgs args) =>
nicknamesService.ClearNicknamesAsync();

[Subscribe(GbxRemoteEvent.BeginMap)]
public async Task OnBeginMapAsync(object sender, MapGbxEventArgs args)
{
await nicknamesService.LoadNicknamesAsync();
await nicknamesService.SendNicknamesManialinkAsync();
}

[Subscribe(GbxRemoteEvent.PlayerConnect)]
public async Task OnPlayerConnectAsync(object sender, PlayerGbxEventArgs args)
{
await nicknamesService.AddNicknameByLoginAsync(args.Login);
await scoreboardService.SendScoreboardAsync();
}
}
75 changes: 25 additions & 50 deletions src/Modules/ScoreboardModule/Services/ScoreboardService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
using EvoSC.Common.Services.Attributes;
using EvoSC.Common.Services.Models;
using EvoSC.Manialinks.Interfaces;
using EvoSC.Modules.Official.GameModeUiModule.Enums;
using EvoSC.Modules.Official.GameModeUiModule.Interfaces;
using EvoSC.Modules.Official.ScoreboardModule.Config;
using EvoSC.Modules.Official.ScoreboardModule.Interfaces;

Expand All @@ -14,68 +16,41 @@ public class ScoreboardService(
IServerClient server,
IScoreboardNicknamesService nicknamesService,
IThemeManager themes,
IScoreboardSettings settings
IScoreboardSettings settings,
IGameModeUiModuleService gameModeUiModuleService
)
: IScoreboardService
{
public static readonly string ScoreboardTemplate = "ScoreboardModule.Scoreboard";
private static readonly string ScoreboardTemplate = "ScoreboardModule.Scoreboard";

public async Task SendScoreboardAsync()
{
await manialinks.SendPersistentManialinkAsync(ScoreboardTemplate, await GetDataAsync());
await nicknamesService.SendNicknamesManialinkAsync();
}

private Task<dynamic> GetDataAsync()
{
return Task.FromResult<dynamic>(new
{
settings,
MaxPlayers = 64, //TODO: make dynamic
PositionColors = new Dictionary<int, string>
{
{ 1, themes.Theme.Gold }, { 2, themes.Theme.Silver }, { 3, themes.Theme.Bronze }
},
});
}

public async Task HideNadeoScoreboardAsync()
private async Task<dynamic> GetDataAsync()
{
var hudSettings = new List<string>
{
@"{
""uimodules"": [
{
""id"": ""Race_ScoresTable"",
""position"": [-50,0],
""scale"": 1,
""visible"": false,
""visible_update"": true
}
]
}"
};
var maxPlayers = await server.Remote.GetMaxPlayersAsync();

await server.Remote.TriggerModeScriptEventArrayAsync("Common.UIModules.SetProperties", hudSettings.ToArray());
return new { settings, MaxPlayers = maxPlayers.CurrentValue };
}

public async Task ShowNadeoScoreboardAsync()
{
var hudSettings = new List<string>
{
@"{
""uimodules"": [
{
""id"": ""Race_ScoresTable"",
""position"": [-50,0],
""scale"": 1,
""visible"": true,
""visible_update"": true
}
]
}"
};
public Task HideNadeoScoreboardAsync() =>
gameModeUiModuleService.ApplyComponentSettingsAsync(
GameModeUiComponents.ScoresTable,
false,
0.0,
0.0,
1.0
);

await server.Remote.TriggerModeScriptEventArrayAsync("Common.UIModules.SetProperties", hudSettings.ToArray());
}
public Task ShowNadeoScoreboardAsync() =>
gameModeUiModuleService.ApplyComponentSettingsAsync(
GameModeUiComponents.ScoresTable,
true,
-50.0,
0.0,
1.0
);
}
7 changes: 5 additions & 2 deletions src/Modules/ScoreboardModule/Templates/Scoreboard.mt
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@

<property type="IScoreboardSettings" name="settings"/>
<property type="int" name="MaxPlayers" default="0"/>
<property type="Dictionary<int, string>" name="PositionColors"/>

<property type="double" name="backgroundBorderRadius" default="3.0"/>
<property type="double" name="headerHeight" default="14.0"/>
Expand Down Expand Up @@ -488,7 +487,11 @@
PlayerRowsFilled = -1;
CurrentScoreMode = -1;

{! string.Join("\n", PositionColors.Select(pc => $"PositionColors[{pc.Key}] = \"{pc.Value}\";")) !}
PositionColors = [
1 => "{{ Theme.Gold }}",
2 => "{{ Theme.Silver }}",
3 => "{{ Theme.Bronze }}"
];
***

*** OnLoop ***
Expand Down

0 comments on commit 4cf4ef3

Please sign in to comment.