This commit is contained in:
ntr
2018-09-16 23:56:37 +10:00
parent 8fb7a3ae1e
commit a78b796c55
10 changed files with 209 additions and 90 deletions
+6 -1
View File
@@ -1,5 +1,5 @@
* Battling
* Logins
* Logins ✔️
* Cryp Ownership
* Matchmaking
* Lobbies
@@ -19,5 +19,10 @@
teams
physical, magic, pure dmg?
elemental?
items give skills
gem td style attr combinations
stoney + spikey = jagged
+32 -6
View File
@@ -1,7 +1,13 @@
use uuid::Uuid;
use rand::prelude::*;
use serde_cbor::*;
use serde_cbor::{to_vec};
use failure::Error;
use failure::err_msg;
use net::Db;
use user::User;
use rpc::{CrypSpawnParams};
use skill::{Skill};
@@ -176,12 +182,32 @@ impl Cryp {
}
pub fn spawn(params: CrypSpawnParams) -> Cryp {
Cryp::new()
.named("hatchling".to_string())
.level(params.level)
.learn(Skill::Stoney)
.create()
pub fn spawn(params: CrypSpawnParams, db: Db, user: User) -> Result<Cryp, Error> {
let cryp = Cryp::new()
.named(params.name)
.level(1)
.create();
let cryp_bytes = to_vec(&cryp)?;
let query = "
INSERT INTO cryps (id, user, data)
VALUES ($1, $2, $3)
RETURNING id, user;
";
let tx = db.transaction()?;
let result = tx
.query(query, &[&cryp.id, &user.id, &cryp_bytes])?;
let _returned = result.iter().next().expect("no row returned");
println!("{:?} spawned cryp {:}", user.id, cryp.id);
tx.commit()?;
return Ok(cryp);
}
+2 -1
View File
@@ -11,7 +11,7 @@ static DB_POOL_SIZE: u32 = 20;
pub type Db = PooledConnection<PostgresConnectionManager>;
use rpc::{Rpc, RpcResult};
use rpc::{Rpc};
struct Server {
out: Sender,
@@ -39,6 +39,7 @@ impl Handler for Server {
self.out.send(response)
},
Err(e) => {
println!("{:?}", e);
let response = to_vec(&RpcErrorResponse { err: e.to_string() })
.expect("failed to serialize error response");
self.out.send(response)
+18 -8
View File
@@ -5,7 +5,7 @@ use failure::err_msg;
use net::Db;
use cryp::{Cryp, spawn};
use user::{User, create, login};
use user::{User, create, login, from_token};
pub struct Rpc;
@@ -18,17 +18,27 @@ impl Rpc {
match from_slice::<RpcMessage>(&data) {
Ok(v) => {
println!("{:?}", v.token);
let user: Option<User> = match v.token {
Some(t) => Some(from_token(t, &db)?),
None => None,
};
println!("{:?}", user);
// now we have the method name
// match on that to determine what fn to call
match v.method.as_ref() {
"cryp_spawn" => {
match from_slice::<CrypSpawnMsg>(&data) {
Ok(v) => Ok(RpcResponse {
method: v.method,
params: RpcResult::Cryp(spawn(v.params))
}),
Ok(v) => {
match user {
Some(u) => Ok(RpcResponse {
method: v.method,
params: RpcResult::SpawnCryp(spawn(v.params, db, u)?)
}),
None => Err(err_msg("auth required")),
}
}
Err(_e) => Err(err_msg("invalid params")),
}
},
@@ -67,7 +77,7 @@ pub struct RpcResponse {
#[derive(Debug,Clone,Serialize,Deserialize)]
pub enum RpcResult {
Cryp(Cryp),
SpawnCryp(Cryp),
User(User),
}
@@ -85,7 +95,7 @@ struct CrypSpawnMsg {
#[derive(Debug,Clone,Serialize,Deserialize)]
pub struct CrypSpawnParams {
pub level: u8,
pub name: String,
}
#[derive(Debug,Clone,Serialize,Deserialize)]
+34 -10
View File
@@ -12,10 +12,12 @@ use rpc::{UserCreateParams, UserLoginParams, RpcResult};
use failure::Error;
use failure::err_msg;
static PASSWORD_MIN_LEN: usize = 12;
#[derive(Debug,Clone,Serialize,Deserialize)]
pub struct User {
id: Uuid,
name: String,
pub id: Uuid,
pub name: String,
token: String,
}
@@ -27,7 +29,31 @@ struct UserEntry {
token: String,
}
static PASSWORD_MIN_LEN: usize = 12;
// MAYBE
// hash tokens with a secret
pub fn from_token(token: String, db: &Db) -> Result<User, Error> {
let query = "
SELECT id, name, token
FROM users
WHERE token = $1;
";
let result = db
.query(query, &[&token])?;
let returned = match result.iter().next() {
Some(row) => row,
None => return Err(err_msg("invalid token")),
};
let entry = User {
id: returned.get(0),
name: returned.get(1),
token: returned.get(2),
};
return Ok(entry);
}
pub fn create(params: UserCreateParams, db: Db) -> Result<RpcResult, Error> {
let id = Uuid::new_v4();
@@ -73,7 +99,7 @@ pub fn create(params: UserCreateParams, db: Db) -> Result<RpcResult, Error> {
tx.commit()?;
Ok(RpcResult::User(entry))
return Ok(RpcResult::User(entry));
}
pub fn login(params: UserLoginParams, db: Db) -> Result<RpcResult, Error> {
@@ -83,13 +109,13 @@ pub fn login(params: UserLoginParams, db: Db) -> Result<RpcResult, Error> {
WHERE name = $1;
";
// let tx = db.transaction()?;
let result = db
.query(query, &[&params.name])?;
let returned = match result.iter().next() {
Some(row) => row,
// MAYBE
// verify gibberish to delay response for timing attacks
None => return Err(err_msg("user not found")),
};
@@ -110,13 +136,11 @@ pub fn login(params: UserLoginParams, db: Db) -> Result<RpcResult, Error> {
// update token?
// don't necessarily want to log every session out when logging in
// tx.commit()?;
let user = User {
id: entry.id,
name: entry.name,
token: entry.token,
};
Ok(RpcResult::User(user))
}
return Ok(RpcResult::User(user));
}