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 Messaging;
import opponent from Room;
import Session;
import StatusHandler;
return {
@ -23,24 +22,19 @@ 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.table.remove(o.from);
Games.entries.remove(o.from);
refresh();
});
Messaging.addEventListener(["Relay", "Invitation"], function(o) {
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();
});
Messaging.addEventListener(["Relay", "Answer"], function(o) {
Games.table.remove(o.from);
Games.entries.remove(o.from);
Games.refresh();
});
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();
});
}

View file

@ -1,28 +1,35 @@
import * as ListSelector from GUI.ListSelector;
import Games;
import I18n;
import Messaging;
import room from Room;
import players from Room;
import dialog from GUI.Screen;
import Table;
import * as Dom from UnitJS.Dom;
var games = Table.make(game, 'date');
var list = ListSelector.make('games', showGame);
initMessageHandlers();
return {
refresh: refresh,
table: games
entries: entries,
refresh: refresh
};
function game(key, config) {
function entry(key, config) {
return {
key: key,
vs: config.vs,
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) {
var liContent;
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) {
var status = room.get(game.vs.id) != undefined ? 'active' : 'inactive'
var status = players.get(game.vs.id) != undefined ? 'active' : 'inactive'
return [
Dom.make('span', { textContent: status}),
Dom.make('a', {
@ -58,7 +65,7 @@ function pendingGame(game) {
function answer(key, accept) {
return function() {
Messaging.send({tag: "Answer", accept: accept, to: key});
games.remove(key);
entries.remove(key);
refresh();
}
}
@ -77,7 +84,7 @@ function answerDialog(key) {
}
function refresh() {
var sortedGames = games.getAll();
var sortedGames = entries.getAll();
list.refresh(sortedGames);
if(sortedGames.length < 1) {
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 I18n;
import Messaging;
import {opponent, room} from Room;
import {opponent, players} from Room;
import * as Dom from UnitJS.Dom;
var form = ConnectedForm.get('room');
@ -21,7 +21,7 @@ function initDOM() {
form.root.addEventListener('submit', function(e) {
e.preventDefault();
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();
});
form.root.them.addEventListener("input", function() {refresh();});
@ -38,7 +38,7 @@ function showPlayer(player) {
function refresh() {
var name = form.root.them.value;
them = null;
var filtered = room.getAll(
var filtered = players.getAll(
function(player) {return player.name.match(name);}
);
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 Table;
var room = Table.make(player, 'name');
var players = Table.make(player, 'name');
initMessageHandlers();
return {
opponent: opponent,
room: room
players: players
};
function opponent(key) {
return {id: key, name: room.get(key).name};
return {id: key, name: players.get(key).name};
}
function player(key, name) {
@ -23,17 +23,17 @@ function player(key, name) {
function initMessageHandlers() {
Messaging.addEventListener(["Okaeri"], function(o) {
room.insertAll(o.room);
players.insertAll(o.room);
});
Messaging.addEventListener(["Welcome"], function(o) {
room.insertAll(o.room);
players.insertAll(o.room);
});
Messaging.addEventListener(["LogIn"], function(o) {
if(!Session.is(o.from)) {
room.insert(o.from, o.as);
players.insert(o.from, o.as);
}
});
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) {
var items = {};
return {
dump: dump,
get: get,
getAll: getAll,
insert: insert,
@ -11,6 +12,10 @@ function Table(itemMaker, sortCriterion) {
remove: remove
};
function dump() {
return items;
}
function get(key) {
return items[key];
}
@ -46,6 +51,10 @@ function Table(itemMaker, sortCriterion) {
function remove(key) {
delete items[key];
}
function restore(dumped) {
items = dumped;
}
}
return {