feat(hs): team-select handler + join action with atomic balance reserve and base warp

This commit is contained in:
Acentech Dev
2026-07-20 23:42:07 +03:00
parent 7e954b610f
commit cd55e726f1
3 changed files with 148 additions and 0 deletions

View File

@@ -10,6 +10,7 @@ using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using MUnique.OpenMU.DataModel.Configuration;
using MUnique.OpenMU.GameLogic.PlayerActions.MiniGames;
/// <summary>
/// The context of the Heykel Savasi (statue war) event.
@@ -51,6 +52,12 @@ public class HeykelSavasiContext : MiniGameContext
private readonly ConcurrentDictionary<Player, HeykelSavasiTeam> _teams = new();
/// <summary>
/// Synchronizes the check-then-act sequence of <see cref="TryJoinTeamAsync"/>, so that concurrent
/// join attempts cannot both pass the balance check before either of them reserves a slot.
/// </summary>
private readonly object _teamLock = new();
/// <summary>
/// Initializes a new instance of the <see cref="HeykelSavasiContext"/> class.
/// </summary>
@@ -125,6 +132,55 @@ public class HeykelSavasiContext : MiniGameContext
/// <returns>All players assigned to <paramref name="team"/>.</returns>
public IEnumerable<Player> PlayersOf(HeykelSavasiTeam team) => this._teams.Where(kv => kv.Value == team).Select(kv => kv.Key);
/// <summary>
/// Atomically attempts to join the given player to the given team, reserving a slot under
/// <see cref="_teamLock"/> to fix a check-then-act race between the balance check and the team
/// assignment, then entering the mini game. If entering fails, the reservation is rolled back.
/// </summary>
/// <param name="player">The player who wants to join.</param>
/// <param name="team">The team which the player wants to join.</param>
/// <returns><c>true</c> if the player successfully joined <paramref name="team"/> and entered the mini game; otherwise, <c>false</c>.</returns>
public async ValueTask<bool> TryJoinTeamAsync(Player player, HeykelSavasiTeam team)
{
bool reserved;
lock (this._teamLock)
{
reserved = this.State == MiniGameState.Open
&& this.GetTeam(player) == HeykelSavasiTeam.None
&& this.CanJoin(team);
if (reserved)
{
// Reserve the slot synchronously so a concurrent join sees the updated team count.
this.AssignTeam(player, team);
}
}
if (!reserved)
{
return false;
}
var enterResult = await this.TryEnterAsync(player).ConfigureAwait(false);
if (enterResult != EnterResult.Success)
{
lock (this._teamLock)
{
// Roll back the reservation; the player never actually entered the mini game.
this.RemoveTeam(player);
}
return false;
}
return true;
}
/// <summary>
/// Removes the given player's team reservation, if any.
/// </summary>
/// <param name="player">The player.</param>
private void RemoveTeam(Player player) => this._teams.TryRemove(player, out _);
/// <inheritdoc />
/// <remarks>
/// Cancels the event if either team is empty at start (no opponent to fight); otherwise