Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
Indy2222 committed Aug 17, 2023
1 parent 9d7616f commit 7a476b3
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 9 deletions.
1 change: 1 addition & 0 deletions crates/lobby/src/db.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use sqlx::postgres::PgRow;

// TODO fix thise
pub const SQLITE_CONSTRAINT_PRIMARYKEY: &str = "1555";
pub const SQLITE_CONSTRAINT_FOREIGNKEY: &str = "787";

Expand Down
31 changes: 22 additions & 9 deletions crates/lobby/src/games/init.sql
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
CREATE TABLE IF NOT EXISTS games (
name CHARACTER({game_name_len}) NOT NULL PRIMARY KEY,
max_players TINYINT NOT NULL,
max_players SMALLINT NOT NULL,
map_hash CHARACTER({map_hash_len}) NOT NULL,
map_name CHARACTER({map_name_len}) NOT NULL,
server CHARACTER({server_len}) NOT NULL
);

CREATE TABLE IF NOT EXISTS players (
ordinal TINYINT NOT NULL,
ordinal SMALLINT NOT NULL,
author BOOLEAN NOT NULL,
username CHARACTER({username_len}) NOT NULL,
game CHARACTER({game_name_len}) NOT NULL,
Expand All @@ -24,12 +24,25 @@ CREATE TABLE IF NOT EXISTS players (
);


CREATE TRIGGER IF NOT EXISTS check_ordinal
BEFORE INSERT ON players
FOR EACH ROW
DO $$
BEGIN
SELECT CASE
WHEN (SELECT max_players FROM games WHERE name = NEW.game) IS NOT NULL AND NEW.ordinal > (SELECT max_players FROM games WHERE name = NEW.game)
THEN RAISE(FAIL, 'TOO-LARGE-ORDINAL')
END;
IF NOT EXISTS (SELECT 1 FROM pg_triggers WHERE tgname = 'check_ordinal') THEN
CREATE TRIGGER check_ordinal
BEFORE INSERT OR UPDATE ON players
FOR EACH ROW
EXECUTE FUNCTION check_max_players();
END IF;
END $$;

CREATE OR REPLACE FUNCTION check_max_players()
RETURNS TRIGGER
LANGUAGE plpgsql
AS
$$
BEGIN
IF (SELECT max_players FROM games WHERE name = NEW.game) IS NOT NULL AND NEW.ordinal > (SELECT max_players FROM games WHERE name = NEW.game) THEN
RAISE EXCEPTION 'TOO-LARGE-ORDINAL';
END IF;
RETURN NEW;
END;
$$

0 comments on commit 7a476b3

Please sign in to comment.