diff --git a/planetwars-server/src/db/bots.rs b/planetwars-server/src/db/bots.rs index 108c692..964deaa 100644 --- a/planetwars-server/src/db/bots.rs +++ b/planetwars-server/src/db/bots.rs @@ -1,7 +1,7 @@ use diesel::prelude::*; use serde::{Deserialize, Serialize}; -use crate::schema::{bots, code_bundles}; +use crate::schema::{bot_versions, bots}; use chrono; #[derive(Insertable)] @@ -44,38 +44,39 @@ pub fn find_all_bots(conn: &PgConnection) -> QueryResult> { } #[derive(Insertable)] -#[table_name = "code_bundles"] +#[table_name = "bot_versions"] pub struct NewCodeBundle<'a> { pub bot_id: Option, - pub path: &'a str, + pub code_bundle_path: &'a str, } #[derive(Queryable, Serialize, Deserialize, Debug)] pub struct CodeBundle { pub id: i32, pub bot_id: Option, - pub path: String, + pub code_bundle_path: Option, pub created_at: chrono::NaiveDateTime, + pub container_digest: Option, } pub fn create_code_bundle( new_code_bundle: &NewCodeBundle, conn: &PgConnection, ) -> QueryResult { - diesel::insert_into(code_bundles::table) + diesel::insert_into(bot_versions::table) .values(new_code_bundle) .get_result(conn) } pub fn find_bot_code_bundles(bot_id: i32, conn: &PgConnection) -> QueryResult> { - code_bundles::table - .filter(code_bundles::bot_id.eq(bot_id)) + bot_versions::table + .filter(bot_versions::bot_id.eq(bot_id)) .get_results(conn) } pub fn active_code_bundle(bot_id: i32, conn: &PgConnection) -> QueryResult { - code_bundles::table - .filter(code_bundles::bot_id.eq(bot_id)) - .order(code_bundles::created_at.desc()) + bot_versions::table + .filter(bot_versions::bot_id.eq(bot_id)) + .order(bot_versions::created_at.desc()) .first(conn) } diff --git a/planetwars-server/src/db/matches.rs b/planetwars-server/src/db/matches.rs index 54fd113..d9d893c 100644 --- a/planetwars-server/src/db/matches.rs +++ b/planetwars-server/src/db/matches.rs @@ -6,7 +6,7 @@ use diesel::{ }; use diesel::{Connection, GroupedBy, PgConnection, QueryResult}; -use crate::schema::{bots, code_bundles, match_players, matches}; +use crate::schema::{bot_versions, bots, match_players, matches}; use super::bots::{Bot, CodeBundle}; @@ -93,10 +93,10 @@ pub fn list_matches(conn: &PgConnection) -> QueryResult> { let match_players = MatchPlayer::belonging_to(&matches) .left_join( - code_bundles::table - .on(match_players::code_bundle_id.eq(code_bundles::id.nullable())), + bot_versions::table + .on(match_players::code_bundle_id.eq(bot_versions::id.nullable())), ) - .left_join(bots::table.on(code_bundles::bot_id.eq(bots::id.nullable()))) + .left_join(bots::table.on(bot_versions::bot_id.eq(bots::id.nullable()))) .load::(conn)? .grouped_by(&matches); @@ -146,10 +146,10 @@ pub fn find_match(id: i32, conn: &PgConnection) -> QueryResult { let match_players = MatchPlayer::belonging_to(&match_base) .left_join( - code_bundles::table - .on(match_players::code_bundle_id.eq(code_bundles::id.nullable())), + bot_versions::table + .on(match_players::code_bundle_id.eq(bot_versions::id.nullable())), ) - .left_join(bots::table.on(code_bundles::bot_id.eq(bots::id.nullable()))) + .left_join(bots::table.on(bot_versions::bot_id.eq(bots::id.nullable()))) .load::(conn)?; let res = FullMatchData { diff --git a/planetwars-server/src/lib.rs b/planetwars-server/src/lib.rs index 8798945..7076604 100644 --- a/planetwars-server/src/lib.rs +++ b/planetwars-server/src/lib.rs @@ -83,10 +83,7 @@ pub fn api() -> Router { "/bots/:bot_id/upload", post(routes::bots::upload_code_multipart), ) - .route( - "/matches", - get(routes::matches::list_matches), - ) + .route("/matches", get(routes::matches::list_matches)) .route("/matches/:match_id", get(routes::matches::get_match_data)) .route( "/matches/:match_id/log", diff --git a/planetwars-server/src/modules/bots.rs b/planetwars-server/src/modules/bots.rs index 843e48d..ddc1589 100644 --- a/planetwars-server/src/modules/bots.rs +++ b/planetwars-server/src/modules/bots.rs @@ -17,7 +17,7 @@ pub fn save_code_bundle( let new_code_bundle = db::bots::NewCodeBundle { bot_id, - path: &bundle_name, + code_bundle_path: &bundle_name, }; db::bots::create_code_bundle(&new_code_bundle, conn) } diff --git a/planetwars-server/src/modules/matches.rs b/planetwars-server/src/modules/matches.rs index 6d9261d..7d6a1dc 100644 --- a/planetwars-server/src/modules/matches.rs +++ b/planetwars-server/src/modules/matches.rs @@ -98,7 +98,8 @@ impl RunMatch { } pub fn code_bundle_to_botspec(code_bundle: &db::bots::CodeBundle) -> Box { - let bundle_path = PathBuf::from(BOTS_DIR).join(&code_bundle.path); + // TODO: get rid of this unwrap + let bundle_path = PathBuf::from(BOTS_DIR).join(code_bundle.code_bundle_path.as_ref().unwrap()); Box::new(DockerBotSpec { code_path: bundle_path, @@ -126,5 +127,5 @@ async fn run_match_task( db::matches::save_match_result(match_id, result, &conn).expect("could not save match result"); - return outcome; + outcome } diff --git a/planetwars-server/src/modules/ranking.rs b/planetwars-server/src/modules/ranking.rs index 72156ee..b1ad0da 100644 --- a/planetwars-server/src/modules/ranking.rs +++ b/planetwars-server/src/modules/ranking.rs @@ -1,8 +1,8 @@ use crate::{db::bots::Bot, DbPool}; use crate::db; -use diesel::{PgConnection, QueryResult}; use crate::modules::matches::{MatchPlayer, RunMatch}; +use diesel::{PgConnection, QueryResult}; use rand::seq::SliceRandom; use std::collections::HashMap; use std::mem; diff --git a/planetwars-server/src/routes/bots.rs b/planetwars-server/src/routes/bots.rs index df0c4d0..6d5d7df 100644 --- a/planetwars-server/src/routes/bots.rs +++ b/planetwars-server/src/routes/bots.rs @@ -215,7 +215,7 @@ pub async fn upload_code_multipart( let bundle = bots::NewCodeBundle { bot_id: Some(bot.id), - path: &folder_name, + code_bundle_path: &folder_name, }; let code_bundle = bots::create_code_bundle(&bundle, &conn).expect("Failed to create code bundle"); diff --git a/planetwars-server/src/routes/matches.rs b/planetwars-server/src/routes/matches.rs index 5f95ce9..0c1bee4 100644 --- a/planetwars-server/src/routes/matches.rs +++ b/planetwars-server/src/routes/matches.rs @@ -1,7 +1,7 @@ -use std::path::PathBuf; use axum::{extract::Path, Json}; use hyper::StatusCode; use serde::{Deserialize, Serialize}; +use std::path::PathBuf; use crate::{ db::matches::{self, MatchState}, @@ -46,7 +46,6 @@ pub fn match_data_to_api(data: matches::FullMatchData) -> ApiMatch { } } - pub async fn get_match_data( Path(match_id): Path, conn: DatabaseConnection, diff --git a/planetwars-server/src/schema.rs b/planetwars-server/src/schema.rs index 92acc8e..d632a32 100644 --- a/planetwars-server/src/schema.rs +++ b/planetwars-server/src/schema.rs @@ -1,6 +1,19 @@ // This file is autogenerated by diesel #![allow(unused_imports)] +table! { + use diesel::sql_types::*; + use crate::db_types::*; + + bot_versions (id) { + id -> Int4, + bot_id -> Nullable, + code_bundle_path -> Nullable, + created_at -> Timestamp, + container_digest -> Nullable, + } +} + table! { use diesel::sql_types::*; use crate::db_types::*; @@ -12,18 +25,6 @@ table! { } } -table! { - use diesel::sql_types::*; - use crate::db_types::*; - - code_bundles (id) { - id -> Int4, - bot_id -> Nullable, - path -> Text, - created_at -> Timestamp, - } -} - table! { use diesel::sql_types::*; use crate::db_types::*; @@ -81,16 +82,16 @@ table! { } } +joinable!(bot_versions -> bots (bot_id)); joinable!(bots -> users (owner_id)); -joinable!(code_bundles -> bots (bot_id)); -joinable!(match_players -> code_bundles (code_bundle_id)); +joinable!(match_players -> bot_versions (code_bundle_id)); joinable!(match_players -> matches (match_id)); joinable!(ratings -> bots (bot_id)); joinable!(sessions -> users (user_id)); allow_tables_to_appear_in_same_query!( + bot_versions, bots, - code_bundles, match_players, matches, ratings,