forked from ably-labs/Ablingo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
174 lines (108 loc) · 5.08 KB
/
index.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
const urlParams = new URLSearchParams(location.search);
const queryGameId = urlParams.get("gameId");
const queryMessage = urlParams.get("message");
const isInviteLink = [...urlParams.keys()].indexOf("invite") > -1;
const isHostLink = [...urlParams.keys()].indexOf("host") > -1;
const linkGenerator = new LinkGenerator(window.location);
var app = new Vue({
el: '#app',
data: {
gameClient: null,
gameServer: null,
identity: new Identity(generateName(2)),
gameId: queryGameId || generateName(3, "-").toLocaleLowerCase(),
message: queryMessage || null,
isInviteLink: isInviteLink,
isHostLink: isHostLink,
soundEnabled: false,
bingoCallIntervalSeconds: 5,
bingoAvailable: true,
bingoDebounce: 5000
},
watch: {
soundEnabled: function (val) { this.gameClient.soundEnabled = val; },
bingoAvailable: function (val) { this.bingoAvailable = val; }
},
computed: {
state: function() { return this.gameClient?.state; },
transmittedServerState: function() { return this.gameClient?.serverState; },
inviteLink: function () { return linkGenerator.linkTo({ gameId: app.gameId, invite: true }); },
gameRunning: function () { return this.gameClient?.serverState?.status === "running"; },
gameComplete: function () { return this.gameClient?.serverState?.status === "complete"; },
gameReady: function () { return this.gameClient?.state?.status == "acknowledged"; },
youAreHost: function () { return this.gameServer != null; },
joinedOrHosting: function () { return this.gameClient != null || this.gameServer != null; },
hasMessage: function () { return this.message != null; },
bingoCallMs: function () { return this.bingoCallIntervalSeconds * 1000; },
allowedToCallBingo: function () { return this.bingoAvailable && this.gameClient?.state?.noticedNumbers.length > 4; }
},
methods: {
startGame: function(evt) {
evt.preventDefault();
this.gameServer?.start(this.bingoCallMs);
},
hostGame: async function(evt) {
evt.preventDefault();
const pubSubClient = new PubSubClient(
(message, metadata) => { handleMessagefromAbly(message, metadata, this.gameClient, this.gameServer); },
() => { onHostDisconnected() }
);
this.gameServer = new BingoServer(this.identity, this.gameId, pubSubClient, () => { onHostingRejected(this); });
await this.gameServer.connect();
this.gameClient = new BingoClient(this.identity, this.gameId, pubSubClient);
this.gameClient.soundEnabled = this.soundEnabled;
await this.gameClient.connect();
},
joinGame: async function(evt) {
evt.preventDefault();
const pubSubClient = new PubSubClient(
(message, metadata) => { handleMessagefromAbly(message, metadata, this.gameClient, this.gameServer); },
() => { onClientDisconnected(); }
);
this.gameClient = new BingoClient(this.identity, this.gameId, pubSubClient, () => { onNoHostFound(this); }, () => { onGameAlreadyStartedError(); });
this.gameClient.soundEnabled = this.soundEnabled;
await this.gameClient.connect();
},
numberClicked: function(evt) {
const asInt = parseInt(evt.target.innerHTML);
if (!isNaN(asInt)) {
this.gameClient.recordNumberClicked(asInt);
evt.target.classList.add("marked");
}
},
copyInviteLink: function(evt) {
navigator.clipboard.writeText(this.inviteLink);
},
sayBingo: function(evt) {
if (!this.bingoAvailable) return;
this.gameClient?.sayBingo();
this.bingoAvailable = false;
setTimeout(() => { this.bingoAvailable = true; }, this.bingoDebounce);
}
}
});
function shouldHandleMessage(message, metadata) { return !message.forClientId || (message.forClientId && message.forClientId === metadata.clientId); }
function handleMessagefromAbly(message, metadata, gameClient, gameServer) {
if (shouldHandleMessage(message, metadata)) {
gameServer?.onReceiveMessage(message);
gameClient?.onReceiveMessage(message);
}
}
function onHostingRejected(app) {
window.location = linkGenerator.linkTo({ gameId: app.gameId, invite: true });
}
function onNoHostFound(app) {
window.location = linkGenerator.linkTo({ gameId: app.gameId, host: true, message: "No host found - consider hosting this game!" });
}
function onHostDisconnected() {
window.location = linkGenerator.linkTo({ gameId: app.gameId, host: true, message: "Oh no, you disconnected. You have to restart your game, as your players will have dropped out of sync. This is probably due to a problem with your internet connection." });
}
function onClientDisconnected() {
window.location = linkGenerator.linkTo({ gameId: app.gameId, message: "You disconnected from the game - did your internet connection go down?" });
}
function onGameAlreadyStartedError() {
window.location = linkGenerator.linkTo({ gameId: app.gameId, message: "You're trying to connect to a game that is already in progress. You'll have to wait until they finish the current round." });
}
try {
module.exports = { app };
} catch { }