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-23 22:11:47 +01:00
|
|
|
var metadata = Table.make(function(o) {return o.value.date;});
|
|
|
|
metadata.insertAll(Save.get('games.metadata') || {});
|
2020-01-13 08:39:32 +01:00
|
|
|
initMessageHandlers();
|
|
|
|
|
|
|
|
return {
|
2020-01-23 22:11:47 +01:00
|
|
|
metadata: metadata,
|
2020-01-14 09:55:04 +01:00
|
|
|
proposal: proposal
|
2020-01-13 08:39:32 +01:00
|
|
|
};
|
|
|
|
|
2020-01-23 22:11:47 +01:00
|
|
|
function makeMetadata(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) {
|
2020-01-23 22:11:47 +01:00
|
|
|
metadata.insert(
|
2020-01-14 09:55:04 +01:00
|
|
|
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-23 22:11:47 +01:00
|
|
|
function pushState(gameID, state) {
|
|
|
|
var latestPath = "games.state." + gameID + ".latest";
|
|
|
|
var formerPath = "games.state." + gameID + ".former";
|
2020-01-18 23:10:22 +01:00
|
|
|
var former = Save.get(latestPath);
|
|
|
|
if(former != undefined) {
|
|
|
|
Save.set(formerPath, former);
|
|
|
|
}
|
2020-01-23 22:11:47 +01:00
|
|
|
Save.set(latestPath, state);
|
2020-01-18 23:10:22 +01:00
|
|
|
}
|
|
|
|
|
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-23 22:11:47 +01:00
|
|
|
var metadata = makeMetadata(o.state);
|
|
|
|
pushState(gameID, o.state);
|
|
|
|
Save.set("games.metadata." + gameID, metadata);
|
|
|
|
metadata.insert(gameID, metadata);
|
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;
|
2020-01-23 22:11:47 +01:00
|
|
|
var latestKnownHere = Save.get("games.state." + gameID + ".latest");
|
2020-01-20 22:58:57 +01:00
|
|
|
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
|
|
|
}
|