Skip to content

Commit

Permalink
Resync techQueue if techQueueLength mismatch
Browse files Browse the repository at this point in the history
  • Loading branch information
starfi5h committed Mar 9, 2024
1 parent 778c874 commit 4faccce
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,18 @@ public class GameHistoryResearchUpdatePacket
{
public GameHistoryResearchUpdatePacket() { }

public GameHistoryResearchUpdatePacket(int techId, long hashUploaded, long hashNeeded, int techHashedFor10Frames)
public GameHistoryResearchUpdatePacket(int techId, long hashUploaded, long hashNeeded, int techHashedFor10Frames, int techQueueLength)
{
TechId = techId;
HashUploaded = hashUploaded;
HashNeeded = hashNeeded;
TechHashedFor10Frames = techHashedFor10Frames;
TechQueueLength = (ushort)techQueueLength;
}

public int TechId { get; set; }
public long HashUploaded { get; set; }
public long HashNeeded { get; set; }
public int TechHashedFor10Frames { get; set; }
public ushort TechQueueLength { get; set; }
}
13 changes: 13 additions & 0 deletions NebulaModel/Packets/GameHistory/GameHistoryTechQueueSyncRequest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
namespace NebulaModel.Packets.GameHistory;

public class GameHistoryTechQueueSyncRequest
{
public GameHistoryTechQueueSyncRequest() { }

public GameHistoryTechQueueSyncRequest(int[] techQueue)
{
TechQueue = techQueue;
}

public int[] TechQueue { get; set; }
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#region

using System;
using NebulaAPI.Packets;
using NebulaModel.Logger;
using NebulaModel.Networking;
Expand Down Expand Up @@ -29,5 +30,11 @@ protected override void ProcessPacket(GameHistoryResearchUpdatePacket packet, Ne
state.hashNeeded = packet.HashNeeded;
data.techStates[data.currentTech] = state;
Multiplayer.Session.Statistics.TechHashedFor10Frames = packet.TechHashedFor10Frames;

if (packet.TechQueueLength != GameMain.history.techQueueLength)
{
// TechQueue length mismatch. Ask from server to get a full queue to stay in sync
conn.SendPacket(new GameHistoryTechQueueSyncRequest([]));
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#region

using NebulaAPI.GameState;
using NebulaAPI.Packets;
using NebulaModel.Networking;
using NebulaModel.Packets;
using NebulaModel.Packets.GameHistory;
using NebulaWorld;

#endregion

namespace NebulaNetwork.PacketProcessors.GameHistory;

[RegisterPacketProcessor]
internal class GameHistoryTechQueueSyncProcessor : PacketProcessor<GameHistoryTechQueueSyncRequest>
{
protected override void ProcessPacket(GameHistoryTechQueueSyncRequest packet, NebulaConnection conn)
{
if (IsHost)
{
packet.TechQueue = GameMain.history.techQueue;
conn.SendPacket(packet);
}
else
{
using (Multiplayer.Session.History.IsIncomingRequest.On())
{
var length = GameMain.history.techQueue.Length;
for (var i = 0; i < length; i++)
{
// Clear the original queue by dequeue for compatibility
GameMain.history.DequeueTech();
}
for (var i = 0; i < packet.TechQueue.Length; i++)
{
if (packet.TechQueue[i] == 0) return;
GameMain.history.EnqueueTech(packet.TechQueue[i]);
}
}
}
}
}
2 changes: 1 addition & 1 deletion NebulaNetwork/Server.cs
Original file line number Diff line number Diff line change
Expand Up @@ -418,7 +418,7 @@ public void Update()
{
var state = GameMain.data.history.techStates[GameMain.data.history.currentTech];
SendPacket(new GameHistoryResearchUpdatePacket(GameMain.data.history.currentTech, state.hashUploaded,
state.hashNeeded, GameMain.statistics.techHashedFor10Frames));
state.hashNeeded, GameMain.statistics.techHashedFor10Frames, GameMain.data.history.techQueueLength));
}
}

Expand Down

0 comments on commit 4faccce

Please sign in to comment.