import * as ListSelector from GUI.ListSelector; import Games; import I18n; import Messaging; import players from Room; import dialog from GUI.Screen; import * as Dom from UnitJS.Dom; var list; return { init: init, refresh: refresh }; function init() { list = ListSelector.make('games', showGame); } 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 = {}; 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 ); }; if(game.value.yourTurn) { properties.onClick = answerDialog(game.value.vs.id); properties.class = 'clickable'; } return [Dom.make('span', properties)]; } function pendingGame(game) { var status = players.get(game.value.vs.id) != undefined ? 'active' : 'inactive' return [ Dom.make('span', { textContent: status}), Dom.make('a', { textContent: I18n.get('pendingGame')(game.value.yourTurn, game.value.vs.name), href: '/game/' + game.key.replace(/Game#/, '') }) ]; } function answer(key, accept) { return function() { Messaging.send({tag: "Answer", accept: accept, to: key}); Games.entries.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.entries.getAll(); list.refresh(sortedGames); if(sortedGames.length < 1) { list.message.textContent = I18n.get('noGames'); } }