Keep sorting things between view, models, full game states and games list entries — should yield to simplifying Table module

This commit is contained in:
Tissevert 2020-01-13 18:05:11 +01:00
parent 2fc3ba8308
commit 26020a5539
4 changed files with 34 additions and 21 deletions

View file

@ -1,10 +1,12 @@
import * as Dom from UnitJS.Dom;
import Games;
import I18n;
import * as Players from GUI.Screen.Hall.Players;
import * as Games from GUI.Screen.Hall.Games;
import * as GamesGUI from GUI.Screen.Hall.Games;
import Messaging;
import opponent from Room;
import StatusHandler;
import Time;
return {
init: init
@ -27,19 +29,22 @@ function init() {
});
Messaging.addEventListener(["Relay", "Invitation"], function(o) {
StatusHandler.set("🎴");
Games.entries.insert(o.from, {vs: opponent(o.from), yourTurn: true});
Games.refresh();
Games.entries.insert(
o.from,
Time.timestamp({vs: opponent(o.from), yourTurn: true})
);
GamesGUI.refresh();
});
Messaging.addEventListener(["Relay", "Answer"], function(o) {
Games.entries.remove(o.from);
Games.refresh();
GamesGUI.refresh();
});
Messaging.addEventListener(["Game"], function(o) {
Games.refresh();
GamesGUI.refresh();
});
}
function refresh() {
Players.refresh();
Games.refresh();
GamesGUI.refresh();
}

View file

@ -7,13 +7,13 @@ import dialog from GUI.Screen;
import * as Dom from UnitJS.Dom;
var list = ListSelector.make('games', showGame);
initMessageHandlers();
//initMessageHandlers();
return {
entries: entries,
refresh: refresh
};
/*
function entry(key, config) {
return {
key: key,
@ -26,9 +26,10 @@ function entry(key, config) {
function initMessageHandlers() {
Messaging.addEventListener(["Game"], function(o) {
var sessionKey = Session.getKey();
entries.insert(o.state.gameID, Games.entry(o.state));
Games.entries.insert(o.state.gameID, Games.entry(o.state));
});
}
*/
function showGame(game) {
var liContent;
@ -65,7 +66,7 @@ function pendingGame(game) {
function answer(key, accept) {
return function() {
Messaging.send({tag: "Answer", accept: accept, to: key});
entries.remove(key);
Games.entries.remove(key);
refresh();
}
}
@ -84,7 +85,7 @@ function answerDialog(key) {
}
function refresh() {
var sortedGames = entries.getAll();
var sortedGames = Games.entries.getAll();
list.refresh(sortedGames);
if(sortedGames.length < 1) {
list.message.textContent = I18n.get('noGames');

View file

@ -1,9 +1,11 @@
import Games;
import * as ConnectedForm from GUI.ConnectedForm;
import * as ListSelector from GUI.ListSelector;
import * as Games from GUI.Screen.Hall.Games;
import * as GamesGUI from GUI.Screen.Hall.Games;
import I18n;
import Messaging;
import {opponent, players} from Room;
import Time;
import * as Dom from UnitJS.Dom;
var form = ConnectedForm.get('room');
@ -21,8 +23,11 @@ function initDOM() {
form.root.addEventListener('submit', function(e) {
e.preventDefault();
Messaging.send({tag: "Invitation", to: them});
Games.entries.insert(them, {vs: opponent(them), yourTurn: false});
Games.refresh();
Games.entries.insert(
them,
Time.timestamp({vs: opponent(them), yourTurn: false})
);
GamesGUI.refresh();
});
form.root.them.addEventListener("input", function() {refresh();});
}

View file

@ -3,34 +3,36 @@ import opponent from Room;
import Save;
import Session;
import Table;
import Time;
var entries = Table.make(makeEntry, 'date');
initMessageHandlers();
return {
entry: entry,
timestamp: timestamp
entries: entries
};
function entry(state) {
var sessionKey = Session.getKey();
return timestamp({
return Time.timestamp({
vs: opponent(state.public.nextPlayer[sessionKey]),
yourTurn: state.public.playing == sessionKey
});
}
function timestamp(config) {
function makeEntry(key, config) {
return {
key: key,
vs: config.vs,
yourTurn: config.yourTurn,
date: Date.now()
date: config.date
};
}
function initMessageHandlers() {
Messaging.addEventListener(["Game"], function(o) {
Save.set("games.state." + o.state.gameID, o.state);
Save.set("games.entry." + o.state.gameID, entry(o.state));
var gameID = o.state.public.gameState.gameID;
Save.set("games.state." + gameID, o.state);
Save.set("games.entry." + gameID, entry(o.state));
});
}