webclient/js/GUI/Screen/Hall/Games.js

86 lines
1.8 KiB
JavaScript

import * as ListSelector from GUI.ListSelector;
import I18n;
import Messaging;
import room 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);
return {
refresh: refresh,
table: games
};
function game(key, config) {
return {
key: key,
vs: config.vs,
yourTurn: config.yourTurn,
date: Date.now()
};
}
function showGame(game) {
var liContent;
if(game.key.match(/^Player#/)) { // Game proposals use the ID of the opponent as ID
liContent = gameProposal(game);
} else {
liContent = pendingGame(game);
}
return Dom.make('li', {}, liContent);
}
function gameProposal(game) {
var properties = {
textContent: I18n.get('proposedGame')(game.yourTurn, game.vs.name),
};
if(game.yourTurn) {
properties.onClick = answerDialog(game.vs.id);
properties.class = 'clickable';
}
return [Dom.make('span', properties)];
}
function pendingGame(game) {
var status = room.get(game.vs.id) != undefined ? 'active' : 'inactive'
return [
Dom.make('span', { textContent: status}),
Dom.make('a', {
textContent: I18n.get('pendingGame')(game.yourTurn, game.vs.name),
href: '/game/' + game.key
})
];
}
function answer(key, accept) {
return function() {
Messaging.send({tag: "Answer", accept: accept, to: key});
games.remove(key);
refresh();
}
}
function answerDialog(key) {
return function() {
dialog({
text: I18n.get('questionAccept'),
answers: [
{label: 'accept', action: answer(key, true)},
{label: 'decline', action: answer(key, false)},
{label: 'notYet', action: function() {}}
]
});
};
}
function refresh() {
var sortedGames = games.getAll();
list.refresh(sortedGames);
if(sortedGames.length < 1) {
list.message.textContent = I18n.get('noGames');
}
}