-
-
Notifications
You must be signed in to change notification settings - Fork 94
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #193 from kaenganxt/syncPlayPauseSpeedStates
Sync play pause speed states better
- Loading branch information
Showing
17 changed files
with
741 additions
and
90 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; } | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.