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-25 10:56:55 +01:00
|
|
|
var states = Table.make(function(o) {return 0;});
|
|
|
|
states.load('games.state');
|
2020-01-23 22:11:47 +01:00
|
|
|
var metadata = Table.make(function(o) {return o.value.date;});
|
2020-01-25 10:56:55 +01:00
|
|
|
metadata.load('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-25 10:56:55 +01:00
|
|
|
function initMessageHandlers() {
|
|
|
|
Messaging.addEventListener(["Game"], function(o) {
|
|
|
|
var gameID = o.state.public.coordinates.gameID;
|
|
|
|
pushState(gameID, o.state);
|
|
|
|
metadata.insert(gameID, getMetadata(o.state));
|
|
|
|
metadata.save('games.metadata');
|
2020-01-13 08:39:32 +01:00
|
|
|
});
|
|
|
|
|
2020-01-25 10:56:55 +01:00
|
|
|
Messaging.addEventListener(["LogIn"], function(o) {
|
|
|
|
var gamesAgainst = states.getAll(
|
|
|
|
function(game) {return isAgainst(game.value, o.from);}
|
|
|
|
);
|
|
|
|
gamesAgainst.forEach(function(game) {
|
|
|
|
Messaging.send({
|
|
|
|
tag: "Sync",
|
|
|
|
latestKnown: game.value.latest.public.coordinates,
|
|
|
|
to: o.from
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
Messaging.addEventListener(["Relay", "Sync"], function(o) {
|
|
|
|
var gameID = o.message.latestKnown.gameID;
|
|
|
|
var state = states.get(gameID);
|
2020-01-30 15:05:12 +01:00
|
|
|
if(state != undefined) {
|
|
|
|
switch(compare(state.latest.public.coordinates, o.message.latestKnown)) {
|
|
|
|
case -1: Messaging.send({tag: "Yield", onGameID: gameID});
|
|
|
|
case 1: Messaging.send({tag: "Share", gameSave: state.latest});
|
|
|
|
}
|
|
|
|
}
|
2020-01-25 10:56:55 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
Messaging.addEventListener(["Relay", "Yield"], function(o) {
|
|
|
|
var gameID = o.message.onGameID;
|
|
|
|
var state = states.get(gameID);
|
|
|
|
if(state != undefined && isAgainst(state, o.from)) {
|
|
|
|
Messaging.send({tag: "Share", gameSave: state.latest});
|
|
|
|
}
|
|
|
|
});
|
2020-01-14 09:55:04 +01:00
|
|
|
}
|
|
|
|
|
2020-01-25 10:56:55 +01:00
|
|
|
function isAgainst(state, playerID) {
|
|
|
|
return state.latest.public.nextPlayer[Session.getKey()] == playerID;
|
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-25 10:56:55 +01:00
|
|
|
function getMetadata(state) {
|
|
|
|
var sessionKey = Session.getKey();
|
|
|
|
return Time.timestamp({
|
|
|
|
vs: player(state.public.nextPlayer[sessionKey]),
|
|
|
|
yourTurn: state.public.playing == sessionKey
|
2020-01-13 08:39:32 +01:00
|
|
|
});
|
2020-01-25 10:56:55 +01:00
|
|
|
}
|
2020-01-20 22:58:57 +01:00
|
|
|
|
2020-01-25 10:56:55 +01:00
|
|
|
function proposal(playerID, yourTurn) {
|
|
|
|
metadata.insert(
|
|
|
|
playerID,
|
|
|
|
Time.timestamp({vs: player(playerID), yourTurn: yourTurn})
|
|
|
|
);
|
|
|
|
}
|
2020-01-20 22:58:57 +01:00
|
|
|
|
2020-01-25 10:56:55 +01:00
|
|
|
function pushState(gameID, newState) {
|
|
|
|
if(states.get(gameID) == undefined) {
|
|
|
|
states.insert(gameID, {});
|
|
|
|
}
|
|
|
|
var state = states.get(gameID);
|
|
|
|
state.former = state.latest;
|
|
|
|
state.latest = newState;
|
|
|
|
states.save('games.state');
|
2020-01-13 08:39:32 +01:00
|
|
|
}
|