Keep previous state of game and use it to replay latest move
This commit is contained in:
parent
02edb77285
commit
ef947f7942
2 changed files with 29 additions and 8 deletions
|
@ -27,11 +27,11 @@ function init(gameID) {
|
|||
initMessageHandlers();
|
||||
Async.run(
|
||||
Async.bind(
|
||||
getSavedState(gameID),
|
||||
function(o) {
|
||||
getSavedMessages(gameID),
|
||||
function(messages) {
|
||||
return Async.sequence(
|
||||
startSession(),
|
||||
setGame(o)
|
||||
previously(messages)
|
||||
);
|
||||
}
|
||||
)
|
||||
|
@ -51,9 +51,9 @@ function fail(errorCode) {
|
|||
}
|
||||
}
|
||||
|
||||
function getSavedState(gameID) {
|
||||
var coordinates = Save.get('games.message.Game#' + gameID);
|
||||
return coordinates != undefined ? Async.wrap(coordinates) : fail('gameNotFound');
|
||||
function getSavedMessages(gameID) {
|
||||
var messages = Save.get('games.message.Game#' + gameID);
|
||||
return messages != undefined ? Async.wrap(messages) : fail('gameNotFound');
|
||||
}
|
||||
|
||||
function startSession() {
|
||||
|
@ -66,6 +66,17 @@ function startSession() {
|
|||
}
|
||||
}
|
||||
|
||||
function previously(messages) {
|
||||
if(messages.former != undefined && messages.latest.logs.length > 0) {
|
||||
return Async.sequence(
|
||||
setGame(messages.former),
|
||||
handleGameMessage(messages.latest)
|
||||
);
|
||||
} else {
|
||||
return setGame(messages.latest);
|
||||
}
|
||||
}
|
||||
|
||||
function initMessageHandlers() {
|
||||
window.addEventListener('focus', catchUp);
|
||||
Messaging.addEventListener(["Game"], function(o) {
|
||||
|
@ -79,7 +90,7 @@ function initMessageHandlers() {
|
|||
}
|
||||
|
||||
function handleGameMessage(o) {
|
||||
if(o.state.public.coordinates.turn == 0 || state.game == null) {
|
||||
if(o.state.public.coordinates.turn == 0) {
|
||||
if(o.logs.length > 0) { // but still some logs, from the previous round
|
||||
return Async.sequence(applyDiff(o), setGame(o)); // so play the diff, then set the new round
|
||||
} else {
|
||||
|
|
12
js/Games.js
12
js/Games.js
|
@ -29,11 +29,21 @@ function proposal(playerID, yourTurn) {
|
|||
);
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
function initMessageHandlers() {
|
||||
Messaging.addEventListener(["Game"], function(o) {
|
||||
var gameID = o.state.public.coordinates.gameID;
|
||||
var entry = makeEntry(o.state);
|
||||
Save.set("games.message." + gameID, o);
|
||||
pushMessage(gameID, o);
|
||||
Save.set("games.entry." + gameID, entry);
|
||||
entries.insert(gameID, entry);
|
||||
});
|
||||
|
|
Loading…
Reference in a new issue