forked from gbtami/pychess-variants
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinvite.ts
128 lines (122 loc) · 6.64 KB
/
invite.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
import { h, VNode } from 'snabbdom';
import { _ } from './i18n';
import { VARIANTS } from './variants';
import { patch } from './document';
import { gameType } from './result';
import { copyTextToClipboard } from './clipboard';
import { timeControlStr } from './view';
import { PyChessModel } from './types';
export function inviteView(model: PyChessModel): VNode[] {
const gameId = model["gameId"];
const variant = VARIANTS[model.variant];
const chess960 = model.chess960 === 'True';
const gameURL = '/' + gameId;
const gameURLPlayer1 = gameURL + '/player1';
const gameURLPlayer2 = gameURL + '/player2';
const seekEmpty = model["seekEmpty"];
const evtSource = new EventSource("/api/invites");
evtSource.onmessage = function(event) {
const message = JSON.parse(event.data);
if (message.gameId === gameId) {
window.location.assign(gameURL);
}
}
let title, formAction, buttonClass, buttonText;
let player: string = window.location.pathname.split("/").pop()!;
if (!player?.match(/player[1-2]/)) player = "";
switch (model["inviter"]) {
case "":
title = seekEmpty ? _('Host a game for others') : _('Challenge to a game');
formAction = '/invite/cancel/' + gameId;
buttonClass = {red: true};
buttonText = _('CANCEL');
break;
case "wait":
title = _('Waiting for the other player');
formAction = '/invite/cancel/' + gameId;
buttonClass = {red: true};
buttonText = _('CANCEL');
break;
case "occupied":
title = _('Sorry, this seat is already occupied');
formAction = '/invite/cancel/' + gameId;
buttonClass = {red: true};
buttonText = _('CANCEL');
break;
case "yourself":
title = _('Sorry, you cannot play with yourself');
formAction = '/invite/cancel/' + gameId;
buttonClass = {red: true};
buttonText = _('CANCEL');
break;
default:
title = model["inviter"];
formAction = '/invite/accept/' + gameId + (!!player ? "/" + player : "");
buttonClass = {join: true};
buttonText = _('JOIN THE GAME');
}
return [
h('div.invite', [
h('h1', { attrs: { align: 'center' } }, title),
h('div.invitegame', [
h('div.info0.games.icon', { attrs: { "data-icon": variant.icon(chess960) } }, [
h('div.info2', [
h('div', variant.displayName(chess960)),
h('div.tc', timeControlStr(model["base"], model["inc"], model["byo"])),
h('div', h('i', { class: {
// TODO Correct icon colour for different variants
"icon": !!player,
"icon-white": player === "player1",
"icon-black": player === "player2"
} })),
]),
h('div.rated', gameType(model["rated"])),
]),
]),
(model["inviter"] === "") ?
( (seekEmpty) ?
h('div.inviteinfo', [
h('div', _('To invite someone to play, give them this URL:')),
h('div', [
h('label', {attrs: {for: "invite-url-player1"}}, _(variant.colors.first)),
h('input#invite-url-player1', {attrs: {readonly: true, spellcheck: false, value: model["home"] + gameURLPlayer1}}),
h('button#paste-url-player1', { class: { "paste-url": true }, on: { click: () => {
copyTextToClipboard(model["home"] + gameURLPlayer1);
patch(document.getElementById('paste-icon-player1') as HTMLElement,
h('i#paste-icon-player1', {props: {title: _('Copy URL')}, class: {"icon": true, "icon-check": true} }));
} } }, [
h('i#paste-icon-player1', {props: {title: _('Copy URL')}, class: {"icon": true, "icon-clipboard": true} })])
]),
h('div', [
h('label', {attrs: {for: "invite-url-player2"}}, _(variant.colors.second)),
h('input#invite-url-player2', {attrs: {readonly: true, spellcheck: false, value: model["home"] + gameURLPlayer2}}),
h('button#paste-url-player2', { class: { "paste-url": true }, on: { click: () => {
copyTextToClipboard(model["home"] + gameURLPlayer2);
patch(document.getElementById('paste-icon-player2') as HTMLElement,
h('i#paste-icon-player2', {props: {title: _('Copy URL')}, class: {"icon": true, "icon-check": true} }));
} } }, [
h('i#paste-icon-player2', {props: {title: _('Copy URL')}, class: {"icon": true, "icon-clipboard": true} })])
]),
h('div', _('The first two people to come to this URL will play with each other.')),
]) :
h('div.inviteinfo', [
h('div', _('To invite someone to play, give them this URL:')),
h('input#invite-url', {attrs: {readonly: true, spellcheck: false, value: model["home"] + gameURL}}),
h('button#paste-url', { class: { "paste-url": true }, on: { click: () => {
copyTextToClipboard(model["home"] + gameURL);
patch(document.getElementById('paste-icon') as HTMLElement,
h('i#paste-icon', {props: {title: _('Copy URL')}, class: {"icon": true, "icon-check": true} }));
} } }, [
h('i#paste-icon', {props: {title: _('Copy URL')}, class: {"icon": true, "icon-clipboard": true} })]),
h('div', _('The first person to come to this URL will play with you.')),
]) ) :
h('div'),
// TODO Wait actually should have cancel button but the server doesn't support it at the moment
model["inviter"] !== "wait" && model["inviter"] !== "occupied" && model["inviter"] !== "yourself" ?
h('form', {props: {method: "post", action: formAction}, class: {invite: true}}, [
h('button', {class: buttonClass}, buttonText),
]) :
h('div'),
]),
];
}