-
Notifications
You must be signed in to change notification settings - Fork 0
/
admin-client.js
216 lines (175 loc) · 5.88 KB
/
admin-client.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
/* globals $ */
import createClient from 'monsterr'
import html from './src/admin-client.html'
import './src/admin-client.css'
//import {gameSettings} from "./src/stages/sharedDB";
import * as db from "./src/stages/sharedDB";
import * as adminContent from './src/adminHTML.html'
let options = {
canvasBackgroundColor: 'white',
htmlContainerHeight: 1.0,
// HTML is included in options for admin
html
}
let events = {
'clientConnected': function (admin, data) {
console.log('Received clientConnected event from server with data: ' + JSON.stringify(data));
updatePlayerList(data.clients);
},
'clientDisconnected': function (admin, data) {
console.log('Received clientDisconnected event from server with data: ' + JSON.stringify(data));
updatePlayerList(data.clients);
},
'resConnections': function (admin, data) {
console.log('Received resConnections event from server with data: ' + JSON.stringify(data));
updatePlayerList(data.clients);
},
'updateRounds': function (admin, data) {
console.log('Received printEntities event from server with data: ' + JSON.stringify(data));
updateRounds(data);
},
'resGameData': (admin, json) => {
download(json, 'gameDataJSON.json', 'text/plain');
/*
let fileName = 'monsterr-modules_' + Date.now() + '.csv'
let data = JSON.stringify(json);
let url = window.URL.createObjectURL(new Blob([data], {type: 'text/json'}))
let a = document.createElement('a');
document.body.appendChild(a);
a.style = 'display: none';
a.href = url;
a.download = fileName;
a.click();
window.URL.revokeObjectURL(url);
*/
},
'resParticipantData': (admin, json) => {
download(json, 'participantsDataJSON.json', 'text/plain');
/*
let fileName = 'monsterr-modules_' + Date.now() + '.csv'
let data = JSON.stringify(json);
let url = window.URL.createObjectURL(new Blob([data], {type: 'text/json'}))
let a = document.createElement('a');
document.body.appendChild(a);
a.style = 'display: none';
a.href = url;
a.download = fileName;
a.click();
window.URL.revokeObjectURL(url);
*/
},
'finishedRegistration': (admin, data) => {
console.log('Received finishedRegistration from server...');
admin.getChat().append('finished registration');
},
'GameOver': function (admin, data) {
console.log('Received GameOver event from server with data: ' + JSON.stringify(data));
gameOver();
},
};
let commands = {};
const admin = createClient({
events,
commands,
options
// no need to add stages to admin
});
//----------------------------
let hasStarted = false;
let maxRounds = 0;
insertHTML();
function insertHTML(){
console.log('loading html');
//$("#admin").load(adminContent);
//document.getElementById("admin").innerHTML='<object type="text/html" data=""></object>';
document.getElementById("admin").innerHTML = (adminContent);
// Button event handlers (if you need more you should probably put them in a separate file and import it here)
$('#startButton').mouseup(e => {
e.preventDefault()
startGame();
});
$('#button-reset').mouseup(e => {
e.preventDefault()
resetGame();
});
$('#button-gameData').mouseup(e => {
e.preventDefault()
requestGameData();
});
$('#button-participantData').mouseup(e => {
e.preventDefault()
requestParticipantData();
});
hideStuff();
admin.sendCommand('getConnections');
}
function hideStuff(){
console.log('hideStuff() called');
if(!hasStarted){
console.log('game has not started yet');
$('#activeGame').hide();
$('#gameSettings').show();
$('#gameOver').hide();
}else{
console.log('game has started yet');
$('#activeGame').show();
$('#gameSettings').hide();
$('#gameOver').hide();
}
}
function requestParticipantData(){
console.log('requesting participant data...');
admin.sendCommand('reqParticipantData');
}
function requestGameData(){
console.log('requesting game data...');
admin.sendCommand('reqGameData');
}
function resetGame(){
//RESET
admin.sendCommand('resetClient');
$('#activeGame').hide();
$('#gameOver').hide();
setTimeout(function(){
admin.sendCommand('reset');
hasStarted = false;
$('#playersId').text('Connected players: Waiting for game to start...');
$('#roundId').text('Current round: Waiting for game to start');
$('#gameSettings').show();
}, 500);
}
function startGame(){
//START
hasStarted = true;
$('#activeGame').show();
$('#gameSettings').hide();
let gameSettings = document.getElementById("gameSettings");
maxRounds = gameSettings.numberOfRounds.value;
let gameMode = gameSettings.gameMode.value;
let networkMode = gameSettings.networkMode.value;
console.log('Starting ' + gameMode + ' game with maxRounds: ' + maxRounds);
admin.sendCommand('start');
admin.sendCommand('setGameSettings', [maxRounds, gameMode, networkMode]);
}
function updateRounds(roundArr){
$('#roundId').text('Current rounds: ' + roundArr + ' of ' + maxRounds);
}
function download(content, fileName, contentType) {
let a = document.createElement("a");
let file = new Blob([content], {type: contentType});
a.href = URL.createObjectURL(file);
a.download = fileName;
a.click();
}
function updatePlayerList(clients){
let ul = document.getElementById("playerList");
ul.innerHTML = "";
for(let clientIndex in clients){
let li = document.createElement("li");
li.appendChild(document.createTextNode(clients[clientIndex]));
ul.appendChild(li);
}
}
function gameOver(){
$('#gameOver').show();
}