@@ -190,10 +190,16 @@ function createRoomGameContext() {
190190function createGameRoom ( roomId ) {
191191 const room = {
192192 id : roomId ,
193+ clientUpdates : [ ] ,
193194 gameStarted : false ,
194195 lastUpdate : Date . now ( ) ,
196+ replayStates : [ ] ,
195197 gameContext : createRoomGameContext ( ) ,
196198 update : function ( ) {
199+ this . clientUpdates . forEach ( update => {
200+ update . function ( update . args ) ;
201+ } )
202+ this . clientUpdates = [ ] ;
197203 this . gameContext . items . forEach ( function ( item ) {
198204 baseGameContext . Items [ item . type ] . update ( item ) ;
199205 } ) ;
@@ -321,41 +327,62 @@ io.on('connection', (socket) => {
321327 socket . on ( 'playerUpdate' , ( data ) => {
322328 const room = gameRooms . get ( data . roomId ) ;
323329 if ( ! room || ! room . gameContext . racers . has ( socket . id ) ) return ;
324-
325- // Update racer state
326- const racer = room . gameContext . racers . get ( socket . id ) ;
327- Object . assign ( racer , data ) ;
328- room . lastUpdate = Date . now ( ) ;
330+ room . clientUpdates . push ( {
331+ function : function ( args ) {
332+ // Update racer state
333+ const racer = args . room . gameContext . racers . get ( args . socketId ) ;
334+ Object . assign ( racer , args . data ) ;
335+ if ( racer . finished ) {
336+ socket . emit ( 'replay' , args . room . replayStates ) ;
337+ }
338+ } ,
339+ args : {
340+ room : room ,
341+ socketId : socket . id ,
342+ data : data
343+ }
344+ } ) ;
329345 } ) ;
330346
331347 // Handle item events
332348 socket . on ( 'itemEvent' , ( data ) => {
333349 const room = gameRooms . get ( data . roomId ) ;
334350 if ( ! room || ! room . gameContext . racers . has ( socket . id ) ) return ;
335351
336- debug ( 'Item event' , {
337- roomId : data . roomId ,
338- playerId : socket . id ,
339- itemType : data . item . type ,
340- itemPosition : data . item . position ,
341- itemState : data . state ,
342- ownerId : data . item . ownerId ,
343- itemId : data . item . id
352+ room . clientUpdates . push ( {
353+ function : function ( args ) {
354+ const newItem = new args . room . gameContext . Item (
355+ args . data . item . type ,
356+ new args . room . gameContext . Point ( args . data . item . position . x , args . data . item . position . y ) ,
357+ args . data . item . target ,
358+ args . data . item . ownerId ,
359+ args . data . item . duration ,
360+ new args . room . gameContext . Point ( args . data . item . velocity . x , args . data . item . velocity . y ) ,
361+ args . data . item . direction ,
362+ args . data . item . state
363+ ) ;
364+ newItem . id = data . item . id ;
365+ room . gameContext . items . push ( newItem ) ;
366+ } ,
367+ args : {
368+ room : room ,
369+ data : data
370+ }
344371 } ) ;
345- const newItem = new room . gameContext . Item ( data . item . type , new room . gameContext . Point ( data . item . position . x , data . item . position . y ) , data . item . target , data . item . ownerId , data . item . duration , new room . gameContext . Point ( data . item . velocity . x , data . item . velocity . y ) , data . item . direction , data . item . state ) ;
346- newItem . id = data . item . id ;
347- room . gameContext . items . push ( newItem ) ;
348372 } ) ;
349373 socket . on ( 'itemUpdate' , ( data ) => {
350374 const room = gameRooms . get ( data . roomId ) ;
351375 if ( ! room || ! room . gameContext . racers . has ( socket . id ) ) return ;
352- debug ( 'Item update' , {
353- roomId : data . roomId ,
354- playerId : socket . id ,
355- itemId : data . itemId ,
356- dataToUpdate : data . dataToUpdate
376+ room . clientUpdates . push ( {
377+ function : function ( args ) {
378+ const item = args . room . gameContext . items . find ( item => item . id === args . data . itemId ) ;
379+ Object . assign ( item , args . data . dataToUpdate ) ;
380+ } ,
381+ args : {
382+ room : room ,
383+ data : data
384+ }
357385 } ) ;
358- Object . assign ( room . gameContext . items . find ( item => item . id === data . itemId ) , data . dataToUpdate ) ;
359386 } ) ;
360387
361388 // Handle disconnection
@@ -427,6 +454,7 @@ setInterval(() => {
427454 }
428455 // Send game state to all players
429456 io . to ( roomId ) . emit ( 'gameStateUpdate' , gameState ) ;
457+ room . replayStates . push ( { time : now , gameState : gameState } ) ;
430458 }
431459 } ) ;
432460} , 1000 / 10 ) ; // 10 times per second
0 commit comments