Try and make sense of the two data structures related to games with a good renaming
This commit is contained in:
parent
428e48f6f4
commit
19aa959c64
3 changed files with 20 additions and 20 deletions
|
@ -24,7 +24,7 @@ function init() {
|
||||||
});
|
});
|
||||||
Messaging.addEventListener(["LogOut"], function(o) {
|
Messaging.addEventListener(["LogOut"], function(o) {
|
||||||
// Just in case there was a game proposal from that player, in which case the game proposal's ID is the player's ID
|
// Just in case there was a game proposal from that player, in which case the game proposal's ID is the player's ID
|
||||||
Games.entries.remove(o.from);
|
Games.metadata.remove(o.from);
|
||||||
refresh();
|
refresh();
|
||||||
});
|
});
|
||||||
Messaging.addEventListener(["Relay", "Invitation"], function(o) {
|
Messaging.addEventListener(["Relay", "Invitation"], function(o) {
|
||||||
|
@ -33,12 +33,12 @@ function init() {
|
||||||
GamesGUI.refresh();
|
GamesGUI.refresh();
|
||||||
});
|
});
|
||||||
Messaging.addEventListener(["Relay", "Answer"], function(o) {
|
Messaging.addEventListener(["Relay", "Answer"], function(o) {
|
||||||
var gameEntry = Games.entries.get(o.from);
|
var gameEntry = Games.metadata.get(o.from);
|
||||||
if(gameEntry != undefined) {
|
if(gameEntry != undefined) {
|
||||||
if(!o.message.accept) {
|
if(!o.message.accept) {
|
||||||
gameEntry.answer = false;
|
gameEntry.answer = false;
|
||||||
} else {
|
} else {
|
||||||
Games.entries.remove(o.from);
|
Games.metadata.remove(o.from);
|
||||||
}
|
}
|
||||||
GamesGUI.refresh();
|
GamesGUI.refresh();
|
||||||
}
|
}
|
||||||
|
|
|
@ -32,7 +32,7 @@ function gameProposal(game) {
|
||||||
if(game.value.answer != undefined) {
|
if(game.value.answer != undefined) {
|
||||||
properties.textContent = I18n.get('refusedGame')(game.value.vs.name);
|
properties.textContent = I18n.get('refusedGame')(game.value.vs.name);
|
||||||
properties.class = 'clickable';
|
properties.class = 'clickable';
|
||||||
properties.onClick = function() {Games.entries.remove(game.key); refresh();};
|
properties.onClick = function() {Games.metadata.remove(game.key); refresh();};
|
||||||
} else {
|
} else {
|
||||||
properties.textContent = I18n.get('proposedGame')(
|
properties.textContent = I18n.get('proposedGame')(
|
||||||
game.value.yourTurn,
|
game.value.yourTurn,
|
||||||
|
@ -60,7 +60,7 @@ function pendingGame(game) {
|
||||||
function answer(key, accept) {
|
function answer(key, accept) {
|
||||||
return function() {
|
return function() {
|
||||||
Messaging.send({tag: "Answer", accept: accept, to: key});
|
Messaging.send({tag: "Answer", accept: accept, to: key});
|
||||||
Games.entries.remove(key);
|
Games.metadata.remove(key);
|
||||||
refresh();
|
refresh();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -79,7 +79,7 @@ function answerDialog(key) {
|
||||||
}
|
}
|
||||||
|
|
||||||
function refresh() {
|
function refresh() {
|
||||||
var sortedGames = Games.entries.getAll();
|
var sortedGames = Games.metadata.getAll();
|
||||||
list.refresh(sortedGames);
|
list.refresh(sortedGames);
|
||||||
if(sortedGames.length < 1) {
|
if(sortedGames.length < 1) {
|
||||||
list.message.textContent = I18n.get('noGames');
|
list.message.textContent = I18n.get('noGames');
|
||||||
|
|
28
js/Games.js
28
js/Games.js
|
@ -5,16 +5,16 @@ import Session;
|
||||||
import Table;
|
import Table;
|
||||||
import Time;
|
import Time;
|
||||||
|
|
||||||
var entries = Table.make(function(o) {return o.value.date;});
|
var metadata = Table.make(function(o) {return o.value.date;});
|
||||||
entries.insertAll(Save.get('games.entry') || {});
|
metadata.insertAll(Save.get('games.metadata') || {});
|
||||||
initMessageHandlers();
|
initMessageHandlers();
|
||||||
|
|
||||||
return {
|
return {
|
||||||
entries: entries,
|
metadata: metadata,
|
||||||
proposal: proposal
|
proposal: proposal
|
||||||
};
|
};
|
||||||
|
|
||||||
function makeEntry(state) {
|
function makeMetadata(state) {
|
||||||
var sessionKey = Session.getKey();
|
var sessionKey = Session.getKey();
|
||||||
return Time.timestamp({
|
return Time.timestamp({
|
||||||
vs: player(state.public.nextPlayer[sessionKey]),
|
vs: player(state.public.nextPlayer[sessionKey]),
|
||||||
|
@ -23,20 +23,20 @@ function makeEntry(state) {
|
||||||
}
|
}
|
||||||
|
|
||||||
function proposal(playerID, yourTurn) {
|
function proposal(playerID, yourTurn) {
|
||||||
entries.insert(
|
metadata.insert(
|
||||||
playerID,
|
playerID,
|
||||||
Time.timestamp({vs: player(playerID), yourTurn: yourTurn})
|
Time.timestamp({vs: player(playerID), yourTurn: yourTurn})
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
function pushMessage(gameID, message) {
|
function pushState(gameID, state) {
|
||||||
var latestPath = "games.message." + gameID + ".latest";
|
var latestPath = "games.state." + gameID + ".latest";
|
||||||
var formerPath = "games.message." + gameID + ".former";
|
var formerPath = "games.state." + gameID + ".former";
|
||||||
var former = Save.get(latestPath);
|
var former = Save.get(latestPath);
|
||||||
if(former != undefined) {
|
if(former != undefined) {
|
||||||
Save.set(formerPath, former);
|
Save.set(formerPath, former);
|
||||||
}
|
}
|
||||||
Save.set(latestPath, message);
|
Save.set(latestPath, state);
|
||||||
}
|
}
|
||||||
|
|
||||||
function compare(gameCoordinatesA, gameCoordinatesB) {
|
function compare(gameCoordinatesA, gameCoordinatesB) {
|
||||||
|
@ -56,15 +56,15 @@ function compare(gameCoordinatesA, gameCoordinatesB) {
|
||||||
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 metadata = makeMetadata(o.state);
|
||||||
pushMessage(gameID, o.state);
|
pushState(gameID, o.state);
|
||||||
Save.set("games.entry." + gameID, entry);
|
Save.set("games.metadata." + gameID, metadata);
|
||||||
entries.insert(gameID, entry);
|
metadata.insert(gameID, metadata);
|
||||||
});
|
});
|
||||||
|
|
||||||
Messaging.addEventListener(["Relay", "Sync"], function(o) {
|
Messaging.addEventListener(["Relay", "Sync"], function(o) {
|
||||||
var gameID = o.message.latestKnown.gameID;
|
var gameID = o.message.latestKnown.gameID;
|
||||||
var latestKnownHere = Save.get("games.message." + gameID + ".latest");
|
var latestKnownHere = Save.get("games.state." + gameID + ".latest");
|
||||||
switch(compare(latestKnownHere, o.message.latestKnown)) {
|
switch(compare(latestKnownHere, o.message.latestKnown)) {
|
||||||
case -1: Messaging.send({tag: "Yield", onGameID: gameID});
|
case -1: Messaging.send({tag: "Yield", onGameID: gameID});
|
||||||
case 1: Messaging.send({tag: "Sync", latestKnown: latestKnownHere});
|
case 1: Messaging.send({tag: "Sync", latestKnown: latestKnownHere});
|
||||||
|
|
Loading…
Reference in a new issue