Skip to content

Commit 68f8e4c

Browse files
Make sure players are spawned before distributing packets (#107)
* Make sure players are spawned before distributing packets * Bump version and format
1 parent 74c7efe commit 68f8e4c

File tree

3 files changed

+46
-21
lines changed

3 files changed

+46
-21
lines changed

RLBotCS/Main.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
if (args.Length > 0 && args[0] == "--version")
1010
{
11-
Console.WriteLine("RLBotServer v5.beta.6.4");
11+
Console.WriteLine("RLBotServer v5.beta.6.5");
1212
Environment.Exit(0);
1313
}
1414

RLBotCS/Server/BridgeHandler.cs

Lines changed: 42 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -75,20 +75,8 @@ private async Task HandleServer()
7575
if (timeAdvanced)
7676
_context.PerfMonitor.AddRLBotSample(deltaTime);
7777

78-
GamePacketT? packet =
79-
timeAdvanced
80-
|| (
81-
_context.ticksSkipped > MAX_TICK_SKIP
82-
&& (
83-
_context.GameState.MatchPhase == MatchPhase.Replay
84-
|| _context.GameState.MatchPhase == MatchPhase.Paused
85-
|| _context.GameState.MatchPhase == MatchPhase.Ended
86-
|| _context.GameState.MatchPhase == MatchPhase.Inactive
87-
)
88-
)
89-
? _context.GameState.ToFlatBuffers()
90-
: null;
91-
_context.Writer.TryWrite(new DistributeGamePacket(packet));
78+
ConsiderDistributingPacket(_context, timeAdvanced);
79+
9280
_context.MatchStarter.SetCurrentMatchPhase(
9381
_context.GameState.MatchPhase,
9482
_context.GetPlayerSpawner()
@@ -189,4 +177,44 @@ public void Cleanup()
189177
}
190178
}
191179
}
180+
181+
private static void ConsiderDistributingPacket(BridgeContext context, bool timeAdvanced)
182+
{
183+
var config = context.MatchConfig;
184+
if (config == null)
185+
return;
186+
187+
// While game is paused (or similar), we distribute less often
188+
bool due =
189+
context
190+
is {
191+
ticksSkipped: > MAX_TICK_SKIP,
192+
GameState.MatchPhase: MatchPhase.Replay
193+
or MatchPhase.Paused
194+
or MatchPhase.Ended
195+
or MatchPhase.Inactive
196+
};
197+
if (!timeAdvanced && !due)
198+
return;
199+
200+
// We only distribute the packet if it has all players from the match config,
201+
// - unless the match is already ongoing, then only the bot players are required (humans may leave).
202+
bool inactive =
203+
context.GameState.MatchPhase is MatchPhase.Inactive or MatchPhase.Ended;
204+
int botCount = config.PlayerConfigurations.Count(p =>
205+
p.Variety.Type != PlayerClass.Human
206+
);
207+
int requiredPlayers = inactive ? config.PlayerConfigurations.Count : botCount;
208+
// Assumption: Bots are always the lower indexes
209+
for (uint i = 0; i < requiredPlayers; i++)
210+
{
211+
if (!context.GameState.GameCars.ContainsKey(i))
212+
{
213+
return;
214+
}
215+
}
216+
217+
var packet = context.GameState.ToFlatBuffers();
218+
context.Writer.TryWrite(new DistributeGamePacket(packet));
219+
}
192220
}

RLBotCS/Server/ServerMessage/DistributeGamePacket.cs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
namespace RLBotCS.Server.ServerMessage;
55

6-
record DistributeGamePacket(GamePacketT? Packet) : IServerMessage
6+
record DistributeGamePacket(GamePacketT Packet) : IServerMessage
77
{
88
private static void DistributeBallPrediction(ServerContext context, GamePacketT packet)
99
{
@@ -50,11 +50,8 @@ private static void DistributeState(ServerContext context, GamePacketT packet)
5050

5151
public ServerAction Execute(ServerContext context)
5252
{
53-
if (Packet is { } packet)
54-
{
55-
DistributeBallPrediction(context, packet);
56-
DistributeState(context, packet);
57-
}
53+
DistributeBallPrediction(context, Packet);
54+
DistributeState(context, Packet);
5855

5956
return ServerAction.Continue;
6057
}

0 commit comments

Comments
 (0)