Skip to content

Commit

Permalink
fixing bug in pidgtm compile (#54)
Browse files Browse the repository at this point in the history
* fixing bug in pidgtm compile

Signed-off-by: danbugs <[email protected]>

* improve

Signed-off-by: danbugs <[email protected]>

---------

Signed-off-by: danbugs <[email protected]>

fixing bug in pidgtm compile (#55)

* fixing bug in pidgtm compile

Signed-off-by: danbugs <[email protected]>

* fix && fmt

Signed-off-by: danbugs <[email protected]>

---------

Signed-off-by: danbugs <[email protected]>

fix pidgtm compile bug

Signed-off-by: danbugs <[email protected]>
  • Loading branch information
danbugs committed Dec 28, 2023
1 parent 8d6d161 commit 7020c84
Show file tree
Hide file tree
Showing 15 changed files with 109 additions and 80 deletions.
25 changes: 1 addition & 24 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions database/migrations/2023-12-27-224952_migration4/down.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
-- Revert primary key change in player_games
ALTER TABLE player_games
DROP CONSTRAINT player_games_pkey;

ALTER TABLE player_games
ADD PRIMARY KEY (game_id, requester_id);

-- Remove the set_id column from player_games
ALTER TABLE player_games
DROP COLUMN set_id;

-- Add the game_ids column back to player_sets
ALTER TABLE player_sets
ADD COLUMN game_ids INTEGER ARRAY;
14 changes: 14 additions & 0 deletions database/migrations/2023-12-27-224952_migration4/up.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
-- Drop the game_ids column from player_sets
ALTER TABLE player_sets
DROP COLUMN game_ids;

-- Add the set_id column to player_games
ALTER TABLE player_games
ADD COLUMN set_id INTEGER;

-- Update the primary key of player_games
ALTER TABLE player_games
DROP CONSTRAINT player_games_pkey;

ALTER TABLE player_games
ADD PRIMARY KEY (game_id, requester_id, set_id);
4 changes: 4 additions & 0 deletions database/src/db_models/game.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,11 @@ pub struct Game {
requester_char_played: Option<String>,
opponent_char_played: Option<String>,
stage: Option<String>,
set_id: i32,
}

impl Game {
#[allow(clippy::too_many_arguments)]
pub fn new(
gid: i32,
rid: i32,
Expand All @@ -25,6 +27,7 @@ impl Game {
rcp_num_o: Option<i32>,
ocp_num_o: Option<i32>,
s: Option<String>,
sid: i32,
) -> Self {
Self {
game_id: gid,
Expand All @@ -34,6 +37,7 @@ impl Game {
requester_char_played: rcp_num_o.map(get_character_from_id),
opponent_char_played: ocp_num_o.map(get_character_from_id),
stage: s,
set_id: sid,
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions database/src/db_models/player.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ pub struct Player {
pub gender_pronouns: Option<String>,
pub birthday: Option<String>,
pub bio: Option<String>,
pub rankings: Option<Vec<String>>,
pub rankings: Option<Vec<Option<String>>>,
}

impl From<startgg::Player> for Player {
Expand Down Expand Up @@ -70,8 +70,8 @@ impl From<startgg::Player> for Player {
bio: u.bio,
rankings: p.rankings.map(|r| {
r.iter()
.map(|pr| format!("#{} @ {}", pr.rank, pr.title))
.collect::<Vec<String>>()
.map(|pr| Some(format!("#{} @ {}", pr.rank, pr.title)))
.collect::<Vec<Option<String>>>()
}),
}
}
Expand Down
6 changes: 0 additions & 6 deletions database/src/db_models/set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ use serde::Serialize;
// — an auto fix for this exists only in Diesel v2.
use crate::schema::player_sets;

use crate::db_models::game::Game;

#[derive(Debug, Serialize, Insertable, Queryable, QueryableByName)]
#[table_name = "player_sets"]
pub struct Set {
Expand All @@ -20,7 +18,6 @@ pub struct Set {
opponent_score: i32,
opponent_seed: i32,
result_type: i32,
game_ids: Option<Vec<i32>>,
event_id: i32,
tournament_id: i32,
is_event_online: bool,
Expand All @@ -35,7 +32,6 @@ impl Set {
is_on: bool,
e_id: i32,
t_id: i32,
maybe_games: Option<Vec<Game>>,
rtag: &str,
rscore: i32,
rseed: i32,
Expand All @@ -59,8 +55,6 @@ impl Set {
is_event_online: is_on,
event_id: e_id,
tournament_id: t_id,
game_ids: maybe_games
.map(|games| games.iter().map(|g| g.game_id).collect::<Vec<i32>>()),
}
}
}
Expand Down
22 changes: 12 additions & 10 deletions database/src/schema.rs
Original file line number Diff line number Diff line change
@@ -1,28 +1,31 @@
table! {
// @generated automatically by Diesel CLI.

diesel::table! {
empty_player_ids (player_id) {
player_id -> Int4,
}
}

table! {
diesel::table! {
last_checked_player_id (player_id) {
player_id -> Int4,
}
}

table! {
player_games (game_id, requester_id) {
diesel::table! {
player_games (game_id, requester_id, set_id) {
game_id -> Int4,
requester_id -> Int4,
requester_win -> Nullable<Bool>,
order_num -> Int4,
requester_char_played -> Nullable<Varchar>,
opponent_char_played -> Nullable<Varchar>,
stage -> Nullable<Varchar>,
set_id -> Int4,
}
}

table! {
diesel::table! {
player_sets (id, requester_id) {
id -> Int4,
completed_at -> Int8,
Expand All @@ -34,14 +37,13 @@ table! {
opponent_score -> Int4,
opponent_seed -> Int4,
result_type -> Int4,
game_ids -> Nullable<Array<Int4>>,
event_id -> Int4,
tournament_id -> Int4,
is_event_online -> Bool,
}
}

table! {
diesel::table! {
player_tournaments (tournament_id, event_id, requester_id) {
tournament_id -> Int4,
event_id -> Int4,
Expand All @@ -56,7 +58,7 @@ table! {
}
}

table! {
diesel::table! {
players (player_id) {
player_id -> Int4,
user_slug -> Varchar,
Expand All @@ -71,11 +73,11 @@ table! {
gender_pronouns -> Nullable<Varchar>,
birthday -> Nullable<Varchar>,
bio -> Nullable<Varchar>,
rankings -> Nullable<Array<Text>>,
rankings -> Nullable<Array<Nullable<Text>>>,
}
}

allow_tables_to_appear_in_same_query!(
diesel::allow_tables_to_appear_in_same_query!(
empty_player_ids,
last_checked_player_id,
player_games,
Expand Down
2 changes: 1 addition & 1 deletion lib/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ test = false
[dependencies]
anyhow = "1"
tracing = "0.1"
tracing-subscriber = "0.3"
diesel = { version = "1.4", features = ["postgres"] }
smithe_database = { path = "../database" }
startgg = { path = "../startgg_api" }
ctrlc = "3.2"
as-any = "0.3"
tokio = { version = "1.1", features = [ "rt", "macros" ] }
tracing-test = "0.2"
chrono = "0.4"
12 changes: 12 additions & 0 deletions lib/src/common.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#![allow(unused)]

use anyhow::Result;
use tracing::Level;
use tracing_subscriber::FmtSubscriber;

use std::{
future::Future,
Expand All @@ -13,6 +15,16 @@ use std::{
time::{Duration, Instant},
};

pub fn init_logger() -> Result<()> {
let subscriber = FmtSubscriber::builder()
.with_max_level(Level::INFO)
.finish();

tracing::subscriber::set_global_default(subscriber)?;

Ok(())
}

use startgg::{GQLData, GQLVars};

#[allow(clippy::too_many_arguments)]
Expand Down
2 changes: 2 additions & 0 deletions lib/src/game.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ pub fn maybe_get_games_from_set(
player_id: i32,
requester_entrant_id: i32,
s: &SGGSet,
sid: i32,
) -> Option<Vec<Game>> {
s.clone().games.map(|gs| {
gs.iter()
Expand Down Expand Up @@ -33,6 +34,7 @@ pub fn maybe_get_games_from_set(
rcp_num,
ocp_num,
g.stage.as_ref().map(|se| se.name.clone()),
sid,
)
})
.collect::<Vec<Game>>()
Expand Down
51 changes: 34 additions & 17 deletions lib/src/player.rs
Original file line number Diff line number Diff line change
Expand Up @@ -236,22 +236,22 @@ where
tracing::info!("🏁 finished compiling results for this player!");
Ok(true)
} else {
tracing::info!("✅ got some results...");
for s in ss {
tracing::info!(
"🍥 processing set from tourney \"{}\"",
s.event.tournament.clone().unwrap().name
);

// we only want to compile results for: double elimination single ssbu brackets
if is_ssbu_singles_double_elimination_tournament(&s) && s.completedAt.is_some() {
tracing::info!(
"🍥 processing set from tourney \"{}\"",
s.event.tournament.clone().unwrap().name
);

let requester_entrant_id = if is_tournament_finished(&s) {
get_requester_id_from_standings(&s, player.id)
} else {
continue;
};

let maybe_games = maybe_get_games_from_set(player.id, requester_entrant_id, &s);
let maybe_games =
maybe_get_games_from_set(player.id, requester_entrant_id, &s, s.id);

// if there are games, we want to add to the vec to insert in the DB at the end
if let Some(mut games) = maybe_games.clone() {
Expand Down Expand Up @@ -292,7 +292,6 @@ where
s.event.isOnline.unwrap(),
s.event.id.unwrap(),
s.event.tournament.as_ref().unwrap().id,
maybe_games.clone(),
r.entrant.as_ref().unwrap().name.as_ref().unwrap(),
r.standing
.as_ref()
Expand Down Expand Up @@ -341,19 +340,37 @@ where
}
// ^^^ unwrapping in these instances is fine due to the query context that we are in, if an error occurs,
// we want to panic regardless
else {
tracing::info!("🚫 skipping set from tourney \"{}\"", s.event.tournament.as_ref().unwrap().name);
continue;
}
}

for g in curated_games {
let res = insert_into(player_games).values(g).execute(&db_connection);

if let Err(e) = res {
tracing::error!("🚨 error inserting game into db: {}", e);
}
}

insert_into(player_games)
.values(curated_games)
.execute(&db_connection)?;
for s in curated_sets {
let res = insert_into(player_sets).values(s).execute(&db_connection);

if let Err(e) = res {
tracing::error!("🚨 error inserting set into db: {}", e);
}
}

insert_into(player_sets)
.values(curated_sets)
.execute(&db_connection)?;
for t in curated_tournaments {
let res = insert_into(player_tournaments)
.values(t)
.execute(&db_connection);

insert_into(player_tournaments)
.values(curated_tournaments)
.execute(&db_connection)?;
if let Err(e) = res {
tracing::error!("🚨 error inserting tournament into db: {}", e);
}
}

Ok(false)
}
Expand Down
12 changes: 7 additions & 5 deletions lib/src/tournament.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,8 @@ pub async fn get_tournaments_from_requester_id(rid: i32) -> Result<Vec<Tournamen

pub fn is_ssbu_singles_double_elimination_tournament(s: &SGGSet) -> bool {
s.event.videogame.as_ref().unwrap().name == "Super Smash Bros. Ultimate"
&& s.phaseGroup.bracketType == "DOUBLE_ELIMINATION"
&& s.phaseGroup.is_some()
&& s.phaseGroup.clone().unwrap().bracketType == "DOUBLE_ELIMINATION"
&& s.event.teamRosterSize.is_none()
}

Expand Down Expand Up @@ -128,14 +129,15 @@ pub fn is_tournament_cached(player_id: i32, s: &SGGSet) -> Result<bool> {
#[cfg(test)]
mod tests {
use anyhow::Result;
use tracing_test::traced_test;

const DANTOTTO_PLAYER_ID: i32 = 1178271;
use crate::common::init_logger;

const HUNGRYBOX_PLAYER_ID: i32 = 1004;

#[traced_test]
#[tokio::test]
async fn get_tournaments_from_requester_id_test() -> Result<()> {
let _ = super::get_tournaments_from_requester_id(DANTOTTO_PLAYER_ID).await?;
init_logger()?;
let _ = super::get_tournaments_from_requester_id(HUNGRYBOX_PLAYER_ID).await?;
Ok(())
}
}
Loading

0 comments on commit 7020c84

Please sign in to comment.