2020-01-13 18:05:11 +01:00
|
|
|
import Games;
|
2020-01-11 19:46:41 +01:00
|
|
|
import * as ConnectedForm from GUI.ConnectedForm;
|
|
|
|
import * as ListSelector from GUI.ListSelector;
|
2020-01-13 18:05:11 +01:00
|
|
|
import * as GamesGUI from GUI.Screen.Hall.Games;
|
2020-01-11 19:46:41 +01:00
|
|
|
import I18n;
|
|
|
|
import Messaging;
|
2020-01-13 08:39:32 +01:00
|
|
|
import {opponent, players} from Room;
|
2020-01-13 18:05:11 +01:00
|
|
|
import Time;
|
2020-01-11 19:46:41 +01:00
|
|
|
import * as Dom from UnitJS.Dom;
|
|
|
|
|
|
|
|
var form = ConnectedForm.get('room');
|
|
|
|
var list = ListSelector.make('players', showPlayer);
|
|
|
|
var them = null;
|
|
|
|
initDOM();
|
|
|
|
|
|
|
|
return {
|
|
|
|
refresh: refresh
|
|
|
|
};
|
|
|
|
|
|
|
|
function initDOM() {
|
|
|
|
form.root.getElementsByTagName('label')[0].textContent = I18n.get('startGameWith');
|
|
|
|
form.root.invite.value = I18n.get('invite');
|
|
|
|
form.root.addEventListener('submit', function(e) {
|
|
|
|
e.preventDefault();
|
|
|
|
Messaging.send({tag: "Invitation", to: them});
|
2020-01-13 18:05:11 +01:00
|
|
|
Games.entries.insert(
|
|
|
|
them,
|
|
|
|
Time.timestamp({vs: opponent(them), yourTurn: false})
|
|
|
|
);
|
|
|
|
GamesGUI.refresh();
|
2020-01-11 19:46:41 +01:00
|
|
|
});
|
|
|
|
form.root.them.addEventListener("input", function() {refresh();});
|
|
|
|
}
|
|
|
|
|
|
|
|
function showPlayer(player) {
|
|
|
|
return Dom.make('li', {
|
|
|
|
textContent: player.name,
|
|
|
|
onClick: function() {form.root.them.value = player.name; refresh();},
|
|
|
|
class: 'clickable'
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function refresh() {
|
|
|
|
var name = form.root.them.value;
|
|
|
|
them = null;
|
2020-01-13 08:39:32 +01:00
|
|
|
var filtered = players.getAll(
|
2020-01-11 19:46:41 +01:00
|
|
|
function(player) {return player.name.match(name);}
|
|
|
|
);
|
|
|
|
list.refresh(filtered);
|
|
|
|
var exact = filtered.find(exactMatch(name));
|
|
|
|
if(exact != undefined) {
|
|
|
|
them = exact.key;
|
|
|
|
} else if(filtered.length == 1) {
|
|
|
|
them = filtered[0].key;
|
|
|
|
} else if(filtered.length == 0) {
|
|
|
|
list.message.textContent = I18n.get(
|
|
|
|
name.length > 0 ? "notFound" : "alone"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
form.enable(them != undefined);
|
|
|
|
}
|
|
|
|
|
|
|
|
function exactMatch(name) {
|
|
|
|
return function(player) {
|
|
|
|
return player.name === name;
|
|
|
|
};
|
|
|
|
}
|