diff --git a/js/GUI/Screen/Hall.js b/js/GUI/Screen/Hall.js index d7cae1b..9b66e73 100644 --- a/js/GUI/Screen/Hall.js +++ b/js/GUI/Screen/Hall.js @@ -4,7 +4,6 @@ import * as Players from GUI.Screen.Hall.Players; import * as Games from GUI.Screen.Hall.Games; import Messaging; import opponent from Room; -import Session; import StatusHandler; return { @@ -23,24 +22,19 @@ function init() { }); Messaging.addEventListener(["LogOut"], function(o) { // Just in case there was a game proposal from that player, in which case the game proposal's ID is the player's ID - Games.table.remove(o.from); + Games.entries.remove(o.from); refresh(); }); Messaging.addEventListener(["Relay", "Invitation"], function(o) { StatusHandler.set("🎴"); - Games.table.insert(o.from, {vs: opponent(o.from), yourTurn: true}); + Games.entries.insert(o.from, {vs: opponent(o.from), yourTurn: true}); Games.refresh(); }); Messaging.addEventListener(["Relay", "Answer"], function(o) { - Games.table.remove(o.from); + Games.entries.remove(o.from); Games.refresh(); }); Messaging.addEventListener(["Game"], function(o) { - var sessionKey = Session.getKey(); - Games.table.insert(o.state.gameID, { - vs: opponent(o.state.public.nextPlayer[sessionKey]), - yourTurn: o.state.public.playing == sessionKey - }); Games.refresh(); }); } diff --git a/js/GUI/Screen/Hall/Games.js b/js/GUI/Screen/Hall/Games.js index 004bc2c..1aeebec 100644 --- a/js/GUI/Screen/Hall/Games.js +++ b/js/GUI/Screen/Hall/Games.js @@ -1,28 +1,35 @@ import * as ListSelector from GUI.ListSelector; +import Games; import I18n; import Messaging; -import room from Room; +import players from Room; import dialog from GUI.Screen; -import Table; import * as Dom from UnitJS.Dom; -var games = Table.make(game, 'date'); var list = ListSelector.make('games', showGame); +initMessageHandlers(); return { - refresh: refresh, - table: games + entries: entries, + refresh: refresh }; -function game(key, config) { +function entry(key, config) { return { key: key, vs: config.vs, yourTurn: config.yourTurn, - date: Date.now() + date: config.date }; } +function initMessageHandlers() { + Messaging.addEventListener(["Game"], function(o) { + var sessionKey = Session.getKey(); + entries.insert(o.state.gameID, Games.entry(o.state)); + }); +} + function showGame(game) { var liContent; if(game.key.match(/^Player#/)) { // Game proposals use the ID of the opponent as ID @@ -45,7 +52,7 @@ function gameProposal(game) { } function pendingGame(game) { - var status = room.get(game.vs.id) != undefined ? 'active' : 'inactive' + var status = players.get(game.vs.id) != undefined ? 'active' : 'inactive' return [ Dom.make('span', { textContent: status}), Dom.make('a', { @@ -58,7 +65,7 @@ function pendingGame(game) { function answer(key, accept) { return function() { Messaging.send({tag: "Answer", accept: accept, to: key}); - games.remove(key); + entries.remove(key); refresh(); } } @@ -77,7 +84,7 @@ function answerDialog(key) { } function refresh() { - var sortedGames = games.getAll(); + var sortedGames = entries.getAll(); list.refresh(sortedGames); if(sortedGames.length < 1) { list.message.textContent = I18n.get('noGames'); diff --git a/js/GUI/Screen/Hall/Players.js b/js/GUI/Screen/Hall/Players.js index 5a9bd52..668bd57 100644 --- a/js/GUI/Screen/Hall/Players.js +++ b/js/GUI/Screen/Hall/Players.js @@ -3,7 +3,7 @@ import * as ListSelector from GUI.ListSelector; import * as Games from GUI.Screen.Hall.Games; import I18n; import Messaging; -import {opponent, room} from Room; +import {opponent, players} from Room; import * as Dom from UnitJS.Dom; var form = ConnectedForm.get('room'); @@ -21,7 +21,7 @@ function initDOM() { form.root.addEventListener('submit', function(e) { e.preventDefault(); Messaging.send({tag: "Invitation", to: them}); - Games.table.insert(them, {vs: opponent(them), yourTurn: false}); + Games.entries.insert(them, {vs: opponent(them), yourTurn: false}); Games.refresh(); }); form.root.them.addEventListener("input", function() {refresh();}); @@ -38,7 +38,7 @@ function showPlayer(player) { function refresh() { var name = form.root.them.value; them = null; - var filtered = room.getAll( + var filtered = players.getAll( function(player) {return player.name.match(name);} ); list.refresh(filtered); diff --git a/js/Games.js b/js/Games.js new file mode 100644 index 0000000..dd20d43 --- /dev/null +++ b/js/Games.js @@ -0,0 +1,36 @@ +import Messaging; +import opponent from Room; +import Save; +import Session; +import Table; + +var entries = Table.make(makeEntry, 'date'); +initMessageHandlers(); + +return { + entry: entry, + timestamp: timestamp +}; + +function entry(state) { + var sessionKey = Session.getKey(); + return timestamp({ + vs: opponent(state.public.nextPlayer[sessionKey]), + yourTurn: state.public.playing == sessionKey + }); +} + +function timestamp(config) { + return { + vs: config.vs, + yourTurn: config.yourTurn, + date: Date.now() + }; +} + +function initMessageHandlers() { + Messaging.addEventListener(["Game"], function(o) { + Save.set("games.state." + o.state.gameID, o.state); + Save.set("games.entry." + o.state.gameID, entry(o.state)); + }); +} diff --git a/js/Room.js b/js/Room.js index 853d454..f996dc8 100644 --- a/js/Room.js +++ b/js/Room.js @@ -2,16 +2,16 @@ import Messaging; import Session; import Table; -var room = Table.make(player, 'name'); +var players = Table.make(player, 'name'); initMessageHandlers(); return { opponent: opponent, - room: room + players: players }; function opponent(key) { - return {id: key, name: room.get(key).name}; + return {id: key, name: players.get(key).name}; } function player(key, name) { @@ -23,17 +23,17 @@ function player(key, name) { function initMessageHandlers() { Messaging.addEventListener(["Okaeri"], function(o) { - room.insertAll(o.room); + players.insertAll(o.room); }); Messaging.addEventListener(["Welcome"], function(o) { - room.insertAll(o.room); + players.insertAll(o.room); }); Messaging.addEventListener(["LogIn"], function(o) { if(!Session.is(o.from)) { - room.insert(o.from, o.as); + players.insert(o.from, o.as); } }); Messaging.addEventListener(["LogOut"], function(o) { - room.remove(o.from); + players.remove(o.from); }); } diff --git a/js/Table.js b/js/Table.js index 5819769..5ab4ade 100644 --- a/js/Table.js +++ b/js/Table.js @@ -4,6 +4,7 @@ import {compare, of, proj} from UnitJS.Fun; function Table(itemMaker, sortCriterion) { var items = {}; return { + dump: dump, get: get, getAll: getAll, insert: insert, @@ -11,6 +12,10 @@ function Table(itemMaker, sortCriterion) { remove: remove }; + function dump() { + return items; + } + function get(key) { return items[key]; } @@ -46,6 +51,10 @@ function Table(itemMaker, sortCriterion) { function remove(key) { delete items[key]; } + + function restore(dumped) { + items = dumped; + } } return {