2020-01-11 19:46:41 +01:00
|
|
|
import * as ListSelector from GUI.ListSelector;
|
2020-01-13 08:39:32 +01:00
|
|
|
import Games;
|
2020-01-11 19:46:41 +01:00
|
|
|
import I18n;
|
|
|
|
import Messaging;
|
2020-01-13 08:39:32 +01:00
|
|
|
import players from Room;
|
2020-01-11 19:46:41 +01:00
|
|
|
import dialog from GUI.Screen;
|
|
|
|
import * as Dom from UnitJS.Dom;
|
|
|
|
|
2020-01-14 17:27:56 +01:00
|
|
|
var list;
|
2020-01-11 19:46:41 +01:00
|
|
|
|
|
|
|
return {
|
2020-01-14 17:27:56 +01:00
|
|
|
init: init,
|
2020-01-13 08:39:32 +01:00
|
|
|
refresh: refresh
|
2020-01-11 19:46:41 +01:00
|
|
|
};
|
|
|
|
|
2020-01-14 17:27:56 +01:00
|
|
|
function init() {
|
|
|
|
list = ListSelector.make('games', showGame);
|
|
|
|
}
|
|
|
|
|
2020-01-11 19:46:41 +01:00
|
|
|
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) {
|
2020-01-14 11:19:03 +01:00
|
|
|
var properties = {};
|
|
|
|
if(game.value.answer != undefined) {
|
|
|
|
properties.textContent = I18n.get('refusedGame')(game.value.vs.name);
|
|
|
|
properties.class = 'clickable';
|
|
|
|
properties.onClick = function() {Games.entries.remove(game.key); refresh();};
|
|
|
|
} else {
|
|
|
|
properties.textContent = I18n.get('proposedGame')(
|
|
|
|
game.value.yourTurn,
|
|
|
|
game.value.vs.name
|
|
|
|
);
|
2020-01-11 19:46:41 +01:00
|
|
|
};
|
2020-01-13 23:08:20 +01:00
|
|
|
if(game.value.yourTurn) {
|
|
|
|
properties.onClick = answerDialog(game.value.vs.id);
|
2020-01-11 19:46:41 +01:00
|
|
|
properties.class = 'clickable';
|
|
|
|
}
|
|
|
|
return [Dom.make('span', properties)];
|
|
|
|
}
|
|
|
|
|
|
|
|
function pendingGame(game) {
|
2020-01-13 23:08:20 +01:00
|
|
|
var status = players.get(game.value.vs.id) != undefined ? 'active' : 'inactive'
|
2020-01-11 19:46:41 +01:00
|
|
|
return [
|
|
|
|
Dom.make('span', { textContent: status}),
|
|
|
|
Dom.make('a', {
|
2020-01-13 23:08:20 +01:00
|
|
|
textContent: I18n.get('pendingGame')(game.value.yourTurn, game.value.vs.name),
|
2020-01-14 17:27:56 +01:00
|
|
|
href: '/game/' + game.key.replace(/Game#/, '')
|
2020-01-11 19:46:41 +01:00
|
|
|
})
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
function answer(key, accept) {
|
|
|
|
return function() {
|
|
|
|
Messaging.send({tag: "Answer", accept: accept, to: key});
|
2020-01-13 18:05:11 +01:00
|
|
|
Games.entries.remove(key);
|
2020-01-11 19:46:41 +01:00
|
|
|
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() {
|
2020-01-13 18:05:11 +01:00
|
|
|
var sortedGames = Games.entries.getAll();
|
2020-01-11 19:46:41 +01:00
|
|
|
list.refresh(sortedGames);
|
|
|
|
if(sortedGames.length < 1) {
|
|
|
|
list.message.textContent = I18n.get('noGames');
|
|
|
|
}
|
|
|
|
}
|