diff --git a/js/pacosako_ui.js b/js/pacosako_ui.js index 094f9b4..d88ba60 100644 --- a/js/pacosako_ui.js +++ b/js/pacosako_ui.js @@ -423,14 +423,14 @@ $(function (){ return name.slice(0, 20) + '…'; } - const PacoSakoUUID = 'b425b812-6bdb-11ea-9414-6f946662bac3'; /* V2 release */ + const PacoSakoUUID = 'b425b812-6bdb-11ea-9414-6f946662bac3'; /* V2 & V3 releases */ function putState() { const boardElem = $('#cb_board'); const gameId = boardElem.data('gameId'); const moves = { past: currentGame.moves, future: currentGame.redoMoves }; boardElem.data('last_state', currentGame.moves); - gun.get(PacoSakoUUID).get('games').get(gameId).put({ board: JSON.stringify(moves) }); + gun.get(PacoSakoUUID + '/game/' + gameId).get('board').put(JSON.stringify(moves)); putMeta(); } @@ -439,21 +439,26 @@ $(function (){ const lightName = $('#cb_light_name').val(); const darkName = $('#cb_dark_name').val(); const turns = currentGame.countTurns(); - let meta = null; + const winner = currentGame.winner; + const lastMove = currentGame.lastMove || {}; + const lastMeta = lastMove.meta || {}; + const status = !winner ? null : (lastMove.took === PS.KING) ? 'mate' : 'ended'; + + const meta = { + lightName, + darkName, + moves: turns, + timestamp: lastMeta.timestamp || new Date(Gun.state()).getTime(), + status, + }; + + const metaRef = gun.get(PacoSakoUUID + '/meta/' + gameId).put(meta); + if (lightName !== '' || darkName !== '' || turns !== 0) { - const winner = currentGame.winner; - const lastMove = currentGame.lastMove || {}; - const lastMeta = lastMove.meta || {}; - const status = !winner ? null : (lastMove.took === PS.KING) ? 'mate' : 'ended'; - meta = { - lightName: lightName, - darkName: darkName, - moves: turns, - timestamp: lastMeta.timestamp || new Date(Gun.state()).getTime(), - status: status, - }; + gun.get(PacoSakoUUID + '/meta').get(gameId).put(metaRef); + } else { + gun.get(PacoSakoUUID + '/meta').get(gameId).put(null); } - gun.get(PacoSakoUUID).get('meta').put({ [gameId]: meta }); } function switchGameId(newId){ @@ -489,7 +494,7 @@ $(function (){ $('#cb_light_name').val(''); $('#cb_dark_name').val(''); - cancelGameCallback = gun.get(PacoSakoUUID).get('games').get(newId).onWithCancel(function(d) { + cancelGameCallback = gun.get(PacoSakoUUID + '/game/' + newId).onWithCancel(function(d) { if (d && d.board) { try { const received = JSON.parse(d.board); @@ -526,7 +531,7 @@ $(function (){ } }); - cancelMetaCallback = gun.get(PacoSakoUUID).get('meta').get(newId).onWithCancel(function(d) { + cancelMetaCallback = gun.get(PacoSakoUUID + '/meta').get(newId).onWithCancel(function(d) { d = d || {}; debug('got meta', d); $('#cb_board').data('lightName', shortenName(String(d.lightName || 'Light'))); @@ -885,7 +890,7 @@ $(function (){ } let cancellers = {}; - let cancelAll = gun.get(PacoSakoUUID).get('meta').onWithCancel(function(meta) { + let cancelAll = gun.get(PacoSakoUUID + '/meta').onWithCancel(function(meta) { for (const gameId in meta) { /* use of 'in' here is deliberate */ /* 'gameId' may include extra GUN fields like '_' */ if (gameId.match(/^[0-9a-f]{16}$/)) { @@ -956,67 +961,22 @@ $(function (){ /* Low-level commands to be run from the JS console */ window.Admin = { - convertFromV1: function() { - const PacoSakoUUIDv1 = '7c38edd4-c931-49c8-9f1a-84de560815db'; - gun.get(PacoSakoUUIDv1).get('games').map().once(function(d,key){ + convertFromV2: function() { + gun.get(PacoSakoUUID).get('games').map().once(function(d,key){ if (d && d.board) { debug('converting ' + key); - const game = new PS.Game(); - let moves = []; - - try { - let board = JSON.parse(d.board); - - while (board.prior) { - moves.push(board.move); - board = board.prior; - } - moves.reverse(); - - for (const move of moves) { - if (move.to) { - game.move(move.from, move.to, move.timestamp && { timestamp: move.timestamp }); - } else if (move.resign) { - game.resign(); - } else { - throw { message: 'unknown move', move: move }; - } - } - - gun.get(PacoSakoUUID).get('games').get(key).put({ board: JSON.stringify({ - past: game.moves, - future: [], - })}); - } catch (err) { - debug('conversion of ' + key + ' failed', err, game, moves); - } + gun.get(PacoSakoUUID + '/game/' + key).get('board').put(d.board); } }); - gun.get(PacoSakoUUIDv1).get('meta').map().once(function(d,key){ + gun.get(PacoSakoUUID).get('meta').map().once(function(d,key){ if (d) { debug('converting metadata for ' + key); - gun.get(PacoSakoUUID).get('meta').get(key).put({ - lightName: d.lightName, - darkName: d.darkName, - moves: d.moves, - timestamp: d.timestamp, - status: d.status, - }); + gun.get(PacoSakoUUID + '/meta').get(key).put(d); } }); }, - cleanupMissingGames: function() { - gun.get(PacoSakoUUID).get('games').once(function(games){ - gun.get(PacoSakoUUID).get('meta').map().once(function(d,key){ - if (!(key in games)) { - gun.get(PacoSakoUUID).get('meta').get(key).put(null); - } - }); - }); - }, - getCurrentGame: function() { return currentGame; }, getVisibleGame: function() { return visibleGame; }, setCurrentGame: setCurrentGame, diff --git a/webpack.config.js b/webpack.config.js index 8a57e72..8cd3dcc 100644 --- a/webpack.config.js +++ b/webpack.config.js @@ -8,6 +8,7 @@ const MiniCssExtractPlugin = require('mini-css-extract-plugin'); const OptimizeCSSAssetsPlugin = require('optimize-css-assets-webpack-plugin'); module.exports = { + mode: 'production', entry: { index: './index.js' },