Skip to content

Commit

Permalink
Simplify schema (remove arena_types & user_data_types, convert roles …
Browse files Browse the repository at this point in the history
…to enum)
  • Loading branch information
serprex committed Oct 3, 2023
1 parent eb42152 commit ff6dee1
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 44 deletions.
21 changes: 4 additions & 17 deletions scripts/initdb.sql
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
create type pbkdf2algo as enum('SHA1', 'SHA512');
create type userrole as enum('Codesmith', 'Mod');
create table users (
id bigserial not null primary key,
name text not null unique,
Expand All @@ -8,14 +9,10 @@ create table users (
algo pbkdf2algo not null,
wealth int not null default(0)
);
create table user_data_types (
id int not null primary key,
val text not null
);
create table user_data (
id bigserial not null primary key,
user_id bigint not null references users(id),
type_id int not null references user_data_types,
type_id int not null,
name text not null,
data json not null
);
Expand All @@ -25,7 +22,7 @@ create table roles (
);
create table user_role (
user_id bigint not null references users(id),
role_id int not null references roles(id),
role_id userrole not null,
unique (user_id, role_id)
);
create table motd (
Expand All @@ -39,13 +36,9 @@ create table bazaar (
q int not null,
p int not null
);
create table arena_types (
id int not null primary key,
val text not null
);
create table arena (
user_id bigint not null references users(id),
arena_id int not null references arena_types(id),
arena_id int not null,
code int not null,
deck text not null,
day int not null,
Expand Down Expand Up @@ -98,15 +91,9 @@ create table match_request (
unique (game_id, user_id)
);

insert into roles values (1, 'Codesmith'), (2, 'Mod');
insert into arena_types values (1, 'A1'), (2, 'A2');
insert into user_data_types values (1, 'open'), (2, 'orig');

create index ix_users_wealth on users (wealth);
create index ix_users_name on users using hash (name);
create index ix_roles_val on roles using hash (val);
create index ix_arena_types_val on arena_types using hash (val);
create index ix_user_data_types_val on user_data_types using hash (val);
create index ix_arena_score on arena (arena_id, score desc, day desc, "rank");
create index ix_arena_user_id on arena using hash (user_id);
create index ix_bazaar_user_id on bazaar using hash (user_id);
Expand Down
55 changes: 28 additions & 27 deletions src/rs/server/src/handlews.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ use crate::json::{
WsResponse,
};
use crate::starters::{ORIGINAL_STARTERS, STARTERS};
use crate::users::{self, HashAlgo, UserData, UserObject, Users};
use crate::users::{self, HashAlgo, UserData, UserObject, UserRole, Users};
use crate::{get_day, PgPool};

static NEXT_SOCK_ID: AtomicUsize = AtomicUsize::new(0);
Expand Down Expand Up @@ -88,13 +88,14 @@ fn wilson(up: f64, total: f64) -> f64 {
/ (1.0 + Z2 / total)
}

async fn role_check<'a>(
role: &'static str,
tx: &'a WsSender,
client: &'a Client,
userid: i64,
) -> bool {
let ret = if let Ok(row) = client.query_one("select exists(select * from user_role ur join roles r on ur.role_id = r.id where ur.user_id = $1 and r.val = $2) res", &[&userid, &role]).await {
async fn role_check<'a>(role: UserRole, tx: &'a WsSender, client: &'a Client, userid: i64) -> bool {
let ret = if let Ok(row) = client
.query_one(
"select exists(select * from user_role where user_id = $1 and role_id = $2) res",
&[&userid, &role],
)
.await
{
row.get::<usize, bool>(0)
} else {
false
Expand All @@ -112,26 +113,26 @@ async fn role_check<'a>(
}

async fn add_role_handler<'a>(
role: &'static str,
role: UserRole,
tx: &'a WsSender,
client: &'a Client,
userid: i64,
m: &'a str,
) {
if role_check(role, tx, client, userid).await {
client.execute("insert into user_role (user_id, role_id) select u.id, r.id from users u, roles r where u.name = $1 and r.val = $2 on conflict do nothing", &[&m, &role]).await.ok();
client.execute("insert into user_role (user_id, role_id) select u.id, $2 from users u where u.name = $1 on conflict do nothing", &[&m, &role]).await.ok();
}
}

async fn rm_role_handler<'a>(
role: &'static str,
role: UserRole,
tx: &'a WsSender,
client: &'a Client,
userid: i64,
m: &'a str,
) {
if role_check(role, tx, client, userid).await {
client.execute("delete from user_role ur using users u, roles r where ur.user_id = u.id and ur.role_id = r.id and u.name = $1 and r.val = $2", &[&m, &role]).await.ok();
client.execute("delete from user_role ur using users u where ur.user_id = u.id and ur.role_id = $2 and u.name = $1", &[&m, &role]).await.ok();
}
}

Expand Down Expand Up @@ -411,10 +412,10 @@ pub async fn handle_ws(
};
match msg {
AuthMessage::modadd { m } => {
add_role_handler("Mod", &tx, &client, userid, &m).await;
add_role_handler(UserRole::Mod, &tx, &client, userid, &m).await;
}
AuthMessage::modrm { m } => {
rm_role_handler("Mod", &tx, &client, userid, &m).await;
rm_role_handler(UserRole::Mod, &tx, &client, userid, &m).await;
}
AuthMessage::modresetpass { m } => {
if u == "serprex" {
Expand All @@ -426,13 +427,13 @@ pub async fn handle_ws(
}
}
AuthMessage::codesmithadd { m } => {
add_role_handler("Codesmith", &tx, &client, userid, &m).await;
add_role_handler(UserRole::Codesmith, &tx, &client, userid, &m).await;
}
AuthMessage::codesmithrm { m } => {
rm_role_handler("Codesmith", &tx, &client, userid, &m).await;
rm_role_handler(UserRole::Codesmith, &tx, &client, userid, &m).await;
}
AuthMessage::modguest { m } => {
if role_check("Mod", &tx, &client, userid).await {
if role_check(UserRole::Mod, &tx, &client, userid).await {
client
.execute(
if m == "off" {
Expand All @@ -447,17 +448,17 @@ pub async fn handle_ws(
}
}
AuthMessage::modmute { m } => {
if role_check("Mod", &tx, &client, userid).await {
if role_check(UserRole::Mod, &tx, &client, userid).await {
broadcast(&socks, &WsResponse::mute { m: &m }).await;
}
}
AuthMessage::modclear => {
if role_check("Mod", &tx, &client, userid).await {
if role_check(UserRole::Mod, &tx, &client, userid).await {
broadcast(&socks, &WsResponse::clear).await;
}
}
AuthMessage::modmotd { m } => {
if role_check("Mod", &tx, &client, userid).await {
if role_check(UserRole::Mod, &tx, &client, userid).await {
let mbytes = m.as_bytes();
let mut nend = 0;
while nend < mbytes.len()
Expand Down Expand Up @@ -745,7 +746,7 @@ pub async fn handle_ws(
client.execute("insert into stats (user_id, \"set\", stats, players) values ($1, $2, $3, $4)", &[&userid, &set, &Json(stats), &players]).await.ok();
}
AuthMessage::setgold { t, g } => {
if role_check("Codesmith", &tx, &client, userid).await {
if role_check(UserRole::Codesmith, &tx, &client, userid).await {
if let Some(tgt) = users.write().await.load(&*client, &t).await {
let mut tgt = tgt.lock().await;
sendmsg(
Expand All @@ -763,7 +764,7 @@ pub async fn handle_ws(
}
}
AuthMessage::addpool { t, pool, bound } => {
if role_check("Codesmith", &tx, &client, userid).await {
if role_check(UserRole::Codesmith, &tx, &client, userid).await {
if let Some(tgt) = users.write().await.load(&*client, &t).await {
let mut tgt = tgt.lock().await;
let curpool = if bound {
Expand Down Expand Up @@ -800,7 +801,7 @@ pub async fn handle_ws(
},
);
} else {
if role_check("Codesmith", &tx, &client, userid).await {
if role_check(UserRole::Codesmith, &tx, &client, userid).await {
use rand::distributions::Alphanumeric;
let mut codebin = [0u8; 8];
let code = {
Expand Down Expand Up @@ -2395,12 +2396,12 @@ pub async fn handle_ws(
}
}
UserMessage::r#mod | UserMessage::codesmith => {
let xstr = if let UserMessage::codesmith { .. } = msg {
"Codesmith"
let role = if let UserMessage::codesmith { .. } = msg {
UserRole::Codesmith
} else {
"Mod"
UserRole::Mod
};
if let Ok(rows) = client.query("select u.name from user_role ur join users u on u.id = ur.user_id join roles r on r.id = ur.role_id where r.val = $1 order by u.name", &[&xstr]).await {
if let Ok(rows) = client.query("select u.name from user_role ur join users u on u.id = ur.user_id where ur.role_id = $1 order by u.name", &[&role]).await {
let mut msgmsg = String::new();
for (idx, row) in rows.iter().enumerate() {
if idx != 0 {
Expand Down
7 changes: 7 additions & 0 deletions src/rs/server/src/users.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,13 @@ use tokio::sync::Mutex;
use crate::cardpool::Cardpool;
use crate::handlews::{AsyncSocks, AsyncUserSocks};

#[derive(Clone, Copy, Debug, ToSql, FromSql)]
#[postgres(name = "userrole")]
pub enum UserRole {
Codesmith,
Mod,
}

#[derive(Clone, Copy, Debug, ToSql, FromSql)]
#[postgres(name = "pbkdf2algo")]
pub enum HashAlgo {
Expand Down

0 comments on commit ff6dee1

Please sign in to comment.