wip: stage-2 online UI draft, paused for t1102 stage-1 fixes
Lobby markup, styles, and the API/tween client. Main.js is not wired up yet; resume after the recolor, merge, and touch fixes.
@@ -12,15 +12,67 @@
12
12
</head>
13
13
<body>
14
14
<div id="stars" aria-hidden="true"></div>
15
<div id="flights" aria-hidden="true"></div>
15
16
16
17
<div id="menu" class="screen">
17
18
<h1>Meteors</h1>
18
19
<p class="tagline">Drie op een rij, en je blokjes krijgen raketmotoren.</p>
19
20
<button type="button" id="btn-play">Spelen</button>
21
<button type="button" id="btn-online" class="btn-secondary">Online spelen</button>
20
22
<p id="last-score" class="last-score" hidden>Laatste score: <strong>0</strong></p>
21
23
<p class="hint">Sleep blokjes verticaal om ze te wisselen. Trek een blokje boven zijn kolom om het omhoog te duwen. Blokjes die het veld verlaten, leveren punten op.</p>
22
24
</div>
23
25
26
<div id="online-panel" class="screen" hidden>
27
<h1>Online</h1>
28
<p id="online-status" class="tagline">Even kijken of online spelen beschikbaar is…</p>
29
<p id="online-error" class="error" hidden></p>
30
31
<div id="online-unconfigured" hidden>
32
<p class="notice">Online spelen is nog niet ingesteld. De beheerder moet bij <a href="https://auth.timfalken.com/admin" target="_blank" rel="noopener">auth.timfalken.com/admin</a> het callback-pad <code>/meteors/callback.php</code> toevoegen (zelfde client als de andere sites op dit domein; het geheim <strong>niet</strong> opnieuw genereren) en dat geheim in <code>data/config.php</code> op de server zetten. Zie de README.</p>
33
<p>Je kunt wel alleen spelen.</p>
34
<button type="button" class="btn-secondary" data-back-menu>Terug</button>
35
</div>
36
37
<div id="online-login" hidden>
38
<p>Om online te spelen log je in met je account op auth.timfalken.com.</p>
39
<a id="btn-login" class="button" href="auth.php">Inloggen</a>
40
<button type="button" class="btn-secondary" data-back-menu>Terug</button>
41
<p id="local-login" class="hint" hidden>
42
Lokale test:
43
<a href="auth.php?test=1">test_user_1</a> ·
44
<a href="auth.php?test=2">test_user_2</a>
45
</p>
46
</div>
47
48
<div id="online-home" hidden>
49
<p class="who">Ingelogd als <strong id="me-name"></strong> · <a href="logout.php">Uitloggen</a></p>
50
<button type="button" id="btn-create">Lobby maken</button>
51
<form id="join-form" class="join-form">
52
<label>
53
<span>Lobbycode</span>
54
<input id="join-code" type="text" maxlength="6" autocomplete="off" spellcheck="false" placeholder="ABCD">
55
</label>
56
<button type="submit" id="btn-join">Deelnemen</button>
57
</form>
58
<button type="button" class="btn-secondary" data-back-menu>Terug</button>
59
</div>
60
61
<div id="online-lobby" hidden>
62
<p class="who">Ingelogd als <strong id="lobby-me-name"></strong></p>
63
<p class="lobby-code">Code: <strong id="lobby-code"></strong> <button type="button" id="btn-copy-code" class="btn-tiny">Kopieer</button></p>
64
<ul id="lobby-players" class="player-list"></ul>
65
<p id="lobby-hint" class="hint"></p>
66
<div class="lobby-actions">
67
<button type="button" id="btn-ready">Gereed</button>
68
<button type="button" id="btn-start" hidden>Starten</button>
69
<button type="button" id="btn-leave-lobby" class="btn-secondary">Verlaten</button>
70
</div>
71
</div>
72
</div>
73
74
<div id="minis" hidden></div>
75
24
76
<div id="hud" hidden>
25
77
<div class="hud-left">
26
78
<div class="hud-label">Punten</div>
@@ -38,6 +90,22 @@
38
90
<p class="go-score">Score: <strong id="go-score">0</strong></p>
39
91
</div>
40
92
93
<div id="spectator" class="screen overlay" hidden>
94
<h2>Je ligt eruit</h2>
95
<p>Al je kolommen zitten vol. Je kunt toekijken of de wedstrijd verlaten.</p>
96
<div class="lobby-actions">
97
<button type="button" id="btn-spectate">Toekijken</button>
98
<button type="button" id="btn-leave-game" class="btn-secondary">Verlaten</button>
99
</div>
100
</div>
101
102
<div id="podium" class="screen overlay" hidden>
103
<h2>Gewonnen!</h2>
104
<p id="podium-title" class="tagline">De laatste speler staat overeind.</p>
105
<ol id="podium-list" class="podium-list"></ol>
106
<p class="hint" id="podium-hint">Over 5 seconden teruggaan naar de lobby…</p>
107
</div>
108
41
109
<script src="framework.js"></script>
42
110
<script src="game.js"></script>
43
111
</body>
@@ -12,15 +12,67 @@12
</head>12
</head>13
<body>13
<body>14
<div id="stars" aria-hidden="true"></div>14
<div id="stars" aria-hidden="true"></div>15
<div id="flights" aria-hidden="true"></div>15
16
16
<div id="menu" class="screen">17
<div id="menu" class="screen">17
<h1>Meteors</h1>18
<h1>Meteors</h1>18
<p class="tagline">Drie op een rij, en je blokjes krijgen raketmotoren.</p>19
<p class="tagline">Drie op een rij, en je blokjes krijgen raketmotoren.</p>19
<button type="button" id="btn-play">Spelen</button>20
<button type="button" id="btn-play">Spelen</button>21
<button type="button" id="btn-online" class="btn-secondary">Online spelen</button>20
<p id="last-score" class="last-score" hidden>Laatste score: <strong>0</strong></p>22
<p id="last-score" class="last-score" hidden>Laatste score: <strong>0</strong></p>21
<p class="hint">Sleep blokjes verticaal om ze te wisselen. Trek een blokje boven zijn kolom om het omhoog te duwen. Blokjes die het veld verlaten, leveren punten op.</p>23
<p class="hint">Sleep blokjes verticaal om ze te wisselen. Trek een blokje boven zijn kolom om het omhoog te duwen. Blokjes die het veld verlaten, leveren punten op.</p>22
</div>24
</div>23
25
26
<div id="online-panel" class="screen" hidden>27
<h1>Online</h1>28
<p id="online-status" class="tagline">Even kijken of online spelen beschikbaar is…</p>29
<p id="online-error" class="error" hidden></p>30
31
<div id="online-unconfigured" hidden>32
<p class="notice">Online spelen is nog niet ingesteld. De beheerder moet bij <a href="https://auth.timfalken.com/admin" target="_blank" rel="noopener">auth.timfalken.com/admin</a> het callback-pad <code>/meteors/callback.php</code> toevoegen (zelfde client als de andere sites op dit domein; het geheim <strong>niet</strong> opnieuw genereren) en dat geheim in <code>data/config.php</code> op de server zetten. Zie de README.</p>33
<p>Je kunt wel alleen spelen.</p>34
<button type="button" class="btn-secondary" data-back-menu>Terug</button>35
</div>36
37
<div id="online-login" hidden>38
<p>Om online te spelen log je in met je account op auth.timfalken.com.</p>39
<a id="btn-login" class="button" href="auth.php">Inloggen</a>40
<button type="button" class="btn-secondary" data-back-menu>Terug</button>41
<p id="local-login" class="hint" hidden>42
Lokale test:43
<a href="auth.php?test=1">test_user_1</a> ·44
<a href="auth.php?test=2">test_user_2</a>45
</p>46
</div>47
48
<div id="online-home" hidden>49
<p class="who">Ingelogd als <strong id="me-name"></strong> · <a href="logout.php">Uitloggen</a></p>50
<button type="button" id="btn-create">Lobby maken</button>51
<form id="join-form" class="join-form">52
<label>53
<span>Lobbycode</span>54
<input id="join-code" type="text" maxlength="6" autocomplete="off" spellcheck="false" placeholder="ABCD">55
</label>56
<button type="submit" id="btn-join">Deelnemen</button>57
</form>58
<button type="button" class="btn-secondary" data-back-menu>Terug</button>59
</div>60
61
<div id="online-lobby" hidden>62
<p class="who">Ingelogd als <strong id="lobby-me-name"></strong></p>63
<p class="lobby-code">Code: <strong id="lobby-code"></strong> <button type="button" id="btn-copy-code" class="btn-tiny">Kopieer</button></p>64
<ul id="lobby-players" class="player-list"></ul>65
<p id="lobby-hint" class="hint"></p>66
<div class="lobby-actions">67
<button type="button" id="btn-ready">Gereed</button>68
<button type="button" id="btn-start" hidden>Starten</button>69
<button type="button" id="btn-leave-lobby" class="btn-secondary">Verlaten</button>70
</div>71
</div>72
</div>73
74
<div id="minis" hidden></div>75
24
<div id="hud" hidden>76
<div id="hud" hidden>25
<div class="hud-left">77
<div class="hud-left">26
<div class="hud-label">Punten</div>78
<div class="hud-label">Punten</div>@@ -38,6 +90,22 @@38
<p class="go-score">Score: <strong id="go-score">0</strong></p>90
<p class="go-score">Score: <strong id="go-score">0</strong></p>39
</div>91
</div>40
92
93
<div id="spectator" class="screen overlay" hidden>94
<h2>Je ligt eruit</h2>95
<p>Al je kolommen zitten vol. Je kunt toekijken of de wedstrijd verlaten.</p>96
<div class="lobby-actions">97
<button type="button" id="btn-spectate">Toekijken</button>98
<button type="button" id="btn-leave-game" class="btn-secondary">Verlaten</button>99
</div>100
</div>101
102
<div id="podium" class="screen overlay" hidden>103
<h2>Gewonnen!</h2>104
<p id="podium-title" class="tagline">De laatste speler staat overeind.</p>105
<ol id="podium-list" class="podium-list"></ol>106
<p class="hint" id="podium-hint">Over 5 seconden teruggaan naar de lobby…</p>107
</div>108
41
<script src="framework.js"></script>109
<script src="framework.js"></script>42
<script src="game.js"></script>110
<script src="game.js"></script>43
</body>111
</body>@@ -45,7 +45,7 @@ html, body {
45
45
text-align: center;
46
46
}
47
47
48
.screen[hidden], #hud[hidden], #game-over[hidden] { display: none !important; }
48
.screen[hidden], #hud[hidden], #game-over[hidden], #online-panel[hidden], #minis[hidden], #spectator[hidden], #podium[hidden], #flights[hidden] { display: none !important; }
49
49
50
50
h1 {
51
51
font-size: clamp(48px, 14vw, 96px);
@@ -183,3 +183,228 @@ canvas {
183
183
h1 { font-size: 42px; }
184
184
#btn-play { padding: 10px 36px; }
185
185
}
186
187
188
/* ---- Stage 2: online, lobby, miniatures, flights, podium ---- */
189
190
#btn-online, .btn-secondary, a.button {
191
margin-top: 12px;
192
padding: 10px 36px;
193
font-size: clamp(16px, 4vw, 20px);
194
border: 1px solid #6a7394;
195
border-radius: 999px;
196
color: #e8eaf2;
197
background: linear-gradient(180deg, #2a3150, #1b2038);
198
cursor: pointer;
199
text-decoration: none;
200
display: inline-block;
201
font-family: inherit;
202
}
203
#btn-online:is(:hover, :focus-visible),
204
.btn-secondary:is(:hover, :focus-visible),
205
a.button:is(:hover, :focus-visible) {
206
border-color: #ffe27a;
207
color: #fff6c8;
208
}
209
#btn-create, #btn-join, #btn-ready, #btn-start, #btn-spectate,
210
#btn-play {
211
/* play already styled */
212
}
213
214
#btn-create, #btn-join, #btn-ready, #btn-start, #btn-spectate {
215
padding: 10px 28px;
216
font-size: clamp(16px, 4vw, 20px);
217
border: 0;
218
border-radius: 999px;
219
color: #1a1020;
220
background: linear-gradient(180deg, #ffe27a, #f0c53a 40%, #e89a20);
221
cursor: pointer;
222
font-weight: 700;
223
font-family: inherit;
224
}
225
#btn-start:disabled, #btn-ready:disabled {
226
opacity: 0.45;
227
cursor: default;
228
}
229
230
#online-panel {
231
z-index: 5;
232
overflow: auto;
233
text-align: center;
234
padding: 32px 16px 48px;
235
}
236
#online-panel .who {
237
color: #c5cae0;
238
margin: 0 0 16px;
239
}
240
#online-panel .notice {
241
max-width: 36em;
242
margin: 0 auto 16px;
243
text-align: left;
244
line-height: 1.45;
245
color: #c5cae0;
246
}
247
#online-panel code {
248
font-size: 0.92em;
249
color: #ffe27a;
250
}
251
#online-panel a { color: #9ec5ff; }
252
.error {
253
color: #ffb0a0;
254
margin: 8px 0;
255
}
256
.notice {
257
background: rgba(20, 24, 48, 0.7);
258
border: 1px solid #3a4268;
259
border-radius: 12px;
260
padding: 14px 16px;
261
}
262
.join-form {
263
display: flex;
264
gap: 8px;
265
justify-content: center;
266
align-items: flex-end;
267
flex-wrap: wrap;
268
margin: 16px 0;
269
}
270
.join-form label {
271
display: flex;
272
flex-direction: column;
273
align-items: flex-start;
274
font-size: 13px;
275
color: #9aa3c2;
276
}
277
.join-form input {
278
font-size: 22px;
279
letter-spacing: 0.2em;
280
text-transform: uppercase;
281
width: 8em;
282
padding: 8px 10px;
283
border-radius: 8px;
284
border: 1px solid #6a7394;
285
background: #141a33;
286
color: #fff6c8;
287
font-family: inherit;
288
text-align: center;
289
}
290
.lobby-code {
291
font-size: clamp(22px, 6vw, 34px);
292
margin: 8px 0 16px;
293
color: #ffe27a;
294
}
295
.btn-tiny {
296
font-size: 13px;
297
padding: 4px 10px;
298
margin-left: 8px;
299
border-radius: 999px;
300
border: 1px solid #6a7394;
301
background: #1b2038;
302
color: #e8eaf2;
303
cursor: pointer;
304
font-family: inherit;
305
letter-spacing: 0;
306
vertical-align: middle;
307
}
308
.player-list {
309
list-style: none;
310
padding: 0;
311
margin: 0 auto 18px;
312
max-width: 22em;
313
text-align: left;
314
}
315
.player-list li {
316
display: flex;
317
justify-content: space-between;
318
padding: 8px 12px;
319
border-bottom: 1px solid #2a3150;
320
color: #e8eaf2;
321
}
322
.player-list .ready { color: #8eec9a; }
323
.player-list .waiting { color: #8e95b3; }
324
.player-list .you { font-weight: 700; }
325
.lobby-actions {
326
display: flex;
327
gap: 10px;
328
justify-content: center;
329
flex-wrap: wrap;
330
margin-top: 12px;
331
}
332
333
#minis {
334
position: fixed;
335
top: 0;
336
left: 0;
337
right: 0;
338
z-index: 4;
339
display: flex;
340
gap: 10px;
341
justify-content: center;
342
align-items: flex-end;
343
flex-wrap: wrap;
344
padding: 6px 8px 0;
345
pointer-events: none;
346
}
347
.mini {
348
text-align: center;
349
color: #c5cae0;
350
font-size: 11px;
351
min-width: 72px;
352
}
353
.mini-name {
354
max-width: 90px;
355
overflow: hidden;
356
text-overflow: ellipsis;
357
white-space: nowrap;
358
font-weight: 700;
359
}
360
.mini-canvas {
361
display: block;
362
width: 80px;
363
height: 104px;
364
background: #0c1020;
365
border: 1px solid #2a3150;
366
border-radius: 4px;
367
image-rendering: pixelated;
368
}
369
.mini.eliminated { opacity: 0.45; }
370
.mini.eliminated .mini-meta { color: #ffb0a0; }
371
372
#flights {
373
position: fixed;
374
inset: 0;
375
z-index: 6;
376
pointer-events: none;
377
overflow: visible;
378
}
379
#flights .flight {
380
position: fixed;
381
top: 0;
382
left: 0;
383
width: 40px;
384
height: 40px;
385
will-change: transform;
386
pointer-events: none;
387
}
388
389
#spectator h2, #podium h2 {
390
color: #ffe27a;
391
}
392
.podium-list {
393
list-style: none;
394
padding: 0;
395
margin: 16px auto;
396
max-width: 16em;
397
font-size: clamp(20px, 5vw, 28px);
398
}
399
.podium-list li {
400
padding: 6px 0;
401
color: #e8eaf2;
402
}
403
.podium-list li:first-child { color: #ffe27a; font-size: 1.2em; font-weight: 700; }
404
.podium-list li:nth-child(2) { color: #d0d6ea; }
405
.podium-list li:nth-child(3) { color: #e0a878; }
406
407
@media (max-height: 520px) {
408
#online-panel .hint, #online-panel .tagline { display: block; }
409
.mini-canvas { width: 56px; height: 73px; }
410
}
@@ -45,7 +45,7 @@ html, body {45
text-align: center;45
text-align: center;46
}46
}47
47
48
.screen[hidden], #hud[hidden], #game-over[hidden] { display: none !important; }48
.screen[hidden], #hud[hidden], #game-over[hidden], #online-panel[hidden], #minis[hidden], #spectator[hidden], #podium[hidden], #flights[hidden] { display: none !important; }49
49
50
h1 {50
h1 {51
font-size: clamp(48px, 14vw, 96px);51
font-size: clamp(48px, 14vw, 96px);@@ -183,3 +183,228 @@ canvas {183
h1 { font-size: 42px; }183
h1 { font-size: 42px; }184
#btn-play { padding: 10px 36px; }184
#btn-play { padding: 10px 36px; }185
}185
}186
187
188
/* ---- Stage 2: online, lobby, miniatures, flights, podium ---- */189
190
#btn-online, .btn-secondary, a.button {191
margin-top: 12px;192
padding: 10px 36px;193
font-size: clamp(16px, 4vw, 20px);194
border: 1px solid #6a7394;195
border-radius: 999px;196
color: #e8eaf2;197
background: linear-gradient(180deg, #2a3150, #1b2038);198
cursor: pointer;199
text-decoration: none;200
display: inline-block;201
font-family: inherit;202
}203
#btn-online:is(:hover, :focus-visible),204
.btn-secondary:is(:hover, :focus-visible),205
a.button:is(:hover, :focus-visible) {206
border-color: #ffe27a;207
color: #fff6c8;208
}209
#btn-create, #btn-join, #btn-ready, #btn-start, #btn-spectate,210
#btn-play {211
/* play already styled */212
}213
214
#btn-create, #btn-join, #btn-ready, #btn-start, #btn-spectate {215
padding: 10px 28px;216
font-size: clamp(16px, 4vw, 20px);217
border: 0;218
border-radius: 999px;219
color: #1a1020;220
background: linear-gradient(180deg, #ffe27a, #f0c53a 40%, #e89a20);221
cursor: pointer;222
font-weight: 700;223
font-family: inherit;224
}225
#btn-start:disabled, #btn-ready:disabled {226
opacity: 0.45;227
cursor: default;228
}229
230
#online-panel {231
z-index: 5;232
overflow: auto;233
text-align: center;234
padding: 32px 16px 48px;235
}236
#online-panel .who {237
color: #c5cae0;238
margin: 0 0 16px;239
}240
#online-panel .notice {241
max-width: 36em;242
margin: 0 auto 16px;243
text-align: left;244
line-height: 1.45;245
color: #c5cae0;246
}247
#online-panel code {248
font-size: 0.92em;249
color: #ffe27a;250
}251
#online-panel a { color: #9ec5ff; }252
.error {253
color: #ffb0a0;254
margin: 8px 0;255
}256
.notice {257
background: rgba(20, 24, 48, 0.7);258
border: 1px solid #3a4268;259
border-radius: 12px;260
padding: 14px 16px;261
}262
.join-form {263
display: flex;264
gap: 8px;265
justify-content: center;266
align-items: flex-end;267
flex-wrap: wrap;268
margin: 16px 0;269
}270
.join-form label {271
display: flex;272
flex-direction: column;273
align-items: flex-start;274
font-size: 13px;275
color: #9aa3c2;276
}277
.join-form input {278
font-size: 22px;279
letter-spacing: 0.2em;280
text-transform: uppercase;281
width: 8em;282
padding: 8px 10px;283
border-radius: 8px;284
border: 1px solid #6a7394;285
background: #141a33;286
color: #fff6c8;287
font-family: inherit;288
text-align: center;289
}290
.lobby-code {291
font-size: clamp(22px, 6vw, 34px);292
margin: 8px 0 16px;293
color: #ffe27a;294
}295
.btn-tiny {296
font-size: 13px;297
padding: 4px 10px;298
margin-left: 8px;299
border-radius: 999px;300
border: 1px solid #6a7394;301
background: #1b2038;302
color: #e8eaf2;303
cursor: pointer;304
font-family: inherit;305
letter-spacing: 0;306
vertical-align: middle;307
}308
.player-list {309
list-style: none;310
padding: 0;311
margin: 0 auto 18px;312
max-width: 22em;313
text-align: left;314
}315
.player-list li {316
display: flex;317
justify-content: space-between;318
padding: 8px 12px;319
border-bottom: 1px solid #2a3150;320
color: #e8eaf2;321
}322
.player-list .ready { color: #8eec9a; }323
.player-list .waiting { color: #8e95b3; }324
.player-list .you { font-weight: 700; }325
.lobby-actions {326
display: flex;327
gap: 10px;328
justify-content: center;329
flex-wrap: wrap;330
margin-top: 12px;331
}332
333
#minis {334
position: fixed;335
top: 0;336
left: 0;337
right: 0;338
z-index: 4;339
display: flex;340
gap: 10px;341
justify-content: center;342
align-items: flex-end;343
flex-wrap: wrap;344
padding: 6px 8px 0;345
pointer-events: none;346
}347
.mini {348
text-align: center;349
color: #c5cae0;350
font-size: 11px;351
min-width: 72px;352
}353
.mini-name {354
max-width: 90px;355
overflow: hidden;356
text-overflow: ellipsis;357
white-space: nowrap;358
font-weight: 700;359
}360
.mini-canvas {361
display: block;362
width: 80px;363
height: 104px;364
background: #0c1020;365
border: 1px solid #2a3150;366
border-radius: 4px;367
image-rendering: pixelated;368
}369
.mini.eliminated { opacity: 0.45; }370
.mini.eliminated .mini-meta { color: #ffb0a0; }371
372
#flights {373
position: fixed;374
inset: 0;375
z-index: 6;376
pointer-events: none;377
overflow: visible;378
}379
#flights .flight {380
position: fixed;381
top: 0;382
left: 0;383
width: 40px;384
height: 40px;385
will-change: transform;386
pointer-events: none;387
}388
389
#spectator h2, #podium h2 {390
color: #ffe27a;391
}392
.podium-list {393
list-style: none;394
padding: 0;395
margin: 16px auto;396
max-width: 16em;397
font-size: clamp(20px, 5vw, 28px);398
}399
.podium-list li {400
padding: 6px 0;401
color: #e8eaf2;402
}403
.podium-list li:first-child { color: #ffe27a; font-size: 1.2em; font-weight: 700; }404
.podium-list li:nth-child(2) { color: #d0d6ea; }405
.podium-list li:nth-child(3) { color: #e0a878; }406
407
@media (max-height: 520px) {408
#online-panel .hint, #online-panel .tagline { display: block; }409
.mini-canvas { width: 56px; height: 73px; }410
}@@ -0,0 +1,516 @@
1
'use strict';
2
3
/**
4
* Online play: talks to api.php (cookie session), polls lobby and game
5
* state, and draws opponent miniatures plus flying-block tweens.
6
* Real-time sync is not required; a 0.75 s poll is enough.
7
*/
8
9
var Online = {
10
POLL_MS: 750,
11
DEFAULT_MISS_CHANCE: 0.30,
12
FLIGHT_DURATION: 1.2,
13
configured: false,
14
local: false,
15
csrf: '',
16
me: null,
17
lobby: null,
18
incoming: [],
19
shotResults: [],
20
error: '',
21
_timer: null,
22
_pendingShots: [],
23
_flights: [],
24
_lastStatus: null,
25
onChange: null,
26
onGameStart: null,
27
onIncoming: null,
28
onFinished: null,
29
onLobbyReturn: null
30
};
31
32
Online._headers = function (json)
33
{
34
var h = { 'Accept': 'application/json' };
35
if (json)
36
{ h['Content-Type'] = 'application/json'; }
37
if (this.csrf)
38
{ h['X-CSRF-Token'] = this.csrf; }
39
return h;
40
};
41
42
Online._apply = function (data)
43
{
44
if (!data)
45
{ return; }
46
if (typeof data.configured === 'boolean')
47
{ this.configured = data.configured; }
48
if (typeof data.local === 'boolean')
49
{ this.local = data.local; }
50
if (data.csrf)
51
{ this.csrf = data.csrf; }
52
if (data.me !== undefined)
53
{ this.me = data.me; }
54
if (data.lobby !== undefined)
55
{ this.lobby = data.lobby; }
56
if (data.incoming)
57
{ this.incoming = data.incoming; }
58
if (data.shot_results)
59
{ this.shotResults = data.shot_results; }
60
if (data.miss_chance_default != null)
61
{ this.DEFAULT_MISS_CHANCE = data.miss_chance_default; }
62
if (data.flight_duration != null)
63
{ this.FLIGHT_DURATION = data.flight_duration; }
64
if (data.error)
65
{ this.error = data.error; }
66
else if (data.ok)
67
{ this.error = ''; }
68
};
69
70
Online.request = function (action, body)
71
{
72
var self = this;
73
var isGet = action === 'me' || action === '';
74
var url = 'api.php?action=' + encodeURIComponent(action || 'me');
75
var opts = {
76
method: isGet && !body ? 'GET' : 'POST',
77
credentials: 'same-origin',
78
headers: this._headers(!!body || !isGet)
79
};
80
if (opts.method === 'POST')
81
{
82
var payload = body ? Object.assign({ action: action }, body) : { action: action };
83
opts.body = JSON.stringify(payload);
84
}
85
return fetch(url, opts).then(function (res)
86
{
87
return res.json().then(function (data)
88
{
89
data = data || {};
90
if (!res.ok && !data.error)
91
{ data.error = 'De server gaf ' + res.status; data.ok = false; }
92
self._apply(data);
93
return data;
94
}, function ()
95
{
96
var fail = { ok: false, error: 'Geen geldig antwoord van de server.' };
97
self.error = fail.error;
98
return fail;
99
});
100
}).catch(function (err)
101
{
102
var fail = { ok: false, error: 'Geen verbinding met de server.' };
103
self.error = fail.error;
104
return fail;
105
});
106
};
107
108
Online.refresh = function ()
109
{
110
return this.request('me');
111
};
112
113
Online.testLogin = function (who)
114
{
115
return this.request('test_login', { user: who });
116
};
117
118
Online.createLobby = function ()
119
{
120
return this.request('lobby_create', { miss_chance: this.DEFAULT_MISS_CHANCE });
121
};
122
123
Online.joinLobby = function (code)
124
{
125
return this.request('lobby_join', { code: code });
126
};
127
128
Online.leaveLobby = function ()
129
{
130
return this.request('lobby_leave');
131
};
132
133
Online.setReady = function (ready)
134
{
135
return this.request('lobby_ready', { ready: !!ready });
136
};
137
138
Online.startGame = function ()
139
{
140
return this.request('lobby_start');
141
};
142
143
Online.queueShots = function (shots)
144
{
145
for (var i = 0; i < shots.length; i++)
146
{
147
this._pendingShots.push({
148
client_id: String(shots[i].id),
149
color: shots[i].color,
150
col: shots[i].col,
151
y: shots[i].y
152
});
153
}
154
};
155
156
Online.poll = function (opts)
157
{
158
opts = opts || {};
159
var body = {};
160
if (opts.snapshot)
161
{ body.snapshot = opts.snapshot; }
162
if (opts.score != null)
163
{ body.score = opts.score; }
164
if (opts.eliminated)
165
{ body.eliminated = true; }
166
if (this._pendingShots.length)
167
{
168
body.outgoing_shots = this._pendingShots.splice(0, this._pendingShots.length);
169
}
170
var self = this;
171
var prevStatus = this.lobby && this.lobby.status;
172
return this.request('poll', body).then(function (data)
173
{
174
self._handleTransitions(prevStatus, data);
175
if (self.onChange)
176
{ self.onChange(self); }
177
return data;
178
});
179
};
180
181
Online._handleTransitions = function (prevStatus, data)
182
{
183
var lobby = this.lobby;
184
var status = lobby && lobby.status;
185
if (status === 'playing' && prevStatus !== 'playing' && this.onGameStart)
186
{ this.onGameStart(lobby); }
187
if (data && data.incoming && data.incoming.length && this.onIncoming)
188
{ this.onIncoming(data.incoming); }
189
if (data && data.shot_results && data.shot_results.length)
190
{ this.spawnFlights(data.shot_results, this._lastQueuedMeta); }
191
if (status === 'finished' && prevStatus !== 'finished' && this.onFinished)
192
{ this.onFinished(lobby); }
193
if (status === 'waiting' && prevStatus === 'finished' && this.onLobbyReturn)
194
{ this.onLobbyReturn(lobby); }
195
};
196
197
Online.startPolling = function (getPayload)
198
{
199
this.stopPolling();
200
var self = this;
201
var tick = function ()
202
{
203
var payload = getPayload ? getPayload() : {};
204
self.poll(payload);
205
};
206
tick();
207
this._timer = setInterval(tick, this.POLL_MS);
208
};
209
210
Online.stopPolling = function ()
211
{
212
if (this._timer)
213
{
214
clearInterval(this._timer);
215
this._timer = null;
216
}
217
};
218
219
Online.loginUrl = function ()
220
{
221
return 'auth.php';
222
};
223
224
Online.logoutUrl = function ()
225
{
226
return 'logout.php';
227
};
228
229
Online.opponents = function ()
230
{
231
if (!this.lobby || !this.lobby.players)
232
{ return []; }
233
return this.lobby.players.filter(function (p) { return !p.is_me; });
234
};
235
236
Online.aliveOpponents = function ()
237
{
238
return this.opponents().filter(function (p) { return !p.eliminated; });
239
};
240
241
Online.mePlayer = function ()
242
{
243
if (!this.lobby || !this.lobby.players)
244
{ return null; }
245
for (var i = 0; i < this.lobby.players.length; i++)
246
{
247
if (this.lobby.players[i].is_me)
248
{ return this.lobby.players[i]; }
249
}
250
return null;
251
};
252
253
/* ---------- miniatures --------------------------------------------- */
254
255
Online.renderMinis = function (container)
256
{
257
if (!container)
258
{ return; }
259
var opponents = this.opponents();
260
var existing = {};
261
var kids = container.querySelectorAll('[data-player]');
262
for (var i = 0; i < kids.length; i++)
263
{ existing[kids[i].getAttribute('data-player')] = kids[i]; }
264
265
var seen = Object.create(null);
266
for (var i = 0; i < opponents.length; i++)
267
{
268
var p = opponents[i];
269
seen[p.id] = true;
270
var el = existing[p.id];
271
if (!el)
272
{
273
el = document.createElement('div');
274
el.className = 'mini';
275
el.setAttribute('data-player', p.id);
276
el.innerHTML = '<div class="mini-name"></div>'
277
+ '<canvas class="mini-canvas" width="80" height="104"></canvas>'
278
+ '<div class="mini-meta"></div>';
279
container.appendChild(el);
280
}
281
el.classList.toggle('eliminated', !!p.eliminated);
282
el.querySelector('.mini-name').textContent = p.name + (p.is_host ? ' (host)' : '');
283
el.querySelector('.mini-meta').textContent = p.eliminated
284
? ('Uit' + (p.place ? ' · ' + p.place + 'e' : ''))
285
: ('Punten ' + (p.score || 0));
286
this._drawMini(el.querySelector('canvas'), p.snapshot);
287
}
288
for (var id in existing)
289
{
290
if (!seen[id])
291
{ existing[id].remove(); }
292
}
293
};
294
295
var MINI_COLORS = ['#d4553a', '#3a9ad4', '#4caf6a', '#e6c04a', '#b86ad4'];
296
297
Online._drawMini = function (canvas, snapshot)
298
{
299
if (!canvas)
300
{ return; }
301
var ctx = canvas.getContext('2d');
302
var w = 10;
303
var h = 13;
304
var tw = canvas.width / w;
305
var th = canvas.height / h;
306
ctx.fillStyle = '#0c1020';
307
ctx.fillRect(0, 0, canvas.width, canvas.height);
308
ctx.strokeStyle = 'rgba(255,255,255,0.06)';
309
ctx.lineWidth = 1;
310
for (var c = 0; c <= w; c++)
311
{
312
ctx.beginPath();
313
ctx.moveTo(c * tw + 0.5, 0);
314
ctx.lineTo(c * tw + 0.5, canvas.height);
315
ctx.stroke();
316
}
317
for (var r = 0; r <= h; r++)
318
{
319
ctx.beginPath();
320
ctx.moveTo(0, r * th + 0.5);
321
ctx.lineTo(canvas.width, r * th + 0.5);
322
ctx.stroke();
323
}
324
if (!snapshot || !snapshot.blocks)
325
{ return; }
326
var blocks = snapshot.blocks;
327
for (var i = 0; i < blocks.length; i++)
328
{
329
var b = blocks[i];
330
var x = b.col * tw;
331
var y = canvas.height - (b.y + 1) * th;
332
if (b.state === 'locked')
333
{ ctx.fillStyle = '#3a3d4a'; }
334
else if (b.color < 0)
335
{ ctx.fillStyle = '#5a5d66'; }
336
else
337
{ ctx.fillStyle = MINI_COLORS[b.color] || '#888'; }
338
ctx.fillRect(x + 0.5, y + 0.5, tw - 1, th - 1);
339
}
340
};
341
342
Online.miniElement = function (playerId)
343
{
344
return document.querySelector('#minis .mini[data-player="' + CSS.escape(playerId) + '"]');
345
};
346
347
/* ---------- flight tweens ------------------------------------------ */
348
349
function colorSpriteUrl (color)
350
{
351
if (color < 0)
352
{ return 'images/block-gray.png'; }
353
var names = ['ember', 'aqua', 'leaf', 'sun', 'orchid'];
354
return 'images/block-' + (names[color] || 'gray') + '.png';
355
}
356
357
/**
358
* Animate shot results. `locals` is a map of client_id → {col, y, color,
359
* startX, startY} in viewport pixels, filled by the view just after
360
* consumeOutgoingShots.
361
*/
362
Online.spawnFlights = function (results, locals)
363
{
364
locals = locals || this._flightLocals || {};
365
var layer = document.getElementById('flights');
366
if (!layer)
367
{ return; }
368
for (var i = 0; i < results.length; i++)
369
{
370
var r = results[i];
371
var meta = locals[r.client_id] || {};
372
var start = this._flightStart(meta.col);
373
var end;
374
var miss = !!r.miss;
375
if (miss)
376
{ end = this._missEnd(start); }
377
else
378
{ end = this._targetEnd(r.target_id, start); }
379
var el = document.createElement('img');
380
el.src = colorSpriteUrl(r.color != null ? r.color : meta.color);
381
el.alt = '';
382
el.className = 'flight';
383
layer.appendChild(el);
384
var spin = (Math.random() * 2 - 1) * 720;
385
var duration = (r.flight_duration || this.FLIGHT_DURATION) * 1000;
386
this._flights.push({
387
el: el,
388
start: start,
389
end: end,
390
ctrl: miss ? this._missCtrl(start, end) : this._hitCtrl(start, end),
391
t0: performance.now(),
392
duration: duration,
393
spin: spin,
394
miss: miss,
395
angle: Math.random() * 360
396
});
397
}
398
this._flightLocals = {};
399
};
400
401
Online.rememberFlightLocal = function (shot, startX, startY)
402
{
403
if (!this._flightLocals)
404
{ this._flightLocals = {}; }
405
this._flightLocals[String(shot.id)] = {
406
col: shot.col,
407
y: shot.y,
408
color: shot.color,
409
startX: startX,
410
startY: startY
411
};
412
};
413
414
Online._flightStart = function (col)
415
{
416
var field = document.querySelector('canvas');
417
var appRef = typeof app !== 'undefined' ? app : null;
418
if (appRef && appRef.scene && appRef.scene.canvas)
419
{
420
var canvas = appRef.scene.canvas;
421
var rect = canvas.getBoundingClientRect();
422
var c = col == null ? 4.5 : col;
423
return {
424
x: rect.left + (c + 0.5) * (rect.width / 10),
425
y: rect.top + 4
426
};
427
}
428
if (field)
429
{
430
var rect = field.getBoundingClientRect();
431
return { x: rect.left + rect.width / 2, y: rect.top };
432
}
433
return { x: window.innerWidth / 2, y: window.innerHeight * 0.3 };
434
};
435
436
Online._targetEnd = function (targetId, start)
437
{
438
var el = targetId && this.miniElement(targetId);
439
if (el)
440
{
441
var r = el.getBoundingClientRect();
442
return { x: r.left + r.width / 2, y: r.top + r.height / 2 };
443
}
444
return { x: start.x + (Math.random() - 0.5) * 80, y: 40 };
445
};
446
447
Online._missEnd = function (start)
448
{
449
var dx = (Math.random() * 2 - 1) * window.innerWidth * 0.35;
450
return {
451
x: start.x + dx,
452
y: start.y - (120 + Math.random() * 220)
453
};
454
};
455
456
Online._missCtrl = function (start, end)
457
{
458
return {
459
x: (start.x + end.x) / 2 + (Math.random() - 0.5) * 160,
460
y: Math.min(start.y, end.y) - 40 - Math.random() * 80
461
};
462
};
463
464
Online._hitCtrl = function (start, end)
465
{
466
return {
467
x: (start.x + end.x) / 2 + (Math.random() - 0.5) * 40,
468
y: (start.y + end.y) / 2 - 30
469
};
470
};
471
472
Online.tickFlights = function (now)
473
{
474
now = now || performance.now();
475
var leftover = [];
476
for (var i = 0; i < this._flights.length; i++)
477
{
478
var f = this._flights[i];
479
var u = (now - f.t0) / f.duration;
480
if (u >= 1)
481
{
482
if (f.el && f.el.parentNode)
483
{ f.el.parentNode.removeChild(f.el); }
484
continue;
485
}
486
// Quadratic bezier, ease-in so they feel like they recede.
487
var e = u * u;
488
var mt = 1 - e;
489
var x = mt * mt * f.start.x + 2 * mt * e * f.ctrl.x + e * e * f.end.x;
490
var y = mt * mt * f.start.y + 2 * mt * e * f.ctrl.y + e * e * f.end.y;
491
var scale = f.miss ? (1 - e) : (1 - 0.85 * e);
492
var angle = f.angle + f.spin * u;
493
f.el.style.transform = 'translate(' + x + 'px,' + y + 'px) translate(-50%,-50%) rotate('
494
+ angle + 'deg) scale(' + scale + ')';
495
f.el.style.opacity = f.miss ? String(1 - e) : '1';
496
leftover.push(f);
497
}
498
this._flights = leftover;
499
};
500
501
Online.clearFlights = function ()
502
{
503
for (var i = 0; i < this._flights.length; i++)
504
{
505
var el = this._flights[i].el;
506
if (el && el.parentNode)
507
{ el.parentNode.removeChild(el); }
508
}
509
this._flights = [];
510
this._flightLocals = {};
511
};
512
513
if (typeof globalThis !== 'undefined')
514
{ globalThis.Online = Online; }
515
if (typeof module !== 'undefined' && module.exports)
516
{ module.exports = Online; }
@@ -0,0 +1,516 @@1
'use strict';2
3
/**4
* Online play: talks to api.php (cookie session), polls lobby and game5
* state, and draws opponent miniatures plus flying-block tweens.6
* Real-time sync is not required; a 0.75 s poll is enough.7
*/8
9
var Online = {10
POLL_MS: 750,11
DEFAULT_MISS_CHANCE: 0.30,12
FLIGHT_DURATION: 1.2,13
configured: false,14
local: false,15
csrf: '',16
me: null,17
lobby: null,18
incoming: [],19
shotResults: [],20
error: '',21
_timer: null,22
_pendingShots: [],23
_flights: [],24
_lastStatus: null,25
onChange: null,26
onGameStart: null,27
onIncoming: null,28
onFinished: null,29
onLobbyReturn: null30
};31
32
Online._headers = function (json)33
{34
var h = { 'Accept': 'application/json' };35
if (json)36
{ h['Content-Type'] = 'application/json'; }37
if (this.csrf)38
{ h['X-CSRF-Token'] = this.csrf; }39
return h;40
};41
42
Online._apply = function (data)43
{44
if (!data)45
{ return; }46
if (typeof data.configured === 'boolean')47
{ this.configured = data.configured; }48
if (typeof data.local === 'boolean')49
{ this.local = data.local; }50
if (data.csrf)51
{ this.csrf = data.csrf; }52
if (data.me !== undefined)53
{ this.me = data.me; }54
if (data.lobby !== undefined)55
{ this.lobby = data.lobby; }56
if (data.incoming)57
{ this.incoming = data.incoming; }58
if (data.shot_results)59
{ this.shotResults = data.shot_results; }60
if (data.miss_chance_default != null)61
{ this.DEFAULT_MISS_CHANCE = data.miss_chance_default; }62
if (data.flight_duration != null)63
{ this.FLIGHT_DURATION = data.flight_duration; }64
if (data.error)65
{ this.error = data.error; }66
else if (data.ok)67
{ this.error = ''; }68
};69
70
Online.request = function (action, body)71
{72
var self = this;73
var isGet = action === 'me' || action === '';74
var url = 'api.php?action=' + encodeURIComponent(action || 'me');75
var opts = {76
method: isGet && !body ? 'GET' : 'POST',77
credentials: 'same-origin',78
headers: this._headers(!!body || !isGet)79
};80
if (opts.method === 'POST')81
{82
var payload = body ? Object.assign({ action: action }, body) : { action: action };83
opts.body = JSON.stringify(payload);84
}85
return fetch(url, opts).then(function (res)86
{87
return res.json().then(function (data)88
{89
data = data || {};90
if (!res.ok && !data.error)91
{ data.error = 'De server gaf ' + res.status; data.ok = false; }92
self._apply(data);93
return data;94
}, function ()95
{96
var fail = { ok: false, error: 'Geen geldig antwoord van de server.' };97
self.error = fail.error;98
return fail;99
});100
}).catch(function (err)101
{102
var fail = { ok: false, error: 'Geen verbinding met de server.' };103
self.error = fail.error;104
return fail;105
});106
};107
108
Online.refresh = function ()109
{110
return this.request('me');111
};112
113
Online.testLogin = function (who)114
{115
return this.request('test_login', { user: who });116
};117
118
Online.createLobby = function ()119
{120
return this.request('lobby_create', { miss_chance: this.DEFAULT_MISS_CHANCE });121
};122
123
Online.joinLobby = function (code)124
{125
return this.request('lobby_join', { code: code });126
};127
128
Online.leaveLobby = function ()129
{130
return this.request('lobby_leave');131
};132
133
Online.setReady = function (ready)134
{135
return this.request('lobby_ready', { ready: !!ready });136
};137
138
Online.startGame = function ()139
{140
return this.request('lobby_start');141
};142
143
Online.queueShots = function (shots)144
{145
for (var i = 0; i < shots.length; i++)146
{147
this._pendingShots.push({148
client_id: String(shots[i].id),149
color: shots[i].color,150
col: shots[i].col,151
y: shots[i].y152
});153
}154
};155
156
Online.poll = function (opts)157
{158
opts = opts || {};159
var body = {};160
if (opts.snapshot)161
{ body.snapshot = opts.snapshot; }162
if (opts.score != null)163
{ body.score = opts.score; }164
if (opts.eliminated)165
{ body.eliminated = true; }166
if (this._pendingShots.length)167
{168
body.outgoing_shots = this._pendingShots.splice(0, this._pendingShots.length);169
}170
var self = this;171
var prevStatus = this.lobby && this.lobby.status;172
return this.request('poll', body).then(function (data)173
{174
self._handleTransitions(prevStatus, data);175
if (self.onChange)176
{ self.onChange(self); }177
return data;178
});179
};180
181
Online._handleTransitions = function (prevStatus, data)182
{183
var lobby = this.lobby;184
var status = lobby && lobby.status;185
if (status === 'playing' && prevStatus !== 'playing' && this.onGameStart)186
{ this.onGameStart(lobby); }187
if (data && data.incoming && data.incoming.length && this.onIncoming)188
{ this.onIncoming(data.incoming); }189
if (data && data.shot_results && data.shot_results.length)190
{ this.spawnFlights(data.shot_results, this._lastQueuedMeta); }191
if (status === 'finished' && prevStatus !== 'finished' && this.onFinished)192
{ this.onFinished(lobby); }193
if (status === 'waiting' && prevStatus === 'finished' && this.onLobbyReturn)194
{ this.onLobbyReturn(lobby); }195
};196
197
Online.startPolling = function (getPayload)198
{199
this.stopPolling();200
var self = this;201
var tick = function ()202
{203
var payload = getPayload ? getPayload() : {};204
self.poll(payload);205
};206
tick();207
this._timer = setInterval(tick, this.POLL_MS);208
};209
210
Online.stopPolling = function ()211
{212
if (this._timer)213
{214
clearInterval(this._timer);215
this._timer = null;216
}217
};218
219
Online.loginUrl = function ()220
{221
return 'auth.php';222
};223
224
Online.logoutUrl = function ()225
{226
return 'logout.php';227
};228
229
Online.opponents = function ()230
{231
if (!this.lobby || !this.lobby.players)232
{ return []; }233
return this.lobby.players.filter(function (p) { return !p.is_me; });234
};235
236
Online.aliveOpponents = function ()237
{238
return this.opponents().filter(function (p) { return !p.eliminated; });239
};240
241
Online.mePlayer = function ()242
{243
if (!this.lobby || !this.lobby.players)244
{ return null; }245
for (var i = 0; i < this.lobby.players.length; i++)246
{247
if (this.lobby.players[i].is_me)248
{ return this.lobby.players[i]; }249
}250
return null;251
};252
253
/* ---------- miniatures --------------------------------------------- */254
255
Online.renderMinis = function (container)256
{257
if (!container)258
{ return; }259
var opponents = this.opponents();260
var existing = {};261
var kids = container.querySelectorAll('[data-player]');262
for (var i = 0; i < kids.length; i++)263
{ existing[kids[i].getAttribute('data-player')] = kids[i]; }264
265
var seen = Object.create(null);266
for (var i = 0; i < opponents.length; i++)267
{268
var p = opponents[i];269
seen[p.id] = true;270
var el = existing[p.id];271
if (!el)272
{273
el = document.createElement('div');274
el.className = 'mini';275
el.setAttribute('data-player', p.id);276
el.innerHTML = '<div class="mini-name"></div>'277
+ '<canvas class="mini-canvas" width="80" height="104"></canvas>'278
+ '<div class="mini-meta"></div>';279
container.appendChild(el);280
}281
el.classList.toggle('eliminated', !!p.eliminated);282
el.querySelector('.mini-name').textContent = p.name + (p.is_host ? ' (host)' : '');283
el.querySelector('.mini-meta').textContent = p.eliminated284
? ('Uit' + (p.place ? ' · ' + p.place + 'e' : ''))285
: ('Punten ' + (p.score || 0));286
this._drawMini(el.querySelector('canvas'), p.snapshot);287
}288
for (var id in existing)289
{290
if (!seen[id])291
{ existing[id].remove(); }292
}293
};294
295
var MINI_COLORS = ['#d4553a', '#3a9ad4', '#4caf6a', '#e6c04a', '#b86ad4'];296
297
Online._drawMini = function (canvas, snapshot)298
{299
if (!canvas)300
{ return; }301
var ctx = canvas.getContext('2d');302
var w = 10;303
var h = 13;304
var tw = canvas.width / w;305
var th = canvas.height / h;306
ctx.fillStyle = '#0c1020';307
ctx.fillRect(0, 0, canvas.width, canvas.height);308
ctx.strokeStyle = 'rgba(255,255,255,0.06)';309
ctx.lineWidth = 1;310
for (var c = 0; c <= w; c++)311
{312
ctx.beginPath();313
ctx.moveTo(c * tw + 0.5, 0);314
ctx.lineTo(c * tw + 0.5, canvas.height);315
ctx.stroke();316
}317
for (var r = 0; r <= h; r++)318
{319
ctx.beginPath();320
ctx.moveTo(0, r * th + 0.5);321
ctx.lineTo(canvas.width, r * th + 0.5);322
ctx.stroke();323
}324
if (!snapshot || !snapshot.blocks)325
{ return; }326
var blocks = snapshot.blocks;327
for (var i = 0; i < blocks.length; i++)328
{329
var b = blocks[i];330
var x = b.col * tw;331
var y = canvas.height - (b.y + 1) * th;332
if (b.state === 'locked')333
{ ctx.fillStyle = '#3a3d4a'; }334
else if (b.color < 0)335
{ ctx.fillStyle = '#5a5d66'; }336
else337
{ ctx.fillStyle = MINI_COLORS[b.color] || '#888'; }338
ctx.fillRect(x + 0.5, y + 0.5, tw - 1, th - 1);339
}340
};341
342
Online.miniElement = function (playerId)343
{344
return document.querySelector('#minis .mini[data-player="' + CSS.escape(playerId) + '"]');345
};346
347
/* ---------- flight tweens ------------------------------------------ */348
349
function colorSpriteUrl (color)350
{351
if (color < 0)352
{ return 'images/block-gray.png'; }353
var names = ['ember', 'aqua', 'leaf', 'sun', 'orchid'];354
return 'images/block-' + (names[color] || 'gray') + '.png';355
}356
357
/**358
* Animate shot results. `locals` is a map of client_id → {col, y, color,359
* startX, startY} in viewport pixels, filled by the view just after360
* consumeOutgoingShots.361
*/362
Online.spawnFlights = function (results, locals)363
{364
locals = locals || this._flightLocals || {};365
var layer = document.getElementById('flights');366
if (!layer)367
{ return; }368
for (var i = 0; i < results.length; i++)369
{370
var r = results[i];371
var meta = locals[r.client_id] || {};372
var start = this._flightStart(meta.col);373
var end;374
var miss = !!r.miss;375
if (miss)376
{ end = this._missEnd(start); }377
else378
{ end = this._targetEnd(r.target_id, start); }379
var el = document.createElement('img');380
el.src = colorSpriteUrl(r.color != null ? r.color : meta.color);381
el.alt = '';382
el.className = 'flight';383
layer.appendChild(el);384
var spin = (Math.random() * 2 - 1) * 720;385
var duration = (r.flight_duration || this.FLIGHT_DURATION) * 1000;386
this._flights.push({387
el: el,388
start: start,389
end: end,390
ctrl: miss ? this._missCtrl(start, end) : this._hitCtrl(start, end),391
t0: performance.now(),392
duration: duration,393
spin: spin,394
miss: miss,395
angle: Math.random() * 360396
});397
}398
this._flightLocals = {};399
};400
401
Online.rememberFlightLocal = function (shot, startX, startY)402
{403
if (!this._flightLocals)404
{ this._flightLocals = {}; }405
this._flightLocals[String(shot.id)] = {406
col: shot.col,407
y: shot.y,408
color: shot.color,409
startX: startX,410
startY: startY411
};412
};413
414
Online._flightStart = function (col)415
{416
var field = document.querySelector('canvas');417
var appRef = typeof app !== 'undefined' ? app : null;418
if (appRef && appRef.scene && appRef.scene.canvas)419
{420
var canvas = appRef.scene.canvas;421
var rect = canvas.getBoundingClientRect();422
var c = col == null ? 4.5 : col;423
return {424
x: rect.left + (c + 0.5) * (rect.width / 10),425
y: rect.top + 4426
};427
}428
if (field)429
{430
var rect = field.getBoundingClientRect();431
return { x: rect.left + rect.width / 2, y: rect.top };432
}433
return { x: window.innerWidth / 2, y: window.innerHeight * 0.3 };434
};435
436
Online._targetEnd = function (targetId, start)437
{438
var el = targetId && this.miniElement(targetId);439
if (el)440
{441
var r = el.getBoundingClientRect();442
return { x: r.left + r.width / 2, y: r.top + r.height / 2 };443
}444
return { x: start.x + (Math.random() - 0.5) * 80, y: 40 };445
};446
447
Online._missEnd = function (start)448
{449
var dx = (Math.random() * 2 - 1) * window.innerWidth * 0.35;450
return {451
x: start.x + dx,452
y: start.y - (120 + Math.random() * 220)453
};454
};455
456
Online._missCtrl = function (start, end)457
{458
return {459
x: (start.x + end.x) / 2 + (Math.random() - 0.5) * 160,460
y: Math.min(start.y, end.y) - 40 - Math.random() * 80461
};462
};463
464
Online._hitCtrl = function (start, end)465
{466
return {467
x: (start.x + end.x) / 2 + (Math.random() - 0.5) * 40,468
y: (start.y + end.y) / 2 - 30469
};470
};471
472
Online.tickFlights = function (now)473
{474
now = now || performance.now();475
var leftover = [];476
for (var i = 0; i < this._flights.length; i++)477
{478
var f = this._flights[i];479
var u = (now - f.t0) / f.duration;480
if (u >= 1)481
{482
if (f.el && f.el.parentNode)483
{ f.el.parentNode.removeChild(f.el); }484
continue;485
}486
// Quadratic bezier, ease-in so they feel like they recede.487
var e = u * u;488
var mt = 1 - e;489
var x = mt * mt * f.start.x + 2 * mt * e * f.ctrl.x + e * e * f.end.x;490
var y = mt * mt * f.start.y + 2 * mt * e * f.ctrl.y + e * e * f.end.y;491
var scale = f.miss ? (1 - e) : (1 - 0.85 * e);492
var angle = f.angle + f.spin * u;493
f.el.style.transform = 'translate(' + x + 'px,' + y + 'px) translate(-50%,-50%) rotate('494
+ angle + 'deg) scale(' + scale + ')';495
f.el.style.opacity = f.miss ? String(1 - e) : '1';496
leftover.push(f);497
}498
this._flights = leftover;499
};500
501
Online.clearFlights = function ()502
{503
for (var i = 0; i < this._flights.length; i++)504
{505
var el = this._flights[i].el;506
if (el && el.parentNode)507
{ el.parentNode.removeChild(el); }508
}509
this._flights = [];510
this._flightLocals = {};511
};512
513
if (typeof globalThis !== 'undefined')514
{ globalThis.Online = Online; }515
if (typeof module !== 'undefined' && module.exports)516
{ module.exports = Online; }