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) {
|
||||
// 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();
|
||||
});
|
||||
Messaging.addEventListener(["Relay", "Invitation"], function(o) {
|
||||
|
@ -33,12 +33,12 @@ function init() {
|
|||
GamesGUI.refresh();
|
||||
});
|
||||
Messaging.addEventListener(["Relay", "Answer"], function(o) {
|
||||
var gameEntry = Games.entries.get(o.from);
|
||||
var gameEntry = Games.metadata.get(o.from);
|
||||
if(gameEntry != undefined) {
|
||||
if(!o.message.accept) {
|
||||
gameEntry.answer = false;
|
||||
} else {
|
||||
Games.entries.remove(o.from);
|
||||
Games.metadata.remove(o.from);
|
||||
}
|
||||
GamesGUI.refresh();
|
||||
}
|
||||
|
|
|
@ -32,7 +32,7 @@ function gameProposal(game) {
|
|||
if(game.value.answer != undefined) {
|
||||
properties.textContent = I18n.get('refusedGame')(game.value.vs.name);
|
||||
properties.class = 'clickable';
|
||||
properties.onClick = function() {Games.entries.remove(game.key); refresh();};
|
||||
properties.onClick = function() {Games.metadata.remove(game.key); refresh();};
|
||||
} else {
|
||||
properties.textContent = I18n.get('proposedGame')(
|
||||
game.value.yourTurn,
|
||||
|
@ -60,7 +60,7 @@ function pendingGame(game) {
|
|||
function answer(key, accept) {
|
||||
return function() {
|
||||
Messaging.send({tag: "Answer", accept: accept, to: key});
|
||||
Games.entries.remove(key);
|
||||
Games.metadata.remove(key);
|
||||
refresh();
|
||||
}
|
||||
}
|
||||
|
@ -79,7 +79,7 @@ function answerDialog(key) {
|
|||
}
|
||||
|
||||
function refresh() {
|
||||
var sortedGames = Games.entries.getAll();
|
||||
var sortedGames = Games.metadata.getAll();
|
||||
list.refresh(sortedGames);
|
||||
if(sortedGames.length < 1) {
|
||||
list.message.textContent = I18n.get('noGames');
|
||||
|
|
28
js/Games.js
28
js/Games.js
|
@ -5,16 +5,16 @@ import Session;
|
|||
import Table;
|
||||
import Time;
|
||||
|
||||
var entries = Table.make(function(o) {return o.value.date;});
|
||||
entries.insertAll(Save.get('games.entry') || {});
|
||||
var metadata = Table.make(function(o) {return o.value.date;});
|
||||
metadata.insertAll(Save.get('games.metadata') || {});
|
||||
initMessageHandlers();
|
||||
|
||||
return {
|
||||
entries: entries,
|
||||
metadata: metadata,
|
||||
proposal: proposal
|
||||
};
|
||||
|
||||
function makeEntry(state) {
|
||||
function makeMetadata(state) {
|
||||
var sessionKey = Session.getKey();
|
||||
return Time.timestamp({
|
||||
vs: player(state.public.nextPlayer[sessionKey]),
|
||||
|
@ -23,20 +23,20 @@ function makeEntry(state) {
|
|||
}
|
||||
|
||||
function proposal(playerID, yourTurn) {
|
||||
entries.insert(
|
||||
metadata.insert(
|
||||
playerID,
|
||||
Time.timestamp({vs: player(playerID), yourTurn: yourTurn})
|
||||
);
|
||||
}
|
||||
|
||||
function pushMessage(gameID, message) {
|
||||
var latestPath = "games.message." + gameID + ".latest";
|
||||
var formerPath = "games.message." + gameID + ".former";
|
||||
function pushState(gameID, state) {
|
||||
var latestPath = "games.state." + gameID + ".latest";
|
||||
var formerPath = "games.state." + gameID + ".former";
|
||||
var former = Save.get(latestPath);
|
||||
if(former != undefined) {
|
||||
Save.set(formerPath, former);
|
||||
}
|
||||
Save.set(latestPath, message);
|
||||
Save.set(latestPath, state);
|
||||
}
|
||||
|
||||
function compare(gameCoordinatesA, gameCoordinatesB) {
|
||||
|
@ -56,15 +56,15 @@ function compare(gameCoordinatesA, gameCoordinatesB) {
|
|||
function initMessageHandlers() {
|
||||
Messaging.addEventListener(["Game"], function(o) {
|
||||
var gameID = o.state.public.coordinates.gameID;
|
||||
var entry = makeEntry(o.state);
|
||||
pushMessage(gameID, o.state);
|
||||
Save.set("games.entry." + gameID, entry);
|
||||
entries.insert(gameID, entry);
|
||||
var metadata = makeMetadata(o.state);
|
||||
pushState(gameID, o.state);
|
||||
Save.set("games.metadata." + gameID, metadata);
|
||||
metadata.insert(gameID, metadata);
|
||||
});
|
||||
|
||||
Messaging.addEventListener(["Relay", "Sync"], function(o) {
|
||||
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)) {
|
||||
case -1: Messaging.send({tag: "Yield", onGameID: gameID});
|
||||
case 1: Messaging.send({tag: "Sync", latestKnown: latestKnownHere});
|
||||
|
|
Loading…
Reference in a new issue