From 634105ef50937acff776e6711a4295ebfe156baf Mon Sep 17 00:00:00 2001 From: Dan Chiarlone Date: Sun, 24 Dec 2023 15:07:22 -0800 Subject: [PATCH] Adds CI & User Updater Setup (#35) * adding build CI for user udpater Signed-off-by: danbugs * clippy Signed-off-by: danbugs * fmt Signed-off-by: danbugs * update CI Signed-off-by: danbugs * fixing build on windows, and adding caching Signed-off-by: danbugs * added docker deploy action on PR merge onto main Signed-off-by: danbugs * added caching to postgres Signed-off-by: danbugs * meaningless change to test caching Signed-off-by: danbugs * adding action for postgres and removing its caching 'cause it breaks build Signed-off-by: danbugs * fixing CI Signed-off-by: danbugs * fixing CI Signed-off-by: danbugs --------- Signed-off-by: danbugs --- .cargo/config.toml | 2 +- .github/workflows/build.yml | 70 +++++++++++ .github/workflows/docker-deploy.yml | 23 ++++ Cargo.lock | 73 +++++++----- Dockerfile-UserUpdater | 3 + Makefile | 12 +- README.md | 12 +- backend/src/main.rs | 8 +- database/src/db_models/player.rs | 2 +- database/src/lib.rs | 2 +- frontend/src/components/footer.rs | 4 +- frontend/src/components/header.rs | 2 +- frontend/src/components/loading_spinner.rs | 2 +- frontend/src/components/mod.rs | 6 +- frontend/src/components/navbar.rs | 2 +- frontend/src/components/player_list.rs | 4 +- frontend/src/components/player_profile.rs | 14 +-- .../player_profile/player_profile_header.rs | 20 ++-- .../player_profile_tournament_list.rs | 20 ++-- frontend/src/main.rs | 112 +++++++++++------- frontend/src/models.rs | 4 +- frontend/src/utils.rs | 12 +- lib/src/common.rs | 1 + lib/src/game.rs | 6 +- lib/src/player.rs | 31 +++-- src/pidgtm/update.rs | 2 +- src/smithe/player.rs | 16 +-- startgg_api/src/lib.rs | 7 +- 28 files changed, 315 insertions(+), 157 deletions(-) create mode 100644 .github/workflows/build.yml create mode 100644 .github/workflows/docker-deploy.yml create mode 100644 Dockerfile-UserUpdater diff --git a/.cargo/config.toml b/.cargo/config.toml index 52cb51a..508ad07 100644 --- a/.cargo/config.toml +++ b/.cargo/config.toml @@ -1,3 +1,3 @@ [target.x86_64-pc-windows-msvc.pq] -rustc-link-search = ["C:\\Program Files\\PostgreSQL\\16\\lib"] +rustc-link-search = ["C:\\Program Files\\PostgreSQL\\14\\lib"] rustc-link-lib = ["libpq"] \ No newline at end of file diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml new file mode 100644 index 0000000..e19a751 --- /dev/null +++ b/.github/workflows/build.yml @@ -0,0 +1,70 @@ +name: CI + +on: + push: + branches: + - main + pull_request: + +jobs: + lint-and-format: + name: Lint with clippy and check formatting + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + + - name: Install Rust toolchain + uses: actions-rs/toolchain@v1 + with: + profile: minimal + toolchain: stable + components: clippy, rustfmt + override: true + + - name: Check formatting + run: cargo fmt -- --check + + - name: Clippy check + run: cargo clippy -p smithe_backend -p smithe_database -p smithe_lib -p startgg -- -D warnings + build: + needs: lint-and-format + strategy: + matrix: + os: [ubuntu-latest, macos-latest, windows-latest] + include: + - os: ubuntu-latest + target: x86_64-unknown-linux-gnu + - os: macos-latest + target: x86_64-apple-darwin + - os: windows-latest + target: x86_64-pc-windows-msvc + + runs-on: ${{ matrix.os }} + + steps: + - uses: actions/checkout@v2 + + - name: Cache Rust dependencies + uses: Swatinem/rust-cache@v2 + + + - name: Install Rust toolchain + uses: actions-rs/toolchain@v1 + with: + profile: minimal + toolchain: stable + override: true + target: ${{ matrix.target }} + + - if: matrix.os == 'windows-latest' + name: Install PostgreSQL + uses: ikalnytskyi/action-setup-postgres@v4 + + - name: Build + run: cargo build --release --target ${{ matrix.target }} + + - name: Upload artifact + uses: actions/upload-artifact@v2 + with: + name: my-binary-${{ matrix.target }} + path: target/${{ matrix.target }}/release/my-binary diff --git a/.github/workflows/docker-deploy.yml b/.github/workflows/docker-deploy.yml new file mode 100644 index 0000000..58cd958 --- /dev/null +++ b/.github/workflows/docker-deploy.yml @@ -0,0 +1,23 @@ +name: Docker Deployment + +on: + push: + branches: + - main + +jobs: + docker: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + + - name: Download artifact + uses: actions/download-artifact@v2 + with: + name: my-binary-x86_64-unknown-linux-gnu + + - name: Build Docker image + run: | + docker build -t pidgtm-user-updater:latest -f Dockerfile-UserUpdater . + echo ${{ secrets.DOCKER_PASSWORD }} | docker login -u ${{ secrets.DOCKER_USERNAME }} --password-stdin + docker push pidgtm-user-updater:latest diff --git a/Cargo.lock b/Cargo.lock index 4fc63de..df55fc8 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -158,6 +158,12 @@ version = "1.3.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a" +[[package]] +name = "bitflags" +version = "2.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "327762f6e5a765692301e5bb513e0d9fef63be86bbc14528052b1cd3e6f03e07" + [[package]] name = "block-buffer" version = "0.10.3" @@ -237,7 +243,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "86447ad904c7fb335a790c9d7fe3d0d971dc523b8ccd1561a520de9a85302750" dependencies = [ "atty", - "bitflags", + "bitflags 1.3.2", "clap_derive", "clap_lex", "indexmap", @@ -402,7 +408,7 @@ version = "0.3.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "841ef46f4787d9097405cac4e70fb8644fc037b526e8c14054247c0263c400d0" dependencies = [ - "bitflags", + "bitflags 1.3.2", "proc-macro2", "proc-macro2-diagnostics", "quote", @@ -426,7 +432,7 @@ version = "1.4.8" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b28135ecf6b7d446b43e27e225622a038cc4e2930a1022f51cdb97ada19b8e4d" dependencies = [ - "bitflags", + "bitflags 1.3.2", "byteorder", "diesel_derives", "pq-sys", @@ -774,9 +780,9 @@ dependencies = [ [[package]] name = "gql_client" -version = "1.0.6" +version = "1.0.7" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a2ad0b978ded703acd23762f99e0f701450c9d0066912c0d7f9bf65bf017d87d" +checksum = "e57f4862d3e5cd4ffe8df03fa2137e620e33578bbc1895e6f8f569630e17b1db" dependencies = [ "log", "reqwest", @@ -1078,7 +1084,7 @@ dependencies = [ "libc", "log", "wasi", - "windows-sys", + "windows-sys 0.36.1", ] [[package]] @@ -1103,9 +1109,9 @@ dependencies = [ [[package]] name = "native-tls" -version = "0.2.10" +version = "0.2.11" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fd7e2f3618557f980e0b17e8856252eee3c97fa12c54dff0ca290fb6266ca4a9" +checksum = "07226173c32f2926027b63cce4bcd8076c3552846cbe7925f3aaffeac0a3b92e" dependencies = [ "lazy_static", "libc", @@ -1126,7 +1132,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e322c04a9e3440c327fca7b6c8a63e6890a32fa2ad689db972425f07e0d22abb" dependencies = [ "autocfg", - "bitflags", + "bitflags 1.3.2", "cfg-if", "libc", ] @@ -1164,11 +1170,11 @@ checksum = "624a8340c38c1b80fd549087862da4ba43e08858af025b236e509b6649fc13d5" [[package]] name = "openssl" -version = "0.10.41" +version = "0.10.62" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "618febf65336490dfcf20b73f885f5651a0c89c64c2d4a8c3662585a70bf5bd0" +checksum = "8cde4d2d9200ad5909f8dac647e29482e07c3a35de8a13fce7c9c7747ad9f671" dependencies = [ - "bitflags", + "bitflags 2.4.1", "cfg-if", "foreign-types", "libc", @@ -1196,11 +1202,10 @@ checksum = "ff011a302c396a5197692431fc1948019154afc178baf7d8e37367442a4601cf" [[package]] name = "openssl-sys" -version = "0.9.75" +version = "0.9.98" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e5f9bd0c2710541a3cda73d6f9ac4f1b240de4ae261065d309dbe73d9dceb42f" +checksum = "c1665caf8ab2dc9aef43d1c0023bd904633a6a05cb30b0ad59bec2ae986e57a7" dependencies = [ - "autocfg", "cc", "libc", "pkg-config", @@ -1233,7 +1238,7 @@ dependencies = [ "libc", "redox_syscall", "smallvec", - "windows-sys", + "windows-sys 0.36.1", ] [[package]] @@ -1299,9 +1304,9 @@ checksum = "8b870d8c151b6f2fb93e84a13146138f05d02ed11c7e7c54f8826aaaf7c9f184" [[package]] name = "pkg-config" -version = "0.3.25" +version = "0.3.28" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1df8c4ec4b0627e53bdf214615ad287367e482558cf84b109250b37464dc03ae" +checksum = "69d3587f8a9e599cc7ec2c00e331f71c4e69a5f9a4b8a6efd5b07466b9736f9a" [[package]] name = "polyval" @@ -1421,7 +1426,7 @@ version = "0.2.16" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "fb5a58c1855b4b6819d59012155603f0b22ad30cad752600aadfcb695265519a" dependencies = [ - "bitflags", + "bitflags 1.3.2", ] [[package]] @@ -1627,12 +1632,11 @@ checksum = "4501abdff3ae82a1c1b477a17252eb69cee9e66eb915c1abaa4f44d873df9f09" [[package]] name = "schannel" -version = "0.1.20" +version = "0.1.22" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "88d6731146462ea25d9244b2ed5fd1d716d25c52e4d54aa4fb0f3c4e9854dbe2" +checksum = "0c3733bf4cf7ea0880754e19cb5a462007c4a8c1914bff372ccc95b464f1df88" dependencies = [ - "lazy_static", - "windows-sys", + "windows-sys 0.48.0", ] [[package]] @@ -1655,11 +1659,11 @@ checksum = "d29ab0c6d3fc0ee92fe66e2d99f700eab17a8d57d1c1d3b748380fb20baa78cd" [[package]] name = "security-framework" -version = "2.7.0" +version = "2.8.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2bc1bb97804af6631813c55739f771071e0f2ed33ee20b68c86ec505d906356c" +checksum = "7c4437699b6d34972de58652c68b98cb5b53a4199ab126db8e20ec8ded29a721" dependencies = [ - "bitflags", + "bitflags 1.3.2", "core-foundation", "core-foundation-sys", "libc", @@ -1668,9 +1672,9 @@ dependencies = [ [[package]] name = "security-framework-sys" -version = "2.6.1" +version = "2.8.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0160a13a177a45bfb43ce71c01580998474f556ad854dcbca936dd2841a5c556" +checksum = "31c9bb296072e961fcbd8853511dd39c2d8be2deb1e17c6860b1d30732b323b4" dependencies = [ "core-foundation-sys", "libc", @@ -2046,9 +2050,9 @@ dependencies = [ [[package]] name = "tokio-native-tls" -version = "0.3.0" +version = "0.3.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f7d995660bd2b7f8c1568414c1126076c13fbb725c40112dc0120b78eb9b717b" +checksum = "bbae76ab933c85776efabc971569dd6119c580d8f5d448769dec1764bf796ef2" dependencies = [ "native-tls", "tokio", @@ -2461,6 +2465,15 @@ dependencies = [ "windows_x86_64_msvc 0.36.1", ] +[[package]] +name = "windows-sys" +version = "0.48.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "677d2418bec65e3338edb076e806bc1ec15693c5d0104683f2efe857f61056a9" +dependencies = [ + "windows-targets", +] + [[package]] name = "windows-targets" version = "0.48.5" diff --git a/Dockerfile-UserUpdater b/Dockerfile-UserUpdater new file mode 100644 index 0000000..181d3dc --- /dev/null +++ b/Dockerfile-UserUpdater @@ -0,0 +1,3 @@ +FROM debian:buster-slim +COPY target/x86_64-unknown-linux-gnu/release/pidgtm /usr/local/bin/pidgtm +ENTRYPOINT ["pidgtm"] diff --git a/Makefile b/Makefile index 4176077..4a6c75f 100644 --- a/Makefile +++ b/Makefile @@ -5,9 +5,9 @@ SMITHE ?= target/release/smithe # CARGO .PHONY: improve improve: - cargo clippy --all --all-targets --all-features -- -D warnings + cargo clippy --fix -p smithe_backend -p smithe_database -p smithe_lib -p startgg cargo fmt --all - + .PHONY: test test: cargo test --workspace -- --show-output 2>&1 | tee test.out @@ -19,14 +19,18 @@ build: # INSTALL .PHONY: install install: - install target/release/smithe $(INSTALL_DIR)/bin - install target/release/pidgtm $(INSTALL_DIR)/bin + cargo install --path . # PIDGTM .PHONY: pidgtm pidgtm-map: $(PIDGTM) map +.PHONY: pidgtm-user-updater +pidgtm-user-updater: + cargo build --release --bin pidgtm --target x86_64-unknown-linux-gnu + docker build -t pidgtm-user-updater -f Dockerfile-UserUpdater . + # BACKEND .PHONY: build-backend build-backend: diff --git a/README.md b/README.md index 0a738bd..913ccff 100644 --- a/README.md +++ b/README.md @@ -36,6 +36,16 @@ Smithereens is not yet available as a website, only as a command-line tool. With Currently, the only way to get Smithereens on your computer is to build it from source. To do so, you need to have the Rust toolchain installed on your machine. To install it, follow instructions [here](https://www.rust-lang.org/tools/install). +> Note: +>> Aside from the Rust toolchain, you will also need: +>> - Postgres, +>> - Diesel, and +>> - Trunk. +> +>> Plus, you need the following Rust targets: +>> - wasm32-unknown-unknown, and +>> - x86_64-unknown-linux-gnu + After that, inside of Smithereens' repository root, run: `cargo build --release`. With that, you'll have Smithereens' binary (i.e., `smithe`) built under `target/release`. You can run it, like: `./target/release/smithe`. After that, feel free to move the binary to a more convenient place on your machine (i.e., perhaps to a place included in your `PATH`, so you can run it simply with `smithe`). -> Note: In the future, there will be releases for `smithe`, so this process will be better streamlined. \ No newline at end of file +> Note: In the future, there will be releases for `smithe`, so this process will be better streamlined. diff --git a/backend/src/main.rs b/backend/src/main.rs index e7b4c15..7f0b636 100644 --- a/backend/src/main.rs +++ b/backend/src/main.rs @@ -11,7 +11,7 @@ use smithe_lib::{ player::{get_all_like, get_player, get_top_two_characters}, set::{ get_competitor_type, get_set_losses_by_dq, get_set_losses_without_dqs, get_set_wins_by_dq, - get_set_wins_without_dqs, get_winrate, get_sets_per_player_id, + get_set_wins_without_dqs, get_sets_per_player_id, get_winrate, }, tournament::get_tournaments_from_requester_id, }; @@ -31,9 +31,7 @@ pub enum Error { impl<'r, 'o: 'r> Responder<'r, 'o> for Error { fn respond_to(self, req: &Request<'_>) -> response::Result<'o> { // todo: use open telemetry at this point - match self { - _ => Status::InternalServerError.respond_to(req), - } + Status::InternalServerError.respond_to(req) } } @@ -149,7 +147,7 @@ async fn main() -> Result<(), Box> { #[cfg(test)] mod tests { - use rocket::{local::blocking::Client, http::Status}; + use rocket::{http::Status, local::blocking::Client}; use super::rocket; diff --git a/database/src/db_models/player.rs b/database/src/db_models/player.rs index b3ecd3f..2cded89 100644 --- a/database/src/db_models/player.rs +++ b/database/src/db_models/player.rs @@ -3,7 +3,7 @@ // — an auto fix for this exists only in Diesel v2. use crate::schema::players; -use serde::{Serialize, Deserialize}; +use serde::{Deserialize, Serialize}; #[derive(Debug, Serialize, Deserialize, Insertable, Queryable, QueryableByName)] #[table_name = "players"] diff --git a/database/src/lib.rs b/database/src/lib.rs index 911e7b0..7fe45dc 100644 --- a/database/src/lib.rs +++ b/database/src/lib.rs @@ -29,4 +29,4 @@ mod tests { fn test_connect() { assert!(connect().is_ok()); } -} \ No newline at end of file +} diff --git a/frontend/src/components/footer.rs b/frontend/src/components/footer.rs index f7e92b7..5a32470 100644 --- a/frontend/src/components/footer.rs +++ b/frontend/src/components/footer.rs @@ -35,6 +35,6 @@ pub fn footer() -> Html { - + } -} \ No newline at end of file +} diff --git a/frontend/src/components/header.rs b/frontend/src/components/header.rs index adceccf..d42cfad 100644 --- a/frontend/src/components/header.rs +++ b/frontend/src/components/header.rs @@ -11,7 +11,7 @@ pub fn header(props: &Props) -> Html { html! {
-
+ } } else { html! { diff --git a/frontend/src/components/loading_spinner.rs b/frontend/src/components/loading_spinner.rs index b834ae9..b4eb0d2 100644 --- a/frontend/src/components/loading_spinner.rs +++ b/frontend/src/components/loading_spinner.rs @@ -12,4 +12,4 @@ pub fn loading_spinner() -> Html {
} -} \ No newline at end of file +} diff --git a/frontend/src/components/mod.rs b/frontend/src/components/mod.rs index 4b676ce..a82deb6 100644 --- a/frontend/src/components/mod.rs +++ b/frontend/src/components/mod.rs @@ -1,6 +1,6 @@ -pub mod navbar; +pub mod footer; pub mod header; +pub mod loading_spinner; +pub mod navbar; pub mod player_list; pub mod player_profile; -pub mod footer; -pub mod loading_spinner; \ No newline at end of file diff --git a/frontend/src/components/navbar.rs b/frontend/src/components/navbar.rs index e009fce..632388b 100644 --- a/frontend/src/components/navbar.rs +++ b/frontend/src/components/navbar.rs @@ -1,5 +1,5 @@ use web_sys::KeyboardEvent; -use yew::{function_component, html, Properties, Callback}; +use yew::{function_component, html, Callback, Properties}; #[derive(Properties, PartialEq)] pub struct Props { diff --git a/frontend/src/components/player_list.rs b/frontend/src/components/player_list.rs index 602ee0b..8b94a14 100644 --- a/frontend/src/components/player_list.rs +++ b/frontend/src/components/player_list.rs @@ -1,6 +1,6 @@ use country_emoji::flag; use web_sys::MouseEvent; -use yew::{function_component, html, Properties, Html, Callback}; +use yew::{function_component, html, Callback, Html, Properties}; use crate::models::Player; @@ -40,7 +40,7 @@ pub fn player_list(props: &Props) -> Html {

{ if p.prefix.is_none() || p.prefix.as_ref().unwrap().is_empty() { - format!("{}", &p.gamer_tag) + (&p.gamer_tag).to_string() } else { format!("{} | {}", p.prefix.as_ref().unwrap(), &p.gamer_tag) } diff --git a/frontend/src/components/player_profile.rs b/frontend/src/components/player_profile.rs index 390b8a9..41142fc 100644 --- a/frontend/src/components/player_profile.rs +++ b/frontend/src/components/player_profile.rs @@ -1,6 +1,6 @@ use yew::{function_component, html, Properties}; -use crate::models::{Player, Tournament, Set}; +use crate::models::{Player, Set, Tournament}; pub mod player_profile_header; use crate::components::player_profile::player_profile_header::PlayerProfileHeader; @@ -31,19 +31,19 @@ pub fn player_profile(props: &Props) -> Html {

-
-
- diff --git a/frontend/src/components/player_profile/player_profile_header.rs b/frontend/src/components/player_profile/player_profile_header.rs index 7c578ae..9cbb383 100644 --- a/frontend/src/components/player_profile/player_profile_header.rs +++ b/frontend/src/components/player_profile/player_profile_header.rs @@ -1,8 +1,8 @@ use country_emoji::flag; -use yew::{function_component, html, Properties, Html}; +use yew::{function_component, html, Html, Properties}; -use crate::models::Player; use crate::components::loading_spinner::LoadingSpinner; +use crate::models::Player; #[derive(Properties, PartialEq)] pub struct Props { @@ -33,7 +33,7 @@ pub fn player_profile_header(props: &Props) -> Html {

{ if props.selected_player.prefix.is_none() || props.selected_player.prefix.as_ref().unwrap().is_empty() { - format!("{}", &props.selected_player.gamer_tag) + (&props.selected_player.gamer_tag).to_string() } else { format!("{} | {}", props.selected_player.prefix.as_ref().unwrap(), &props.selected_player.gamer_tag) } @@ -45,8 +45,8 @@ pub fn player_profile_header(props: &Props) -> Html { html! { // display name and flag in a p in grey

{ - format!("{} {}", - props.selected_player.name.as_ref().unwrap(), + format!("{} {}", + props.selected_player.name.as_ref().unwrap(), if let Some(pc) = props.selected_player.country.clone() { flag(&pc).unwrap_or("".to_string()) } else { @@ -100,14 +100,14 @@ pub fn player_profile_header(props: &Props) -> Html { } } } -

+

{ if props.display { html! { -
+
{ if props.selected_player_summary_data.as_ref().unwrap().4.is_empty() { html! { @@ -130,9 +130,9 @@ pub fn player_profile_header(props: &Props) -> Html { } } - } -
+ } +
} -} \ No newline at end of file +} diff --git a/frontend/src/components/player_profile/player_profile_tournament_list.rs b/frontend/src/components/player_profile/player_profile_tournament_list.rs index 653433f..5f749a8 100644 --- a/frontend/src/components/player_profile/player_profile_tournament_list.rs +++ b/frontend/src/components/player_profile/player_profile_tournament_list.rs @@ -1,6 +1,6 @@ -use yew::{function_component, html, Properties, Html}; +use yew::{function_component, html, Html, Properties}; -use crate::models::{Tournament, Set}; +use crate::models::{Set, Tournament}; use crate::components::loading_spinner::LoadingSpinner; use crate::utils::calculate_spr_or_uf; @@ -26,7 +26,7 @@ pub fn player_profile_tournament_list(props: &Props) -> Html {

@@ -86,9 +86,9 @@ pub fn player_profile_tournament_list(props: &Props) -> Html {
{ - if s.requester_score > s.opponent_score - && s.requester_seed > s.opponent_seed - && s.result_type > 1 + if s.requester_score > s.opponent_score + && s.requester_seed > s.opponent_seed + && s.result_type > 1 && calculate_spr_or_uf(s.requester_seed, s.opponent_seed) > 0 { format!("upset factor: {}", calculate_spr_or_uf(s.requester_seed, s.opponent_seed)) } else { @@ -121,4 +121,4 @@ pub fn player_profile_tournament_list(props: &Props) -> Html { } } -} \ No newline at end of file +} diff --git a/frontend/src/main.rs b/frontend/src/main.rs index ebda366..ba64545 100644 --- a/frontend/src/main.rs +++ b/frontend/src/main.rs @@ -1,7 +1,5 @@ -use std::env; - use gloo_net::http::Request; -use models::{Player, Tournament, Set}; +use models::{Player, Set, Tournament}; use utils::parse_text_vector; use wasm_bindgen::JsCast; use web_sys::{HtmlButtonElement, HtmlInputElement}; @@ -58,7 +56,12 @@ impl Component for App { let link = ctx.link().clone(); wasm_bindgen_futures::spawn_local(async move { - let endpoint = format!("{}/players/{}", env!("SERVER_ADDRESS"), value); + let endpoint = format!( + "{}/players/{}", + std::env::var("SERVER_ADDRESS") + .expect("could not find SERVER_ADDRESS env var"), + value + ); let fetched_players: Vec = Request::get(&endpoint) .send() .await @@ -82,7 +85,7 @@ impl Component for App { Msg::SelectPlayer(pid) => { // console log pid web_sys::console::log_1(&pid.into()); - + self.selected_player = Some( self.search_results .iter() @@ -94,22 +97,31 @@ impl Component for App { let link = ctx.link().clone(); wasm_bindgen_futures::spawn_local(async move { - let fetched_tournaments: Vec = Request::get(&format!("{}/tournaments/{}", env!("SERVER_ADDRESS"), pid)) - .send() - .await - .unwrap() - .json() - .await - .unwrap(); + let fetched_tournaments: Vec = Request::get(&format!( + "{}/tournaments/{}", + std::env::var("SERVER_ADDRESS") + .expect("could not find SERVER_ADDRESS env var"), + pid + )) + .send() + .await + .unwrap() + .json() + .await + .unwrap(); - let fetched_sets: Vec = Request::get(&format!("{}/sets/{}", env!("SERVER_ADDRESS"), pid)) - .send() - .await - .unwrap() - .json() - .await - .unwrap(); - + let fetched_sets: Vec = Request::get(&format!( + "{}/sets/{}", + std::env::var("SERVER_ADDRESS") + .expect("could not find SERVER_ADDRESS env var"), + pid + )) + .send() + .await + .unwrap() + .json() + .await + .unwrap(); link.send_message(Msg::GetTournaments(fetched_tournaments, fetched_sets, pid)); }); @@ -126,18 +138,23 @@ impl Component for App { let link = ctx.link().clone(); wasm_bindgen_futures::spawn_local(async move { - let fetch_winrate: String = - Request::get(&format!("{}/sets/{}/winrate", env!("SERVER_ADDRESS"), pid)) - .send() - .await - .unwrap() - .text() - .await - .unwrap(); + let fetch_winrate: String = Request::get(&format!( + "{}/sets/{}/winrate", + std::env::var("SERVER_ADDRESS") + .expect("could not find SERVER_ADDRESS env var"), + pid + )) + .send() + .await + .unwrap() + .text() + .await + .unwrap(); let fetch_competitor_type: String = Request::get(&format!( "{}/sets/{}/competitor_type", - env!("SERVER_ADDRESS"), + std::env::var("SERVER_ADDRESS") + .expect("could not find SERVER_ADDRESS env var"), pid )) .send() @@ -149,7 +166,8 @@ impl Component for App { let fetch_wins_without_dqs: String = Request::get(&format!( "{}/sets/{}/wins_without_dqs", - env!("SERVER_ADDRESS"), + std::env::var("SERVER_ADDRESS") + .expect("could not find SERVER_ADDRESS env var"), pid )) .send() @@ -161,7 +179,8 @@ impl Component for App { let fetch_losses_without_dqs: String = Request::get(&format!( "{}/sets/{}/losses_without_dqs", - env!("SERVER_ADDRESS"), + std::env::var("SERVER_ADDRESS") + .expect("could not find SERVER_ADDRESS env var"), pid )) .send() @@ -173,7 +192,8 @@ impl Component for App { let fetch_top_two_characters: String = Request::get(&format!( "{}/player/{}/top_two_characters", - env!("SERVER_ADDRESS"), + std::env::var("SERVER_ADDRESS") + .expect("could not find SERVER_ADDRESS env var"), pid )) .send() @@ -192,15 +212,26 @@ impl Component for App { fetch_competitor_type, wins_losses, tournaments_attended, - parse_text_vector(&fetch_top_two_characters) + parse_text_vector(&fetch_top_two_characters), )); }); false } - Msg::GetSummaryData(winrate, competitor_type, wins_losses, tournaments_attended, top_two_chars) => { - self.selected_player_summary_data = - Some((winrate, competitor_type, wins_losses, tournaments_attended, top_two_chars)); + Msg::GetSummaryData( + winrate, + competitor_type, + wins_losses, + tournaments_attended, + top_two_chars, + ) => { + self.selected_player_summary_data = Some(( + winrate, + competitor_type, + wins_losses, + tournaments_attended, + top_two_chars, + )); true } @@ -221,14 +252,9 @@ impl Component for App { }); let plist_onclick = link.batch_callback(|e: MouseEvent| { - if let Some(elem) = e - .target() + e.target() .and_then(|t| t.dyn_into::().ok()) - { - Some(Msg::SelectPlayer(elem.id().parse().unwrap())) - } else { - None - } + .map(|elem| Msg::SelectPlayer(elem.id().parse().unwrap())) }); html! { diff --git a/frontend/src/models.rs b/frontend/src/models.rs index 22a1d7a..7733fcf 100644 --- a/frontend/src/models.rs +++ b/frontend/src/models.rs @@ -1,4 +1,4 @@ -use serde::{Serialize, Deserialize}; +use serde::{Deserialize, Serialize}; #[derive(Debug, Serialize, Deserialize, Clone, PartialEq)] pub struct Player { @@ -48,4 +48,4 @@ pub struct Set { pub event_id: i32, pub tournament_id: i32, pub is_event_online: bool, -} \ No newline at end of file +} diff --git a/frontend/src/utils.rs b/frontend/src/utils.rs index c79e834..9d62c8b 100644 --- a/frontend/src/utils.rs +++ b/frontend/src/utils.rs @@ -2,13 +2,13 @@ pub fn calculate_spr_or_uf(a: i32, b: i32) -> i32 { let s = if a == 1 { 0. } else { - (a as f32 - 1.).log2().floor() + (2./3. * a as f32).log2().ceil() + (a as f32 - 1.).log2().floor() + (2. / 3. * a as f32).log2().ceil() }; - + let p = if b == 1 { 0. } else { - (b as f32 - 1.).log2().floor() + (2./3. * b as f32).log2().ceil() + (b as f32 - 1.).log2().floor() + (2. / 3. * b as f32).log2().ceil() }; (s - p) as i32 @@ -17,11 +17,11 @@ pub fn calculate_spr_or_uf(a: i32, b: i32) -> i32 { pub fn parse_text_vector(text: &str) -> Vec { // remove the first and last characters let text = &text[1..text.len() - 1]; - + if text.is_empty() { return Vec::new(); } - + let mut result = Vec::new(); let mut current = String::new(); let mut in_quotes = false; @@ -37,4 +37,4 @@ pub fn parse_text_vector(text: &str) -> Vec { } result.push(current); result -} \ No newline at end of file +} diff --git a/lib/src/common.rs b/lib/src/common.rs index 70edde8..a205264 100644 --- a/lib/src/common.rs +++ b/lib/src/common.rs @@ -15,6 +15,7 @@ use std::{ use startgg::{GQLData, GQLVars}; +#[allow(clippy::too_many_arguments)] pub async fn start_read_all_by_increment_execute_finish_maybe_cancel( is_cli: bool, gql_vars: Arc>, diff --git a/lib/src/game.rs b/lib/src/game.rs index b92592f..169f69f 100644 --- a/lib/src/game.rs +++ b/lib/src/game.rs @@ -28,11 +28,7 @@ pub fn maybe_get_games_from_set( Game::new( g.id, player_id, - if let Some(w) = &g.winnerId { - Some(w.eq(&requester_entrant_id)) - } else { - None - }, + g.winnerId.as_ref().map(|w| w.eq(&requester_entrant_id)), g.orderNum, rcp_num, ocp_num, diff --git a/lib/src/player.rs b/lib/src/player.rs index cee2d73..140b299 100644 --- a/lib/src/player.rs +++ b/lib/src/player.rs @@ -7,7 +7,12 @@ use smithe_database::{ }, }; -use diesel::{dsl::{max, count}, insert_into, prelude::*, update}; +use diesel::{ + dsl::{count, max}, + insert_into, + prelude::*, + update, +}; use smithe_database::{ db_models::{empty_player_ids::EmptyPlayerId, last_checked_player_id::LastCheckedPlayerId}, schema::last_checked_player_id, @@ -54,9 +59,15 @@ pub fn get_last_cached_player_id() -> Result { if let Some(val) = max_checked_player_id { Ok(val) } else { - Ok(1) - // ^^^ don't start at 0 because it is uniquely populated - // w/ all null values but non null player + // return max player id from players table + let max_player_id = players + .select(max(smithe_database::schema::players::player_id)) + .first::>(&db_connection)?; + if let Some(val) = max_player_id { + Ok(val) + } else { + Ok(1000) // nothing in db, start at 1000 + } } } @@ -166,10 +177,13 @@ where } else { tracing::info!("✅ got some results..."); for s in ss { - tracing::info!("🍥 processing set from tourney \"{}\"", s.event.tournament.clone().unwrap().name); + 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(){ + if is_ssbu_singles_double_elimination_tournament(&s) && s.completedAt.is_some() { let requester_entrant_id = if is_tournament_finished(&s) { get_requester_id_from_standings(&s, player.id) } else { @@ -186,7 +200,8 @@ where let rslot = get_requester_set_slot(requester_entrant_id, &s); let oslot = get_opponent_set_slot(requester_entrant_id, &s); - if let (Some(r), Some(o)) = (rslot, oslot) { // tournaments could be finished, but not have actually finished + if let (Some(r), Some(o)) = (rslot, oslot) { + // tournaments could be finished, but not have actually finished // some sets only have a reported winner, ignore them // e.g., https://www.start.gg/tournament/mainstage-2021/event/ultimate-singles/brackets/952392/1513154 if r.standing @@ -296,4 +311,4 @@ pub fn get_top_two_characters(pid: i32) -> Result>> { .get_results::>(&db_connection)?; Ok(top_two_characters) -} \ No newline at end of file +} diff --git a/src/pidgtm/update.rs b/src/pidgtm/update.rs index 371df0f..586511c 100644 --- a/src/pidgtm/update.rs +++ b/src/pidgtm/update.rs @@ -41,7 +41,7 @@ where let pgd = player_getter_data.downcast_ref::(); let mut player = pgd.as_ref().unwrap().player.clone(); // vvv have to do this because people might have deleted their account: - if let Some(pti) = (&mut player).as_mut() { + if let Some(pti) = player.as_mut() { if pti.user.is_none() { tracing::info!("❎ caught a deleted account #1 (id: '{}')...", pti.id); pti.user = get_empty_user_with_slug(pti.id)?; diff --git a/src/smithe/player.rs b/src/smithe/player.rs index 8968ec5..e2756ad 100644 --- a/src/smithe/player.rs +++ b/src/smithe/player.rs @@ -2,21 +2,17 @@ use std::sync::{Arc, Mutex}; use anyhow::Result; -use smithe_database::{ - db_models::{player::Player} -}; +use smithe_database::db_models::player::Player; use smithe_lib::{ common::start_read_all_by_increment_execute_finish_maybe_cancel, + player::{execute, get_all_like}, set::{ - get_all_from_player_id, get_competitor_type, get_last_completed_at, - get_set_losses_by_dq, - get_set_wins_without_dqs, get_winrate, get_set_losses_without_dqs, get_set_wins_by_dq, - }, player::{get_all_like, execute}, -}; -use startgg::{ - queries::set_getter::{make_set_getter_query, SetGetterVars} + get_all_from_player_id, get_competitor_type, get_last_completed_at, get_set_losses_by_dq, + get_set_losses_without_dqs, get_set_wins_by_dq, get_set_wins_without_dqs, get_winrate, + }, }; +use startgg::queries::set_getter::{make_set_getter_query, SetGetterVars}; use dialoguer::{theme::ColorfulTheme, Select}; diff --git a/startgg_api/src/lib.rs b/startgg_api/src/lib.rs index aa1bcfd..cff1a35 100644 --- a/startgg_api/src/lib.rs +++ b/startgg_api/src/lib.rs @@ -3,7 +3,7 @@ pub mod queries; -use std::{collections::HashMap, env}; +use std::collections::HashMap; use as_any::AsAny; use serde::Deserialize; @@ -199,7 +199,10 @@ pub struct StartGG { impl StartGG { pub fn connect() -> Self { let mut headers = HashMap::new(); - let bearer_token = format!("Bearer {}", env!("STARTGG_TOKEN")); + let bearer_token = format!( + "Bearer {}", + std::env::var("STARTGG_TOKEN").expect("could not find STARTGG_TOKEN env var") + ); headers.insert("authorization", bearer_token.as_str()); Self { gql_client: gql_client::Client::new_with_headers(STARTGG_ENDPOINT, headers),