From 6c5df51ed4d6cd0adbab53df277102738fccc70d Mon Sep 17 00:00:00 2001 From: Tor Andersson Date: Thu, 5 Oct 2023 15:05:30 +0200 Subject: Add game "setups" for automated match making and tournaments. --- public/join.js | 15 +++++++++++++++ schema.sql | 11 +++++++++++ server.js | 22 ++++++++++++++++++---- 3 files changed, 44 insertions(+), 4 deletions(-) diff --git a/public/join.js b/public/join.js index a22e0fa..2774241 100644 --- a/public/join.js +++ b/public/join.js @@ -156,6 +156,21 @@ function update() { 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", is_active(player, role)) + 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 = `Empty` + } + continue + } + if (player) { element.classList.remove("is_invite") switch (game.status) { diff --git a/schema.sql b/schema.sql index 04c613b..4c64ecd 100644 --- a/schema.sql +++ b/schema.sql @@ -198,6 +198,16 @@ create view player_rating_view as left join ratings using(title_id, user_id) ; +create table if not exists setups ( + setup_id integer primary key, + setup_name text, + title_id text, + player_count integer, + scenario text, + options text, + unique (title_id, player_count, scenario) +); + -- Friend and Block Lists -- create table if not exists contacts ( @@ -356,6 +366,7 @@ create table if not exists games ( pace integer default 0, is_private boolean default 0, is_random boolean default 0, + is_match boolean default 0, ctime datetime default current_timestamp, mtime datetime default current_timestamp, diff --git a/server.js b/server.js index 9eb426c..0fcaf43 100644 --- a/server.js +++ b/server.js @@ -1051,6 +1051,8 @@ let RULES = {} let TITLE_TABLE = app.locals.TITLE_TABLE = {} let TITLE_LIST = app.locals.TITLE_LIST = [] let TITLE_NAME = app.locals.TITLE_NAME = {} +let SETUP_LIST = app.locals.SETUP_LIST = [] +let SETUP_TABLE = app.locals.SETUP_TABLE = {} const STATUS_OPEN = 0 const STATUS_ACTIVE = 1 @@ -1065,7 +1067,8 @@ const PACE_SLOW = 3 const PACE_NAME = [ "Any", "Live", "Fast", "Slow" ] function load_rules() { - const SQL_SELECT_TITLES = SQL("SELECT * FROM titles") + const SQL_SELECT_TITLES = SQL("select * from titles") + const SQL_SELECT_SETUPS = SQL("select * from setups where title_id=? order by setup_id") for (let title of SQL_SELECT_TITLES.all()) { let title_id = title.title_id if (fs.existsSync(__dirname + "/public/" + title_id + "/rules.js")) { @@ -1075,6 +1078,17 @@ function load_rules() { TITLE_LIST.push(title) TITLE_TABLE[title_id] = title TITLE_NAME[title_id] = title.title_name + title.setups = SQL_SELECT_SETUPS.all(title_id) + for (let setup of title.setups) { + if (!setup.setup_name) { + if (title.setups.length > 1 && setup.scenario !== "Standard") + setup.setup_name = title.title_name + " - " + setup.scenario + else + setup.setup_name = title.title_name + } + SETUP_LIST.push(setup) + SETUP_TABLE[setup.setup_id] = setup + } title.about_html = fs.readFileSync("./public/" + title_id + "/about.html") title.create_html = fs.readFileSync("./public/" + title_id + "/create.html") } catch (err) { @@ -1139,7 +1153,7 @@ function is_game_ready(player_count, players) { load_rules() -const SQL_INSERT_GAME = SQL("INSERT INTO games (owner_id,title_id,scenario,options,player_count,pace,is_private,is_random,notice) VALUES (?,?,?,?,?,?,?,?,?)") +const SQL_INSERT_GAME = SQL("INSERT INTO games (owner_id,title_id,scenario,options,player_count,pace,is_private,is_random,notice,is_match) VALUES (?,?,?,?,?,?,?,?,?,?) returning game_id").pluck() const SQL_DELETE_GAME = SQL("DELETE FROM games WHERE game_id=? AND owner_id=?") const SQL_START_GAME = SQL(` @@ -1607,8 +1621,8 @@ app.post("/create/:title_id", must_be_logged_in, function (req, res) { let player_count = get_game_roles(title_id, scenario, parse_game_options(options)).length - let info = SQL_INSERT_GAME.run(user_id, title_id, scenario, options, player_count, pace, priv, rand, notice) - res.redirect("/join/" + info.lastInsertRowid) + let game_id = SQL_INSERT_GAME.get(user_id, title_id, scenario, options, player_count, pace, priv, rand, notice, 0) + res.redirect("/join/" + game_id) }) app.get('/delete/:game_id', must_be_logged_in, function (req, res) { -- cgit v1.2.3