cryps list

This commit is contained in:
ntr
2018-09-24 23:18:15 +10:00
parent 25058e185c
commit 162ddd1fb4
12 changed files with 119 additions and 53 deletions
+35 -5
View File
@@ -3,12 +3,16 @@ use bcrypt::{hash, verify};
use rand::{thread_rng, Rng};
use rand::distributions::Alphanumeric;
use std::iter;
use serde_cbor::{from_slice, to_vec};
use std::str;
use net::Db;
use rpc::{AccountCreateParams, AccountLoginParams, RpcResult};
use cryp::Cryp;
use failure::Error;
use failure::err_msg;
@@ -55,7 +59,7 @@ pub fn from_token(token: String, db: &Db) -> Result<Account, Error> {
return Ok(entry);
}
pub fn create(params: AccountCreateParams, db: Db) -> Result<RpcResult, Error> {
pub fn create(params: AccountCreateParams, db: Db) -> Result<Account, Error> {
let id = Uuid::new_v4();
if params.password.len() < PASSWORD_MIN_LEN {
@@ -99,10 +103,10 @@ pub fn create(params: AccountCreateParams, db: Db) -> Result<RpcResult, Error> {
tx.commit()?;
return Ok(RpcResult::Account(entry));
return Ok(entry);
}
pub fn login(params: AccountLoginParams, db: Db) -> Result<RpcResult, Error> {
pub fn login(params: AccountLoginParams, db: Db) -> Result<Account, Error> {
let query = "
SELECT id, name, token, password
FROM accounts
@@ -142,5 +146,31 @@ pub fn login(params: AccountLoginParams, db: Db) -> Result<RpcResult, Error> {
token: entry.token,
};
return Ok(RpcResult::Account(account));
}
return Ok(account);
}
pub fn fetch_cryps(db: Db, account: Account) -> Result<Vec<Cryp>, Error> {
let query = "
SELECT data
FROM cryps
WHERE account = $1;
";
let result = db
.query(query, &[&account.id])?;
let cryps: Result<Vec<Cryp>, _> = result.iter().map(|row| {
let cryp_bytes: Vec<u8> = row.get(0);
from_slice::<Cryp>(&cryp_bytes)
}).collect();
// catch any errors
if cryps.is_err() {
return Err(err_msg("could not deserialize a cryp"));
}
// now unwrap is safe
return Ok(cryps.unwrap());
}
+1 -4
View File
@@ -121,10 +121,7 @@ where F: Fn(&Battle) -> Result<(), Error>
loop {
battle.next();
match send(&battle) {
Err(e) => break Err(err_msg("could not reply")),
_ => (),
};
send(&battle)?;
if battle.finished() {
break Ok(battle)
+1 -2
View File
@@ -1,7 +1,7 @@
use uuid::Uuid;
use rand::prelude::*;
use serde_cbor::*;
use serde_cbor::{to_vec};
use serde_cbor::{from_slice, to_vec};
use failure::Error;
use failure::err_msg;
@@ -217,7 +217,6 @@ pub fn spawn(params: CrypSpawnParams, db: Db, account: Account) -> Result<Cryp,
return Ok(cryp);
}
#[cfg(test)]
mod tests {
use cryp::*;
+19 -4
View File
@@ -8,7 +8,7 @@ use net::Db;
use cryp::{Cryp, spawn};
use battle::{Battle};
use combat::{pve};
use account::{Account, create, login, from_token};
use account::{Account, create, login, from_token, fetch_cryps};
pub struct Rpc;
@@ -75,7 +75,7 @@ impl Rpc {
match from_slice::<AccountCreateMsg>(&data) {
Ok(v) => Ok(RpcResponse {
method: v.method,
params: create(v.params, db)?
params: RpcResult::Account(create(v.params, db)?)
}),
Err(_e) => Err(err_msg("invalid params")),
}
@@ -84,12 +84,20 @@ impl Rpc {
match from_slice::<AccountLoginMsg>(&data) {
Ok(v) => Ok(RpcResponse {
method: v.method,
params: login(v.params, db)?
params: RpcResult::Account(login(v.params, db)?)
}),
Err(_e) => Err(err_msg("invalid params")),
}
},
"account_cryps" => {
match account {
Some(u) => Ok(RpcResponse {
method: v.method,
params: RpcResult::CrypList(fetch_cryps(db, u)?)
}),
None => Err(err_msg("auth required")),
}
},
_ => Err(err_msg("unknown method")),
}
},
@@ -108,6 +116,7 @@ pub struct RpcResponse {
pub enum RpcResult {
SpawnCryp(Cryp),
Account(Account),
CrypList(Vec<Cryp>),
Pve(Battle),
}
@@ -163,6 +172,12 @@ pub struct AccountLoginParams {
pub password: String,
}
#[derive(Debug,Clone,Serialize,Deserialize)]
struct AccountCrypsMsg {
method: String,
params: (),
}
// #[cfg(test)]
// mod tests {
// use super::*;