2020-01-13 08:39:32 +01:00
|
|
|
import Messaging;
|
2020-01-14 17:27:56 +01:00
|
|
|
import player from Room;
|
2020-01-13 08:39:32 +01:00
|
|
|
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-14 17:27:56 +01:00
|
|
|
vs: player(state.public.nextPlayer[sessionKey]),
|
2020-01-13 08:39:32 +01:00
|
|
|
yourTurn: state.public.playing == sessionKey
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-01-14 09:55:04 +01:00
|
|
|
function proposal(playerID, yourTurn) {
|
|
|
|
entries.insert(
|
|
|
|
playerID,
|
2020-01-14 17:27:56 +01:00
|
|
|
Time.timestamp({vs: player(playerID), yourTurn: yourTurn})
|
2020-01-14 09:55:04 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-01-18 23:10:22 +01:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2020-01-20 22:58:57 +01:00
|
|
|
function compare(gameCoordinatesA, gameCoordinatesB) {
|
|
|
|
if(gameCoordinatesA.month < gameCoordinatesB.month) {
|
|
|
|
return -1;
|
|
|
|
} else if(gameCoordinatesA.month > gameCoordinatesB.month) {
|
|
|
|
return 1;
|
|
|
|
} else if(gameCoordinatesA.turn < gameCoordinatesB.turn) {
|
|
|
|
return -1;
|
|
|
|
} else if(gameCoordinatesA.turn > gameCoordinatesB.turn) {
|
|
|
|
return 1;
|
|
|
|
} else {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-13 08:39:32 +01:00
|
|
|
function initMessageHandlers() {
|
|
|
|
Messaging.addEventListener(["Game"], function(o) {
|
2020-01-18 09:35:51 +01:00
|
|
|
var gameID = o.state.public.coordinates.gameID;
|
2020-01-13 23:09:02 +01:00
|
|
|
var entry = makeEntry(o.state);
|
2020-01-20 22:58:57 +01:00
|
|
|
pushMessage(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
|
|
|
});
|
2020-01-20 22:58:57 +01:00
|
|
|
|
|
|
|
Messaging.addEventListener(["Relay", "Sync"], function(o) {
|
|
|
|
var gameID = o.message.latestKnown.gameID;
|
|
|
|
var latestKnownHere = Save.get("games.message." + gameID + ".latest");
|
|
|
|
switch(compare(latestKnownHere, o.message.latestKnown)) {
|
|
|
|
case -1: Messaging.send({tag: "Yield", onGameID: gameID});
|
|
|
|
case 1: Messaging.send({tag: "Sync", latestKnown: latestKnownHere});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
Messaging.addEventListener(["Relay", "Yield"], function(o) {
|
|
|
|
var gameID = o.message.latestKnown.gameID;
|
|
|
|
var latestKnownHere = Save.get("games.message." + gameID + ".latest");
|
|
|
|
if(o.from == latestKnownHere.public.nextPlayer[Session.getKey()]) {
|
|
|
|
Messaging.send({tag: "Share", gameSave: latestKnownHere});
|
|
|
|
}
|
|
|
|
});
|
2020-01-13 08:39:32 +01:00
|
|
|
}
|