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

View file

@ -7,13 +7,13 @@ import dialog from GUI.Screen;
import * as Dom from UnitJS.Dom; import * as Dom from UnitJS.Dom;
var list = ListSelector.make('games', showGame); var list = ListSelector.make('games', showGame);
initMessageHandlers(); //initMessageHandlers();
return { return {
entries: entries,
refresh: refresh refresh: refresh
}; };
/*
function entry(key, config) { function entry(key, config) {
return { return {
key: key, key: key,
@ -26,9 +26,10 @@ function entry(key, config) {
function initMessageHandlers() { function initMessageHandlers() {
Messaging.addEventListener(["Game"], function(o) { Messaging.addEventListener(["Game"], function(o) {
var sessionKey = Session.getKey(); 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) { function showGame(game) {
var liContent; var liContent;
@ -65,7 +66,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});
entries.remove(key); Games.entries.remove(key);
refresh(); refresh();
} }
} }
@ -84,7 +85,7 @@ function answerDialog(key) {
} }
function refresh() { function refresh() {
var sortedGames = entries.getAll(); var sortedGames = Games.entries.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');

View file

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

View file

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