88
99namespace Lockstep . Network . Server
1010{
11+ public class StartedEventArgs : EventArgs
12+ {
13+ public int SimulationSpeed { get ; set ; }
14+
15+ public byte [ ] ActorIds { get ; set ; }
16+
17+ public StartedEventArgs ( int simulationSpeed , byte [ ] actorIds )
18+ {
19+ SimulationSpeed = simulationSpeed ;
20+ ActorIds = actorIds ;
21+ }
22+ }
23+
24+ public class InputReceivedEventArgs : EventArgs
25+ {
26+ public byte ActorId { get ; set ; }
27+ public uint Tick { get ; set ; }
28+
29+ public InputReceivedEventArgs ( byte actorId , uint tick )
30+ {
31+ ActorId = actorId ;
32+ Tick = tick ;
33+ }
34+ }
35+
36+
1137 /// <summary>
1238 /// Relays input
1339 /// </summary>
@@ -18,24 +44,28 @@ public class Room
1844 /// </summary>
1945 private const int SimulationSpeed = 20 ;
2046
21- private byte _nextPlayerId ;
22- private readonly int _size ;
23-
24- private readonly IServer _server ;
47+
48+ public event EventHandler < StartedEventArgs > Starting ;
49+ public event EventHandler < StartedEventArgs > Started ;
50+ public event EventHandler < InputReceivedEventArgs > InputReceived ;
2551
2652 public bool Running { get ; private set ; }
2753
2854 /// <summary>
29- /// Mapping: clientId -> playerId
55+ /// Mapping: clientId -> actorId
3056 /// </summary>
31- private readonly Dictionary < int , byte > _playerIds = new Dictionary < int , byte > ( ) ;
57+ private readonly Dictionary < int , byte > _actorIds = new Dictionary < int , byte > ( ) ;
3258
3359 /// <summary>
3460 /// Mapping: Framenumber -> received hashcode
3561 /// </summary>
3662 private readonly Dictionary < ulong , long > _hashCodes = new Dictionary < ulong , long > ( ) ;
3763
3864 private uint inputMessageCounter = 0 ;
65+ private byte _nextPlayerId ;
66+ private readonly int _size ;
67+
68+ private readonly IServer _server ;
3969
4070 public Room ( IServer server , int size )
4171 {
@@ -55,34 +85,37 @@ public void Open(int port)
5585
5686 private void OnClientConnected ( int clientId )
5787 {
58- _playerIds . Add ( clientId , _nextPlayerId ++ ) ;
88+ _actorIds . Add ( clientId , _nextPlayerId ++ ) ;
5989
60- if ( _playerIds . Count == _size )
90+ if ( _actorIds . Count == _size )
6191 {
6292 Console . WriteLine ( "Room is full, starting new simulation..." ) ;
6393 StartSimulationOnConnectedPeers ( ) ;
6494 }
6595 else
6696 {
67- Console . WriteLine ( _playerIds . Count + " / " + _size + " players have connected." ) ;
97+ Console . WriteLine ( _actorIds . Count + " / " + _size + " players have connected." ) ;
6898 }
6999 }
70100
71101 private void OnDataReceived ( int clientId , byte [ ] data )
72102 {
73103 var reader = new Deserializer ( Compressor . Decompress ( data ) ) ;
74- var messageTag = ( MessageTag ) reader . PeekByte ( ) ;
104+ var messageTag = ( MessageTag ) reader . GetByte ( ) ;
75105 switch ( messageTag )
76106 {
77107 case MessageTag . Input :
78108 ++ inputMessageCounter ;
79109
80110 var clientTick = reader . GetUInt ( ) ;
111+ reader . GetByte ( ) ; //Client's lag-compensation
81112 var commandsCount = reader . GetInt ( ) ;
82113 if ( commandsCount > 0 || inputMessageCounter % 8 == 0 )
83114 {
84115 _server . Distribute ( clientId , data ) ;
85116 }
117+
118+ InputReceived ? . Invoke ( this , new InputReceivedEventArgs ( _actorIds [ clientId ] , clientTick ) ) ;
86119 break ;
87120
88121 case MessageTag . HashCode :
@@ -106,15 +139,15 @@ private void OnDataReceived(int clientId, byte[] data)
106139
107140 private void OnClientDisconnected ( int clientId )
108141 {
109- _playerIds . Remove ( clientId ) ;
110- if ( _playerIds . Count == 0 )
142+ _actorIds . Remove ( clientId ) ;
143+ if ( _actorIds . Count == 0 )
111144 {
112145 Console . WriteLine ( "All players left, stopping current simulation..." ) ;
113146 Running = false ;
114147 }
115148 else
116149 {
117- Console . WriteLine ( _playerIds . Count + " players remaining." ) ;
150+ Console . WriteLine ( _actorIds . Count + " players remaining." ) ;
118151 }
119152 }
120153
@@ -126,20 +159,24 @@ private void StartSimulationOnConnectedPeers()
126159 //The message also contains the respective player-id and the initial simulation speed
127160 var seed = new Random ( ) . Next ( int . MinValue , int . MaxValue ) ;
128161
129- foreach ( var player in _playerIds )
162+
163+ Starting ? . Invoke ( this , new StartedEventArgs ( SimulationSpeed , _actorIds . Values . ToArray ( ) ) ) ;
164+ foreach ( var player in _actorIds )
130165 {
131166 writer . Reset ( ) ;
132167 writer . Put ( ( byte ) MessageTag . Init ) ;
133168 new Init
134169 {
135170 Seed = seed ,
136171 ActorID = player . Value ,
137- AllActors = _playerIds . Values . ToArray ( ) ,
172+ AllActors = _actorIds . Values . ToArray ( ) ,
138173 SimulationSpeed = SimulationSpeed
139174 } . Serialize ( writer ) ;
140175
141176 _server . Send ( player . Key , Compressor . Compress ( writer ) ) ;
142- }
177+ }
178+
179+ Started ? . Invoke ( this , new StartedEventArgs ( SimulationSpeed , _actorIds . Values . ToArray ( ) ) ) ;
143180 }
144181 }
145182}
0 commit comments