From 9e068aa062bb07f1f1b8704beffefbcaba260eb7 Mon Sep 17 00:00:00 2001 From: Tor Andersson Date: Fri, 24 Feb 2023 11:59:40 +0100 Subject: Use URL search params instead of funky colon separated arguments. --- public/join.js | 16 ++++++++-------- server.js | 50 ++++++++++++++++++++------------------------------ views/head.pug | 15 ++++++--------- 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 = `${player.name}` + element.innerHTML = `${player.name}` 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 = `${player.name}\u274c` + element.innerHTML = `${player.name}\u274c` else if (game.owner_id === user_id) element.innerHTML = `${player.name}\u274c` else @@ -181,11 +181,11 @@ function update() { else message.innerHTML = "Waiting for players to join..." } else if (game.status === 1) { - message.innerHTML = `Observe` + message.innerHTML = `Observe` } else if (game.status === 2) { - message.innerHTML = `Review` + message.innerHTML = `Review` } else { - message.innerHTML = `Enter` + message.innerHTML = `Enter` } if (game.owner_id === user_id) { diff --git a/server.js b/server.js index 1bf38fe..6ca6692 100644 --- a/server.js +++ b/server.js @@ -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 - | #{item.game_id} – - | #{item.title_name} - | (#{item.scenario}) + | #{item.game_id} – #{item.title_name} (#{item.scenario}) else div - | #{item.game_id} – - | #{item.title_name} + | #{item.game_id} – #{item.title_name} 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: #{game.owner_name} (private) else - | Owner: #{game.owner_name} + | Owner: #{game.owner_name} div Scenario: #{game.scenario} div Options: #{game.human_options} if game.status === 2 -- cgit v1.2.3