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.Linq;
using System.Threading.Tasks; using System.Threading.Tasks;
using MUnique.OpenMU.DataModel.Configuration; using MUnique.OpenMU.DataModel.Configuration;
using MUnique.OpenMU.GameLogic.PlayerActions.MiniGames;
/// <summary> /// <summary>
/// The context of the Heykel Savasi (statue war) event. /// The context of the Heykel Savasi (statue war) event.
@@ -51,6 +52,12 @@ public class HeykelSavasiContext : MiniGameContext
private readonly ConcurrentDictionary<Player, HeykelSavasiTeam> _teams = new(); 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> /// <summary>
/// Initializes a new instance of the <see cref="HeykelSavasiContext"/> class. /// Initializes a new instance of the <see cref="HeykelSavasiContext"/> class.
/// </summary> /// </summary>
@@ -125,6 +132,55 @@ public class HeykelSavasiContext : MiniGameContext
/// <returns>All players assigned to <paramref name="team"/>.</returns> /// <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); 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 /> /// <inheritdoc />
/// <remarks> /// <remarks>
/// Cancels the event if either team is empty at start (no opponent to fight); otherwise /// Cancels the event if either team is empty at start (no opponent to fight); otherwise

View File

@@ -0,0 +1,35 @@
// <copyright file="HeykelSavasiJoinAction.cs" company="MUnique">
// Licensed under the MIT License. See LICENSE file in the project root for full license information.
// </copyright>
namespace MUnique.OpenMU.GameLogic.PlayerActions.MiniGames;
using MUnique.OpenMU.GameLogic.MiniGames;
using MUnique.OpenMU.GameLogic.Views;
using MUnique.OpenMU.Interfaces;
/// <summary>
/// Player action which implements joining a team of the Heykel Savasi (statue war) event.
/// </summary>
public class HeykelSavasiJoinAction
{
/// <summary>
/// Tries to join the given player to the given team of the Heykel Savasi event, and warps the
/// player to the team's base spawn gate on success.
/// </summary>
/// <param name="player">The player who wants to join.</param>
/// <param name="team">The team which the player wants to join.</param>
/// <param name="context">The currently open Heykel Savasi context.</param>
public async ValueTask JoinAsync(Player player, HeykelSavasiTeam team, HeykelSavasiContext context)
{
if (await context.TryJoinTeamAsync(player, team).ConfigureAwait(false))
{
await player.WarpToAsync(context.GetTeamSpawnGate(team)).ConfigureAwait(false);
}
else
{
await player.InvokeViewPlugInAsync<IShowMessagePlugIn>(p =>
p.ShowMessageAsync("Takima simdi katilamadin (denge/durum).", MessageType.BlueNormal)).ConfigureAwait(false);
}
}
}

View File

@@ -0,0 +1,57 @@
// <copyright file="HeykelSavasiTeamSelectHandlerPlugIn.cs" company="MUnique">
// Licensed under the MIT License. See LICENSE file in the project root for full license information.
// </copyright>
namespace MUnique.OpenMU.GameServer.MessageHandler.MiniGames;
using System;
using System.Runtime.InteropServices;
using MUnique.OpenMU.GameLogic;
using MUnique.OpenMU.GameLogic.MiniGames;
using MUnique.OpenMU.GameLogic.PlayerActions.MiniGames;
using MUnique.OpenMU.Network.Packets.ClientToServer;
using MUnique.OpenMU.PlugIns;
/// <summary>
/// Handler for the Heykel Savasi (statue war) team-select request packets.
/// </summary>
[PlugIn]
[Guid("49D4C881-B0CD-44A5-9341-FDF1B432B602")]
public class HeykelSavasiTeamSelectHandlerPlugIn : IPacketHandlerPlugIn
{
/// <summary>
/// The game action which contains the logic to join a team of the event.
/// </summary>
private readonly HeykelSavasiJoinAction _joinAction = new();
/// <inheritdoc/>
public bool IsEncryptionExpected => true;
/// <inheritdoc/>
public byte Key => HeykelSavasiTeamSelect.Code;
/// <inheritdoc/>
public async ValueTask HandlePacketAsync(Player player, Memory<byte> packet)
{
HeykelSavasiTeamSelect request = packet;
var team = request.Team switch
{
1 => HeykelSavasiTeam.Red,
2 => HeykelSavasiTeam.Blue,
_ => HeykelSavasiTeam.None,
};
if (team == HeykelSavasiTeam.None)
{
return;
}
var context = player.GameContext is GameContext gameContext
? await gameContext.GetOpenHeykelSavasiAsync().ConfigureAwait(false)
: null;
if (context is not null)
{
await this._joinAction.JoinAsync(player, team, context).ConfigureAwait(false);
}
}
}