add match_state to matches

This commit is contained in:
Ilion Beyst 2022-01-03 23:33:00 +01:00
parent 1cde40b459
commit 5b10d5e98e
9 changed files with 56 additions and 6 deletions

View file

@ -10,6 +10,7 @@ tokio = { version = "1.15", features = ["full"] }
hyper = "0.14" hyper = "0.14"
axum = { version = "0.4", features = ["json", "headers", "multipart"] } axum = { version = "0.4", features = ["json", "headers", "multipart"] }
diesel = { version = "1.4.4", features = ["postgres", "chrono"] } diesel = { version = "1.4.4", features = ["postgres", "chrono"] }
diesel-derive-enum = { version = "1.1", features = ["postgres"] }
bb8 = "0.7" bb8 = "0.7"
bb8-diesel = "0.2" bb8-diesel = "0.2"
dotenv = "0.15.0" dotenv = "0.15.0"
@ -28,4 +29,4 @@ planetwars-matchrunner = { path = "../planetwars-matchrunner" }
shlex = "1.1" shlex = "1.1"
[dev-dependencies] [dev-dependencies]
parking_lot = "0.11" parking_lot = "0.11"

View file

@ -3,3 +3,4 @@
[print_schema] [print_schema]
file = "src/schema.rs" file = "src/schema.rs"
import_types = ["diesel::sql_types::*", "crate::db_types::*"]

View file

@ -1,3 +1,4 @@
DROP TABLE match_players; DROP TABLE match_players;
DROP INDEX match_created_at; DROP INDEX match_created_at;
DROP TABLE matches; DROP TABLE matches;
DROP TYPE match_state;

View file

@ -1,5 +1,8 @@
CREATE TYPE match_state AS ENUM ('playing', 'ended');
CREATE TABLE matches ( CREATE TABLE matches (
id SERIAL PRIMARY KEY, id SERIAL PRIMARY KEY NOT NULL,
state match_state NOT NULL,
log_path text NOT NULL, log_path text NOT NULL,
created_at timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP created_at timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP
); );

View file

@ -1,3 +1,4 @@
pub use crate::db_types::MatchState;
use chrono::NaiveDateTime; use chrono::NaiveDateTime;
use diesel::{BelongingToDsl, QueryDsl, RunQueryDsl}; use diesel::{BelongingToDsl, QueryDsl, RunQueryDsl};
use diesel::{Connection, GroupedBy, PgConnection, QueryResult}; use diesel::{Connection, GroupedBy, PgConnection, QueryResult};
@ -7,6 +8,7 @@ use crate::schema::{match_players, matches};
#[derive(Insertable)] #[derive(Insertable)]
#[table_name = "matches"] #[table_name = "matches"]
pub struct NewMatch<'a> { pub struct NewMatch<'a> {
pub state: MatchState,
pub log_path: &'a str, pub log_path: &'a str,
} }
@ -25,6 +27,7 @@ pub struct NewMatchPlayer {
#[table_name = "matches"] #[table_name = "matches"]
pub struct MatchBase { pub struct MatchBase {
pub id: i32, pub id: i32,
pub state: MatchState,
pub log_path: String, pub log_path: String,
pub created_at: NaiveDateTime, pub created_at: NaiveDateTime,
} }

View file

@ -0,0 +1,9 @@
use diesel_derive_enum::DbEnum;
#[derive(DbEnum, Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[DieselType = "Match_state"]
pub enum MatchState {
Playing,
Finished,
}

View file

@ -4,6 +4,7 @@
extern crate diesel; extern crate diesel;
pub mod db; pub mod db;
pub mod db_types;
pub mod routes; pub mod routes;
pub mod schema; pub mod schema;

View file

@ -1,4 +1,4 @@
use std::{io::Read, path::PathBuf}; use std::path::PathBuf;
use axum::{ use axum::{
extract::{Extension, Path}, extract::{Extension, Path},
@ -10,7 +10,11 @@ use rand::{distributions::Alphanumeric, Rng};
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use crate::{ use crate::{
db::{bots, matches, users::User}, db::{
bots,
matches::{self, MatchState},
users::User,
},
ConnectionPool, DatabaseConnection, BOTS_DIR, MAPS_DIR, MATCHES_DIR, ConnectionPool, DatabaseConnection, BOTS_DIR, MAPS_DIR, MATCHES_DIR,
}; };
@ -81,6 +85,7 @@ async fn run_match_task(
pool: ConnectionPool, pool: ConnectionPool,
) { ) {
let match_data = matches::NewMatch { let match_data = matches::NewMatch {
state: MatchState::Finished,
log_path: &log_file_name, log_path: &log_file_name,
}; };

View file

@ -1,4 +1,7 @@
table! { table! {
use diesel::sql_types::*;
use crate::db_types::*;
bots (id) { bots (id) {
id -> Int4, id -> Int4,
owner_id -> Int4, owner_id -> Int4,
@ -7,6 +10,9 @@ table! {
} }
table! { table! {
use diesel::sql_types::*;
use crate::db_types::*;
code_bundles (id) { code_bundles (id) {
id -> Int4, id -> Int4,
bot_id -> Int4, bot_id -> Int4,
@ -16,6 +22,9 @@ table! {
} }
table! { table! {
use diesel::sql_types::*;
use crate::db_types::*;
match_players (match_id, player_id) { match_players (match_id, player_id) {
match_id -> Int4, match_id -> Int4,
bot_id -> Int4, bot_id -> Int4,
@ -24,14 +33,21 @@ table! {
} }
table! { table! {
use diesel::sql_types::*;
use crate::db_types::*;
matches (id) { matches (id) {
id -> Int4, id -> Int4,
state -> Match_state,
log_path -> Text, log_path -> Text,
created_at -> Timestamp, created_at -> Timestamp,
} }
} }
table! { table! {
use diesel::sql_types::*;
use crate::db_types::*;
sessions (id) { sessions (id) {
id -> Int4, id -> Int4,
user_id -> Int4, user_id -> Int4,
@ -40,6 +56,9 @@ table! {
} }
table! { table! {
use diesel::sql_types::*;
use crate::db_types::*;
users (id) { users (id) {
id -> Int4, id -> Int4,
username -> Varchar, username -> Varchar,
@ -54,4 +73,11 @@ joinable!(match_players -> bots (bot_id));
joinable!(match_players -> matches (match_id)); joinable!(match_players -> matches (match_id));
joinable!(sessions -> users (user_id)); joinable!(sessions -> users (user_id));
allow_tables_to_appear_in_same_query!(bots, code_bundles, match_players, matches, sessions, users,); allow_tables_to_appear_in_same_query!(
bots,
code_bundles,
match_players,
matches,
sessions,
users,
);