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

View File

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

View File

@ -13,6 +13,7 @@ use cryp::{Cryp, CrypRecover, cryp_write, cryp_recover};
use game::Game; use game::Game;
// use zone::{Zone, zone_delete}; // use zone::{Zone, zone_delete};
use skill::{Skill}; use skill::{Skill};
use player::{Player, player_delete};
use failure::Error; use failure::Error;
use failure::err_msg; use failure::err_msg;
@ -234,4 +235,31 @@ pub fn account_game_history(tx: &mut Transaction, account: &Account) -> Result<V
// return Ok(zone); // 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) 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> { pub fn player_state(params: PlayerStateParams, tx: &mut Transaction, account: &Account) -> Result<Player, Error> {
player_get(tx, account.id, params.instance_id) player_get(tx, account.id, params.instance_id)
} }

View File

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