import Messaging; import player from Room; import Save; import Session; import Table; import Time; var entries = Table.make(function(o) {return o.value.date;}); entries.insertAll(Save.get('games.entry') || {}); initMessageHandlers(); return { entries: entries, proposal: proposal }; function makeEntry(state) { var sessionKey = Session.getKey(); return Time.timestamp({ vs: player(state.public.nextPlayer[sessionKey]), yourTurn: state.public.playing == sessionKey }); } function proposal(playerID, yourTurn) { entries.insert( playerID, Time.timestamp({vs: player(playerID), yourTurn: yourTurn}) ); } function pushMessage(gameID, message) { var latestPath = "games.message." + gameID + ".latest"; var formerPath = "games.message." + gameID + ".former"; var former = Save.get(latestPath); if(former != undefined) { Save.set(formerPath, former); } Save.set(latestPath, message); } function initMessageHandlers() { Messaging.addEventListener(["Game"], function(o) { var gameID = o.state.public.coordinates.gameID; var entry = makeEntry(o.state); pushMessage(gameID, o); Save.set("games.entry." + gameID, entry); entries.insert(gameID, entry); }); }