Skip to content

Commit

Permalink
GH-284 improve team info behaviour (#285)
Browse files Browse the repository at this point in the history
Co-authored-by: snixtho <[email protected]>
  • Loading branch information
araszka and snixtho authored Aug 17, 2024
1 parent 9cb81bd commit 895bdf2
Show file tree
Hide file tree
Showing 14 changed files with 192 additions and 39 deletions.
3 changes: 3 additions & 0 deletions src/Modules/TeamInfoModule/Config/ITeamInfoSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ public interface ITeamInfoSettings

[Option(DefaultValue = 80.0), Description("Specifies the vertical top edge position of the wigdet. Values from -90 to 90 allowed.")]
public double Y { get; set; }

[Option(DefaultValue = 1.0), Description("Specifies the scale of the widget.")]
public double Scale { get; set; }

[Option(DefaultValue = false), Description("If enabled a smaller version of the widget is shown, without the large team name.")]
public bool CompactMode { get; set; }
Expand Down
36 changes: 30 additions & 6 deletions src/Modules/TeamInfoModule/Controllers/TeamInfoEventController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@
using EvoSC.Common.Controllers.Attributes;
using EvoSC.Common.Events.Attributes;
using EvoSC.Common.Interfaces.Controllers;
using EvoSC.Common.Models;
using EvoSC.Common.Remote;
using EvoSC.Common.Remote.EventArgsModels;
using EvoSC.Modules.Official.TeamInfoModule.Interfaces;
using EvoSC.Modules.Official.TeamSettingsModule.Events;
using EvoSC.Modules.Official.TeamSettingsModule.Events.EventArgs;
using GbxRemoteNet.Events;

namespace EvoSC.Modules.Official.TeamInfoModule.Controllers;
Expand Down Expand Up @@ -36,14 +37,25 @@ public async Task OnScoresAsync(object sender, ScoresEventArgs eventArgs)
await teamInfoService.SetModeIsTeamsAsync(true);
}

var teamInfos = eventArgs.Teams.ToList();
var team1Points = teamInfos[0]!.MatchPoints;
var team2Points = teamInfos[1]!.MatchPoints;
if (teamInfoService.ShouldUpdateTeamPoints(eventArgs.Section))
{
await teamInfoService.UpdatePointsAsync(
eventArgs.Teams.FirstOrDefault()?.MatchPoints ?? 0,
eventArgs.Teams.Skip(1).FirstOrDefault()?.MatchPoints ?? 0,
teamInfoService.ShouldExecuteManiaScript(eventArgs.Section)
);
}
}

if (eventArgs.Section is ModeScriptSection.EndRound or ModeScriptSection.Undefined)
[Subscribe(ModeScriptEvent.StartMatchStart)]
public async Task OnMatchStartAsync(object sender, MatchEventArgs args)
{
if (!await teamInfoService.GetModeIsTeamsAsync())
{
await teamInfoService.UpdatePointsAsync(team1Points, team2Points);
return;
}

await teamInfoService.UpdatePointsAsync(0, 0, false);
}

[Subscribe(ModeScriptEvent.StartRoundStart)]
Expand Down Expand Up @@ -78,4 +90,16 @@ public async Task OnEndMapAsync(object sender, MapGbxEventArgs args)

await teamInfoService.HideTeamInfoWidgetEveryoneAsync();
}

[Subscribe(TeamSettingsEvents.SettingsUpdated, IsAsync = true)]
public async Task OnTeamSettingsUpdatedAsync(object sender, TeamSettingsEventArgs args)
{
if (!await teamInfoService.GetModeIsTeamsAsync())
{
return;
}

await Task.Delay(500);
await teamInfoService.SendTeamInfoWidgetEveryoneAsync();
}
}
20 changes: 18 additions & 2 deletions src/Modules/TeamInfoModule/Interfaces/ITeamInfoService.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using EvoSC.Common.Util.MatchSettings.Models.ModeScriptSettingsModels;
using EvoSC.Common.Models;
using EvoSC.Common.Util.MatchSettings.Models.ModeScriptSettingsModels;

namespace EvoSC.Modules.Official.TeamInfoModule.Interfaces;

Expand Down Expand Up @@ -63,8 +64,9 @@ public interface ITeamInfoService
/// </summary>
/// <param name="team1Points"></param>
/// <param name="team2Points"></param>
/// <param name="executeManiaScript"></param>
/// <returns></returns>
public Task UpdatePointsAsync(int team1Points, int team2Points);
public Task UpdatePointsAsync(int team1Points, int team2Points, bool executeManiaScript);

/// <summary>
/// Tells whether the service is currently active for teams mode.
Expand All @@ -78,4 +80,18 @@ public interface ITeamInfoService
/// <param name="modeIsTeams"></param>
/// <returns></returns>
public Task SetModeIsTeamsAsync(bool modeIsTeams);

/// <summary>
/// Determines whether the team points need to be updated.
/// </summary>
/// <param name="section"></param>
/// <returns></returns>
public bool ShouldUpdateTeamPoints(ModeScriptSection section);

/// <summary>
/// Determines when to execute the ManiaScript that updates team points client side.
/// </summary>
/// <param name="section"></param>
/// <returns></returns>
public bool ShouldExecuteManiaScript(ModeScriptSection section);
}
16 changes: 15 additions & 1 deletion src/Modules/TeamInfoModule/Services/TeamInfoService.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using EvoSC.Common.Interfaces;
using EvoSC.Common.Models;
using EvoSC.Common.Services.Attributes;
using EvoSC.Common.Services.Models;
using EvoSC.Common.Util.MatchSettings.Models.ModeScriptSettingsModels;
Expand All @@ -22,6 +23,7 @@ ITeamInfoSettings settings
private int _currentRound;
private int _team1Points;
private int _team2Points;
private bool _executeManiaScript;

public async Task InitializeModuleAsync()
{
Expand Down Expand Up @@ -52,6 +54,7 @@ public async Task<dynamic> GetWidgetDataAsync()
roundNumber = _currentRound,
team1Points = _team1Points,
team2Points = _team2Points,
executeManiaScript = _executeManiaScript,
neutralEmblemUrl = modeScriptSettings.NeutralEmblemUrl
};
}
Expand Down Expand Up @@ -125,10 +128,11 @@ public async Task UpdateRoundNumberAsync(int round)
await SendTeamInfoWidgetEveryoneAsync();
}

public async Task UpdatePointsAsync(int team1Points, int team2Points)
public async Task UpdatePointsAsync(int team1Points, int team2Points, bool executeManiaScript)
{
_team1Points = team1Points;
_team2Points = team2Points;
_executeManiaScript = executeManiaScript;
await SendTeamInfoWidgetEveryoneAsync();
}

Expand All @@ -143,4 +147,14 @@ public Task SetModeIsTeamsAsync(bool modeIsTeams)

return Task.CompletedTask;
}

public bool ShouldUpdateTeamPoints(ModeScriptSection section)
{
return section is ModeScriptSection.EndRound or ModeScriptSection.PreEndRound or ModeScriptSection.Undefined;
}

public bool ShouldExecuteManiaScript(ModeScriptSection section)
{
return section is ModeScriptSection.PreEndRound;
}
}
1 change: 1 addition & 0 deletions src/Modules/TeamInfoModule/TeamInfoModule.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,6 @@
<Generator>ResXFileCodeGenerator</Generator>
<LastGenOutput>Localization.Designer.cs</LastGenOutput>
</EmbeddedResource>
<ProjectReference Include="..\TeamSettingsModule\TeamSettingsModule.csproj" />
</ItemGroup>
</Project>
4 changes: 3 additions & 1 deletion src/Modules/TeamInfoModule/Templates/Components/PointsBox.mt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<component>
<property type="string" name="id" default="evosc_points_box"/>
<property type="double" name="width" default="12.0"/>
<property type="double" name="height" default="10.0"/>
<property type="double" name="x" default="0.0"/>
Expand All @@ -13,7 +14,8 @@
bgcolor="{{ color }}"
/>

<label pos="{{ width / 2.0 }} {{ height / -2.0 + 0.5 }}"
<label id="{{ id }}"
pos="{{ width / 2.0 }} {{ height / -2.0 + 0.5 }}"
class="text-3xl"
text="{{ points }}"
textcolor="{{ Theme.UI_TextPrimary }}"
Expand Down
30 changes: 26 additions & 4 deletions src/Modules/TeamInfoModule/Templates/TeamInfoWidget.mt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<using namespace="GbxRemoteNet.Structs"/>
<using namespace="EvoSC.Modules.Official.TeamInfoModule.Config"/>

<import component="EvoSC.Style.UIStyle" as="UIStyle" />
<import component="EvoSC.Style.UIStyle" as="UIStyle"/>
<import component="TeamInfoModule.Components.RoundCounter" as="RoundCounter"/>
<import component="TeamInfoModule.Components.PointsBox" as="PointsBox"/>
<import component="TeamInfoModule.Components.BottomInfoBox" as="BottomInfoBox"/>
Expand All @@ -16,6 +16,7 @@
<property type="TmTeamInfo" name="team1"/>
<property type="TmTeamInfo" name="team2"/>
<property type="string?" name="infoBoxText"/>
<property type="bool" name="executeManiaScript"/>
<property type="int" name="roundNumber" default="-1"/>
<property type="int" name="team1Points" default="0"/>
<property type="int" name="team2Points" default="0"/>
Expand All @@ -27,7 +28,7 @@
<template>
<UIStyle/>

<frame pos="{{ settings.X }} {{ settings.Y }}">
<frame pos="{{ settings.X }} {{ settings.Y }}" scale="{{ settings.Scale }}">
<BottomInfoBox if="infoBoxText != null"
y="-10"
text="{{ infoBoxText }}"
Expand All @@ -36,7 +37,8 @@
<RoundCounter roundNumber="{{ roundNumber }}"/>

<!-- TEAM 1 -->
<PointsBox x="-10.0"
<PointsBox id="pointsBox1"
x="-10.0"
points="{{ team1Points }}"
color="{{ team1.RGB }}"
halign="right"
Expand Down Expand Up @@ -64,7 +66,8 @@
</frame>

<!-- TEAM 2 -->
<PointsBox x="10.0"
<PointsBox id="pointsBox2"
x="10.0"
points="{{ team2Points }}"
color="{{ team2.RGB }}"
/>
Expand All @@ -91,4 +94,23 @@
</frame>
</frame>
</template>

<script>
<!--
main() {
if({{ !executeManiaScript ? "True" : "False" }}){
return;
}

declare pointsBox1Label <=> (Page.MainFrame.GetFirstChild("pointsBox1") as CMlLabel);
declare pointsBox2Label <=> (Page.MainFrame.GetFirstChild("pointsBox2") as CMlLabel);

while(True){
pointsBox1Label.Value = ClanScores[1] ^ "";
pointsBox2Label.Value = ClanScores[2] ^ "";
sleep(50);
}
}
-->
</script>
</component>
3 changes: 3 additions & 0 deletions src/Modules/TeamInfoModule/info.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,6 @@ summary = "Displays teams and their points in a widget on the screen."
version = "1.0.0"
# The name of the author that created this module
author = "Evo"

[dependencies]
TeamSettingsModule = "1.0.0"
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
using EvoSC.Modules.Official.TeamSettingsModule.Models;

namespace EvoSC.Modules.Official.TeamSettingsModule.Events.EventArgs;

public class TeamSettingsEventArgs: System.EventArgs
{
public required TeamSettingsModel Settings { get; init; }
}
9 changes: 9 additions & 0 deletions src/Modules/TeamSettingsModule/Events/TeamSettingsEvents.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace EvoSC.Modules.Official.TeamSettingsModule.Events;

public enum TeamSettingsEvents
{
/// <summary>
/// Raised after team settings have been changed.
/// </summary>
SettingsUpdated
}
20 changes: 15 additions & 5 deletions src/Modules/TeamSettingsModule/Services/TeamSettingsService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,26 @@
using EvoSC.Common.Services.Attributes;
using EvoSC.Common.Services.Models;
using EvoSC.Manialinks.Interfaces;
using EvoSC.Modules.Official.TeamSettingsModule.Events;
using EvoSC.Modules.Official.TeamSettingsModule.Events.EventArgs;
using EvoSC.Modules.Official.TeamSettingsModule.Interfaces;
using EvoSC.Modules.Official.TeamSettingsModule.Models;

namespace EvoSC.Modules.Official.TeamSettingsModule.Services;

[Service(LifeStyle = ServiceLifeStyle.Singleton)]
public class TeamSettingsService(IServerClient server, IManialinkManager manialinks, Locale locale)
public class TeamSettingsService(
IServerClient server,
IManialinkManager manialinks,
IEventManager events,
Locale locale)
: ITeamSettingsService
{
private const string ClubLinkGeneratorUrl = "https://club-link.evotm.workers.dev";
public const string DefaultTeam1Name = "Blue";
public const string DefaultTeam2Name = "Red";
public const string DefaultTeam1Color = "00f";
public const string DefaultTeam2Color = "f00";
public static readonly string DefaultTeam1Name = "Blue";
public static readonly string DefaultTeam2Name = "Red";
public static readonly string DefaultTeam1Color = "00f";
public static readonly string DefaultTeam2Color = "f00";

public async Task<TeamSettingsModel> GetCurrentTeamSettingsModel()
{
Expand Down Expand Up @@ -51,6 +57,10 @@ public async Task SetTeamSettingsAsync(TeamSettingsModel teamSettings)
teamSettings.Team2SecondaryColor, teamSettings.Team2EmblemUrl);

await server.Remote.SetForcedClubLinksAsync(clubLinkUrlTeam1, clubLinkUrlTeam2);
await events.RaiseAsync(TeamSettingsEvents.SettingsUpdated, new TeamSettingsEventArgs
{
Settings = teamSettings
});
}

public Task<NameValueCollection> ParseClubLinkUrl(string? clubLinkUrl)
Expand Down
Loading

0 comments on commit 895bdf2

Please sign in to comment.