diff options
author | Ilion Beyst <ilion.beyst@gmail.com> | 2022-11-22 20:42:34 +0100 |
---|---|---|
committer | Ilion Beyst <ilion.beyst@gmail.com> | 2022-11-22 20:42:34 +0100 |
commit | 1eb81092d7a3e7925ae8e4258aa35a009f911b6e (patch) | |
tree | 48846d3ecd8741e9d7e9748a8557d89068f32c11 | |
parent | b75c0e15dcd93794f7822ff5dfbfda0fe74f7c2a (diff) | |
download | planetwars.dev-1eb81092d7a3e7925ae8e4258aa35a009f911b6e.tar.xz planetwars.dev-1eb81092d7a3e7925ae8e4258aa35a009f911b6e.zip |
add is_ranked attribute to matches
6 files changed, 16 insertions, 3 deletions
diff --git a/planetwars-server/migrations/2022-11-21-135622_add_is_ranked_to_maps/down.sql b/planetwars-server/migrations/2022-11-21-135622_add_is_ranked_to_maps/down.sql new file mode 100644 index 0000000..1165b38 --- /dev/null +++ b/planetwars-server/migrations/2022-11-21-135622_add_is_ranked_to_maps/down.sql @@ -0,0 +1 @@ +ALTER TABLE maps DROP COLUMN is_ranked; diff --git a/planetwars-server/migrations/2022-11-21-135622_add_is_ranked_to_maps/up.sql b/planetwars-server/migrations/2022-11-21-135622_add_is_ranked_to_maps/up.sql new file mode 100644 index 0000000..88abfac --- /dev/null +++ b/planetwars-server/migrations/2022-11-21-135622_add_is_ranked_to_maps/up.sql @@ -0,0 +1 @@ +ALTER TABLE maps ADD COLUMN is_ranked BOOLEAN NOT NULL DEFAULT FALSE; diff --git a/planetwars-server/src/db/maps.rs b/planetwars-server/src/db/maps.rs index 8972461..4f0e026 100644 --- a/planetwars-server/src/db/maps.rs +++ b/planetwars-server/src/db/maps.rs @@ -14,6 +14,7 @@ pub struct Map { pub id: i32, pub name: String, pub file_path: String, + pub is_ranked: bool, } pub fn create_map(new_map: NewMap, conn: &mut PgConnection) -> QueryResult<Map> { @@ -33,3 +34,9 @@ pub fn find_map_by_name(name: &str, conn: &mut PgConnection) -> QueryResult<Map> pub fn list_maps(conn: &mut PgConnection) -> QueryResult<Vec<Map>> { maps::table.get_results(conn) } + +pub fn get_ranked_maps(conn: &mut PgConnection) -> QueryResult<Vec<Map>> { + maps::table + .filter(maps::is_ranked.eq(true)) + .get_results(conn) +} diff --git a/planetwars-server/src/db/matches.rs b/planetwars-server/src/db/matches.rs index 2aff44b..7af5b4d 100644 --- a/planetwars-server/src/db/matches.rs +++ b/planetwars-server/src/db/matches.rs @@ -140,12 +140,15 @@ fn fetch_full_match_data( } // TODO: this method should disappear -pub fn list_matches(amount: i64, conn: &mut PgConnection) -> QueryResult<Vec<FullMatchData>> { +pub fn fetch_ranked_maps(amount: i64, conn: &mut PgConnection) -> QueryResult<Vec<FullMatchData>> { conn.transaction(|conn| { let matches = matches::table + .inner_join(maps::table) .filter(matches::state.eq(MatchState::Finished)) + .filter(maps::is_ranked.eq(true)) .order_by(matches::created_at.desc()) .limit(amount) + .select(matches::all_columns) .get_results::<MatchBase>(conn)?; fetch_full_match_data(matches, conn) diff --git a/planetwars-server/src/modules/ranking.rs b/planetwars-server/src/modules/ranking.rs index aa4060c..b0ebad8 100644 --- a/planetwars-server/src/modules/ranking.rs +++ b/planetwars-server/src/modules/ranking.rs @@ -38,7 +38,7 @@ pub async fn run_ranker(config: Arc<GlobalConfig>, db_pool: DbPool) { .cloned() .collect(); - let maps = db::maps::list_maps(&mut db_conn).expect("could not load map"); + let maps = db::maps::get_ranked_maps(&mut db_conn).expect("could not load map"); let map = match maps.choose(&mut rand::thread_rng()).cloned() { None => continue, // no maps available Some(map) => map, @@ -93,7 +93,7 @@ struct MatchStats { } fn fetch_match_stats(db_conn: &mut PgConnection) -> QueryResult<HashMap<(i32, i32), MatchStats>> { - let matches = db::matches::list_matches(RANKER_NUM_MATCHES, db_conn)?; + let matches = db::matches::fetch_ranked_maps(RANKER_NUM_MATCHES, db_conn)?; let mut match_stats = HashMap::<(i32, i32), MatchStats>::new(); for m in matches { diff --git a/planetwars-server/src/schema.rs b/planetwars-server/src/schema.rs index 2c5ab2a..803b78f 100644 --- a/planetwars-server/src/schema.rs +++ b/planetwars-server/src/schema.rs @@ -42,6 +42,7 @@ diesel::table! { id -> Int4, name -> Text, file_path -> Text, + is_ranked -> Bool, } } |