-
Notifications
You must be signed in to change notification settings - Fork 0
/
PlayGround.js
246 lines (219 loc) · 8.72 KB
/
PlayGround.js
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
define([
'GameClient',
'mustache.min',
'PlayerVisual'
], function (
/** GameClient */ GameClient,
/** Mustache */ Mustache,
/** PlayerVisual */PlayerVisual
) {
let appTemplate = '\
<div class="app__panel js-app__panel"></div> \
<div class="app__board js-app__board"></div>';
let panelTemplate = '\
<div class="app__panel-games-list">\
<form class="js-update-game-form">\
<input type="submit" value="Обновить список игр" /> \
</form>\
<form class="js-join-game-form">\
{{^hasGames}}\
<div class="games__item">\
Созданных игр еще нет\
</div>\
{{/hasGames}}\
{{#hasGames}}\
{{#games}}\
<div class="games__item">\
<label>\
Игра для {{maxPlayersCount}} игроков, в игре уже {{players}} <input type="radio" name="join" value="{{id}}">\
</label>\
</div>\
{{/games}}\
<button type="submit" {{^playerId}}disabled="disabled"{{/playerId}}>Войти в игру</button> \
{{/hasGames}}\
</form>\
</div>\
<div class="app__panel-create-game">\
<form class="js-create-form"> \
{{#playerId}}\
<div class="panel"> \
<div class="panel__item"> \
<label class="panel__title" for="sizeField">Размерность доски</label> \
<input type="number" class="panel__input js-first-input" name="size" id="sizeField" value="{{defaultSize}}" min="{{min}}" max="{{max}}" required/> \
</div> \
<div class="panel__item"> \
<label class="panel__title" for="playersCount">Количество игроков</label> \
<input type="number" class="panel__input" name="count" id="playersCount" value="{{defaultPlayersCount}}" min="2" required /> \
</div> \
<div class="panel__item"> \
<input type="submit" class="panel__submit" /> \
</div> \
</div> \
{{/playerId}}\
</form>\
</div>\
<div class="app__panel-login">\
{{^playerId}}\
<form class="js-login-form">\
<input type="text" name="playerName" value="" placeholder="Логин" required />\
<input type="submit" value="Войти" /> \
</form>\
{{/playerId}}\
{{#playerId}}\
<form class="js-logout-form">\
{{playerName}} <input type="submit" value="Выйти" /> \
</form>\
{{/playerId}}\
</div>';
class PlayGround {
constructor() {
this.element = document.querySelector('.app');
this.player = null;
this.boardRoot = null;
this.loginForm = null;
this.logoutForm = null;
this.createForm = null;
this.updateGameForm = null;
this.joinGameForm = null;
this.games = null;
this.defaultData = {
defaultSize: 3,
defaultPlayersCount: 2,
min: 3,
max: 10
};
this.url = '127.0.0.1:3000/';
this.host = 'http://' + this.url;
this.reloginTimeout = null;
this.createLayout();
this.getGamesList();
}
createLayout() {
this.element.innerHTML = Mustache.render(appTemplate, {});
this.panelRoot = document.querySelector('.js-app__panel');
this.boardRoot = document.querySelector('.js-app__board');
}
render() {
let data = Object.assign({}, this.defaultData, {
playerId: this.player ? this.player.id : null,
playerName: this.player ? this.player.name : null
}, {
games: this.games,
hasGames: !!this.games.length
});
this.panelRoot.innerHTML = Mustache.render(panelTemplate, data);
this.activate();
}
activate() {
this.loginForm = document.querySelector('.js-login-form');
this.logoutForm = document.querySelector('.js-logout-form');
this.createForm = document.querySelector('.js-create-form');
this.updateGameForm = document.querySelector('.js-update-game-form');
this.joinGameForm = document.querySelector('.js-join-game-form');
if (this.loginForm) {
this.loginForm.addEventListener('submit', (e) => {
e.preventDefault();
this.login(e.currentTarget.elements.playerName.value);
});
}
if (this.logoutForm) {
this.logoutForm.addEventListener('submit', (e) => {
e.preventDefault();
this.logout();
});
}
this.createForm.addEventListener('submit', (e) => {
e.preventDefault();
this.createGame(e.currentTarget.elements.size.value, e.currentTarget.elements.count.value);
});
this.updateGameForm.addEventListener('submit', (e) => {
e.preventDefault();
this.getGamesList();
});
this.joinGameForm.addEventListener('submit', (e) => {
e.preventDefault();
let joinGameId = e.currentTarget.elements.join.value;
if (joinGameId) {
this.joinGame(joinGameId);
}
this.getGamesList();
});
}
login(playerName, playerId) {
var xhr = new XMLHttpRequest();
xhr.addEventListener("load", () => {
let result = JSON.parse(xhr.responseText);
if (result.status === 'success') {
this.player = {
id: result.data.id,
name: result.data.name
};
this.reloginTimeout = setTimeout(() => {
this.login(result.data.name, result.data.id);
}, (result.data.loginTimeout - 1000));
this.getGamesList();
}
});
xhr.open("POST", this.host + "login");
xhr.send(JSON.stringify({
login: playerName,
playerId: playerId
}));
}
logout() {
if (!this.player) {
return;
}
var xhr = new XMLHttpRequest();
xhr.addEventListener("load", () => {
this.player = null;
clearTimeout(this.reloginTimeout);
this.getGamesList();
});
xhr.open("POST", this.host + "logout");
xhr.send(JSON.stringify({
playerId: this.player.id
}));
}
getGamesList() {
let xhr = new XMLHttpRequest();
let playerId = this.player
? this.player.id
: null;
xhr.addEventListener("load", () => {
let result = JSON.parse(xhr.responseText);
if (result.status === 'success') {
this.games = result.data.games;
}
this.render();
});
xhr.open("GET", this.host + "games?playerId=" + playerId);
xhr.send();
}
createGame(boardSize, playersCount) {
if (!this.player) {
return;
}
var xhr = new XMLHttpRequest();
xhr.addEventListener("load", () => {
let result = JSON.parse(xhr.responseText);
if (result.status === 'success') {
this.joinGame(result.data.id);
}
});
xhr.open("POST", this.host + "create");
xhr.send(JSON.stringify({
playerId: this.player.id,
boardSize: boardSize,
playersCount: playersCount
}));
}
joinGame(gameId) {
if (!this.player) {
return;
}
new GameClient(gameId, this.url, this.player, new PlayerVisual(this.player.name, this.boardRoot));
}
}
new PlayGround();
});