Skip to content

Commit

Permalink
Improve wait for bot load
Browse files Browse the repository at this point in the history
  • Loading branch information
Lacyway committed May 17, 2024
1 parent 42e1e43 commit 14d546b
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 6 deletions.
4 changes: 4 additions & 0 deletions Fika.Core/Coop/Components/CoopHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -475,7 +475,9 @@ private LocalPlayer SpawnObservedPlayer(Profile profile, Vector3 position, int p
null).Result;

if (otherPlayer == null)
{
return null;
}

((CoopPlayer)otherPlayer).NetId = netId;
Logger.LogInfo($"SpawnObservedPlayer: {profile.Nickname} spawning with NetId {netId}");
Expand All @@ -494,7 +496,9 @@ private LocalPlayer SpawnObservedPlayer(Profile profile, Vector3 position, int p
}

if (!Singleton<GameWorld>.Instance.RegisteredPlayers.Any(x => x.Profile.ProfileId == profile.ProfileId))
{
Singleton<GameWorld>.Instance.RegisteredPlayers.Add(otherPlayer);
}

foreach (CoopPlayer player in Players.Values)
{
Expand Down
16 changes: 11 additions & 5 deletions Fika.Core/Coop/GameMode/CoopGame.cs
Original file line number Diff line number Diff line change
Expand Up @@ -369,7 +369,7 @@ private async Task<LocalPlayer> CreateBot(Profile profile, Vector3 position)

if (server.NetServer.ConnectedPeersCount > 0)
{
await WaitForPlayersToLoadBotProfile(netId, profile);
await WaitForPlayersToLoadBotProfile(netId);
}

localPlayer = await CoopBot.CreateBot(num, position, Quaternion.identity, "Player",
Expand Down Expand Up @@ -447,15 +447,21 @@ public void IncreaseLoadedPlayers(int netId)
}
}

private async Task WaitForPlayersToLoadBotProfile(int netId, Profile profile)
private async Task WaitForPlayersToLoadBotProfile(int netId)
{
botQueue.Add(netId, 0);
int waitTime = 0;
DateTime start = DateTime.Now;
int connectedPeers = Singleton<FikaServer>.Instance.NetServer.ConnectedPeersCount;

while (botQueue[netId] < connectedPeers || waitTime < 60) // ~15 second failsafe
while (botQueue[netId] < connectedPeers)
{
waitTime++;
if (start.Subtract(DateTime.Now).TotalSeconds >= 30) // ~30 second failsafe
{
Logger.LogWarning("WaitForPlayersToLoadBotProfile: Took too long to receive all packets!");
botQueue.Remove(netId);
return;
}

await Task.Delay(250);
}

Expand Down
2 changes: 1 addition & 1 deletion Fika.Core/Networking/FikaServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -376,7 +376,7 @@ private void OnGenericPacketReceived(GenericPacket packet, NetPeer peer)
else if (packet.PacketType == EPackageType.LoadBot)
{
CoopGame coopGame = (CoopGame)Singleton<IFikaGame>.Instance;
coopGame.IncreaseLoadedPlayers(packet.NetId);
coopGame.IncreaseLoadedPlayers(packet.BotNetId);

return;
}
Expand Down

0 comments on commit 14d546b

Please sign in to comment.