M client/src/api/user/login.rs => client/src/api/user/login.rs +5 -29
@@ 6,33 6,13 @@
* See LICENSE for licensing information.
*/
-use serde::{Deserialize, Serialize};
+use serde::Serialize;
use crate::{NetworkingOptions, post_request};
-use super::User;
+use super::UserToken;
-#[derive(Deserialize)]
-struct LoginResponseJson {
- id: String,
- created_at: String,
- updated_at: String,
- username: String,
- token: String,
-}
-
-impl LoginResponseJson {
- fn to_user(&self) -> User {
- User {
- id: self.id.clone(),
- created_at: self.created_at.clone(),
- updated_at: self.updated_at.clone(),
- username: self.username.clone(),
- }
- }
-}
-
-pub type LoginResponse = Result<User, ()>;
+pub type LoginResponse = Result<UserToken, ()>;
#[derive(Serialize)]
struct LoginPost {
@@ 40,15 20,11 @@ struct LoginPost {
password: String,
}
-pub fn login(no: &mut NetworkingOptions, email: &str, password: &str) -> LoginResponse {
+pub fn login(no: &NetworkingOptions, email: &str, password: &str) -> LoginResponse {
let res = post_request!(no, "/user/token", &LoginPost {
email: email.to_string(),
password: password.to_string(),
});
- let tmp: LoginResponseJson = res.json().unwrap();
-
- no.user_token = tmp.token.clone();
-
- Ok(tmp.to_user())
+ Ok(res.json().unwrap())
}
M client/src/api/user/mod.rs => client/src/api/user/mod.rs +9 -0
@@ 21,3 21,12 @@ pub struct User {
pub updated_at: String,
pub username: String,
}
+
+#[derive(Deserialize)]
+pub struct UserToken {
+ pub id: String,
+ pub created_at: String,
+ pub updated_at: String,
+ pub username: String,
+ pub token: String,
+}
M client/src/macros/async_task.rs => client/src/macros/async_task.rs +23 -1
@@ 33,7 33,7 @@ macro_rules! async_task_start_call {
for ev in start_ev_r.iter() {
let thread_pool = AsyncComputeTaskPool::get();
let $ev = ev.clone();
- let mut $no = no.clone();
+ let $no = no.clone();
let task = thread_pool.spawn(async move {
$func
});
@@ 88,4 88,26 @@ macro_rules! async_task_handle_call {
}
};
+ ($call_type:ty, |$response:ident, $menu_data:ident, $no:ident| $handler_func:expr) => {
+ pub fn handle_call(
+ mut commands: Commands,
+ mut tasks: Query<(Entity, &mut $call_type)>,
+ mut $menu_data: ResMut<MenuData>,
+ mut $no: ResMut<NetworkingOptions>,
+ ) {
+ match tasks.get_single_mut() {
+ Ok((entity, mut task)) => {
+ if let Some($response) = future::block_on(future::poll_once(&mut task.0)) {
+ $handler_func;
+
+ // remove the task
+ commands.entity(entity).remove::<$call_type>();
+ commands.entity(entity).despawn_recursive();
+ }
+ }
+ // NOTE: don't do anything if the wanted thingy doesn't exist
+ _ => {}
+ }
+ }
+ };
}
M client/src/plugins/async_tasks/req_game_create.rs => client/src/plugins/async_tasks/req_game_create.rs +3 -3
@@ 21,13 21,13 @@ pub struct GameCreateCall(Task<CreateResponse>);
#[derive(Clone)]
pub struct GameCreateCallEvent;
-async_task_start_call!(GameCreateCallEvent, GameCreateCall, |ev, no| {
- let res = api::game::create(&mut no);
+async_task_start_call!(GameCreateCallEvent, GameCreateCall, |_ev, no| {
+ let res = api::game::create(&no);
res
});
-async_task_handle_call!(GameCreateCall, |response, menu_data| {
+async_task_handle_call!(GameCreateCall, |response, _menu_data| {
match response {
Err(_err) => panic!("game create failed, handle me"),
Ok(resp) => {
M client/src/plugins/async_tasks/req_game_forming.rs => client/src/plugins/async_tasks/req_game_forming.rs +1 -1
@@ 22,7 22,7 @@ pub struct GameFormingCall(Task<FormingResponse>);
pub struct GameFormingCallEvent;
async_task_start_call!(GameFormingCallEvent, GameFormingCall, |_ev, no| {
- let res = api::game::forming(&mut no);
+ let res = api::game::forming(&no);
res
});
M client/src/plugins/async_tasks/req_game_mygames.rs => client/src/plugins/async_tasks/req_game_mygames.rs +1 -1
@@ 22,7 22,7 @@ pub struct GameMyGamesCall(Task<MyGamesResponse>);
pub struct GameMyGamesCallEvent;
async_task_start_call!(GameMyGamesCallEvent, GameMyGamesCall, |_ev, no| {
- let res = api::game::my_games(&mut no);
+ let res = api::game::my_games(&no);
res
});
M client/src/plugins/async_tasks/req_login.rs => client/src/plugins/async_tasks/req_login.rs +3 -2
@@ 25,16 25,17 @@ pub struct LoginCallEvent {
}
async_task_start_call!(LoginCallEvent, LoginCall, |ev, no| {
- let res = api::user::login(&mut no, &ev.email, &ev.password);
+ let res = api::user::login(&no, &ev.email, &ev.password);
res
});
-async_task_handle_call!(LoginCall, |response, menu_data| {
+async_task_handle_call!(LoginCall, |response, menu_data, no| {
match response {
Err(_err) => panic!("login failed, handle me"),
Ok(resp) => {
info!("logged in {}", resp.username);
+ no.user_token = resp.token;
menu_data.ui_state = MenuUIState::Main;
}
}
M client/src/plugins/async_tasks/req_register.rs => client/src/plugins/async_tasks/req_register.rs +1 -1
@@ 26,7 26,7 @@ pub struct RegisterCallEvent {
}
async_task_start_call!(RegisterCallEvent, RegisterCall, |ev, no| {
- let res = api::user::register(&mut no, &ev.username, &ev.email, &ev.password);
+ let res = api::user::register(&no, &ev.username, &ev.email, &ev.password);
res
});