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();
|
initMessageHandlers();
|
||||||
Async.run(
|
Async.run(
|
||||||
Async.bind(
|
Async.bind(
|
||||||
getSavedState(gameID),
|
getSavedMessages(gameID),
|
||||||
function(o) {
|
function(messages) {
|
||||||
return Async.sequence(
|
return Async.sequence(
|
||||||
startSession(),
|
startSession(),
|
||||||
setGame(o)
|
previously(messages)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
@ -51,9 +51,9 @@ function fail(errorCode) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function getSavedState(gameID) {
|
function getSavedMessages(gameID) {
|
||||||
var coordinates = Save.get('games.message.Game#' + gameID);
|
var messages = Save.get('games.message.Game#' + gameID);
|
||||||
return coordinates != undefined ? Async.wrap(coordinates) : fail('gameNotFound');
|
return messages != undefined ? Async.wrap(messages) : fail('gameNotFound');
|
||||||
}
|
}
|
||||||
|
|
||||||
function startSession() {
|
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() {
|
function initMessageHandlers() {
|
||||||
window.addEventListener('focus', catchUp);
|
window.addEventListener('focus', catchUp);
|
||||||
Messaging.addEventListener(["Game"], function(o) {
|
Messaging.addEventListener(["Game"], function(o) {
|
||||||
|
@ -79,7 +90,7 @@ function initMessageHandlers() {
|
||||||
}
|
}
|
||||||
|
|
||||||
function handleGameMessage(o) {
|
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
|
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
|
return Async.sequence(applyDiff(o), setGame(o)); // so play the diff, then set the new round
|
||||||
} else {
|
} 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() {
|
function initMessageHandlers() {
|
||||||
Messaging.addEventListener(["Game"], function(o) {
|
Messaging.addEventListener(["Game"], function(o) {
|
||||||
var gameID = o.state.public.coordinates.gameID;
|
var gameID = o.state.public.coordinates.gameID;
|
||||||
var entry = makeEntry(o.state);
|
var entry = makeEntry(o.state);
|
||||||
Save.set("games.message." + gameID, o);
|
pushMessage(gameID, o);
|
||||||
Save.set("games.entry." + gameID, entry);
|
Save.set("games.entry." + gameID, entry);
|
||||||
entries.insert(gameID, entry);
|
entries.insert(gameID, entry);
|
||||||
});
|
});
|
||||||
|
|
Loading…
Reference in a new issue