first commit

This commit is contained in:
2026-09-19 17:24:02 +08:00
commit afb343aa49
109 changed files with 13011 additions and 0 deletions
@@ -0,0 +1,214 @@
using OSM.Client.Autoload;
using OSM.Client.Contracts;
namespace OSM.Client.Diagnostics;
public partial class SoloVerticalSliceSmokeTest : Node
{
private GameSession _session = null!;
private BattleStore _store = null!;
private int _moves;
private bool _rejectionRecovered;
private bool _continued;
public override void _Ready()
{
_session = GetNode<GameSession>("/root/GameSession");
_store = GetNode<BattleStore>("/root/BattleStore");
_ = RunAsync();
}
private async Task RunAsync()
{
try
{
if (!await _session.ConnectAsync(new Uri("ws://localhost:11030/ws/solo")))
{
throw new InvalidOperationException("WebSocket connection failed.");
}
await WaitUntilAsync(() => _session.State == SessionState.Connected, 5.0);
await _store.StartGameAsync(new SoloStartOptions
{
CharacterCount = 2,
TeamMode = false,
TeamSize = 1,
MaxRound = 30,
MaxRespawnTimes = 0,
MaxScoreToWin = 0,
DecisionTimeoutSeconds = 30,
RequireContinue = true,
RoundDelayMs = 0,
DropItemsIntervalSeconds = 0,
Seed = 20260919
});
await WaitForDecisionAsync(SoloDecisionKinds.SelectCharacter, 10.0);
var character = _store.PendingDecision!
.ReadPayload<SelectCharacterPayload>()?
.Characters
.FirstOrDefault()
?? throw new InvalidOperationException("Server returned no selectable characters.");
await _store.SubmitDecisionAsync(new { characterGuid = character.Guid });
await WaitUntilAsync(
() => _store.Snapshot is { PlayerGuid.Length: > 0, Characters.Count: > 1, Map: not null },
10.0);
await WaitUntilAsync(
() => GetTree().GetNodesInGroup("battle_unit").Count == _store.Snapshot!.Characters.Count,
5.0);
var attacked = false;
for (var step = 0; step < 24 && (!attacked || !_continued); step++)
{
await WaitUntilAsync(() => _store.PendingDecision is not null, 45.0);
var decision = _store.PendingDecision!;
switch (decision.Kind)
{
case SoloDecisionKinds.ActionType:
await _store.SubmitDecisionAsync(new
{
actionType = attacked
? "EndTurn"
: _moves == 0
? "Move"
: HasServerAttackTarget(decision) ? "NormalAttack" : "Move"
});
break;
case SoloDecisionKinds.TargetGrid:
if (!_rejectionRecovered)
{
var rejectedRequestId = decision.RequestId;
await _store.SubmitDecisionAsync(new { gridId = long.MaxValue });
await WaitUntilAsync(
() => _store.PendingDecision is not null
&& _store.PendingDecision.RequestId != rejectedRequestId,
10.0);
_rejectionRecovered = true;
}
else
{
await SubmitBestMoveAsync(decision);
}
break;
case SoloDecisionKinds.Targets:
{
var target = decision.ReadPayload<TargetsPayload>()?.Targets.FirstOrDefault()
?? throw new InvalidOperationException("Attack request contained no legal target.");
await _store.SubmitDecisionAsync(new { targetGuids = new[] { target.Guid } });
attacked = true;
break;
}
case SoloDecisionKinds.Continue:
await _store.SubmitDecisionAsync(new { cancelled = false });
_continued = true;
break;
default:
await _store.SubmitDecisionAsync(new { cancelled = true });
break;
}
}
if (_moves == 0)
{
throw new InvalidOperationException("Vertical slice did not complete a movement.");
}
if (!attacked)
{
throw new InvalidOperationException("Vertical slice did not reach an attack target request.");
}
if (!_rejectionRecovered)
{
throw new InvalidOperationException("Vertical slice did not recover from a rejected grid.");
}
if (!_continued)
{
throw new InvalidOperationException("Vertical slice did not complete a Continue decision.");
}
GD.Print(
$"SOLO_VERTICAL_OK game={_store.Snapshot!.GameId} moves={_moves} "
+ $"attackTargetSubmitted=true rejectionRecovered=true "
+ $"continueSubmitted=true "
+ $"units={GetTree().GetNodesInGroup("battle_unit").Count}");
await _session.EndSoloGameAsync();
await _session.DisconnectAsync();
GetTree().Quit(0);
}
catch (Exception exception)
{
GD.PushError($"SOLO_VERTICAL_FAILED {exception.Message}");
await _session.DisconnectAsync();
GetTree().Quit(1);
}
}
private static bool HasServerAttackTarget(SoloDecisionRequest decision)
{
var payload = decision.ReadPayload<ActionTypePayload>();
return payload is not null
&& (payload.EnemyOptions.Count > 0 || payload.Enemys.Count > 0);
}
private async Task SubmitBestMoveAsync(SoloDecisionRequest decision)
{
var state = _store.Snapshot ?? throw new InvalidOperationException("Missing state before movement.");
var map = state.Map ?? throw new InvalidOperationException("Missing map before movement.");
var player = state.Characters.First(character => character.Guid == state.PlayerGuid);
var grids = map.Grids.ToDictionary(grid => grid.Id);
var enemies = state.Characters
.Where(character => !character.IsPlayer && !character.IsEliminated)
.Where(character => grids.ContainsKey(character.GridId))
.Select(character => grids[character.GridId])
.ToArray();
var allowed = decision.ReadPayload<TargetGridPayload>()?.GridIds ?? [];
var destination = allowed
.Where(gridId => gridId != player.GridId && grids.ContainsKey(gridId))
.Select(gridId => grids[gridId])
.OrderBy(grid => enemies.Length == 0 ? 0 : enemies.Min(enemy => Manhattan(grid, enemy)))
.FirstOrDefault();
if (destination is null)
{
await _store.SubmitDecisionAsync(new { cancelled = true });
return;
}
var originGridId = player.GridId;
await _store.SubmitDecisionAsync(new { gridId = destination.Id });
await WaitUntilAsync(
() => _store.Snapshot?.Characters
.FirstOrDefault(character => character.Guid == state.PlayerGuid)?.GridId != originGridId,
10.0);
_moves++;
}
private async Task WaitForDecisionAsync(string kind, double timeoutSeconds)
{
await WaitUntilAsync(() => _store.PendingDecision?.Kind == kind, timeoutSeconds);
}
private async Task WaitUntilAsync(Func<bool> predicate, double timeoutSeconds)
{
var startedAt = Time.GetTicksMsec();
var timeoutMs = timeoutSeconds * 1_000.0;
while (!predicate())
{
if (Time.GetTicksMsec() - startedAt >= timeoutMs)
{
throw new TimeoutException($"Timed out after {timeoutSeconds:0.#} seconds.");
}
await ToSignal(GetTree().CreateTimer(0.05), SceneTreeTimer.SignalName.Timeout);
}
}
private static int Manhattan(SoloGridDto left, SoloGridDto right)
{
return Math.Abs(left.X - right.X) + Math.Abs(left.Y - right.Y);
}
}