add network time synchronization & fix initial timestamp

This commit is contained in:
Jesse D. McDonald 2020-03-15 20:18:30 -05:00
parent 8a3e3ca4f2
commit 89f335479b
2 changed files with 13 additions and 9 deletions

View File

@ -276,6 +276,7 @@
//console.STAT = console.real_log; //console.STAT = console.real_log;
</script> </script>
<script src="gun/gun.js"></script> <script src="gun/gun.js"></script>
<script src="gun/nts.js"></script>
<script src="gun/sea.js"></script> <script src="gun/sea.js"></script>
<script src="gun/lib/webrtc.js"></script> <script src="gun/lib/webrtc.js"></script>
<script> <script>

View File

@ -29,9 +29,12 @@ let initialBoard = (function (){
'rnbqkbnr', 'rnbqkbnr',
], ],
player: 'light', player: 'light',
timestamp: new Date().getTime(),
}); });
return function (){ return JSON.parse(init); } return function (){
const board = JSON.parse(init);
board.timestamp = new Date(Gun.state()).getTime();
return board;
};
})(); })();
function cloneJSON(obj){ function cloneJSON(obj){
@ -305,7 +308,7 @@ function movePiece(priorBoard, side, from, to){
let board = cloneJSON(undoBoard); let board = cloneJSON(undoBoard);
board.prior = undoBoard; board.prior = undoBoard;
board.timestamp = new Date().getTime(); board.timestamp = new Date(Gun.state()).getTime();
board.move = { board.move = {
side: normalizeSide(side), side: normalizeSide(side),
@ -737,7 +740,7 @@ function putMeta(){
lightName: lightName, lightName: lightName,
darkName: darkName, darkName: darkName,
moves: countMoves(board), moves: countMoves(board),
timestamp: board.timestamp || new Date().getTime(), timestamp: board.timestamp || new Date(Gun.state()).getTime(),
status: stat, status: stat,
}); });
} }
@ -941,7 +944,7 @@ $(function (){
$('#cb_redo').on('click', function(){ $('#cb_redo').on('click', function(){
const board = stepForward($('#cb_board').data('board')); const board = stepForward($('#cb_board').data('board'));
board.timestamp = new Date().getTime(); board.timestamp = new Date(Gun.state()).getTime();
putState(board); putState(board);
}); });
@ -1000,7 +1003,7 @@ $(function (){
newBoard.prior = board; newBoard.prior = board;
newBoard.move = { side: board.player, pass: true }; newBoard.move = { side: board.player, pass: true };
newBoard.player = otherSide(board.player); newBoard.player = otherSide(board.player);
newBoard.timestamp = new Date().getTime(); newBoard.timestamp = new Date(Gun.state()).getTime();
putState(newBoard); putState(newBoard);
} }
}); });
@ -1012,7 +1015,7 @@ $(function (){
newBoard.prior = board; newBoard.prior = board;
newBoard.move = { side: board.player, resign: true }; newBoard.move = { side: board.player, resign: true };
newBoard.player = otherSide(board.player); newBoard.player = otherSide(board.player);
newBoard.timestamp = new Date().getTime(); newBoard.timestamp = new Date(Gun.state()).getTime();
putState(newBoard); putState(newBoard);
} }
}); });
@ -1053,7 +1056,7 @@ $(function (){
opt = $(opt); opt = $(opt);
const then = opt.data('then'); const then = opt.data('then');
const now = new Date().getTime(); const now = new Date(Gun.state()).getTime();
let age_str = ''; let age_str = '';
if (then > now) { if (then > now) {
age_str = ' (future)'; age_str = ' (future)';
@ -1096,7 +1099,7 @@ $(function (){
opt.data('gameId', d.gameId); opt.data('gameId', d.gameId);
opt.data('title', lightName + ' vs. ' + darkName + moves + stat); opt.data('title', lightName + ' vs. ' + darkName + moves + stat);
opt.data('then', d.timestamp || new Date().getTime()); opt.data('then', d.timestamp || new Date(Gun.state()).getTime());
opt.addClass('cb-game-option'); opt.addClass('cb-game-option');
opt.appendTo('#cb_select_game'); opt.appendTo('#cb_select_game');
updateTitle(opt); updateTitle(opt);