summaryrefslogtreecommitdiff
path: root/public/join.js
blob: 78d9137ab2421c0cc0c666f3ce7a36ae775985f6 (plain)
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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
"use strict"

let start_status = game.status
let evtsrc = null
let timer = 0
let invite_role = null

function confirm_delete() {
	let warning = `Are you sure you want to DELETE this game?`
	if (window.confirm(warning))
		window.location.href = "/delete/" + game.game_id
}

function post(url) {
	fetch(url, { method: "POST" })
		.then(r => r.text())
		.then(t => window.error.textContent = (t === "SUCCESS") ? "" : t)
		.catch(e => window.error.textContent = e)
	start_event_source()
}

function start() {
	post(`/start/${game.game_id}`)
}

function join(role) {
	post(`/join/${game.game_id}/${encodeURIComponent(role)}`)
}

function part(role) {
	let warning = "Are you sure you want to LEAVE this game?"
	if (game.status === 0 || window.confirm(warning))
		post(`/part/${game.game_id}/${encodeURIComponent(role)}`)
}

function kick(role) {
	let player = players.find(p => p.role === role)
	let warning = `Are you sure you want to KICK player ${player.name} (${role}) from this game?`
	if (game.status === 0 || window.confirm(warning))
		post(`/part/${game.game_id}/${encodeURIComponent(role)}`)
}

function accept(role) {
	post(`/accept/${game.game_id}/${encodeURIComponent(role)}`)
}

function send_invite() {
	let invite_user = document.getElementById("invite_user").value
	if (invite_user) {
		post(`/invite/${game.game_id}/${encodeURIComponent(invite_role)}/${encodeURIComponent(invite_user)}`)
		document.getElementById("invite").close()
	}
}

function show_invite(role) {
	invite_role = role
	document.getElementById("invite").showModal()
}

function hide_invite() {
	document.getElementById("invite").close()
}

let blink_title = document.title
let blink_timer = 0

function start_blinker(message) {
	let tick = false
	if (blink_timer)
		stop_blinker()
	if (!document.hasFocus()) {
		document.title = message
		blink_timer = setInterval(function () {
			document.title = tick ? message : blink_title
			tick = !tick
		}, 1000)
	}
}

function stop_blinker() {
	document.title = blink_title
	clearInterval(blink_timer)
	blink_timer = 0
}

window.addEventListener("focus", stop_blinker)

function start_event_source() {
	if (!evtsrc || evtsrc.readyState === 2) {
		console.log("STARTING EVENT SOURCE")
		evtsrc = new EventSource("/join-events/" + game.game_id)
		evtsrc.addEventListener("players", function (evt) {
			console.log("PLAYERS:", evt.data)
			players = JSON.parse(evt.data)
			update()
		})
		evtsrc.addEventListener("ready", function (evt) {
			console.log("READY:", evt.data)
			ready = JSON.parse(evt.data)
			update()
		})
		evtsrc.addEventListener("game", function (evt) {
			console.log("GAME:", evt.data)
			game = JSON.parse(evt.data)
			if (game.status > 1) {
				console.log("CLOSED EVENT SOURCE")
				clearInterval(timer)
				evtsrc.close()
			}
			update()
		})
		evtsrc.addEventListener("deleted", function (evt) {
			console.log("DELETED")
			window.location.href = '/' + game.title_id
		})
		evtsrc.onerror = function (err) {
			window.message.innerHTML = "Disconnected from server..."
		}
		window.addEventListener('beforeunload', function (evt) {
			evtsrc.close()
		})
	}
}

function is_friend(p) {
	return whitelist && whitelist.includes(p.user_id)
}

function is_enemy(p) {
	return blacklist && blacklist.includes(p.user_id)
}

function user_link(player) {
	return `<a class="black" href="/user/${player.name}">${player.name}</a>`
}

function play_link(player) {
	return `\xbb <a href="/${game.title_id}/play.html?game=${game.game_id}&role=${encodeURIComponent(player.role)}">${player.name}</a>`
}

function action_link(player, action, color, text) {
	return `<a class="${color}" href="javascript:${action}('${player.role}')">${text}</a>`
}

function update() {
	update_common()
	if (user_id)
		update_login()
	else
		update_no_login()
}

function update_common() {
	if (game.scenario !== "Standard")
		document.querySelector("h1").textContent = game.title_name + " - " + game.scenario
	else
		document.querySelector("h1").textContent = game.title_name

	let message = window.message
	if (game.status === 0) {
		if (ready)
			message.innerHTML = "Waiting to start..."
		else
			message.innerHTML = "Waiting for players to join..."
	} else if (game.status === 1) {
		message.innerHTML = `<a href="/${game.title_id}/play.html?game=${game.game_id}">Observe</a>`
	} else if (game.status === 2) {
		message.innerHTML = `<a href="/${game.title_id}/play.html?game=${game.game_id}">Review</a>`
	} else if (game.status === 3) {
		message.innerHTML = "Archived"
	}
}

function update_no_login() {
	for (let i = 0; i < roles.length; ++i) {
		let role = roles[i]
		let role_id = "role_" + role.replace(/ /g, "_")
		if (game.is_random && game.status === 0)
			role = "Random " + (i+1)
		document.getElementById(role_id + "_name").textContent = role
		let player = players.find(p => p.role === role)
		let element = document.getElementById(role_id)

		if (game.is_match) {
			if (player) {
				if (game.status === 1)
					element.classList.toggle("is_active", player.is_active)
				element.innerHTML = user_link(player)
			} else {
				element.innerHTML = `<i>Empty</i>`
			}
			continue
		}

		if (player) {
			element.classList.remove("is_invite")
			switch (game.status) {
			case 3:
				element.innerHTML = player.name
				break
			case 2:
				element.innerHTML = user_link(player)
				break
			case 1:
				if (player.is_invite) {
					element.classList.add("is_invite")
					element.innerHTML = user_link(player) + " ?"
				} else {
					element.classList.toggle("is_active", player.is_active)
					element.innerHTML = user_link(player)
				}
				break
			case 0:
				if (player.is_invite) {
					element.classList.add("is_invite")
					element.innerHTML = user_link(player) + " ?"
				} else {
					element.innerHTML = user_link(player)
				}
				break
			}
		} else {
			element.classList.remove("is_invite")
			element.innerHTML = `<i>Empty</i>`
		}
	}
}

function update_login() {
	for (let i = 0; i < roles.length; ++i) {
		let role = roles[i]
		let role_id = "role_" + role.replace(/ /g, "_")
		if (game.is_random && game.status === 0)
			role = "Random " + (i+1)
		document.getElementById(role_id + "_name").textContent = role
		let player = players.find(p => p.role === role)
		let element = document.getElementById(role_id)

		if (game.is_match) {
			if (player) {
				if (game.status === 1)
					element.classList.toggle("is_active", player.is_active)
				if (player.user_id === user_id && (game.status === 1 || game.status === 2))
					element.innerHTML = play_link(player)
				else
					element.innerHTML = user_link(player)
			} else {
				element.innerHTML = `<i>Empty</i>`
			}
			continue
		}

		if (player) {
			element.classList.remove("is_invite")
			switch (game.status) {
			case 3:
				element.innerHTML = player.name
				break
			case 2:
				if (player.user_id === user_id)
					element.innerHTML = play_link(player)
				else
					element.innerHTML = user_link(player)
				break
			case 1:
				if (player.is_invite) {
					element.classList.add("is_invite")
					if (player.user_id === user_id)
						element.innerHTML = player.name + " ?" +
							action_link(player, "part", "red", "\u274c") +
							action_link(player, "accept", "green", "\u2714")
					else if (game.owner_id === user_id)
						element.innerHTML = user_link(player) + " ?" + action_link(player, "kick", "red", "\u274c")
					else
						element.innerHTML = user_link(player) + " ?"
				} else {
					element.classList.toggle("is_active", player.is_active)
					if (player.user_id === user_id)
						element.innerHTML = play_link(player) + action_link(player, "part", "red", "\u274c")
					else if (game.owner_id === user_id)
						element.innerHTML = user_link(player) + action_link(player, "kick", "red", "\u274c")
					else
						element.innerHTML = user_link(player)
				}
				break
			case 0:
				if (player.is_invite) {
					element.classList.add("is_invite")
					if (player.user_id === user_id)
						element.innerHTML = player.name + " ?" +
							action_link(player, "part", "red", "\u274c") +
							action_link(player, "accept", "green", "\u2714")
					else if (game.owner_id === user_id)
						element.innerHTML = user_link(player) + " ?" + action_link(player, "kick", "red", "\u274c")
					else
						element.innerHTML = user_link(player) + " ?"
				} else {
					if (player.user_id === user_id)
						element.innerHTML = player.name + action_link(player, "part", "red", "\u274c")
					else if (game.owner_id === user_id)
						element.innerHTML = user_link(player) + action_link(player, "kick", "red", "\u274c")
					else
						element.innerHTML = user_link(player)
				}
				break
			}
			element.classList.toggle("friend", is_friend(player))
			element.classList.toggle("enemy", is_enemy(player))
			if (is_enemy(player))
				element.title = "You have blacklisted this user!"
			else
				element.title = ""
		} else {
			element.classList.remove("is_invite")
			switch (game.status) {
			case 2:
				element.innerHTML = `<i>Empty</i>`
				break
			case 1:
			case 0:
				if (limit)
					element.innerHTML = `<i>Empty</i>`
				else if (game.owner_id === user_id)
					element.innerHTML = `\xbb <a class="join" href="javascript:join('${role}')">Join</a><a class="green" href="javascript:show_invite('${role}')">\u{2795}</a>`
				else
					element.innerHTML = `\xbb <a class="join" href="javascript:join('${role}')">Join</a>`
				break
			}
			element.classList.remove("friend")
			element.classList.remove("enemy")
			element.title = ""
		}
	}

	if (game.owner_id === user_id) {
		window.start_button.disabled = !ready
		window.start_button.classList = (game.status === 0) ? "" : "hide"
		window.delete_button.classList = (game.status === 0 || game.user_count <= 1) ? "" : "hide"
		if (game.status === 0 && ready)
			start_blinker("READY TO START")
		else
			stop_blinker()
	} else {
		if (start_status === 0 && game.status === 1)
			start_blinker("STARTED")
		else
			stop_blinker()
	}
}

window.onload = function () {
	update()
	if (user_id && game.status <= 1 && !game.is_match) {
		start_event_source()
		timer = setInterval(start_event_source, 15000)
	}
}