Skip to content

Commit

Permalink
update api
Browse files Browse the repository at this point in the history
  • Loading branch information
LIU9293 committed Jul 12, 2023
1 parent 4435256 commit c0fa79c
Show file tree
Hide file tree
Showing 14 changed files with 252 additions and 99 deletions.
2 changes: 2 additions & 0 deletions .env
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
SUPABASE_API_KEY=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZSIsInJlZiI6ImV5YXFndHVueXhvZHh5YWN4cG5rIiwicm9sZSI6ImFub24iLCJpYXQiOjE2ODkwNzg5MzYsImV4cCI6MjAwNDY1NDkzNn0.4gXuwv8BfQ0-MYuj4J_MbE_h5WyJKxRY7YlIGJYMSC8
SUPABASE_URL=https://eyaqgtunyxodxyacxpnk.supabase.co
1 change: 1 addition & 0 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
}

if (counter === 1) {
e.preventDefault();
counter = 0
new_e = new e.constructor(e.type, e);
gamearea.dispatchEvent(new_e);
Expand Down
2 changes: 2 additions & 0 deletions packages/client/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ edition = "2021"
[dependencies]
rand = "0.8.5"
getrandom = { version = "0.2", features = ["js"] }
uuid = { version = "1.2.2", features = ["v4", "serde"] }
futures = "0.3"
reqwest = { version = "0.11", features = ["json"] }
serde = { version = "1.0", features = ["derive"] }
Expand All @@ -16,6 +17,7 @@ log = "0.4"
shared = { path = "../shared" }
dioxus-html-macro = "0.3.0"
fermi = "0.3.0"
gloo-storage = "0.2.2"

[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
dioxus = "0.3.0"
Expand Down
6 changes: 2 additions & 4 deletions packages/client/src/components/header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use dioxus::prelude::*;
use dioxus::html::MouseEvent;
use fermi::*;
use dioxus_html_macro::html;
use shared::translate::{TRANSLATION};
use shared::translate::TRANSLATION;

#[cfg(not(target_arch = "wasm32"))]
use dioxus_desktop::use_eval;
Expand Down Expand Up @@ -71,9 +71,7 @@ pub fn LanguageButton<'a> (cx: Scope<'a, LanguageButtonProps<'a>>) -> Element<'a
cx.render(rsx!(
li {
onclick: move |evt| cx.props.onclick.call(evt),
a {
cx.props.language.clone()
}
a { cx.props.language.clone() }
}
))
}
Expand Down
1 change: 1 addition & 0 deletions packages/client/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ fn main() {
);

#[cfg(target_arch = "wasm32")]
wasm_logger::init(wasm_logger::Config::default());
dioxus_web::launch(app);
}

Expand Down
103 changes: 69 additions & 34 deletions packages/client/src/pages/game.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,50 +5,76 @@ use dioxus::html::KeyboardEvent;
use shared::types::{Board, GameStatus, ProgressReqeust};
use reqwest;
use shared::logic::{get_initial_board_data, add_random, move_up, move_down, move_left, move_right, check_and_do_next};
use log;
use uuid::Uuid;
use crate::components::row::Row;
use shared::translate::TRANSLATION;
use gloo_storage::{LocalStorage, Storage, errors::StorageError};

pub fn Game(cx: Scope) -> Element {
let game_status = use_state(cx, || GameStatus::Playing);
let board_data: &UseState<Board> = use_state(cx, || get_initial_board_data());
let is_first_load = use_state(cx, || true);
let translator = use_read(cx, TRANSLATION);

use_effect(cx, (is_first_load), |_| async move {
let a: Result<String, StorageError> = LocalStorage::get("uuid");
match a {
Ok(uuid) => {
log::info!("Found uuid: {}", uuid);
},
StorageError => {
log::info!("No uuid found, creating one...");
let uuid = Uuid::new_v4();
LocalStorage::set("uuid", uuid.to_string()).unwrap();
}
};
});

use_effect(cx, (is_first_load, board_data, game_status), |(is_first_load, board_data, game_status)| async move {
if !is_first_load.get() {
return;
}

let client = reqwest::Client::new();
let res = client.get("http://localhost:3000/progress").send().await;
match res {
Ok(response) => {
let payload = response.json::<ProgressReqeust>().await;
match payload {
Ok(data) => {
is_first_load.set(false);
board_data.set(data.board);

match check_and_do_next(&data.board) {
GameStatus::Win => {
game_status.set(GameStatus::Win);
},
GameStatus::Fail => {
game_status.set(GameStatus::Fail);
let a: Result<String, StorageError> = LocalStorage::get("uuid");
match a {
Ok(uuid) => {
let client = reqwest::Client::new();
let url = format!("http://localhost:3000/progress?uuid={uuid}", uuid=uuid);
let res = client.get(url).send().await;
match res {
Ok(response) => {
let payload = response.json::<ProgressReqeust>().await;
match payload {
Ok(data) => {
is_first_load.set(false);
board_data.set(data.board);

match check_and_do_next(&data.board) {
GameStatus::Win => {
game_status.set(GameStatus::Win);
},
GameStatus::Fail => {
game_status.set(GameStatus::Fail);
},
GameStatus::Playing => { game_status.set(GameStatus::Playing); },
}
},
GameStatus::Playing => { game_status.set(GameStatus::Playing); },
Err(err) => {
log::error!("Failed to parse JSON: {}", err);
}
}
},
Err(err) => {
log::error!("Failed to parse JSON: {}", err);
log::error!("Failed to send request: {}", err);
}
}
},
Err(err) => {
log::error!("Failed to send request: {}", err);
StorageError => {
log::info!("No uuid found, creating one...");
let uuid = Uuid::new_v4();
LocalStorage::set("uuid", uuid.to_string()).unwrap();
}
}
};
});

let handle_key_down_event = move |evt: KeyboardEvent| -> () {
Expand Down Expand Up @@ -82,18 +108,27 @@ pub fn Game(cx: Scope) -> Element {

cx.spawn({
async move {
let client = reqwest::Client::new();
let res = client.post("http://localhost:3000/progress")
.json(&ProgressReqeust {
board: new_data
})
.send()
.await;

match res {
Ok(_) => {},
Err(err) => {
log::error!("Failed to record progress: {}", err);
let a: Result<String, StorageError> = LocalStorage::get("uuid");
match a {
Ok(uuid) => {
let client = reqwest::Client::new();
let res = client.post("http://localhost:3000/progress")
.json(&ProgressReqeust {
board: new_data,
uuid: Some(uuid),
})
.send()
.await;

match res {
Ok(_) => {},
Err(err) => {
log::error!("Failed to record progress: {}", err);
}
}
},
StorageError => {
log::info!("No uuid found, skip...");
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion packages/client/src/pages/homepage.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use fermi::*;
use dioxus::prelude::*;
use dioxus_router::{Link};
use dioxus_router::Link;
use dioxus_html_macro::html;
use shared::translate::TRANSLATION;

Expand Down
4 changes: 3 additions & 1 deletion packages/server/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,6 @@ tokio = { version = "1.0", features = ["full"] }
tower-http = { version = "0.3.0", features = ["cors"] }
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
shared = { path = "../shared" }
shared = { path = "../shared" }
postgrest = "1.0"
dotenv = "0.15.0"
Loading

0 comments on commit c0fa79c

Please sign in to comment.