-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathindex.html
262 lines (248 loc) · 7.22 KB
/
index.html
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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
<html>
<head>
<title>My first Electron App</title>
<style>
holes{
display: none;
justify-content: space-around;
flex-wrap: wrap;
}
hole {
flex-basis: 25%;
background-image: url(assets/img/hole_t.png);
background-color: white;
height: 20vw;
/*border: 3px solid black;*/
background-size: cover;
box-sizing: border-box;
}
username{
}
username label{
}
username input{
}
username myname{
display: none;
}
error{
display: none;
color: red;
width: 100%;
text-align: center;
}
dash{
display: none;
justify-content: space-between;
width: 100%;
align-items: stretch;
}
myinfo, scores{
flex-grow: 1;
border: 1px solid #ccc;
display: block;
}
myinfo{
flex-basis: 40%;
}
myinfo name{
font-size: 30px;
text-transform: capitalize;
display: block;
text-align: center;
}
myinfo score{
text-align: center;
}
scores{
flex-basis: 60%;
}
scores h2{
font-weight: normal;
text-align: center;
}
scores h2 span {
font-style: italic;
}
playerlist player{
display: block;
}
</style>
</head>
<body>
<h1>Gun Moles!</h1>
<username>
<label for="username">Please Choose a Username</label>
<input name="username" id="username" type="text" value="" />
<button name="submit" id="submit">Let's Go!</button>
</username>
<error></error>
<dash>
<myinfo>
<name></name>
<score>Score: </score>
</myinfo>
<scores>
<h2>Those <span>other</span> guys:</h2>
<playerlist></playerlist>
</scores>
</dash>
<holes>
<hole data-id="1"></hole>
<hole data-id="2"></hole>
<hole data-id="3"></hole>
<hole data-id="4"></hole>
<hole data-id="5"></hole>
<hole data-id="6"></hole>
<hole data-id="7"></hole>
</holes>
</body>
<script>
const electron = require('electron');
const { ipcRenderer } = electron;
var holeElements = document.querySelectorAll('hole');
var holes = [];
var i, username;
var players = [];
var molesAppeared = {};
var sessionId = 0;
/*==== initialize page ====*/
for( i = 0; i < holeElements.length; i++){
holes[i+1] = holeElements[i];
holeElements[i].addEventListener('click', function(event){
clickHole( this )
});
}
document.querySelector('input').onkeypress = (event)=>{
event = event || window.event;
if(event.keyCode == 13){
username = document.querySelector('input').value;
if(!username){
showError('Please enter a username!');
}else{
startGame(username);
}
}
};
document.querySelector('button').addEventListener('click',(e)=>{
username = document.querySelector('input').value;
if(!username){
showError('Please enter a username!');
}else{
startGame(username);
}
});
/*==== server event handlers ====*/
ipcRenderer.on('myself:update', (event,data) => {
console.log('The app sent me an update for myself!',data);
// set the username
document.querySelector('myinfo name').innerHTML = data.player.name;
document.querySelector('myinfo score').innerHTML = "Score: " + data.player.score;
sessionId = data.sessionId;
});
ipcRenderer.on('player:update', (event,data) => {
console.log('The app sent me an update for a player!',data);
// time to build this
console.log('building player', data.sessionId, data.player);
players[data.sessionId] = data.player;
console.log('players',players);
renderPlayers();
});
ipcRenderer.on('holes:update', (event,data) => {
console.log('The app sent me an update for a hole!',data);
if(data.status == 'ready'){
holes[data.hole].style.backgroundImage = "url(assets/img/hole_t.png)"; // clear the mole
}
});
ipcRenderer.on('mole:update', (event,data) => {
console.log('The app sent me an update for a mole!',data);
if( typeof molesAppeared[data.id] == 'undefined'){ // new mole
molesAppeared[data.id] = {
'createdBy':data.createdBy,
'hole':data.hole,
'appeared':Date.now(),
'whacked':false };
holes[data.hole].style.backgroundImage = "url(assets/img/hole_w_mole.png)";
setTimeout(function(){
expireMole(data.id);
}, 5000);
}else if( molesAppeared[data.id].expired ){ // if the mole expired,
holes[data.hole].style.backgroundImage = "url(assets/img/hole_t.png)";// remove it
}
});
/*==== page event handlers ====*/
function startGame( username ){
console.log("Ready! Let's do this " + username + "!");
// fire the username off to the server
ipcRenderer.send('username:put',{'username':username});
// request playerlist
ipcRenderer.send('player_list:get', {});
// hide the form, show the game
document.querySelector('username').style.display = "none";
document.querySelector('dash').style.display = "flex";
document.querySelector('holes').style.display = "flex";
}
function showError( error ){
var errorElem = document.querySelector('error');
errorElem.innerHTML = 'Error: ' + error;
errorElem.style.display = 'block';
}
/*
When the user clicks on a hole,
if there is no mole, send one.
If there is a mole, whack it and record the time.
*/
function clickHole( node ) {
let holeId = node.dataset.id;
console.log('clicked', holeId );
let moleHere = false, moleId = 0, mole, delta = 0;
for( moleId in molesAppeared ){
mole = molesAppeared[moleId];
if(mole.hole == holeId ){
moleHere = true;
break;
}
}
if(moleHere){ // If there is a mole here
console.log("There's a mole here");
if( mole.createdBy != sessionId){ // if it's not mine
delta = Date.now() - molesAppeared[moleId].appeared; // record how quickly I whacked it
console.log('whacking a mole', Date.now(), molesAppeared[moleId].appeared);
ipcRenderer.send('mole:whacked',{
'hole':holeId,
'myTime':delta,
'moleId':moleId }); // send that
holes[holeId].style.backgroundImage = "url(assets/img/whacked.png)"; // switch to whacked
molesAppeared[moleId].whacked = true; // reset the timer
setTimeout(function(){
expireMole(moleId);
},2000);
}
}else{ // if there is no mole here,
console.log("No mole here");
ipcRenderer.send('hole:click', {
id: holeId
});
}
}
function renderPlayers( ){
const fiveMinutes = 1000 * 60 * 5;
console.log('render players', players);
var i=0, html='';
for( i in players ){
if ( i != '_' && players[i].lastAction > (Date.now() - fiveMinutes) ) { // Don't render players who have been idle for 5 minutes
html += "<player><name>"+players[i].name+"</name>: <score>"+players[i].score+"</score></player>";
console.log('player html', players[i], html);
}
}
document.querySelector('scores playerlist').innerHTML = html;
}
function expireMole( moleId ){
console.log('expire mole ' + moleId );
ipcRenderer.send('mole:expire',{
'moleId':moleId,
'hole':molesAppeared[moleId].hole});
delete molesAppeared[moleId];
}
</script>
</html>