rpc rework for cryp status
This commit is contained in:
parent
4d862a7900
commit
e99439002f
@ -57,6 +57,7 @@ function createSocket(store) {
|
|||||||
|
|
||||||
account = login;
|
account = login;
|
||||||
store.dispatch(actions.setAccount(login));
|
store.dispatch(actions.setAccount(login));
|
||||||
|
// send({ method: 'cryp_spawn', params: { name: 'muji' } });
|
||||||
send({ method: 'account_cryps', params: {} });
|
send({ method: 'account_cryps', params: {} });
|
||||||
console.log(account);
|
console.log(account);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -59,7 +59,7 @@ pub fn from_token(token: String, db: &Db) -> Result<Account, Error> {
|
|||||||
return Ok(entry);
|
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();
|
let id = Uuid::new_v4();
|
||||||
|
|
||||||
if params.password.len() < PASSWORD_MIN_LEN {
|
if params.password.len() < PASSWORD_MIN_LEN {
|
||||||
@ -106,7 +106,7 @@ pub fn create(params: AccountCreateParams, db: Db) -> Result<Account, Error> {
|
|||||||
return Ok(entry);
|
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 = "
|
let query = "
|
||||||
SELECT id, name, token, password
|
SELECT id, name, token, password
|
||||||
FROM accounts
|
FROM accounts
|
||||||
@ -149,7 +149,7 @@ pub fn login(params: AccountLoginParams, db: Db) -> Result<Account, Error> {
|
|||||||
return Ok(account);
|
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 = "
|
let query = "
|
||||||
SELECT data
|
SELECT data
|
||||||
FROM cryps
|
FROM cryps
|
||||||
|
|||||||
@ -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>
|
pub fn pve(params: CombatPveParams, db: &Db, account: &Account) -> Result<Battle, Error> {
|
||||||
where F: Fn(&Battle) -> Result<(), Error>
|
|
||||||
{
|
|
||||||
let query = "
|
let query = "
|
||||||
SELECT *
|
SELECT *
|
||||||
FROM cryps
|
FROM cryps
|
||||||
@ -122,8 +120,6 @@ where F: Fn(&Battle) -> Result<(), Error>
|
|||||||
loop {
|
loop {
|
||||||
battle.next();
|
battle.next();
|
||||||
|
|
||||||
send(&battle)?;
|
|
||||||
|
|
||||||
if battle.finished() {
|
if battle.finished() {
|
||||||
let success = match battle.winner() {
|
let success = match battle.winner() {
|
||||||
Some(c) => c.id == plr.id,
|
Some(c) => c.id == plr.id,
|
||||||
|
|||||||
@ -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()
|
let cryp = Cryp::new()
|
||||||
.named(¶ms.name)
|
.named(¶ms.name)
|
||||||
.level(1)
|
.level(1)
|
||||||
@ -217,7 +217,7 @@ pub fn spawn(params: CrypSpawnParams, db: Db, account: Account) -> Result<Cryp,
|
|||||||
return Ok(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 cryp_bytes = to_vec(&cryp)?;
|
||||||
|
|
||||||
let query = "
|
let query = "
|
||||||
|
|||||||
@ -32,7 +32,7 @@ impl Handler for Server {
|
|||||||
|
|
||||||
fn on_message(&mut self, msg: Message) -> Result<()> {
|
fn on_message(&mut self, msg: Message) -> Result<()> {
|
||||||
let db = self.db.get().expect("unable to get db connection");
|
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) => {
|
Ok(reply) => {
|
||||||
let response = to_vec(&reply)
|
let response = to_vec(&reply)
|
||||||
.expect("failed to serialize response");
|
.expect("failed to serialize response");
|
||||||
|
|||||||
@ -13,7 +13,7 @@ use account::{Account, create, login, from_token, fetch_cryps};
|
|||||||
pub struct Rpc;
|
pub struct Rpc;
|
||||||
|
|
||||||
impl 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
|
// consume the ws data into bytes
|
||||||
let data = msg.into_data();
|
let data = msg.into_data();
|
||||||
|
|
||||||
@ -32,7 +32,7 @@ impl Rpc {
|
|||||||
// match on that to determine what fn to call
|
// match on that to determine what fn to call
|
||||||
match v.method.as_ref() {
|
match v.method.as_ref() {
|
||||||
"cryp_spawn" => Rpc::cryp_spawn(data, db, account),
|
"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_create" => Rpc::account_create(data, db),
|
||||||
"account_login" => Rpc::account_login(data, db),
|
"account_login" => Rpc::account_login(data, db),
|
||||||
"account_cryps" => Rpc::account_cryps(db, account),
|
"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> {
|
fn send_msg(socket: &Sender, msg: RpcResponse) -> Result<(), Error> {
|
||||||
let send = |b: &Battle| -> Result<(), Error> {
|
let bytes = to_vec(&msg)?;
|
||||||
let reply = RpcResponse {
|
match socket.send(bytes) {
|
||||||
method: "battle_state".to_string(),
|
Ok(()) => Ok(()),
|
||||||
params: RpcResult::Pve(b.clone())
|
Err(e) => Err(err_msg(e))
|
||||||
};
|
|
||||||
let response = to_vec(&reply)?;
|
|
||||||
match out.send(response) {
|
|
||||||
Ok(()) => Ok(()),
|
|
||||||
Err(e) => Err(err_msg(e))
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
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")),
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn cryp_spawn(data: Vec<u8>, db: Db, account: Option<Account>) -> Result<RpcResponse, Error> {
|
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")),
|
||||||
|
};
|
||||||
|
|
||||||
|
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> {
|
||||||
match from_slice::<CrypSpawnMsg>(&data) {
|
match from_slice::<CrypSpawnMsg>(&data) {
|
||||||
Ok(v) => {
|
Ok(v) => {
|
||||||
match account {
|
match account {
|
||||||
Some(u) => Ok(RpcResponse {
|
Some(u) => Ok(RpcResponse {
|
||||||
method: v.method,
|
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")),
|
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) {
|
match from_slice::<AccountCreateMsg>(&data) {
|
||||||
Ok(v) => Ok(RpcResponse {
|
Ok(v) => Ok(RpcResponse {
|
||||||
method: v.method,
|
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) {
|
match from_slice::<AccountLoginMsg>(&data) {
|
||||||
Ok(v) => Ok(RpcResponse {
|
Ok(v) => Ok(RpcResponse {
|
||||||
method: v.method,
|
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 {
|
match account {
|
||||||
Some(u) => Ok(RpcResponse {
|
Some(u) => Ok(RpcResponse {
|
||||||
method: "account_cryps".to_string(),
|
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")),
|
None => Err(err_msg("auth required")),
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user