sql stuff

This commit is contained in:
ntr 2018-11-22 12:11:16 +11:00
parent 421987c53b
commit 992b6ecd7d
6 changed files with 129 additions and 11 deletions

View File

@ -22,26 +22,34 @@ class GameList extends Phaser.GameObjects.Group {
const WIDTH = GAME_LIST.width();
const HEIGHT = GAME_LIST.height();
console.log(GAME_LIST.y(0))
const pvp = list.add
.rectangle(X, GAME_LIST.y(0), WIDTH, HEIGHT, 0x440000)
.setInteractive()
.setOrigin(0);
this
.add(list.add.text(pvp.getCenter().x, pvp.getCenter().y, 'PVP', TEXT.HEADER));
.add(list.add.text(pvp.getCenter().x, pvp.getCenter().y, 'NEW', TEXT.HEADER));
const pve = list.add
.rectangle(X, GAME_LIST.y(1), WIDTH, HEIGHT, 0x004400)
.rectangle(X, GAME_LIST.y(1), Math.floor(WIDTH / 2), HEIGHT, 0x004400)
.setInteractive()
.setOrigin(0);
this
.add(list.add.text(pve.getCenter().x, pve.getCenter().y, 'PVE', TEXT.HEADER));
const refresh = list.add
.rectangle(X + Math.floor(WIDTH / 2), GAME_LIST.y(1), Math.floor(WIDTH / 2), HEIGHT, 0x000044)
.setInteractive()
.setOrigin(0);
this
.add(list.add.text(refresh.getCenter().x, refresh.getCenter().y, 'REFRESH', TEXT.HEADER));
pvp.on('pointerdown', () => {
const team = cryps.filter(c => c.active).map(c => c.id);
list.scene.switch('Combat');
return ws.sendGamePvp(team);
});
@ -51,8 +59,10 @@ class GameList extends Phaser.GameObjects.Group {
return ws.sendGamePve(team);
});
this.add(pve);
this.add(pvp);
refresh.on('pointerdown', () => {
return ws.sendGameJoinableList();
});
return true;
}

View File

@ -22,6 +22,23 @@ function createSocket(store) {
let gameStateTimeout;
function bootstrap() {
send({ method: 'account_create', params: { name: 'ntr', password: 'grepgrepgrep' } });
send({ method: 'cryp_spawn', params: { name: 'muji' } });
send({ method: 'cryp_spawn', params: { name: 'drake' } });
send({ method: 'cryp_spawn', params: { name: 'xray' } });
}
function bootstrapMashy() {
send({ method: 'account_create', params: { name: 'mashy', password: 'grepgrepgrep' } });
send({ method: 'cryp_spawn', params: { name: 'maury' } });
send({ method: 'cryp_spawn', params: { name: 'midshaker' } });
send({ method: 'cryp_spawn', params: { name: 'artour' } });
}
window.bootstrap = bootstrap;
window.bootstrapMashy = bootstrapMashy;
function connect() {
ws = new WebSocket(SOCKET_URL);
ws.binaryType = 'arraybuffer';
@ -61,7 +78,6 @@ function createSocket(store) {
account = login;
store.dispatch(actions.setAccount(login));
// send({ method: 'cryp_spawn', params: { name: 'bees' } });
send({ method: 'account_cryps', params: {} });
send({ method: 'item_list', params: {} });
console.log(account);
@ -80,6 +96,12 @@ function createSocket(store) {
store.dispatch(actions.setGame(game));
}
function gameJoinableList(response) {
const [structName, gameList] = response;
gameStateTimeout = setTimeout(() => sendGameState(game.id), 1000);
store.dispatch(actions.setGame(game));
}
function crypSpawn(response) {
const [structName, cryp] = response;
console.log('got a new cryp', cryp);
@ -140,6 +162,11 @@ function createSocket(store) {
send({ method: 'game_join', params: { game_id: gameId, cryp_ids: crypIds } });
}
function sendGameJoinableList() {
send({ method: 'game_joinable_list', params: { } });
}
function sendGameSkill(gameId, crypId, targetTeamId, skill) {
send({
method: 'game_skill',
@ -179,6 +206,7 @@ function createSocket(store) {
cryp_spawn: crypSpawn,
game_pve: gamePve,
game_state: gameState,
game_joinable_list: gameJoinableList,
account_login: accountLogin,
account_create: accountLogin,
account_cryps: accountCryps,
@ -208,6 +236,7 @@ function createSocket(store) {
sendGamePve,
sendGamePvp,
sendGameJoin,
sendGameJoinableList,
sendGameSkill,
sendGameTarget,
sendCrypSpawn,
@ -215,6 +244,7 @@ function createSocket(store) {
sendCrypForget,
sendItemUse,
connect,
bootstrap,
};
}

View File

@ -5,6 +5,11 @@ exports.up = async knex => {
table.timestamps();
table.binary('data').notNullable();
table.boolean('open')
.defaultTo(true)
.notNullable();
// table.boolean('active').notNullable();
});
await knex.schema.createTable('players', table => {

View File

@ -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());
}

View File

@ -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 {

View File

@ -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,