WIP: still trying to figure things out between Games models, views and the part persisted in localStorage

This commit is contained in:
Tissevert 2020-01-13 08:39:32 +01:00
parent cc8d9e096f
commit abba380f3f
6 changed files with 75 additions and 29 deletions

View file

@ -4,7 +4,6 @@ import * as Players from GUI.Screen.Hall.Players;
import * as Games from GUI.Screen.Hall.Games; import * as Games from GUI.Screen.Hall.Games;
import Messaging; import Messaging;
import opponent from Room; import opponent from Room;
import Session;
import StatusHandler; import StatusHandler;
return { return {
@ -23,24 +22,19 @@ 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.table.remove(o.from); Games.entries.remove(o.from);
refresh(); refresh();
}); });
Messaging.addEventListener(["Relay", "Invitation"], function(o) { Messaging.addEventListener(["Relay", "Invitation"], function(o) {
StatusHandler.set("🎴"); StatusHandler.set("🎴");
Games.table.insert(o.from, {vs: opponent(o.from), yourTurn: true}); Games.entries.insert(o.from, {vs: opponent(o.from), yourTurn: true});
Games.refresh(); Games.refresh();
}); });
Messaging.addEventListener(["Relay", "Answer"], function(o) { Messaging.addEventListener(["Relay", "Answer"], function(o) {
Games.table.remove(o.from); Games.entries.remove(o.from);
Games.refresh(); Games.refresh();
}); });
Messaging.addEventListener(["Game"], function(o) { Messaging.addEventListener(["Game"], function(o) {
var sessionKey = Session.getKey();
Games.table.insert(o.state.gameID, {
vs: opponent(o.state.public.nextPlayer[sessionKey]),
yourTurn: o.state.public.playing == sessionKey
});
Games.refresh(); Games.refresh();
}); });
} }

View file

@ -1,28 +1,35 @@
import * as ListSelector from GUI.ListSelector; import * as ListSelector from GUI.ListSelector;
import Games;
import I18n; import I18n;
import Messaging; import Messaging;
import room from Room; import players from Room;
import dialog from GUI.Screen; import dialog from GUI.Screen;
import Table;
import * as Dom from UnitJS.Dom; import * as Dom from UnitJS.Dom;
var games = Table.make(game, 'date');
var list = ListSelector.make('games', showGame); var list = ListSelector.make('games', showGame);
initMessageHandlers();
return { return {
refresh: refresh, entries: entries,
table: games refresh: refresh
}; };
function game(key, config) { function entry(key, config) {
return { return {
key: key, key: key,
vs: config.vs, vs: config.vs,
yourTurn: config.yourTurn, yourTurn: config.yourTurn,
date: Date.now() date: config.date
}; };
} }
function initMessageHandlers() {
Messaging.addEventListener(["Game"], function(o) {
var sessionKey = Session.getKey();
entries.insert(o.state.gameID, Games.entry(o.state));
});
}
function showGame(game) { function showGame(game) {
var liContent; var liContent;
if(game.key.match(/^Player#/)) { // Game proposals use the ID of the opponent as ID if(game.key.match(/^Player#/)) { // Game proposals use the ID of the opponent as ID
@ -45,7 +52,7 @@ function gameProposal(game) {
} }
function pendingGame(game) { function pendingGame(game) {
var status = room.get(game.vs.id) != undefined ? 'active' : 'inactive' var status = players.get(game.vs.id) != undefined ? 'active' : 'inactive'
return [ return [
Dom.make('span', { textContent: status}), Dom.make('span', { textContent: status}),
Dom.make('a', { Dom.make('a', {
@ -58,7 +65,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.remove(key); entries.remove(key);
refresh(); refresh();
} }
} }
@ -77,7 +84,7 @@ function answerDialog(key) {
} }
function refresh() { function refresh() {
var sortedGames = games.getAll(); var sortedGames = 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

@ -3,7 +3,7 @@ import * as ListSelector from GUI.ListSelector;
import * as Games from GUI.Screen.Hall.Games; import * as Games from GUI.Screen.Hall.Games;
import I18n; import I18n;
import Messaging; import Messaging;
import {opponent, room} from Room; import {opponent, players} from Room;
import * as Dom from UnitJS.Dom; import * as Dom from UnitJS.Dom;
var form = ConnectedForm.get('room'); var form = ConnectedForm.get('room');
@ -21,7 +21,7 @@ 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.table.insert(them, {vs: opponent(them), yourTurn: false}); Games.entries.insert(them, {vs: opponent(them), yourTurn: false});
Games.refresh(); Games.refresh();
}); });
form.root.them.addEventListener("input", function() {refresh();}); form.root.them.addEventListener("input", function() {refresh();});
@ -38,7 +38,7 @@ function showPlayer(player) {
function refresh() { function refresh() {
var name = form.root.them.value; var name = form.root.them.value;
them = null; them = null;
var filtered = room.getAll( var filtered = players.getAll(
function(player) {return player.name.match(name);} function(player) {return player.name.match(name);}
); );
list.refresh(filtered); list.refresh(filtered);

36
js/Games.js Normal file
View file

@ -0,0 +1,36 @@
import Messaging;
import opponent from Room;
import Save;
import Session;
import Table;
var entries = Table.make(makeEntry, 'date');
initMessageHandlers();
return {
entry: entry,
timestamp: timestamp
};
function entry(state) {
var sessionKey = Session.getKey();
return timestamp({
vs: opponent(state.public.nextPlayer[sessionKey]),
yourTurn: state.public.playing == sessionKey
});
}
function timestamp(config) {
return {
vs: config.vs,
yourTurn: config.yourTurn,
date: Date.now()
};
}
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));
});
}

View file

@ -2,16 +2,16 @@ import Messaging;
import Session; import Session;
import Table; import Table;
var room = Table.make(player, 'name'); var players = Table.make(player, 'name');
initMessageHandlers(); initMessageHandlers();
return { return {
opponent: opponent, opponent: opponent,
room: room players: players
}; };
function opponent(key) { function opponent(key) {
return {id: key, name: room.get(key).name}; return {id: key, name: players.get(key).name};
} }
function player(key, name) { function player(key, name) {
@ -23,17 +23,17 @@ function player(key, name) {
function initMessageHandlers() { function initMessageHandlers() {
Messaging.addEventListener(["Okaeri"], function(o) { Messaging.addEventListener(["Okaeri"], function(o) {
room.insertAll(o.room); players.insertAll(o.room);
}); });
Messaging.addEventListener(["Welcome"], function(o) { Messaging.addEventListener(["Welcome"], function(o) {
room.insertAll(o.room); players.insertAll(o.room);
}); });
Messaging.addEventListener(["LogIn"], function(o) { Messaging.addEventListener(["LogIn"], function(o) {
if(!Session.is(o.from)) { if(!Session.is(o.from)) {
room.insert(o.from, o.as); players.insert(o.from, o.as);
} }
}); });
Messaging.addEventListener(["LogOut"], function(o) { Messaging.addEventListener(["LogOut"], function(o) {
room.remove(o.from); players.remove(o.from);
}); });
} }

View file

@ -4,6 +4,7 @@ import {compare, of, proj} from UnitJS.Fun;
function Table(itemMaker, sortCriterion) { function Table(itemMaker, sortCriterion) {
var items = {}; var items = {};
return { return {
dump: dump,
get: get, get: get,
getAll: getAll, getAll: getAll,
insert: insert, insert: insert,
@ -11,6 +12,10 @@ function Table(itemMaker, sortCriterion) {
remove: remove remove: remove
}; };
function dump() {
return items;
}
function get(key) { function get(key) {
return items[key]; return items[key];
} }
@ -46,6 +51,10 @@ function Table(itemMaker, sortCriterion) {
function remove(key) { function remove(key) {
delete items[key]; delete items[key];
} }
function restore(dumped) {
items = dumped;
}
} }
return { return {