sql stuff
This commit is contained in:
+27
-3
@@ -7,13 +7,11 @@ use serde_cbor::{from_slice};
|
||||
|
||||
use postgres::transaction::Transaction;
|
||||
|
||||
use std::str;
|
||||
|
||||
use net::Db;
|
||||
use rpc::{AccountCreateParams, AccountLoginParams};
|
||||
use item::{Item, ItemAction, item_create};
|
||||
|
||||
use cryp::Cryp;
|
||||
use game::Game;
|
||||
|
||||
use failure::Error;
|
||||
use failure::err_msg;
|
||||
@@ -177,4 +175,30 @@ pub fn account_cryps(tx: &mut Transaction, account: &Account) -> Result<Vec<Cryp
|
||||
return Ok(cryps.unwrap());
|
||||
}
|
||||
|
||||
pub fn account_game_history(tx: &mut Transaction, account: &Account) -> Result<Vec<Game>, Error> {
|
||||
let query = "
|
||||
SELECT games.data
|
||||
FROM players join games
|
||||
ON (players.game = games.id)
|
||||
WHERE account = $1;
|
||||
";
|
||||
|
||||
let result = tx
|
||||
.query(query, &[&account.id])?;
|
||||
|
||||
let games: Result<Vec<Game>, _> = result.iter().map(|row| {
|
||||
let cryp_bytes: Vec<u8> = row.get(0);
|
||||
from_slice::<Game>(&cryp_bytes)
|
||||
}).collect();
|
||||
|
||||
// catch any errors
|
||||
if games.is_err() {
|
||||
return Err(err_msg("could not deserialize a game"));
|
||||
}
|
||||
|
||||
// now unwrap is safe
|
||||
return Ok(games.unwrap());
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -702,6 +702,30 @@ pub fn game_join(params: GameJoinParams, tx: &mut Transaction, account: &Account
|
||||
Ok(game)
|
||||
}
|
||||
|
||||
pub fn game_joinable_list(tx: &mut Transaction, _account: &Account) -> Result<Vec<Game>, Error> {
|
||||
let query = "
|
||||
SELECT games.data
|
||||
FROM games
|
||||
WHERE open;
|
||||
";
|
||||
|
||||
let result = tx
|
||||
.query(query, &[])?;
|
||||
|
||||
let games: Result<Vec<Game>, _> = result.iter().map(|row| {
|
||||
let cryp_bytes: Vec<u8> = row.get(0);
|
||||
from_slice::<Game>(&cryp_bytes)
|
||||
}).collect();
|
||||
|
||||
// catch any errors
|
||||
if games.is_err() {
|
||||
return Err(err_msg("could not deserialize a game"));
|
||||
}
|
||||
|
||||
// now unwrap is safe
|
||||
return Ok(games.unwrap());
|
||||
}
|
||||
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
|
||||
+26
-1
@@ -11,7 +11,7 @@ use failure::err_msg;
|
||||
|
||||
use net::Db;
|
||||
use cryp::{Cryp, cryp_spawn, cryp_learn, cryp_forget};
|
||||
use game::{Game, game_state, game_pve, game_pvp, game_join, game_skill, game_target};
|
||||
use game::{Game, game_state, game_pve, game_pvp, game_join, game_joinable_list, game_skill, game_target};
|
||||
use account::{Account, account_create, account_login, account_from_token, account_cryps};
|
||||
use item::{Item, items_list, item_use};
|
||||
use skill::{Skill};
|
||||
@@ -45,6 +45,7 @@ impl Rpc {
|
||||
"game_pve" => Rpc::game_pve(data, &mut tx, account, client),
|
||||
"game_pvp" => Rpc::game_pvp(data, &mut tx, account, client),
|
||||
"game_join" => Rpc::game_join(data, &mut tx, account, client),
|
||||
"game_joinable_list" => Rpc::game_joinable_list(data, &mut tx, account, client),
|
||||
"game_skill" => Rpc::game_skill(data, &mut tx, account, client),
|
||||
"game_target" => Rpc::game_target(data, &mut tx, account, client),
|
||||
"account_create" => Rpc::account_create(data, &mut tx, account, client),
|
||||
@@ -143,6 +144,23 @@ impl Rpc {
|
||||
return Ok(game_response);
|
||||
}
|
||||
|
||||
fn game_joinable_list(_data: Vec<u8>, tx: &mut Transaction, account: Option<Account>, _client: &mut WebSocket<TcpStream>) -> Result<RpcResponse, Error> {
|
||||
let a = match account {
|
||||
Some(a) => a,
|
||||
None => return Err(err_msg("auth required")),
|
||||
};
|
||||
|
||||
// let msg = from_slice::<GameJoinMsg>(&data).or(Err(err_msg("invalid params")))?;
|
||||
|
||||
let game_list = RpcResponse {
|
||||
method: "game_joinable_list".to_string(),
|
||||
params: RpcResult::GameJoinableList(game_joinable_list(tx, &a)?)
|
||||
};
|
||||
|
||||
return Ok(game_list);
|
||||
}
|
||||
|
||||
|
||||
|
||||
fn game_skill(data: Vec<u8>, tx: &mut Transaction, account: Option<Account>, _client: &mut WebSocket<TcpStream>) -> Result<RpcResponse, Error> {
|
||||
let a = match account {
|
||||
@@ -326,6 +344,7 @@ pub enum RpcResult {
|
||||
Account(Account),
|
||||
CrypList(Vec<Cryp>),
|
||||
GameState(Game),
|
||||
GameJoinableList(Vec<Game>),
|
||||
ItemList(Vec<Item>),
|
||||
ItemUse(()),
|
||||
}
|
||||
@@ -416,6 +435,12 @@ pub struct GameJoinParams {
|
||||
pub cryp_ids: Vec<Uuid>,
|
||||
}
|
||||
|
||||
#[derive(Debug,Clone,Serialize,Deserialize)]
|
||||
struct GameJoinableListMsg {
|
||||
method: String,
|
||||
params: (),
|
||||
}
|
||||
|
||||
#[derive(Debug,Clone,Serialize,Deserialize)]
|
||||
struct GameTargetMsg {
|
||||
method: String,
|
||||
|
||||
Reference in New Issue
Block a user