Skip to content

Commit

Permalink
Merge pull request #193 from kaenganxt/syncPlayPauseSpeedStates
Browse files Browse the repository at this point in the history
Sync play pause speed states better
  • Loading branch information
DominicMaas authored Aug 18, 2020
2 parents ad17a87 + cb43fff commit 2428e24
Show file tree
Hide file tree
Showing 17 changed files with 741 additions and 90 deletions.
11 changes: 7 additions & 4 deletions src/CSM.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@
<Compile Include="Commands\Data\Events\EventColorChangedCommand.cs" />
<Compile Include="Commands\Data\Events\EventSetSecurityBudgetCommand.cs" />
<Compile Include="Commands\Data\Events\EventSetTicketPriceCommand.cs" />
<Compile Include="Commands\Data\Game\SpeedPauseReachedCommand.cs" />
<Compile Include="Commands\Data\Internal\ClientJoiningCommand.cs" />
<Compile Include="Commands\Data\Internal\ClientLevelLoadedCommand.cs" />
<Compile Include="Commands\Data\Internal\PlayerLocationCommand.cs" />
Expand All @@ -98,8 +99,8 @@
<Compile Include="Commands\Data\Districts\DistrictPolicyCommand.cs" />
<Compile Include="Commands\Data\Districts\DistrictPolicyUnsetCommand.cs" />
<Compile Include="Commands\Data\Districts\DistrictReleaseCommand.cs" />
<Compile Include="Commands\Data\Game\PauseCommand.cs" />
<Compile Include="Commands\Data\Game\SpeedCommand.cs" />
<Compile Include="Commands\Data\Game\SpeedPauseResponseCommand.cs" />
<Compile Include="Commands\Data\Game\SpeedPauseRequestCommand.cs" />
<Compile Include="Commands\Data\Internal\ChatMessageCommand.cs" />
<Compile Include="Commands\Data\Internal\ClientConnectCommand.cs" />
<Compile Include="Commands\Data\Internal\ClientDisconnectCommand.cs" />
Expand Down Expand Up @@ -178,8 +179,9 @@
<Compile Include="Commands\Handler\Events\EventColorChangedHandler.cs" />
<Compile Include="Commands\Handler\Events\EventSetSecurityBudgetHandler.cs" />
<Compile Include="Commands\Handler\Events\EventSetTicketPriceHandler.cs" />
<Compile Include="Commands\Handler\Game\PauseHandler.cs" />
<Compile Include="Commands\Handler\Game\SpeedHandler.cs" />
<Compile Include="Commands\Handler\Game\SpeedPauseReachedHandler.cs" />
<Compile Include="Commands\Handler\Game\SpeedPauseResponseHandler.cs" />
<Compile Include="Commands\Handler\Game\SpeedPauseRequestHandler.cs" />
<Compile Include="Commands\Handler\Internal\ChatMessageHandler.cs" />
<Compile Include="Commands\Handler\Internal\ClientConnectHandler.cs" />
<Compile Include="Commands\Handler\Internal\ClientDisonnectHandler.cs" />
Expand Down Expand Up @@ -243,6 +245,7 @@
<Compile Include="Helpers\InfoPanelHelper.cs" />
<Compile Include="Helpers\ReflectionHelper.cs" />
<Compile Include="Helpers\SlowdownHelper.cs" />
<Compile Include="Helpers\SpeedPauseHelper.cs" />
<Compile Include="Helpers\ToolSimulator.cs" />
<Compile Include="Injections\ArrayHandler.cs" />
<Compile Include="Injections\BuildingHandler.cs" />
Expand Down
20 changes: 0 additions & 20 deletions src/Commands/Data/Game/PauseCommand.cs

This file was deleted.

19 changes: 0 additions & 19 deletions src/Commands/Data/Game/SpeedCommand.cs

This file was deleted.

19 changes: 19 additions & 0 deletions src/Commands/Data/Game/SpeedPauseReachedCommand.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using ProtoBuf;

namespace CSM.Commands.Data.Game
{
/// <summary>
/// Sent when the requested new state was reached.
/// </summary>
/// Sent by:
/// - SpeedPauseHelper
[ProtoContract]
public class SpeedPauseReachedCommand : CommandBase
{
/// <summary>
/// The id of the associated SpeedPauseRequest.
/// </summary>
[ProtoMember(1)]
public int RequestId { get; set; }
}
}
34 changes: 34 additions & 0 deletions src/Commands/Data/Game/SpeedPauseRequestCommand.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using ProtoBuf;

namespace CSM.Commands.Data.Game
{
/// <summary>
/// Sent when the game speed or pause state is changed to request a SpeedPauseResponseCommand.
/// </summary>
/// Sent by:
/// - SpeedPauseHelper
[ProtoContract]
public class SpeedPauseRequestCommand : CommandBase
{
/// <summary>
/// The current game speed.
/// Not set when SimulationPaused equals true.
/// </summary>
[ProtoMember(1)]
public int SelectedSimulationSpeed { get; set; }

/// <summary>
/// If the simulation is paused.
/// </summary>
[ProtoMember(2)]
public bool SimulationPaused { get; set; }

/// <summary>
/// The request id to uniquely identify this SpeedPauseRequest.
/// This id will be used in the SpeedPauseResponse.
/// Not set if this command is used to request the play state.
/// </summary>
[ProtoMember(3)]
public int RequestId { get; set; }
}
}
38 changes: 38 additions & 0 deletions src/Commands/Data/Game/SpeedPauseResponseCommand.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
using ProtoBuf;

namespace CSM.Commands.Data.Game
{
/// <summary>
/// Sent as a response to the SpeedPauseRequestCommand to share the current game time with all clients.
/// </summary>
/// Sent by:
/// - SpeedPauseHelper
[ProtoContract]
public class SpeedPauseResponseCommand : CommandBase
{
/// <summary>
/// The current game time in DateTime ticks.
/// </summary>
[ProtoMember(1)]
public long CurrentTime { get; set; }

/// <summary>
/// The id of the associated SpeedPauseRequest.
/// </summary>
[ProtoMember(2)]
public int RequestId { get; set; }

/// <summary>
/// The maximum latency of the connected clients in milliseconds.
/// </summary>
[ProtoMember(3)]
public long MaxLatency { get; set; }

/// <summary>
/// The number of connected clients.
/// Only filled by the server, otherwise -1.
/// </summary>
[ProtoMember(4)]
public int NumberOfClients { get; set; }
}
}
12 changes: 0 additions & 12 deletions src/Commands/Handler/Game/PauseHandler.cs

This file was deleted.

12 changes: 0 additions & 12 deletions src/Commands/Handler/Game/SpeedHandler.cs

This file was deleted.

42 changes: 42 additions & 0 deletions src/Commands/Handler/Game/SpeedPauseReachedHandler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
using CSM.Commands.Data.Game;
using CSM.Helpers;

namespace CSM.Commands.Handler.Game
{
public class SpeedPauseReachedHandler : CommandHandler<SpeedPauseReachedCommand>
{
private static int _currentWaitingId;
private static int _maxNumberOfClients;
private static int _numberOfClients;

public SpeedPauseReachedHandler()
{
TransactionCmd = false;
}

protected override void Handle(SpeedPauseReachedCommand command)
{
if (command.RequestId == _currentWaitingId)
{
_numberOfClients++;
if (_numberOfClients == _maxNumberOfClients)
{
SpeedPauseHelper.StateReached();
_currentWaitingId = -1;
}
}
}

public static void SetWaitingFor(int currentWaitingId, int maxNumberOfClients)
{
_currentWaitingId = currentWaitingId;
_maxNumberOfClients = maxNumberOfClients;
_numberOfClients = 0;
}

public static int GetCurrentId()
{
return _currentWaitingId;
}
}
}
18 changes: 18 additions & 0 deletions src/Commands/Handler/Game/SpeedPauseRequestHandler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using CSM.Commands.Data.Game;
using CSM.Helpers;

namespace CSM.Commands.Handler.Game
{
public class SpeedPauseRequestHandler : CommandHandler<SpeedPauseRequestCommand>
{
public SpeedPauseRequestHandler()
{
TransactionCmd = false;
}

protected override void Handle(SpeedPauseRequestCommand command)
{
SpeedPauseHelper.PlayPauseRequest(command.SimulationPaused, command.SelectedSimulationSpeed, command.RequestId);
}
}
}
80 changes: 80 additions & 0 deletions src/Commands/Handler/Game/SpeedPauseResponseHandler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
using CSM.Commands.Data.Game;
using CSM.Helpers;
using NLog;

namespace CSM.Commands.Handler.Game
{
public class SpeedPauseResponseHandler : CommandHandler<SpeedPauseResponseCommand>
{
private int _currentWaitingId;
private int _maxNumberOfClients;
private int _numberOfClients;
private long _highestLatency;
private long _highestTime;

private static readonly Logger _logger = LogManager.GetCurrentClassLogger();

public SpeedPauseResponseHandler()
{
TransactionCmd = false;
ResetValues();
}

protected override void Handle(SpeedPauseResponseCommand command)
{
int requestId = command.RequestId;

// Use highest request id in case of conflicting requests
if (requestId < _currentWaitingId)
{
return;
}

// If the new id is new, reset all other values
if (requestId != _currentWaitingId)
{
ResetValues();
}

_currentWaitingId = requestId;

if (command.NumberOfClients != -1)
{
_maxNumberOfClients = command.NumberOfClients;
}

if (command.MaxLatency != -1 && command.MaxLatency > _highestLatency)
{
_highestLatency = command.MaxLatency;
}

if (command.CurrentTime > _highestTime)
{
_highestTime = command.CurrentTime;
}

_numberOfClients++;

// Check if all clients have answered
if (_numberOfClients == _maxNumberOfClients)
{
SpeedPauseHelper.SpeedPauseResponseReceived(_highestTime, _highestLatency);

// Set waiting target for the SpeedPauseReachedCommand
SpeedPauseReachedHandler.SetWaitingFor(_currentWaitingId, _maxNumberOfClients);

// Reset waiting id
_currentWaitingId = -1;
}
}

private void ResetValues()
{
_currentWaitingId = -1;
_maxNumberOfClients = -1;
_numberOfClients = 0;
_highestLatency = -1;
_highestTime = -1;
}
}
}
1 change: 1 addition & 0 deletions src/Commands/Handler/Internal/ConnectionResultHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ protected override void Handle(ConnectionResultCommand command)
{
// Log and set that we are connected.
_logger.Info("Successfully connected to server. Downloading world...");
MultiplayerManager.Instance.CurrentClient.ClientPlayer = new Player();
MultiplayerManager.Instance.CurrentClient.Status = ClientStatus.Downloading;
MultiplayerManager.Instance.CurrentClient.ClientId = command.ClientId;
}
Expand Down
Loading

0 comments on commit 2428e24

Please sign in to comment.