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

update branch #314

Merged
merged 4 commits into from
Oct 8, 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
63 changes: 44 additions & 19 deletions src/Modules/LocalRecordsModule/Services/LocalRecordsService.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using EvoSC.Common.Interfaces;

Check warning on line 1 in src/Modules/LocalRecordsModule/Services/LocalRecordsService.cs

View workflow job for this annotation

GitHub Actions / Qodana for .NET

Redundant using directive

Using directive is not required by the code and can be safely removed
using EvoSC.Common.Interfaces.Models;
using EvoSC.Common.Interfaces.Services;
using EvoSC.Common.Interfaces.Themes;
Expand Down Expand Up @@ -29,7 +29,7 @@
IPlayerRecordsRepository playerRecordsRepository) : ILocalRecordsService
{
private const string WidgetName = "LocalRecordsModule.LocalRecordsWidget";

public async Task<ILocalRecord[]> GetLocalsOfCurrentMapAsync()
{
var currentMap = await mapService.GetCurrentMapAsync();
Expand All @@ -39,7 +39,7 @@
throw new InvalidOperationException("Failed to get current map");
}

var records = (IEnumerable<ILocalRecord>)await localRecordRepository.GetLocalRecordsOfMapByIdAsync(currentMap.Id);
IEnumerable<ILocalRecord> records = await localRecordRepository.GetLocalRecordsOfMapByIdAsync(currentMap.Id);
return records.ToArray();
}

Expand All @@ -64,7 +64,7 @@
var playerRecords = GetRecordsWithPlayer(player, records);
await transaction.SendManialinkAsync(player, WidgetName, new { currentPlayer = player, records = playerRecords });
}

await transaction.CommitAsync();
}
catch (Exception ex)
Expand All @@ -83,9 +83,9 @@
// player did not get a local record good enough to be registered
return;
}

var localRaceTime = RaceTime.FromMilliseconds(localRecord.Record.Score).ToString();

if (localRaceTime == null)
{
throw new InvalidOperationException($"Failed to convert {localRecord.Record.Score} to race time");
Expand All @@ -106,18 +106,43 @@

if (record.Score < oldRecord.Record.Score)
{
await server.InfoMessageAsync(new TextFormatter()
.AddText(record.Player.NickName)
.AddText(" improved the ")
.AddText($"{localRecord.Position}.", s => s.WithColor(themeManager.Theme.Info))
.AddText(" local record ")
.AddText(localRaceTime, s => s.WithColor(themeManager.Theme.Info))
.AddText(" (")
.AddText($"{oldRecord.Position}.", s => s.WithColor(themeManager.Theme.Info))
.AddText(" - ")
.AddText($"{localRecord.Position}.", s => s.WithColor(themeManager.Theme.Info))
.AddText(" )")
.ToString());
var timeDifference = RaceTime.FromMilliseconds(oldRecord.Record.Score - record.Score);
var timeDifferenceStr = timeDifference.ToString();

if (timeDifferenceStr == null)
{
throw new InvalidOperationException($"Failed to convert {timeDifference} to race time difference");
}

if (localRecord.Position < oldRecord.Position)
{
await server.InfoMessageAsync(new TextFormatter()
.AddText(record.Player.NickName)
.AddText(" claimed ")
.AddText($"{localRecord.Position}.", s => s.WithColor(themeManager.Theme.Info))
.AddText(" (from ")
.AddText($"{oldRecord.Position}.", s => s.WithColor(themeManager.Theme.Info))
.AddText(") local record ")
.AddText(localRaceTime, s => s.WithColor(themeManager.Theme.Info))
.AddText(" (-")
.AddText(timeDifferenceStr, s => s.WithColor(themeManager.Theme.Info))
.AddText(")")
.ToString());
}
else
{
await server.InfoMessageAsync(new TextFormatter()
.AddText(record.Player.NickName)
.AddText(" improved their ")
.AddText($"{localRecord.Position}.", s => s.WithColor(themeManager.Theme.Info))
.AddText(" local record ")
.AddText(localRaceTime, s => s.WithColor(themeManager.Theme.Info))
.AddText(" (-")
.AddText(timeDifferenceStr, s => s.WithColor(themeManager.Theme.Info))
.AddText(")")
.ToString());
}

await ShowWidgetToAllAsync();
}
else if (record.Score == localRecord.Record.Score)
Expand Down Expand Up @@ -151,7 +176,7 @@
{
return records;
}

var playerRecord = records.FirstOrDefault(r => r.Record.Player.Id == player.Id);
var topMaxRows = Math.Min(settings.MaxWidgetRows, records.Length);

Expand All @@ -166,7 +191,7 @@
{
return records[..topMaxRows];
}

// return top records + records around the player
var topRecords = records[..Math.Min(settings.WidgetShowTop, records.Length)];

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,15 @@ public Task OnBeginMatchAsync(object sender, EventArgs args)
}

[Subscribe(FlowControlEvent.MatchStarted)]
public Task OnMatchStarted(object sender, EventArgs args) => tracker.BeginMatchAsync();
public Task OnMatchStarted(object sender, EventArgs args)
{
if (settings.AutomaticTracking)
{
return Task.CompletedTask;
}

return tracker.BeginMatchAsync();
}

[Subscribe(FlowControlEvent.MatchEnded)]
public Task OnMatchEnded(object sender, EventArgs args) => tracker.EndMatchAsync();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,14 @@ public class SpectatorTargetInfoEventController(ISpectatorTargetInfoService spec
{
[Subscribe(GbxRemoteEvent.PlayerDisconnect)]
public Task OnPlayerDisconnectAsync(object sender, PlayerGbxEventArgs eventArgs) =>
spectatorTargetInfoService.RemovePlayerFromSpectatorsListAsync(eventArgs.Login);
spectatorTargetInfoService.RemovePlayerAsync(eventArgs.Login);

[Subscribe(GbxRemoteEvent.BeginMap)]
public Task OnBeginMapAsync(object sender, MapGbxEventArgs eventArgs) =>
spectatorTargetInfoService.UpdateIsTeamsModeAsync();
public async Task OnBeginMapAsync(object sender, MapGbxEventArgs eventArgs)
{
await spectatorTargetInfoService.DetectIsTeamsModeAsync();
await spectatorTargetInfoService.DetectIsTimeAttackModeAsync();
}

[Subscribe(ModeScriptEvent.WayPoint)]
public Task OnWayPointAsync(object sender, WayPointEventArgs wayPointEventArgs) =>
Expand All @@ -44,4 +47,16 @@ public async Task OnNewWarmUpRoundAsync(object sender, WarmUpRoundEventArgs roun
await spectatorTargetInfoService.FetchAndCacheTeamInfoAsync();
await spectatorTargetInfoService.ResetWidgetForSpectatorsAsync();
}

[Subscribe(ModeScriptEvent.WarmUpStart)]
public Task OnWarmUpStartAsync(object sender, EventArgs args) =>
spectatorTargetInfoService.UpdateIsTimeAttackModeAsync(true);

[Subscribe(ModeScriptEvent.WarmUpEnd)]
public Task OnWarmUpEndAsync(object sender, EventArgs args) =>
spectatorTargetInfoService.DetectIsTimeAttackModeAsync();

[Subscribe(ModeScriptEvent.GiveUp)]
public Task OnPlayerGiveUpAsync(object sender, PlayerUpdateEventArgs args) =>
spectatorTargetInfoService.ClearCheckpointsAsync(args.Login);
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public async Task ReportSpectatorTargetAsync(string targetLogin)
}
else
{
await spectatorTargetInfoService.RemovePlayerFromSpectatorsListAsync(spectatorLogin);
await spectatorTargetInfoService.RemovePlayerAsync(spectatorLogin);
await spectatorTargetInfoService.HideSpectatorInfoWidgetAsync(spectatorLogin);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@ public interface ISpectatorTargetInfoService
/// </summary>
/// <returns></returns>
public Task ClearCheckpointsAsync();

/// <summary>
/// Clears all registered checkpoint times of the given player.
/// </summary>
/// <returns></returns>
public Task ClearCheckpointsAsync(string playerLogin);

/// <summary>
/// Retrieve an IOnlinePlayer instance by their login.
Expand Down Expand Up @@ -59,9 +65,9 @@ public interface ISpectatorTargetInfoService
/// <summary>
/// Remove a player from the spectators list.
/// </summary>
/// <param name="spectatorLogin"></param>
/// <param name="playerLogin"></param>
/// <returns></returns>
public Task RemovePlayerFromSpectatorsListAsync(string spectatorLogin);
public Task RemovePlayerAsync(string playerLogin);

/// <summary>
/// Gets the logins of a players spectating the given target.
Expand Down Expand Up @@ -98,6 +104,12 @@ public interface ISpectatorTargetInfoService
/// <returns></returns>
public Dictionary<int, CheckpointsGroup> GetCheckpointTimes();

/// <summary>
/// Returns the current spectator targets.
/// </summary>
/// <returns></returns>
public Dictionary<string, IOnlinePlayer> GetSpectatorTargets();

/// <summary>
/// Resets the widget for all spectating players.
/// </summary>
Expand Down Expand Up @@ -166,10 +178,23 @@ public interface ISpectatorTargetInfoService
public Task FetchAndCacheTeamInfoAsync();

/// <summary>
/// Updates whether team mode is active or not.
/// Updates whether team mode is active.
/// </summary>
/// <returns></returns>
public Task DetectIsTeamsModeAsync();

/// <summary>
/// Detects whether time attack mode is active.
/// </summary>
/// <returns></returns>
public Task DetectIsTimeAttackModeAsync();

/// <summary>
/// Manually sets active state of time attack mode.
/// </summary>
/// <param name="isTimeAttack"></param>
/// <returns></returns>
public Task UpdateIsTeamsModeAsync();
public Task UpdateIsTimeAttackModeAsync(bool isTimeAttack);

/// <summary>
/// Hides the default game mode UI.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,16 @@ public int GetRank(string playerLogin)
{
return rank;
}

rank++;
}

return rank;
}

public bool ForgetPlayer(string playerLogin) =>
(from checkpointData in this
where checkpointData.player.GetLogin() == playerLogin
select this.Remove(checkpointData)
).FirstOrDefault();
}
Loading
Loading