Skip to content

Commit

Permalink
update branch (#314)
Browse files Browse the repository at this point in the history
Co-authored-by: snixtho <[email protected]>
Co-authored-by: Kjell Dankert <[email protected]>
Co-authored-by: Chris <[email protected]>
  • Loading branch information
4 people authored Oct 8, 2024
1 parent 80fb3d6 commit 94dd88f
Show file tree
Hide file tree
Showing 16 changed files with 428 additions and 110 deletions.
63 changes: 44 additions & 19 deletions src/Modules/LocalRecordsModule/Services/LocalRecordsService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public class LocalRecordsService(
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 @@ public async Task<ILocalRecord[]> GetLocalsOfCurrentMapAsync()
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 @@ public async Task ShowWidgetToAllAsync()
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 @@ public async Task UpdatePbAsync(IPlayerRecord record)
// 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 @@ await server.InfoMessageAsync(new TextFormatter()

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 @@ private ILocalRecord[] GetRecordsWithPlayer(IPlayer player, ILocalRecord[] recor
{
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 @@ private ILocalRecord[] GetRecordsWithPlayer(IPlayer player, ILocalRecord[] recor
{
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

0 comments on commit 94dd88f

Please sign in to comment.