2020-01-13 08:39:32 +01:00
|
|
|
import Messaging;
|
|
|
|
import opponent from Room;
|
|
|
|
import Save;
|
|
|
|
import Session;
|
|
|
|
import Table;
|
2020-01-13 18:05:11 +01:00
|
|
|
import Time;
|
2020-01-13 08:39:32 +01:00
|
|
|
|
2020-01-13 23:08:20 +01:00
|
|
|
var entries = Table.make(function(o) {return o.value.date;});
|
2020-01-13 23:09:02 +01:00
|
|
|
entries.insertAll(Save.get('games.entry') || {});
|
2020-01-13 08:39:32 +01:00
|
|
|
initMessageHandlers();
|
|
|
|
|
|
|
|
return {
|
2020-01-14 09:55:04 +01:00
|
|
|
entries: entries,
|
|
|
|
proposal: proposal
|
2020-01-13 08:39:32 +01:00
|
|
|
};
|
|
|
|
|
2020-01-13 23:09:02 +01:00
|
|
|
function makeEntry(state) {
|
2020-01-13 08:39:32 +01:00
|
|
|
var sessionKey = Session.getKey();
|
2020-01-13 18:05:11 +01:00
|
|
|
return Time.timestamp({
|
2020-01-13 08:39:32 +01:00
|
|
|
vs: opponent(state.public.nextPlayer[sessionKey]),
|
|
|
|
yourTurn: state.public.playing == sessionKey
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-01-14 09:55:04 +01:00
|
|
|
function proposal(playerID, yourTurn) {
|
|
|
|
entries.insert(
|
|
|
|
playerID,
|
|
|
|
Time.timestamp({vs: opponent(playerID), yourTurn: yourTurn})
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-01-13 08:39:32 +01:00
|
|
|
function initMessageHandlers() {
|
|
|
|
Messaging.addEventListener(["Game"], function(o) {
|
2020-01-13 18:05:11 +01:00
|
|
|
var gameID = o.state.public.gameState.gameID;
|
2020-01-13 23:09:02 +01:00
|
|
|
var entry = makeEntry(o.state);
|
2020-01-13 18:05:11 +01:00
|
|
|
Save.set("games.state." + gameID, o.state);
|
2020-01-13 23:09:02 +01:00
|
|
|
Save.set("games.entry." + gameID, entry);
|
|
|
|
entries.insert(gameID, entry);
|
2020-01-13 08:39:32 +01:00
|
|
|
});
|
|
|
|
}
|