diff --git a/packages/client/src/main.rs b/packages/client/src/main.rs index f513880..63701ed 100644 --- a/packages/client/src/main.rs +++ b/packages/client/src/main.rs @@ -81,7 +81,7 @@ fn main() { ); #[cfg(target_arch = "wasm32")] - wasm_logger::init(wasm_logger::Config::default()); + // wasm_logger::init(wasm_logger::Config::default()); dioxus_web::launch(app); } diff --git a/packages/client/src/pages/game.rs b/packages/client/src/pages/game.rs index 7edc517..20dd245 100644 --- a/packages/client/src/pages/game.rs +++ b/packages/client/src/pages/game.rs @@ -120,6 +120,15 @@ pub fn Game(cx: Scope) -> Element { } }); }; + + let restart_game = |_| -> () { + board_data.set(get_initial_board_data()); + game_status.set(GameStatus::Playing); + + // reset game uuid + let uuid = Uuid::new_v4(); + LocalStorage::set("uuid", uuid.to_string()).unwrap(); + }; let total_score = board_data.get().iter().flatten().sum::(); let highest_score = board_data.get().iter().flatten().max().unwrap_or(&0); @@ -157,13 +166,9 @@ pub fn Game(cx: Scope) -> Element { } button { class: "bg-blue-500 hover:bg-blue-700 text-white py-2 px-4 rounded", - onclick: |_| { - board_data.set(get_initial_board_data()); - game_status.set(GameStatus::Playing); - }, + onclick: restart_game, "Restart" } - } ), GameStatus::Fail => rsx!( div { class: "flex flex-col items-center mt-8", @@ -173,10 +178,7 @@ pub fn Game(cx: Scope) -> Element { } button { class: "btn btn-primary mt-4", - onclick: |_| { - board_data.set(get_initial_board_data()); - game_status.set(GameStatus::Playing); - }, + onclick: restart_game, "Restart" } } ), diff --git a/packages/server/src/main.rs b/packages/server/src/main.rs index d357a11..64b7395 100644 --- a/packages/server/src/main.rs +++ b/packages/server/src/main.rs @@ -14,7 +14,10 @@ use shared::types::{ GetProgressResponse, SaveProgressResponse, ProgressReqeust, - DbBoard}; + DbBoard, + LeaderboardRow, + LeaderboardResponse +}; use shared::logic::get_initial_board_data; use postgrest::Postgrest; use dotenv::dotenv; @@ -54,6 +57,7 @@ async fn main() { let app = Router::new() .route("/", get(ping)) .route("/progress", get(get_user_progress).post(save_user_progress)) + .route("/leaderboard", get(get_leaderboard)) .layer( tower_http::cors::CorsLayer::new() .allow_origin(Any) @@ -81,9 +85,19 @@ async fn save_user_progress( Some(uuid) => { let client = &app_state.supabase_client; let board_str = serde_json::to_string(&input.board).unwrap(); - let s = format!("[{{ \"uuid\": \"{}\", \"progress\": {} }}]", + let score = input.board + .iter() + .fold(0, |acc, row| { + acc + row.iter().fold(0, |acc, cell| { + acc + cell + }) + }); + + let s = format!("[{{ \"uuid\": \"{}\", \"progress\": {}, \"score\": {} }}]", uuid, - board_str); + board_str, + score); + match client .from("user_progress") .upsert(s) @@ -152,3 +166,40 @@ fn get_default_progress() -> (StatusCode, Json) { board: Some(get_initial_board_data()), })) } + + +async fn get_leaderboard( + State(app_state): State>, +) -> impl IntoResponse { + let client = &app_state.supabase_client; + + match client + .from("user_progress") + .select("*") + .order("score.desc") + .limit(20) + .execute() + .await { + Ok(res) => match res.text().await { + Ok(resp) => { + let leadboard_data: Vec = serde_json::from_str(&resp).unwrap(); + (StatusCode::OK, Json(LeaderboardResponse { + success: true, + leaderboard: leadboard_data + })) + }, + Err(_) => { + (StatusCode::OK, Json(LeaderboardResponse { + success: false, + leaderboard: vec![] + })) + } + } + Err(_) => { + (StatusCode::OK, Json(LeaderboardResponse { + success: false, + leaderboard: vec![] + })) + }, + } +} \ No newline at end of file diff --git a/packages/server/src/sql/schema.sql b/packages/server/src/sql/schema.sql index 9bdf018..f40f887 100644 --- a/packages/server/src/sql/schema.sql +++ b/packages/server/src/sql/schema.sql @@ -1,10 +1,7 @@ CREATE TABLE `user_progress` ( `uuid` VARCHAR(36) NOT NULL, `progress` JSON, + `score` INT, + `user_name` VARCHAR(36), PRIMARY KEY (`uuid`) ); - -CREATE TABLE `user_score` ( - `name` VARCHAR(36) NOT NULL, - `score` INT NOT NULL -); \ No newline at end of file diff --git a/packages/shared/src/types.rs b/packages/shared/src/types.rs index ad677a1..2ef2d9c 100644 --- a/packages/shared/src/types.rs +++ b/packages/shared/src/types.rs @@ -56,4 +56,15 @@ pub struct DbBoardRow { } pub type DbBoard = Vec; - \ No newline at end of file + +#[derive(Debug, Serialize, Deserialize)] +pub struct LeaderboardRow { + pub uuid: String, + pub score: i32, +} + +#[derive(Debug, Serialize, Deserialize)] +pub struct LeaderboardResponse { + pub success: bool, + pub leaderboard: Vec, +} \ No newline at end of file