webclient/js/Games.js

41 lines
923 B
JavaScript
Raw Normal View History

import Messaging;
import opponent 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: opponent(state.public.nextPlayer[sessionKey]),
yourTurn: state.public.playing == sessionKey
});
}
function proposal(playerID, yourTurn) {
entries.insert(
playerID,
Time.timestamp({vs: opponent(playerID), yourTurn: yourTurn})
);
}
function initMessageHandlers() {
Messaging.addEventListener(["Game"], function(o) {
var gameID = o.state.public.gameState.gameID;
var entry = makeEntry(o.state);
Save.set("games.state." + gameID, o.state);
Save.set("games.entry." + gameID, entry);
entries.insert(gameID, entry);
});
}