Skip to content

Commit f88b7d0

Browse files
committed
fix: snowwar input cap, interleaved game lookup, arena rotation and unclosed game cleanup
Adds a per-player input grain capped by SnowWarConfig.MaxInputsPerSecond in front of every in-game input, makes the directory lookup interleaved with an O(1) player index, rotates arenas with SnowWarArenaCatalog.Next, frees a game that deactivates without closing, and stores a server full status fixture for scenario-1.
1 parent 4c468cf commit f88b7d0

28 files changed

Lines changed: 1013 additions & 56 deletions

‎Turbo.Games/Configuration/SnowWarConfig.cs‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ public sealed class SnowWarConfig
3030

3131
public bool RayGunsEnabled { get; init; } = true;
3232

33+
public int MaxInputsPerSecond { get; init; } = 20;
34+
3335
public int LeaderboardMaxRows { get; init; } = 50;
3436

3537
public int LeaderboardRequestIntervalMilliseconds { get; init; } = 1000;

‎Turbo.Games/Grains/SnowWarDirectoryGrain.Lobbies.cs‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,9 @@ await SendAsync(
154154

155155
private SnowWarLobby CreateLobby()
156156
{
157-
var fieldType = _arenas.Arenas[Random.Shared.Next(_arenas.Arenas.Length)];
157+
var fieldType = _arenas.Next(_state.LastFieldType);
158+
159+
_state.LastFieldType = fieldType;
158160

159161
return new SnowWarLobby
160162
{

‎Turbo.Games/Grains/SnowWarDirectoryGrain.Queue.cs‎

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -62,11 +62,7 @@ lobby with
6262
}
6363
);
6464

65-
_state.Games[lobby.Id] = new SnowWarDirectoryGame
66-
{
67-
Id = lobby.Id,
68-
Players = lobby.PlayerIds,
69-
};
65+
StoreGame(new SnowWarDirectoryGame { Id = lobby.Id, Players = lobby.PlayerIds });
7066

7167
await SendAsync(
7268
lobby.PlayerIds,
@@ -88,7 +84,7 @@ await SendAsync(
8884

8985
private async Task CancelGameAsync(SnowWarGameLobbySnapshot game, CancellationToken ct)
9086
{
91-
_state.Games.Remove(game.GameId);
87+
ForgetGame(game.GameId);
9288

9389
await SendAsync(
9490
game.Players.Select(p => (PlayerId)p.UserId),

‎Turbo.Games/Grains/SnowWarDirectoryGrain.cs‎

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,16 @@ public async Task OnGameFinishedAsync(int gameId, CancellationToken ct)
120120
await AdvanceQueueAsync(ct);
121121
}
122122

123+
public async Task OnGameDeactivatedAsync(int gameId, CancellationToken ct)
124+
{
125+
if (!_state.Games.ContainsKey(gameId))
126+
return;
127+
128+
ForgetGame(gameId);
129+
130+
await AdvanceQueueAsync(ct);
131+
}
132+
123133
private async Task OnLobbyTimerAsync(CancellationToken ct)
124134
{
125135
ForgetExpiredBlocks();
@@ -153,12 +163,40 @@ private void ForgetExpiredBlocks()
153163
}
154164

155165
private SnowWarDirectoryGame? GameOf(PlayerId playerId) =>
156-
_state.Games.Values.FirstOrDefault(g => g.Players.Contains(playerId));
166+
_state.GameIdOfPlayer.TryGetValue(playerId, out var gameId)
167+
&& _state.Games.TryGetValue(gameId, out var game)
168+
? game
169+
: null;
170+
171+
private void StoreGame(SnowWarDirectoryGame game)
172+
{
173+
_state.Games[game.Id] = game;
174+
175+
foreach (var playerId in game.Players)
176+
_state.GameIdOfPlayer[playerId] = game.Id;
177+
}
178+
179+
private void ForgetGame(int gameId)
180+
{
181+
if (!_state.Games.Remove(gameId, out var game))
182+
return;
183+
184+
foreach (var playerId in game.Players)
185+
ForgetPlayerGame(playerId, gameId);
186+
}
187+
188+
private void ForgetPlayerGame(PlayerId playerId, int gameId)
189+
{
190+
if (_state.GameIdOfPlayer.TryGetValue(playerId, out var current) && current == gameId)
191+
_state.GameIdOfPlayer.Remove(playerId);
192+
}
157193

158194
private void RemoveFromGame(SnowWarDirectoryGame game, PlayerId playerId)
159195
{
160196
var remaining = game.Players.Remove(playerId);
161197

198+
ForgetPlayerGame(playerId, game.Id);
199+
162200
if (remaining.IsEmpty)
163201
_state.Games.Remove(game.Id);
164202
else

‎Turbo.Games/Grains/SnowWarDirectoryLiveState.cs‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,15 @@ internal sealed class SnowWarDirectoryLiveState
99
{
1010
public int LastId { get; set; }
1111

12+
public int LastFieldType { get; set; }
13+
1214
public List<SnowWarLobby> Lobbies { get; } = [];
1315

1416
public List<int> ArenaQueue { get; } = [];
1517

1618
public Dictionary<int, SnowWarDirectoryGame> Games { get; } = [];
1719

20+
public Dictionary<PlayerId, int> GameIdOfPlayer { get; } = [];
21+
1822
public Dictionary<PlayerId, DateTimeOffset> BlockedUntil { get; } = [];
1923
}

‎Turbo.Games/Grains/SnowWarGameGrain.cs‎

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,11 +69,27 @@ ILogger<ISnowWarGameGrain> logger
6969

7070
private ISnowWarDirectoryGrain Directory => _grainFactory.GetSnowWarDirectoryGrain();
7171

72-
public override Task OnDeactivateAsync(DeactivationReason reason, CancellationToken ct)
72+
public override async Task OnDeactivateAsync(DeactivationReason reason, CancellationToken ct)
7373
{
7474
StopTimer();
7575

76-
return Task.CompletedTask;
76+
if (_state.Phase is SnowWarGamePhase.Idle or SnowWarGamePhase.Closed)
77+
return;
78+
79+
_state.Phase = SnowWarGamePhase.Closed;
80+
81+
try
82+
{
83+
await Directory.OnGameDeactivatedAsync(_state.GameId, ct);
84+
}
85+
catch (Exception ex) when (ex is not OperationCanceledException)
86+
{
87+
_logger.LogError(
88+
ex,
89+
"SnowWar game {GameId} could not tell the directory it went away",
90+
_state.GameId
91+
);
92+
}
7793
}
7894

7995
public async Task StartAsync(SnowWarGameLobbySnapshot lobby, CancellationToken ct)
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
using System;
2+
using System.Threading;
3+
using System.Threading.Tasks;
4+
using Microsoft.Extensions.Options;
5+
using Orleans;
6+
using Turbo.Games.Configuration;
7+
using Turbo.Primitives.Game.Enums;
8+
using Turbo.Primitives.Game.Grains;
9+
using Turbo.Primitives.Orleans;
10+
11+
namespace Turbo.Games.Grains;
12+
13+
internal sealed class SnowWarInputGrain : Grain, ISnowWarInputGrain
14+
{
15+
private static readonly TimeSpan InputWindow = TimeSpan.FromSeconds(1);
16+
17+
private readonly SnowWarConfig _config;
18+
private readonly IGrainFactory _grainFactory;
19+
private readonly TimeProvider _time;
20+
21+
private readonly SnowWarInputLiveState _state;
22+
23+
public SnowWarInputGrain(
24+
IOptions<SnowWarConfig> config,
25+
IGrainFactory grainFactory,
26+
TimeProvider time
27+
)
28+
{
29+
_config = config.Value;
30+
_grainFactory = grainFactory;
31+
_time = time;
32+
33+
_state = new() { PlayerId = this.GetPlayerId() };
34+
}
35+
36+
public async Task QueueInputAsync(
37+
SnowWarInput input,
38+
int[] values,
39+
int turn,
40+
int subturn,
41+
CancellationToken ct
42+
)
43+
{
44+
if (!TryAdmit() || await GameAsync(ct) is not { } game)
45+
return;
46+
47+
await game.QueueInputAsync(_state.PlayerId, input, values, turn, subturn, ct);
48+
}
49+
50+
public async Task RequestFullStatusAsync(CancellationToken ct)
51+
{
52+
if (!TryAdmit() || await GameAsync(ct) is not { } game)
53+
return;
54+
55+
await game.RequestFullStatusAsync(_state.PlayerId, ct);
56+
}
57+
58+
private bool TryAdmit()
59+
{
60+
var now = _time.GetUtcNow();
61+
62+
if (now - _state.WindowStartedAt >= InputWindow)
63+
{
64+
_state.WindowStartedAt = now;
65+
_state.InputsInWindow = 0;
66+
}
67+
68+
if (_state.InputsInWindow >= _config.MaxInputsPerSecond)
69+
return false;
70+
71+
_state.InputsInWindow++;
72+
73+
return true;
74+
}
75+
76+
private async Task<ISnowWarGameGrain?> GameAsync(CancellationToken ct)
77+
{
78+
var gameId = await _grainFactory
79+
.GetSnowWarDirectoryGrain()
80+
.GetGameIdOfPlayerAsync(_state.PlayerId, ct);
81+
82+
return gameId is { } id ? _grainFactory.GetSnowWarGameGrain(id) : null;
83+
}
84+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
using System;
2+
using Turbo.Primitives.Players;
3+
4+
namespace Turbo.Games.Grains;
5+
6+
internal sealed class SnowWarInputLiveState
7+
{
8+
public PlayerId PlayerId { get; init; }
9+
10+
public DateTimeOffset WindowStartedAt { get; set; }
11+
12+
public int InputsInWindow { get; set; }
13+
}

‎Turbo.Games/OrleansContracts.txt‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,6 @@ class [GrainType("snowwardirectory")] Turbo.Games.Grains.SnowWarDirectoryGrain
1414

1515
class [GrainType("snowwargame")] Turbo.Games.Grains.SnowWarGameGrain
1616

17+
class [GrainType("snowwarinput")] Turbo.Games.Grains.SnowWarInputGrain
18+
1719
class [GrainType("snowwarscore")] Turbo.Games.Grains.SnowWarScoreGrain

‎Turbo.PacketHandlers/Game/Ingame/Game2MakeSnowballMessageHandler.cs‎

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using Turbo.Messages.Registry;
55
using Turbo.Primitives.Game.Enums;
66
using Turbo.Primitives.Messages.Incoming.Game.Ingame;
7+
using Turbo.Primitives.Orleans;
78

89
namespace Turbo.PacketHandlers.Game.Ingame;
910

@@ -21,17 +22,9 @@ CancellationToken ct
2122
if (ctx.PlayerId <= 0)
2223
return;
2324

24-
if (await _grainFactory.GameOfAsync(ctx.PlayerId, ct).ConfigureAwait(false) is not { } game)
25-
return;
26-
27-
await game.QueueInputAsync(
28-
ctx.PlayerId,
29-
SnowWarInput.MakeSnowball,
30-
[],
31-
message.Turn,
32-
message.Subturn,
33-
ct
34-
)
25+
await _grainFactory
26+
.GetSnowWarInputGrain(ctx.PlayerId)
27+
.QueueInputAsync(SnowWarInput.MakeSnowball, [], message.Turn, message.Subturn, ct)
3528
.ConfigureAwait(false);
3629
}
3730
}

0 commit comments

Comments
 (0)