account players method

This commit is contained in:
ntr 2019-02-25 14:06:41 +11:00
parent c6329de8ab
commit afe992d986
5 changed files with 67 additions and 3 deletions

View File

@ -45,6 +45,10 @@ function createSocket(events) {
send({ method: 'account_cryps', params: {} });
}
function sendAccountPlayers() {
send({ method: 'account_players', params: {} });
}
function sendAccountZone() {
send({ method: 'account_zone', params: {} });
}
@ -150,6 +154,7 @@ function createSocket(events) {
account = login;
events.setAccount(login);
sendAccountCryps();
sendAccountPlayers();
}
function accountCryps(response) {
@ -222,12 +227,12 @@ function createSocket(events) {
// decode binary msg from server
const blob = new Uint8Array(event.data);
const res = cbor.decode(blob);
const { method, params } = res;
console.log(res);
// check for error and split into response type and data
if (res.err) return errHandler(res.err);
const { method, params } = res;
if (!handlers[method]) return errorToast(`${method} handler missing`);
return handlers[method](params);
}
@ -277,6 +282,7 @@ function createSocket(events) {
sendAccountCreate,
sendAccountDemo,
sendAccountCryps,
sendAccountPlayers,
sendAccountZone,
sendGameState,
sendGamePve,

View File

@ -43,9 +43,11 @@ round system for games
* notifications
* elo + leaderboards
$$$
* Items
* Colour scheme
* Highlight (dota) colour
* fx colours + styles
# Db maintenance

View File

@ -13,6 +13,7 @@ use cryp::{Cryp, CrypRecover, cryp_write, cryp_recover};
use game::Game;
// use zone::{Zone, zone_delete};
use skill::{Skill};
use player::{Player, player_delete};
use failure::Error;
use failure::err_msg;
@ -234,4 +235,31 @@ pub fn account_game_history(tx: &mut Transaction, account: &Account) -> Result<V
// return Ok(zone);
// }
pub fn account_players(tx: &mut Transaction, account: &Account) -> Result<Vec<Player>, Error> {
let query = "
SELECT data, id
FROM players
WHERE account = $1;
";
let result = tx
.query(query, &[&account.id])?;
let mut list = vec![];
for row in result.into_iter() {
let bytes: Vec<u8> = row.get(0);
let id = row.get(1);
match from_slice::<Player>(&bytes) {
Ok(i) => list.push(i),
Err(_e) => {
player_delete(tx, id)?;
}
};
}
list.sort_by_key(|c| c.id);
return Ok(list);
}

View File

@ -102,6 +102,25 @@ pub fn player_update(tx: &mut Transaction, player: Player) -> Result<Player, Err
return Ok(player)
}
pub fn player_delete(tx: &mut Transaction, id: Uuid) -> Result<(), Error> {
let query = "
DELETE
FROM players
WHERE id = $1;
";
let result = tx
.execute(query, &[&id])?;
if result != 1 {
return Err(format_err!("unable to delete player {:?}", id));
}
println!("player deleted {:?}", id);
return Ok(());
}
pub fn player_state(params: PlayerStateParams, tx: &mut Transaction, account: &Account) -> Result<Player, Error> {
player_get(tx, account.id, params.instance_id)
}

View File

@ -17,7 +17,7 @@ use failure::err_msg;
use net::Db;
use cryp::{Cryp, cryp_spawn, cryp_learn};
use game::{Game, game_state, game_pve, game_skill};
use account::{Account, account_create, account_login, account_from_token, account_cryps};
use account::{Account, account_create, account_login, account_from_token, account_cryps, account_players};
use skill::{Skill};
// use zone::{Zone, zone_create, zone_join, zone_close};
use spec::{Spec};
@ -64,6 +64,7 @@ impl Rpc {
// auth methods
"account_cryps" => Rpc::account_cryps(data, &mut tx, account.unwrap(), client),
"account_players" => Rpc::account_players(data, &mut tx, account.unwrap(), client),
// "account_zone" => Rpc::account_zone(data, &mut tx, account.unwrap(), client),
"cryp_spawn" => Rpc::cryp_spawn(data, &mut tx, account.unwrap(), client),
@ -220,6 +221,13 @@ impl Rpc {
})
}
fn account_players(_data: Vec<u8>, tx: &mut Transaction, account: Account, _client: &mut WebSocket<TcpStream>) -> Result<RpcResponse, Error> {
Ok(RpcResponse {
method: "account_players".to_string(),
params: RpcResult::PlayerList(account_players(tx, &account)?)
})
}
// fn account_zone(_data: Vec<u8>, tx: &mut Transaction, account: Account, _client: &mut WebSocket<TcpStream>) -> Result<RpcResponse, Error> {
// Ok(RpcResponse {
// method: "zone_state".to_string(),
@ -374,6 +382,7 @@ pub enum RpcResult {
// ZoneState(Zone),
// ZoneClose(()),
PlayerList(Vec<Player>),
PlayerState(Player),
}