diff --git a/holycow_backend/src/database/model.rs b/holycow_backend/src/database/model.rs index 3f98a01..0dcadaa 100644 --- a/holycow_backend/src/database/model.rs +++ b/holycow_backend/src/database/model.rs @@ -159,11 +159,11 @@ impl WengLinRating { log::debug!("Getting human score for: {rating:?}±{uncertainty:?}"); let uncertain = self.0.rating - self.0.uncertainty; log::trace!("Minimum score is: {uncertain:?}"); - let multiplied = uncertain * 100.0; + let multiplied = uncertain * 300.0 / 5.0; log::trace!("Multiplied score is: {multiplied:?}"); - let floored: f64 = multiplied.floor(); - log::trace!("Floored score is: {floored:?}"); - let converted: i64 = floored as i64; + let ceiled: f64 = multiplied.ceil(); + log::trace!("Ceiled score is: {ceiled:?}"); + let converted: i64 = ceiled as i64; log::debug!("Human score for {rating:?}±{uncertainty:?} is {converted:?}"); converted } @@ -231,6 +231,15 @@ impl Match { .get_results::(conn) } + pub fn played_by(conn: &mut PgConnection, player_id: i32) -> QueryResult> { + schema::matches::table + .select(Self::as_select()) + .or_filter(schema::matches::player_a_id.eq(player_id)) + .or_filter(schema::matches::player_b_id.eq(player_id)) + .order_by(schema::matches::instant.desc()) + .get_results::(conn) + } + pub fn played_by_count(conn: &mut PgConnection, player_id: i32) -> QueryResult { schema::matches::table .select(diesel::dsl::count_star()) diff --git a/holycow_backend/src/main.rs b/holycow_backend/src/main.rs index e3cc964..5b308f7 100644 --- a/holycow_backend/src/main.rs +++ b/holycow_backend/src/main.rs @@ -75,6 +75,9 @@ async fn main() -> anyhow::Result { .route("/api/matches/", axum::routing::post(routes::matches::post_match) ) + .route("/api/matches/holycow/:player_id", + axum::routing::post(routes::matches::get_played_by_id) + ) .nest("/telegram/webhook", telegram_router ) diff --git a/holycow_backend/src/routes/matches.rs b/holycow_backend/src/routes/matches.rs index eac8e96..e3bec3e 100644 --- a/holycow_backend/src/routes/matches.rs +++ b/holycow_backend/src/routes/matches.rs @@ -1,5 +1,6 @@ use axum::http::StatusCode; use axum::{Extension, Json}; +use axum::extract::Path; use diesel::{Connection, PgConnection}; use serde::Deserialize; use skillratings::weng_lin::WengLinConfig; @@ -9,6 +10,14 @@ use teloxide::types::{ChatId, MessageId, ParseMode, ThreadId}; use crate::config; use crate::database::model::{Match, MatchI, Outcome, Player, WengLinRating}; +#[derive(Debug, Clone, Deserialize)] +pub struct MatchII { + name: Option, + player_a: i32, + player_b: i32, + outcome: Outcome, +} + #[axum::debug_handler] pub async fn get_all() -> Result>, StatusCode> @@ -22,29 +31,46 @@ pub async fn get_all() Ok(Json(matches)) } -#[derive(Debug, Clone, Deserialize)] -pub struct MatchII { - name: Option, - player_a: i32, - player_b: i32, - outcome: Outcome, +#[axum::debug_handler] +pub async fn get_played_by_id( + Path(player_id): Path, +) + -> Result>, StatusCode> +{ + let mut conn = PgConnection::establish(config::DATABASE_URL()) + .map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?; + + let matches = Match::played_by(&mut conn, player_id) + .map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?; + + Ok(Json(matches)) } fn player_to_text(player: &Player, before: &WengLinRating, after: &WengLinRating) -> String { let name = &player.username; let competitive = &player.competitive; - let telegram_id = &player.telegram_id; + let telegram_id = &player.telegram_id.clone().map(|t| t.0); match competitive { false => { - format!("{name}") + match telegram_id { + None => + format!(r#"{name}"#), + Some(telegram_id) => + format!(r#"{name}"#), + } }, true => { let before = before.human_score(); let after = after.human_score(); let change = after - before; - format!(r#"{name} ({change:+})"#) + match telegram_id { + None => + format!(r#"{name} ({change:+} ★)"#), + Some(telegram_id) => + format!(r#"{name} ({change:+} ★)"#), + } }, } } diff --git a/holycow_backend/src/routes/results.rs b/holycow_backend/src/routes/results.rs index f3567e8..3ee2846 100644 --- a/holycow_backend/src/routes/results.rs +++ b/holycow_backend/src/routes/results.rs @@ -43,11 +43,22 @@ pub async fn get_all() Ok(Json(players.into_iter().map(Into::into).collect())) } +// this is an awful hack but idc i'm out of time +#[derive(Debug, Clone, Serialize, Deserialize)] +pub struct PlayerOO { + id: i32, + telegram_id: Option, + username: String, + human_score: Option, + played: i64, + wins: i64, +} + #[axum::debug_handler] pub async fn get_by_id( Path(player_id): Path, ) - -> Result, StatusCode> + -> Result, StatusCode> { let mut conn = PgConnection::establish(config::DATABASE_URL()) .map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?; @@ -56,14 +67,29 @@ pub async fn get_by_id( .map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)? .ok_or(StatusCode::NOT_FOUND)?; - Ok(Json(player.into())) + let played = player.played_count(&mut conn) + .map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?; + + let wins = player.won_count(&mut conn) + .map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?; + + let player_o = PlayerO::from(player); + + Ok(Json(PlayerOO { + id: player_o.id, + telegram_id: player_o.telegram_id, + username: player_o.username, + human_score: player_o.human_score, + played, + wins, + })) } #[axum::debug_handler] pub async fn get_by_telegram_id( Path(telegram_id): Path, ) - -> Result, StatusCode> + -> Result, StatusCode> { let mut conn = PgConnection::establish(config::DATABASE_URL()) .map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?; @@ -72,5 +98,20 @@ pub async fn get_by_telegram_id( .map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)? .ok_or(StatusCode::NOT_FOUND)?; - Ok(Json(player.into())) + let played = player.played_count(&mut conn) + .map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?; + + let wins = player.won_count(&mut conn) + .map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?; + + let player_o = PlayerO::from(player); + + Ok(Json(PlayerOO { + id: player_o.id, + telegram_id: player_o.telegram_id, + username: player_o.username, + human_score: player_o.human_score, + played, + wins, + })) } diff --git a/holycow_frontend/src/app/[telegramId]/profile/page.tsx b/holycow_frontend/src/app/[telegramId]/profile/page.tsx index 8e4f877..8a74d6e 100644 --- a/holycow_frontend/src/app/[telegramId]/profile/page.tsx +++ b/holycow_frontend/src/app/[telegramId]/profile/page.tsx @@ -1,3 +1,14 @@ -export default async function Page() { +import {ProfileBox} from "@/components/ProfileBox" +import {PlayerOO} from "@/holycow" + +export default async function Page({params: {telegramId}}) { + const playerResponse = await fetch(`${process.env.BASE_URL}/api/results/telegram/${telegramId}`) + const player: PlayerOO = await playerResponse.json() + + return ( + + ) } \ No newline at end of file diff --git a/holycow_frontend/src/app/layout.css b/holycow_frontend/src/app/layout.css index 7a167a5..501384a 100644 --- a/holycow_frontend/src/app/layout.css +++ b/holycow_frontend/src/app/layout.css @@ -18,3 +18,7 @@ hr { /* TODO: Fix in bluelib */ align-items: center; } + +.fof { + text-align: center; +} \ No newline at end of file diff --git a/holycow_frontend/src/app/layout.tsx b/holycow_frontend/src/app/layout.tsx index e4c2c8b..a0e82c4 100644 --- a/holycow_frontend/src/app/layout.tsx +++ b/holycow_frontend/src/app/layout.tsx @@ -28,7 +28,13 @@ export default function RootLayout({ children }) {

- Stagione 1 + + Stagione 1: + +
+ + Standard Brawl +

@@ -38,7 +44,9 @@ export default function RootLayout({ children }) {

© Stefano Pigozzi  -  - che cursata, non ha senso, che cursata + Star Shard +  -  + che cursata le miniapp di telegram

diff --git a/holycow_frontend/src/app/none/page.tsx b/holycow_frontend/src/app/none/page.tsx new file mode 100644 index 0000000..1cdd6ca --- /dev/null +++ b/holycow_frontend/src/app/none/page.tsx @@ -0,0 +1,19 @@ +import {ProfileBox} from "@/components/ProfileBox" +import {PlayerOO} from "@/holycow" + + +export default async function Page() { + return ( +
+

+ 👍 +

+

+ hai scoperto la pagina di tutti i tempi +

+

+ congrats!! +

+
+ ) +} \ No newline at end of file diff --git a/holycow_frontend/src/app/page.tsx b/holycow_frontend/src/app/page.tsx index 63c34de..e52354a 100644 --- a/holycow_frontend/src/app/page.tsx +++ b/holycow_frontend/src/app/page.tsx @@ -14,8 +14,6 @@ export default function Page() { useEffect( () => { switch(startParam) { - case undefined: - return case "profile": router.replace(`/${data.user.id}/profile`) return @@ -23,7 +21,7 @@ export default function Page() { router.replace(`/${data.user.id}/report`) return default: - router.replace(`/error404`) + router.replace(`/none`) return } }, @@ -32,7 +30,7 @@ export default function Page() { return ( - Connecting to Telegram... + Connessione a Telegram in corso... ) } diff --git a/holycow_frontend/src/components/ProfileBox.tsx b/holycow_frontend/src/components/ProfileBox.tsx index d2ece1c..68e6394 100644 --- a/holycow_frontend/src/components/ProfileBox.tsx +++ b/holycow_frontend/src/components/ProfileBox.tsx @@ -1,81 +1,57 @@ import {StatPanel} from "@/components/StatPanel" +import {PlayerOO} from "@/holycow" import classNames from "classnames" export type ProfileBoxProps = { - userName: string, - played: number, - won: number, - rating: number, - uncertainty: number, + player: PlayerOO } -export function ProfileBox({userName, played, won, rating, uncertainty}: ProfileBoxProps) { +export function ProfileBox({player}: ProfileBoxProps) { return (

- {userName} + {player.username}

- {played} + + {player.played} )} /> - {won} + + {player.wins} )} /> - - {rating === 0 - ? - <> - - - - - - : - <> - - {rating} - - - ± - - - {uncertainty} - - - } - - )} - /> + {player.human_score !== null && + + + {player.human_score} + + + )} + /> + }
diff --git a/holycow_frontend/src/components/StatPanel.tsx b/holycow_frontend/src/components/StatPanel.tsx index ceb9808..cf32698 100644 --- a/holycow_frontend/src/components/StatPanel.tsx +++ b/holycow_frontend/src/components/StatPanel.tsx @@ -1,9 +1,9 @@ import classNames from "classnames"; import style from "./StatPanel.module.css" -export function StatPanel({name, value, display = value}) { +export function StatPanel({className, name, value, display = value}) { return ( -
+

{name}

diff --git a/holycow_frontend/src/holycow.ts b/holycow_frontend/src/holycow.ts index 97ff8da..7f0caec 100644 --- a/holycow_frontend/src/holycow.ts +++ b/holycow_frontend/src/holycow.ts @@ -10,3 +10,12 @@ export type PlayerO = { username: string, human_score: null | number, } + +export type PlayerOO = { + id: number, + telegram_id: number, + username: string, + human_score: null | number, + played: number, + wins: number, +}