diff options
author | Tor Andersson <tor@ccxvii.net> | 2023-02-24 11:59:40 +0100 |
---|---|---|
committer | Tor Andersson <tor@ccxvii.net> | 2023-02-24 13:39:07 +0100 |
commit | 9e068aa062bb07f1f1b8704beffefbcaba260eb7 (patch) | |
tree | 38a39f22bd44a068b2f6e048367d0b0b1a250e1b | |
parent | d097e2db5862177d1c30cbf7df0e8143e67d901d (diff) | |
download | server-9e068aa062bb07f1f1b8704beffefbcaba260eb7.tar.gz |
Use URL search params instead of funky colon separated arguments.
-rw-r--r-- | public/join.js | 16 | ||||
-rw-r--r-- | server.js | 50 | ||||
-rw-r--r-- | views/head.pug | 15 | ||||
-rw-r--r-- | views/join.pug | 4 |
4 files changed, 36 insertions, 49 deletions
diff --git a/public/join.js b/public/join.js index d140230..5bb8b9c 100644 --- a/public/join.js +++ b/public/join.js @@ -23,20 +23,20 @@ function start() { } function join(role) { - post(`/join/${game.game_id}/${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}/${role}`) + 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}/${role}`) + post(`/part/${game.game_id}/${encodeURIComponent(role)}`) } let blink_title = document.title @@ -128,14 +128,14 @@ function update() { switch (game.status) { case 2: if (player.user_id === user_id) - element.innerHTML = `<a href="/${game.title_id}/play:${game.game_id}:${role}">${player.name}</a>` + element.innerHTML = `<a href="/${game.title_id}/play.html?game=${game.game_id}&role=${encodeURIComponent(role)}">${player.name}</a>` else element.innerHTML = player.name break case 1: element.classList.toggle("is_active", is_active(player, role)) if (player.user_id === user_id) - element.innerHTML = `<a href="/${game.title_id}/play:${game.game_id}:${role}">${player.name}</a><a class="red" href="javascript:part('${role}')">\u274c</a>` + element.innerHTML = `<a href="/${game.title_id}/play.html?game=${game.game_id}&role=${encodeURIComponent(role)}">${player.name}</a><a class="red" href="javascript:part('${role}')">\u274c</a>` else if (game.owner_id === user_id) element.innerHTML = `${player.name}<a class="red" href="javascript:kick('${role}')">\u274c</a>` else @@ -181,11 +181,11 @@ function update() { else message.innerHTML = "Waiting for players to join..." } else if (game.status === 1) { - message.innerHTML = `<a href="/${game.title_id}/play:${game.game_id}">Observe</a>` + 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:${game.game_id}">Review</a>` + message.innerHTML = `<a href="/${game.title_id}/play.html?game=${game.game_id}">Review</a>` } else { - message.innerHTML = `<a href="/${game.title_id}/play:${game.game_id}">Enter</a>` + message.innerHTML = `<a href="/${game.title_id}/play.html?game=${game.game_id}">Enter</a>` } if (game.owner_id === user_id) { @@ -166,6 +166,17 @@ http_server.listen(HTTP_PORT, HTTP_HOST, () => console.log(`Listening to HTTP on * MISC FUNCTIONS */ +function play_url(title_id, game_id, role, mode) { + if (mode && mode) + return `/${title_id}/play.html?mode=${mode}&game=${game_id}&role=${encodeURIComponent(role)}` + else if (mode) + return `/${title_id}/play.html?mode=${mode}&game=${game_id}` + else if (role) + return `/${title_id}/play.html?game=${game_id}&role=${encodeURIComponent(role)}` + else + return `/${title_id}/play.html?mode=${mode}` +} + function random_seed() { return crypto.randomInt(1, 2**35-31) } @@ -1265,7 +1276,7 @@ function sort_your_turn(a, b) { app.get('/games/next', must_be_logged_in, function (req, res) { let next = QUERY_NEXT_GAME_OF_USER.get(req.user.user_id) if (next !== undefined) - res.redirect(`/${next.title_id}/play:${next.game_id}:${next.role}`) + res.redirect(play_url(next.title_id, next.game_id, next.role)) else res.redirect(`/games/active`) }) @@ -1613,7 +1624,7 @@ app.get('/play/:game_id/:role', function (req, res) { let title = SQL_SELECT_GAME_TITLE.get(game_id) if (!title) return res.status(404).send("Invalid game ID.") - res.redirect('/'+title+'/play:'+game_id+':'+role) + res.redirect(play_url(title, game_id, role)) }) app.get('/play/:game_id', function (req, res) { @@ -1624,18 +1635,9 @@ app.get('/play/:game_id', function (req, res) { return res.status(404).send("Invalid game ID.") let role = SQL_SELECT_PLAYER_ROLE.get(game_id, user_id) if (role) - res.redirect('/'+title+'/play:'+game_id+':'+role) + res.redirect(play_url(title, game_id, role)) else - res.redirect('/'+title+'/play:'+game_id) -}) - -app.get('/debug/:game_id', function (req, res) { - let game_id = req.params.game_id | 0 - let user_id = req.user ? req.user.user_id : 0 - let title = SQL_SELECT_GAME_TITLE.get(game_id) - if (!title) - return res.status(404).send("Invalid game ID.") - res.redirect('/'+title+'/debug:'+game_id) + res.redirect(play_url(title, game_id, "Observer")) }) app.get('/:title_id/play\::game_id\::role', must_be_logged_in, function (req, res) { @@ -1643,31 +1645,19 @@ app.get('/:title_id/play\::game_id\::role', must_be_logged_in, function (req, re let title_id = req.params.title_id let game_id = req.params.game_id let role = req.params.role - if (!SQL_AUTHORIZE_GAME_ROLE.get(title_id, game_id, role, user_id)) - return res.status(404).send("Invalid game ID.") - return res.sendFile(__dirname + '/public/' + title_id + '/play.html') + return res.redirect(play_url(title_id, game_id, role)) }) app.get('/:title_id/play\::game_id', function (req, res) { let title_id = req.params.title_id let game_id = req.params.game_id - let a_title = SQL_SELECT_GAME_TITLE.get(game_id) - if (a_title !== title_id) - return res.status(404).send("Invalid game ID.") - return res.sendFile(__dirname + '/public/' + title_id + '/play.html') + return res.redirect(play_url(title_id, game_id, "Observer")) }) app.get('/:title_id/replay\::game_id', function (req, res) { let title_id = req.params.title_id let game_id = req.params.game_id - let game = SQL_SELECT_GAME.get(game_id) - if (!game) - return res.status(404).send("Invalid game ID.") - if (game.title_id !== title_id) - return res.status(404).send("Invalid game ID.") - if (game.status < 2) - return res.status(404).send("Invalid game ID.") - return res.sendFile(__dirname + '/public/' + title_id + '/play.html') + return res.redirect(play_url(title_id, game_id, "Observer", "replay")) }) app.get('/api/replay/:game_id', function (req, res) { @@ -1729,8 +1719,8 @@ function send_webhook(user_id, webhook, message) { function webhook_game_link(game, user) { if (user.role) - return `${SITE_URL}/${game.title_id}/play:${game.game_id}:${encodeURI(user.role)}` - return `${SITE_URL}/join/${game.game_id}` + return SITE_URL + play_url(game.title_id, game.game_id, user.role) + return SITE_URL + "/join/" + game.game_id } function webhook_ready_to_start(user, game_id) { diff --git a/views/head.pug b/views/head.pug index 6ea9285..2cb50a0 100644 --- a/views/head.pug +++ b/views/head.pug @@ -55,13 +55,10 @@ mixin gamelist(list,hide_title=0) div.game_head if item.scenario.length <= 2 div - | <a href="/join/#{item.game_id}">#{item.game_id}</a> – - | <a href="/#{item.title_id}">#{item.title_name}</a> - | (#{item.scenario}) + | <a href="/join/#{item.game_id}">#{item.game_id} – #{item.title_name} (#{item.scenario})</a> else div - | <a href="/join/#{item.game_id}">#{item.game_id}</a> – - | <a href="/#{item.title_id}">#{item.title_name}</a> + | <a href="/join/#{item.game_id}">#{item.game_id} – #{item.title_name}</a> case item.status when 0 @@ -72,19 +69,19 @@ mixin gamelist(list,hide_title=0) when 1 if item.is_yours if item.your_role - a(class="command" href=`/${item.title_id}/play:${item.game_id}:${item.your_role}`) Play + a(class="command" href=`/${item.title_id}/play.html?game=${item.game_id}&role=${encodeURIComponent(item.your_role)}`) Play else a(class="command" href="/join/"+item.game_id) Play else - a(class="command" href=`/${item.title_id}/play:${item.game_id}`) View + a(class="command" href=`/${item.title_id}/play.html?game=${item.game_id}&role=Observer`) View when 2 if item.is_yours if item.your_role - a(class="command" href=`/${item.title_id}/play:${item.game_id}:${item.your_role}`) Review + a(class="command" href=`/${item.title_id}/play.html?game=${item.game_id}&role=${encodeURIComponent(item.your_role)}`) Review else a(class="command" href="/join/"+item.game_id) Review else - a(class="command" href=`/${item.title_id}/play:${item.game_id}`) Review + a(class="command" href=`/${item.title_id}/play.html?game=${item.game_id}&role=Observer`) Review div.game_main div.game_info diff --git a/views/join.pug b/views/join.pug index ab53ddc..90f0db7 100644 --- a/views/join.pug +++ b/views/join.pug @@ -38,9 +38,9 @@ html div if game.is_private - | Owner: #{game.owner_name} (private) + | Owner: <a href="/user/#{game.owner_name}">#{game.owner_name}</a> (private) else - | Owner: #{game.owner_name} + | Owner: <a href="/user/#{game.owner_name}">#{game.owner_name}</a> div Scenario: #{game.scenario} div Options: #{game.human_options} if game.status === 2 |