From e566bbc7e920aeada4fa1d361c26ded7773a7ff2 Mon Sep 17 00:00:00 2001 From: ntr Date: Fri, 23 Nov 2018 13:33:16 +1100 Subject: [PATCH 1/2] handle auth early --- server/WORKLOG.md | 24 ------ server/src/rpc.rs | 187 +++++++++++++++++----------------------------- 2 files changed, 69 insertions(+), 142 deletions(-) diff --git a/server/WORKLOG.md b/server/WORKLOG.md index 3a5237b4..3f304363 100755 --- a/server/WORKLOG.md +++ b/server/WORKLOG.md @@ -11,9 +11,6 @@ * Global rolls -* Stats - * Scrabble grid - * skills * handle setting account better maybe? * ensure cryp untargetable and doesn't resolve when KO @@ -22,8 +19,6 @@ * private fields for opponents * attack * can you attack yourself? - * fetch existing battles - * check for game participation * write players row for every team+cryp added * return results<> * defensive @@ -40,25 +35,6 @@ * Bosses - - * rez ✔ -* move rpc functions out ✔ -* cooldowns reduce each turn ✔ -* statuses reduce each turn ✔ -* teach cyps skills ✔ -* spell/phys dmg ✔ -* offensive -> choose target ✔ -* ws reconnect ✔ -* Levelling ✔ -* Logins ✔️ - * Cryp Ownership ✔ -* Matchmaking ✔ - * Lobbies ✔ - * Create ✔ - * Join ✔ - * Resolve ✔ - - # Db maintenance * delete games when a cryp is deleted * does this need to happen? can have historical games diff --git a/server/src/rpc.rs b/server/src/rpc.rs index 77459d88..d2f91bb4 100755 --- a/server/src/rpc.rs +++ b/server/src/rpc.rs @@ -33,26 +33,38 @@ impl Rpc { None => None, }; - println!("{:?}", account); + // check the method + // if no auth required + match v.method.as_ref() { + "account_create" => (), + "account_login" => (), + _ => match account { + Some(_) => (), + None => return Err(err_msg("auth required")), + }, + }; // now we have the method name // match on that to determine what fn to call let response = match v.method.as_ref() { - "cryp_spawn" => Rpc::cryp_spawn(data, &mut tx, account, client), - "cryp_learn" => Rpc::cryp_learn(data, &mut tx, account, client), - "cryp_forget" => Rpc::cryp_forget(data, &mut tx, account, client), - "game_state" => Rpc::game_state(data, &mut tx, account, client), - "game_pve" => Rpc::game_pve(data, &mut tx, account, client), - "game_pvp" => Rpc::game_pvp(data, &mut tx, account, client), - "game_join" => Rpc::game_join(data, &mut tx, account, client), - "game_joinable_list" => Rpc::game_joinable_list(data, &mut tx, account, client), - "game_skill" => Rpc::game_skill(data, &mut tx, account, client), - "game_target" => Rpc::game_target(data, &mut tx, account, client), - "account_create" => Rpc::account_create(data, &mut tx, account, client), - "account_login" => Rpc::account_login(data, &mut tx, account, client), - "account_cryps" => Rpc::account_cryps(data, &mut tx, account, client), - "item_list" => Rpc::item_list(data, &mut tx, account, client), - "item_use" => Rpc::item_use(data, &mut tx, account, client), + // no auth methods + "account_create" => Rpc::account_create(data, &mut tx, client), + "account_login" => Rpc::account_login(data, &mut tx, client), + + // auth methods + "cryp_spawn" => Rpc::cryp_spawn(data, &mut tx, account.unwrap(), client), + "cryp_learn" => Rpc::cryp_learn(data, &mut tx, account.unwrap(), client), + "cryp_forget" => Rpc::cryp_forget(data, &mut tx, account.unwrap(), client), + "game_state" => Rpc::game_state(data, &mut tx, account.unwrap(), client), + "game_pve" => Rpc::game_pve(data, &mut tx, account.unwrap(), client), + "game_pvp" => Rpc::game_pvp(data, &mut tx, account.unwrap(), client), + "game_join" => Rpc::game_join(data, &mut tx, account.unwrap(), client), + "game_joinable_list" => Rpc::game_joinable_list(data, &mut tx, account.unwrap(), client), + "game_skill" => Rpc::game_skill(data, &mut tx, account.unwrap(), client), + "game_target" => Rpc::game_target(data, &mut tx, account.unwrap(), client), + "account_cryps" => Rpc::account_cryps(data, &mut tx, account.unwrap(), client), + "item_list" => Rpc::item_list(data, &mut tx, account.unwrap(), client), + "item_use" => Rpc::item_use(data, &mut tx, account.unwrap(), client), _ => Err(err_msg("unknown method")), }; @@ -73,88 +85,63 @@ impl Rpc { } } - fn game_state(data: Vec, tx: &mut Transaction, account: Option, _client: &mut WebSocket) -> Result { - let a = match account { - Some(a) => a, - None => return Err(err_msg("auth required")), - }; - + fn game_state(data: Vec, tx: &mut Transaction, account: Account, _client: &mut WebSocket) -> Result { let msg = from_slice::(&data).or(Err(err_msg("invalid params")))?; let game_response = RpcResponse { method: "game_state".to_string(), - params: RpcResult::GameState(game_state(msg.params, tx, &a)?) + params: RpcResult::GameState(game_state(msg.params, tx, &account)?) }; return Ok(game_response); } - fn game_pve(data: Vec, tx: &mut Transaction, account: Option, client: &mut WebSocket) -> Result { - let a = match account { - Some(a) => a, - None => return Err(err_msg("auth required")), - }; - + fn game_pve(data: Vec, tx: &mut Transaction, account: Account, client: &mut WebSocket) -> Result { let msg = from_slice::(&data).or(Err(err_msg("invalid params")))?; let game_response = RpcResponse { method: "game_state".to_string(), - params: RpcResult::GameState(game_pve(msg.params, tx, &a)?) + params: RpcResult::GameState(game_pve(msg.params, tx, &account)?) }; Rpc::send_msg(client, RpcResponse { method: "account_cryps".to_string(), - params: RpcResult::CrypList(account_cryps(tx, &a)?) + params: RpcResult::CrypList(account_cryps(tx, &account)?) })?; return Ok(game_response); } - fn game_pvp(data: Vec, tx: &mut Transaction, account: Option, _client: &mut WebSocket) -> Result { - let a = match account { - Some(a) => a, - None => return Err(err_msg("auth required")), - }; - + fn game_pvp(data: Vec, tx: &mut Transaction, account: Account, _client: &mut WebSocket) -> Result { let msg = from_slice::(&data).or(Err(err_msg("invalid params")))?; let game_response = RpcResponse { method: "game_state".to_string(), - params: RpcResult::GameState(game_pvp(msg.params, tx, &a)?) + params: RpcResult::GameState(game_pvp(msg.params, tx, &account)?) }; return Ok(game_response); } - fn game_join(data: Vec, tx: &mut Transaction, account: Option, _client: &mut WebSocket) -> Result { - let a = match account { - Some(a) => a, - None => return Err(err_msg("auth required")), - }; - + fn game_join(data: Vec, tx: &mut Transaction, account: Account, _client: &mut WebSocket) -> Result { let msg = from_slice::(&data).or(Err(err_msg("invalid params")))?; let game_response = RpcResponse { method: "game_state".to_string(), - params: RpcResult::GameState(game_join(msg.params, tx, &a)?) + params: RpcResult::GameState(game_join(msg.params, tx, &account)?) }; return Ok(game_response); } - fn game_joinable_list(_data: Vec, tx: &mut Transaction, account: Option, _client: &mut WebSocket) -> Result { - let a = match account { - Some(a) => a, - None => return Err(err_msg("auth required")), - }; - + fn game_joinable_list(_data: Vec, tx: &mut Transaction, account: Account, _client: &mut WebSocket) -> Result { // let msg = from_slice::(&data).or(Err(err_msg("invalid params")))?; let game_list = RpcResponse { method: "game_joinable_list".to_string(), - params: RpcResult::GameJoinableList(game_joinable_list(tx, &a)?) + params: RpcResult::GameJoinableList(game_joinable_list(tx, &account)?) }; return Ok(game_list); @@ -162,114 +149,89 @@ impl Rpc { - fn game_skill(data: Vec, tx: &mut Transaction, account: Option, _client: &mut WebSocket) -> Result { - let a = match account { - Some(a) => a, - None => return Err(err_msg("auth required")), - }; - + fn game_skill(data: Vec, tx: &mut Transaction, account: Account, _client: &mut WebSocket) -> Result { let msg = from_slice::(&data).or(Err(err_msg("invalid params")))?; let game_response = RpcResponse { method: "game_state".to_string(), - params: RpcResult::GameState(game_skill(msg.params, tx, &a)?) + params: RpcResult::GameState(game_skill(msg.params, tx, &account)?) }; // Rpc::send_msg(client, RpcResponse { // method: "account_cryps".to_string(), - // params: RpcResult::CrypList(account_cryps(tx, &a)?) + // params: RpcResult::CrypList(account_cryps(tx, &account)?) // })?; return Ok(game_response); } - fn game_target(data: Vec, tx: &mut Transaction, account: Option, _client: &mut WebSocket) -> Result { - let a = match account { - Some(a) => a, - None => return Err(err_msg("auth required")), - }; - + fn game_target(data: Vec, tx: &mut Transaction, account: Account, _client: &mut WebSocket) -> Result { let msg = from_slice::(&data).or(Err(err_msg("invalid params")))?; let game_response = RpcResponse { method: "game_state".to_string(), - params: RpcResult::GameState(game_target(msg.params, tx, &a)?) + params: RpcResult::GameState(game_target(msg.params, tx, &account)?) }; // Rpc::send_msg(client, RpcResponse { // method: "account_cryps".to_string(), - // params: RpcResult::CrypList(account_cryps(tx, &a)?) + // params: RpcResult::CrypList(account_cryps(tx, &account)?) // })?; return Ok(game_response); } - fn cryp_spawn(data: Vec, tx: &mut Transaction, account: Option, client: &mut WebSocket) -> Result { - let a = match account { - Some(a) => a, - None => return Err(err_msg("auth required")), - }; - + fn cryp_spawn(data: Vec, tx: &mut Transaction, account: Account, client: &mut WebSocket) -> Result { let msg = from_slice::(&data).or(Err(err_msg("invalid params")))?; Rpc::send_msg(client, RpcResponse { method: "cryp_spawn".to_string(), - params: RpcResult::CrypSpawn(cryp_spawn(msg.params, tx, &a)?) + params: RpcResult::CrypSpawn(cryp_spawn(msg.params, tx, &account)?) })?; let cryp_list = RpcResponse { method: "account_cryps".to_string(), - params: RpcResult::CrypList(account_cryps(tx, &a)?) + params: RpcResult::CrypList(account_cryps(tx, &account)?) }; Ok(cryp_list) } - fn cryp_learn(data: Vec, tx: &mut Transaction, account: Option, client: &mut WebSocket) -> Result { - let a = match account { - Some(a) => a, - None => return Err(err_msg("auth required")), - }; - + fn cryp_learn(data: Vec, tx: &mut Transaction, account: Account, client: &mut WebSocket) -> Result { let msg = from_slice::(&data).or(Err(err_msg("invalid params")))?; Rpc::send_msg(client, RpcResponse { method: "cryp_learn".to_string(), - params: RpcResult::CrypLearn(cryp_learn(msg.params, tx, &a)?) + params: RpcResult::CrypLearn(cryp_learn(msg.params, tx, &account)?) })?; let cryp_list = RpcResponse { method: "account_cryps".to_string(), - params: RpcResult::CrypList(account_cryps(tx, &a)?) + params: RpcResult::CrypList(account_cryps(tx, &account)?) }; Ok(cryp_list) } - fn cryp_forget(data: Vec, tx: &mut Transaction, account: Option, client: &mut WebSocket) -> Result { - let a = match account { - Some(a) => a, - None => return Err(err_msg("auth required")), - }; - + fn cryp_forget(data: Vec, tx: &mut Transaction, account: Account, client: &mut WebSocket) -> Result { let msg = from_slice::(&data).or(Err(err_msg("invalid params")))?; Rpc::send_msg(client, RpcResponse { method: "cryp_forget".to_string(), - params: RpcResult::CrypForget(cryp_forget(msg.params, tx, &a)?) + params: RpcResult::CrypForget(cryp_forget(msg.params, tx, &account)?) })?; let cryp_list = RpcResponse { method: "account_cryps".to_string(), - params: RpcResult::CrypList(account_cryps(tx, &a)?) + params: RpcResult::CrypList(account_cryps(tx, &account)?) }; Ok(cryp_list) } - fn account_create(data: Vec, tx: &mut Transaction, _account: Option, _client: &mut WebSocket) -> Result { + fn account_create(data: Vec, tx: &mut Transaction, _client: &mut WebSocket) -> Result { match from_slice::(&data) { Ok(v) => Ok(RpcResponse { method: v.method, @@ -279,7 +241,7 @@ impl Rpc { } } - fn account_login(data: Vec, tx: &mut Transaction, _account: Option, _client: &mut WebSocket) -> Result { + fn account_login(data: Vec, tx: &mut Transaction, _client: &mut WebSocket) -> Result { match from_slice::(&data) { Ok(v) => Ok(RpcResponse { method: v.method, @@ -289,39 +251,28 @@ impl Rpc { } } - fn account_cryps(_data: Vec, tx: &mut Transaction, account: Option, _client: &mut WebSocket) -> Result { - match account { - Some(a) => Ok(RpcResponse { - method: "account_cryps".to_string(), - params: RpcResult::CrypList(account_cryps(tx, &a)?) - }), - None => Err(err_msg("auth required")), - } + fn account_cryps(_data: Vec, tx: &mut Transaction, account: Account, _client: &mut WebSocket) -> Result { + Ok(RpcResponse { + method: "account_cryps".to_string(), + params: RpcResult::CrypList(account_cryps(tx, &account)?) + }) } - fn item_list(_data: Vec, tx: &mut Transaction, account: Option, _client: &mut WebSocket) -> Result { - match account { - Some(a) => Ok(RpcResponse { - method: "item_list".to_string(), - params: RpcResult::ItemList(items_list(tx, &a)?) - }), - None => Err(err_msg("auth required")), - } + fn item_list(_data: Vec, tx: &mut Transaction, account: Account, _client: &mut WebSocket) -> Result { + Ok(RpcResponse { + method: "item_list".to_string(), + params: RpcResult::ItemList(items_list(tx, &account)?) + }) } - fn item_use(data: Vec, tx: &mut Transaction, account: Option, _client: &mut WebSocket) -> Result { - let a = match account { - Some(a) => a, - None => return Err(err_msg("auth required")), - }; - + fn item_use(data: Vec, tx: &mut Transaction, account: Account, _client: &mut WebSocket) -> Result { let msg = from_slice::(&data).or(Err(err_msg("invalid params")))?; - item_use(msg.params, tx, &a)?; + item_use(msg.params, tx, &account)?; let cryps_list = RpcResponse { method: "account_cryps".to_string(), - params: RpcResult::CrypList(account_cryps(tx, &a)?) + params: RpcResult::CrypList(account_cryps(tx, &account)?) }; return Ok(cryps_list); From fa603009a15d1c83d4436330bb51124ba7c4546e Mon Sep 17 00:00:00 2001 From: ntr Date: Fri, 23 Nov 2018 13:40:44 +1100 Subject: [PATCH 2/2] max skills --- server/src/cryp.rs | 6 ++++++ server/src/game.rs | 2 +- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/server/src/cryp.rs b/server/src/cryp.rs index 7a0316ab..d7b8f6bb 100755 --- a/server/src/cryp.rs +++ b/server/src/cryp.rs @@ -359,6 +359,12 @@ pub fn cryp_spawn(params: CrypSpawnParams, tx: &mut Transaction, account: &Accou pub fn cryp_learn(params: CrypLearnParams, tx: &mut Transaction, account: &Account) -> Result { let mut cryp = cryp_get(tx, params.id, account.id)?; + + let max_skills = 4; + if cryp.skills.len() >= max_skills { + return Err(format_err!("cryp at max skills ({:?})", max_skills)); + } + cryp = cryp.learn(params.skill); return cryp_write(cryp, tx); } diff --git a/server/src/game.rs b/server/src/game.rs index 726d309a..3c72f300 100755 --- a/server/src/game.rs +++ b/server/src/game.rs @@ -747,7 +747,7 @@ mod tests { use cryp::*; fn create_test_game() -> Game { - let x = Cryp::new() + let mut x = Cryp::new() .named(&"pronounced \"creeep\"".to_string()) .level(8) .learn(Skill::TestStun)