-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
323 lines (289 loc) · 15.1 KB
/
index.html
File metadata and controls
323 lines (289 loc) · 15.1 KB
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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no">
<title>MultiTug</title>
<style>
html, body {
height: 100%;
margin: 0;
overflow: hidden;
-webkit-touch-callout: none;
-webkit-user-select: none;
user-select: none;
touch-action: none;
background: #000;
color: #69F;
font-family: monospace;
}
body {
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
text-align: center;
font-size: 2em;
}
#players {
height: 4em;
margin-bottom: 1em;
}
#score {
font-size: 2em;
}
#timer {
font-size: 2em;
}
#button {
margin-top: 1em;
padding: .5em 1em;
font-size: 1em;
cursor: pointer;
height: 3.5em;
border: none;
border-radius: 1em;
font-family: monospace;
}
#button.spectating {
background: #AAA;
}
.bubble {
position: fixed;
padding: .5em;
transform: translate(-50%, -50%);
transition: top 1s, left 1s, right 1s, opacity 1s;
border: 2px solid;
border-radius: 2em;
}
#button,.bubble {
background: #69F;
color: #000;
border-color: #000;
}
#icon {
width: 1em;
height: 1em;
background: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAGQAAABkCAYAAABw4pVUAAALtklEQVR4Ae2dD5BVVR3Hf/ftLizIn8E/yB/TzUmMXaQYdSytsRnJzEoaNW2aQCUZlj9aiGgzkVFa5NjoTMP+UVOKjAnCGhRrkmockShkoYg/QbI2sBIkKJKAsLw9fX533915u+7uu/e+e+6+9/aembPn3HPP+Z3f/X7vOb/z790VSVyCQIJAgkCCQIJAgkCCQIJAgkCCQIJAgkCCQIJAgkCCQIJAcSDgWFGzwYxE7nIxcgXhUimT+TLTabVSV4kJjZ6QOjNEHHkJnC7NwuoPclpukrudo1lpSbQbBFLdpIVPWmTKIeNXCMgmQ+VNlgp5RX5szgsvvH+UjJaQc+VJYLuuW+iMXAIpG6TeTOz2fpLoIhAdIQ3mIWzG7b3iakRbyDpImdxrvn58MxobUm9qwbAhAI5q4GfIbOdnAcr0i6z5E7LETJGUPAtaZSEQWyyz5FviOCZE2ZIskh8hjebj0iZ/BJlBeaDza6icyrD4eB4ySqZoeELqzcWgsB5/Vt5oOLIZGTfILOeNvGUVuYBwhNSZUQxvN/DsVRE+/366vilS62yKUGbRiQo+ylpqKiHjdzxpVcRPO4bu72WpM7dGLLeoxAUn5Lh8mSf8qKWnHATZv2RY/IisNGEGCZbUik9scEIcORaDevfKYfm9LDH526cYlI2yiuCEzJJVKLAsSiW6lWXkGmxKkzSYSd3eL9HE4ITonGG2cxuz8gdjwOQC6lmPXZkWQ10FUUW4UZaneoOZDmCPc1nuJVkM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A/wGXmAcV9YPt8wAAAABJRU5ErkJggg==');
background-size: contain;
display: inline-block;
vertical-align: middle;
}
#codelink {
position: fixed; bottom: 10px; right: 10px; padding: .5em; border-radius: 30px;
opacity: 50%; background: lightgray; box-shadow: 1px 1px 5px black;
text-decoration: none; font-family: monospace; font-size: 1em;
}
#codelink:hover, #initials:hover {
opacity: 100%;
}
@media (prefers-color-scheme: light) {
body { background: #EEF; color: #00F; }
#button,.bubble { background: #00F; color: #EEF; border-color: #EEF; }
#codelink { background: #FFF; color: #00F; }
}
</style>
<script src="https://cdn.jsdelivr.net/npm/@multisynq/client@1.1.0/bundled/multisynq-client.min.js"></script>
</head>
<body>
<h1><div id="icon"></div> MultiTug</h1>
<div id="players">...</div>
<div id="score"></div>
<div id="timer">−−:−−</div>
<button id="button">Waiting</button>
<a id="codelink" href="https://github.com/multisynq/multitug/blob/main/index.html"
title="View source code on GitHub" target="_blank">{}</a>
<script>
/////////// Model code is executed inside of synced VM ///////////
const GAMETIME = 60; // seconds per round
const COUNTDOWN = 3; // seconds to wait before game starts
const TIMEOUT = 5; // seconds to wait after game ends
class TugGame extends Multisynq.Model {
init(_) {
this.counter = 0;
this.timer = -TIMEOUT;
this.players = new Map();
this.subscribe(this.sessionId, "view-join", this.onPlayerJoin);
this.subscribe(this.sessionId, "view-exit", this.onPlayerExit);
this.subscribe("button", "click", this.onButtonClicked);
}
onPlayerJoin({ viewData: { playerId }}) {
const views = this.players.get(playerId) || 0;
this.players.set(playerId, views + 1);
this.publish("players", "changed");
}
onPlayerExit({ viewData: { playerId }}) {
const views = this.players.get(playerId) - 1;
if (views > 0) {
this.players.set(playerId, views);
} else {
if (this.getPlayerPos(playerId) && this.counter) {
this.counter = 0;
this.publish("score", "changed");
this.timer = -TIMEOUT;
this.cancelFuture(this.tick);
this.publish("timer", "changed");
}
this.players.delete(playerId);
this.publish("players", "changed");
}
}
onButtonClicked(playerId) {
if (this.players.size < 2) return;
if (this.timer === -TIMEOUT) return this.onGameStart();
if (this.timer > 0 && this.timer <= GAMETIME) this.onPlayerTug(playerId);
}
onGameStart() {
this.timer = GAMETIME + COUNTDOWN;
this.counter = 0;
this.publish("score", "changed");
this.publish("timer", "changed");
this.future(1000).tick();
}
onPlayerTug(playerId) {
if (this.getPlayerPos(playerId) === 1) {
this.counter++;
if (this.counter === 0) this.counter = 1;
this.publish("score", "changed", playerId);
} else if (this.getPlayerPos(playerId) === 2) {
this.counter--;
if (this.counter === 0) this.counter = -1;
this.publish("score", "changed", playerId);
}
}
tick() {
this.timer--;
this.publish("timer", "changed");
if (this.timer > -TIMEOUT) this.future(1000).tick(); // wait 5 sec before new game
if (this.timer === 0) this.publish("score", "changed");
}
getPlayerPos(playerId) {
const index = [...this.players.keys()].indexOf(playerId) + 1;
return index <= 2 ? index : 0;
}
}
TugGame.register("TugGame");
/////////// Code below is executed outside of synced VM ///////////
const PlayerIdKey = window.location.pathname + "#PlayerId";
let PlayerId = localStorage.getItem(PlayerIdKey);
if (!PlayerId) {
const random = crypto.getRandomValues(new BigUint64Array(2));
PlayerId = random.reduce((a, b) => a + b.toString(36), "");
localStorage.setItem(PlayerIdKey, PlayerId);
}
function s(c, w) { return `${c} ${c === 1 ? w.slice(0, -1) : w}`; }
class TugUI extends Multisynq.View {
constructor(model) {
super(model);
this.model = model;
button.onclick = () => this.publish("button", "click", PlayerId);
this.subscribe("players", "changed", this.showPlayers);
this.subscribe("score" , "changed", this.showScore);
this.subscribe("timer" , "changed", this.showTimer);
this.showPlayers();
this.showScore();
this.showTimer();
}
updateButton() {
let enable = false;
let spectating = false;
if (this.model.getPlayerPos(PlayerId) === 0) {
button.textContent = "Spectating";
spectating = true;
} else if (this.model.players.size < 2) {
button.textContent = "Waiting for other player";
} else if (this.model.timer === -TIMEOUT) {
button.textContent = "Start"; enable = true;
} else if (this.model.timer <= 0) {
button.textContent = `GG! (${TIMEOUT + this.model.timer})`;
} else if (this.model.timer > GAMETIME) {
button.textContent = `Ready (${this.model.timer - GAMETIME})`;
} else {
button.textContent = "Tug"; enable = true;
}
button.disabled = !enable;
button.classList.toggle("spectating", spectating);
button.style.pointerEvents = enable ? "auto" : "none"; // prevent zoom on double tap
}
showPlayers() {
const total = this.model.players.size;
const playing = Math.min(total, 2);
const spectating = Math.max(total - 2, 0);
const myPos = this.model.getPlayerPos(PlayerId);
players.textContent = `You are ${myPos ? `Player#${myPos}` : "Spectating"}. There are ${s(playing, "players")} and ${s(spectating, "spectators")}.`;
this.updateButton();
}
showScore(playerId) {
const counter = this.model.counter;
// Show the score as graphic like −−−−−+−−−−− based on the counter value
function graphic(c = counter) {
const n = Math.max(0,Math.min(10, c / 2 + 5|0));
return `${'−'.repeat(10-n)}+${'−'.repeat(n)}`;
}
if (this.model.timer === -TIMEOUT || this.model.timer > GAMETIME) {
score.innerHTML = `<br>${graphic(0)}`;
return;
}
const inGame = this.model.timer > 0;
const myPos = this.model.getPlayerPos(PlayerId);
const leadingPos = counter > 0 ? 1 : 2;
if (myPos === leadingPos) {
score.innerHTML = `${inGame ? "Ahead" : "You won"} by ${Math.abs(counter)}<br>${graphic()}`;
} else if (myPos) {
score.innerHTML = `${inGame ? "Behind" : "You lost"} by ${Math.abs(counter)}<br>${graphic()}`;
} else {
score.innerHTML = `Player#${leadingPos} ${inGame ? "leads" : "won"} by ${Math.abs(counter)}<br>${graphic()}`;
}
// if this event was triggered by a player tug, show the tug animation
if (playerId) this.animateTug(playerId);
}
showTimer() {
let time = this.model.timer;
if (time <= 0 && this.model.counter) {
timer.textContent = "Game Over";
} if (time === -TIMEOUT) {
timer.innerHTML = "−−:−−";
this.showScore();
} else {
time = Math.max(0, Math.min(GAMETIME, time));
const minutes = time / 60 | 0;
const seconds = time % 60;
timer.textContent = `${minutes.toString().padStart(2, "0")}:${seconds.toString().padStart(2, "0")}`;
}
this.updateButton();
}
animateTug(playerId) {
const playerPos = this.model.getPlayerPos(playerId);
if (!playerPos) return;
const isSpectator = !this.model.getPlayerPos(PlayerId);
const bubble = document.createElement("div");
bubble.className = "bubble";
bubble.textContent = playerPos === 1 ? "#1" : "#2";
bubble.style.top = playerId === PlayerId ? button.offsetTop + 'px' : 0;
bubble.style.left = "50%";
bubble.style.opacity = 1;
const target = playerPos === 1 ? "20%" : "80%";
document.body.appendChild(bubble);
setTimeout(() => {
bubble.style.top = "50%";
bubble.style.left = target;
bubble.style.opacity = 0;
}, 0);
setTimeout(() => bubble.remove(), 1000);
}
}
document.body.ontouchstart = e => e.preventDefault(); // prevent zoom on double tap
Multisynq.App.makeWidgetDock(); // shows QR code
Multisynq.Session.join({
apiKey: '2d0fPrsWoWQFrelRWB1TftMoNJLfOk4Ni6XkQvswX3', // get your own from multisynq.io
appId: 'io.multisynq.multitug',
name: Multisynq.App.autoSession(),
password: Multisynq.App.autoPassword(),
model: TugGame,
view: TugUI,
viewData: { playerId: PlayerId },
tps: 5,
});
</script>
</body>
</html>