rpc rework for cryp status

This commit is contained in:
ntr 2018-10-09 00:09:38 +11:00
parent 4d862a7900
commit e99439002f
6 changed files with 42 additions and 43 deletions

View File

@ -57,6 +57,7 @@ function createSocket(store) {
account = login;
store.dispatch(actions.setAccount(login));
// send({ method: 'cryp_spawn', params: { name: 'muji' } });
send({ method: 'account_cryps', params: {} });
console.log(account);
}

View File

@ -59,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<Account, Error> {
pub fn create(params: AccountCreateParams, db: &Db) -> Result<Account, Error> {
let id = Uuid::new_v4();
if params.password.len() < PASSWORD_MIN_LEN {
@ -106,7 +106,7 @@ pub fn create(params: AccountCreateParams, db: Db) -> Result<Account, Error> {
return Ok(entry);
}
pub fn login(params: AccountLoginParams, db: Db) -> Result<Account, Error> {
pub fn login(params: AccountLoginParams, db: &Db) -> Result<Account, Error> {
let query = "
SELECT id, name, token, password
FROM accounts
@ -149,7 +149,7 @@ pub fn login(params: AccountLoginParams, db: Db) -> Result<Account, Error> {
return Ok(account);
}
pub fn fetch_cryps(db: Db, account: Account) -> Result<Vec<Cryp>, Error> {
pub fn fetch_cryps(db: &Db, account: &Account) -> Result<Vec<Cryp>, Error> {
let query = "
SELECT data
FROM cryps

View File

@ -94,9 +94,7 @@ fn generate_mob(plr: &Cryp) -> Cryp {
}
pub fn pve<F>(params: CombatPveParams, db: Db, account: Account, send: F) -> Result<Battle, Error>
where F: Fn(&Battle) -> Result<(), Error>
{
pub fn pve(params: CombatPveParams, db: &Db, account: &Account) -> Result<Battle, Error> {
let query = "
SELECT *
FROM cryps
@ -122,8 +120,6 @@ where F: Fn(&Battle) -> Result<(), Error>
loop {
battle.next();
send(&battle)?;
if battle.finished() {
let success = match battle.winner() {
Some(c) => c.id == plr.id,

View File

@ -188,7 +188,7 @@ impl Cryp {
}
pub fn spawn(params: CrypSpawnParams, db: Db, account: Account) -> Result<Cryp, Error> {
pub fn spawn(params: CrypSpawnParams, db: &Db, account: &Account) -> Result<Cryp, Error> {
let cryp = Cryp::new()
.named(&params.name)
.level(1)
@ -217,7 +217,7 @@ pub fn spawn(params: CrypSpawnParams, db: Db, account: Account) -> Result<Cryp,
return Ok(cryp);
}
pub fn write_cryp(cryp: Cryp, db: Db, account: Account) -> Result<Cryp, Error> {
pub fn write_cryp(cryp: Cryp, db: &Db, account: &Account) -> Result<Cryp, Error> {
let cryp_bytes = to_vec(&cryp)?;
let query = "

View File

@ -32,7 +32,7 @@ impl Handler for Server {
fn on_message(&mut self, msg: Message) -> Result<()> {
let db = self.db.get().expect("unable to get db connection");
match self.rpc.receive(msg, db, &self.out) {
match self.rpc.receive(msg, &db, &self.out) {
Ok(reply) => {
let response = to_vec(&reply)
.expect("failed to serialize response");

View File

@ -13,7 +13,7 @@ use account::{Account, create, login, from_token, fetch_cryps};
pub struct Rpc;
impl Rpc {
pub fn receive(&self, msg: Message, db: Db, out: &Sender) -> Result<RpcResponse, Error> {
pub fn receive(&self, msg: Message, db: &Db, socket: &Sender) -> Result<RpcResponse, Error> {
// consume the ws data into bytes
let data = msg.into_data();
@ -32,7 +32,7 @@ impl Rpc {
// match on that to determine what fn to call
match v.method.as_ref() {
"cryp_spawn" => Rpc::cryp_spawn(data, db, account),
"combat_pve" => Rpc::combat_pve(data, db, account, out),
"combat_pve" => Rpc::combat_pve(data, db, account, socket),
"account_create" => Rpc::account_create(data, db),
"account_login" => Rpc::account_login(data, db),
"account_cryps" => Rpc::account_cryps(db, account),
@ -44,40 +44,42 @@ impl Rpc {
}
}
fn combat_pve(data: Vec<u8>, db: Db, account: Option<Account>, out: &Sender) -> Result<RpcResponse, Error> {
let send = |b: &Battle| -> Result<(), Error> {
let reply = RpcResponse {
method: "battle_state".to_string(),
params: RpcResult::Pve(b.clone())
};
let response = to_vec(&reply)?;
match out.send(response) {
fn send_msg(socket: &Sender, msg: RpcResponse) -> Result<(), Error> {
let bytes = to_vec(&msg)?;
match socket.send(bytes) {
Ok(()) => Ok(()),
Err(e) => Err(err_msg(e))
}
}
fn combat_pve(data: Vec<u8>, db: &Db, account: Option<Account>, socket: &Sender) -> Result<RpcResponse, Error> {
let u = match account {
Some(u) => u,
None => return Err(err_msg("auth required")),
};
match from_slice::<CombatPveMsg>(&data) {
Ok(v) => {
match account {
Some(u) => Ok(RpcResponse {
method: v.method,
params: RpcResult::Pve(pve(v.params, db, u, send)?)
}),
None => Err(err_msg("auth required")),
}
}
Err(_e) => Err(err_msg("invalid params")),
}
let msg = from_slice::<CombatPveMsg>(&data).or(Err(err_msg("invalid params")))?;
let battle_response = RpcResponse {
method: "battle_state".to_string(),
params: RpcResult::Pve(pve(msg.params, db, &u)?)
};
Rpc::send_msg(socket, RpcResponse {
method: "account_cryps".to_string(),
params: RpcResult::CrypList(fetch_cryps(db, &u)?)
})?;
return Ok(battle_response);
}
fn cryp_spawn(data: Vec<u8>, db: Db, account: Option<Account>) -> Result<RpcResponse, Error> {
fn cryp_spawn(data: Vec<u8>, db: &Db, account: Option<Account>) -> Result<RpcResponse, Error> {
match from_slice::<CrypSpawnMsg>(&data) {
Ok(v) => {
match account {
Some(u) => Ok(RpcResponse {
method: v.method,
params: RpcResult::SpawnCryp(spawn(v.params, db, u)?)
params: RpcResult::SpawnCryp(spawn(v.params, db, &u)?)
}),
None => Err(err_msg("auth required")),
}
@ -86,7 +88,7 @@ impl Rpc {
}
}
fn account_create(data: Vec<u8>, db: Db) -> Result<RpcResponse, Error> {
fn account_create(data: Vec<u8>, db: &Db) -> Result<RpcResponse, Error> {
match from_slice::<AccountCreateMsg>(&data) {
Ok(v) => Ok(RpcResponse {
method: v.method,
@ -96,7 +98,7 @@ impl Rpc {
}
}
fn account_login(data: Vec<u8>, db: Db) -> Result<RpcResponse, Error> {
fn account_login(data: Vec<u8>, db: &Db) -> Result<RpcResponse, Error> {
match from_slice::<AccountLoginMsg>(&data) {
Ok(v) => Ok(RpcResponse {
method: v.method,
@ -106,11 +108,11 @@ impl Rpc {
}
}
fn account_cryps(db: Db, account: Option<Account>) -> Result<RpcResponse, Error> {
fn account_cryps(db: &Db, account: Option<Account>) -> Result<RpcResponse, Error> {
match account {
Some(u) => Ok(RpcResponse {
method: "account_cryps".to_string(),
params: RpcResult::CrypList(fetch_cryps(db, u)?)
params: RpcResult::CrypList(fetch_cryps(db, &u)?)
}),
None => Err(err_msg("auth required")),
}