filthy timeout

This commit is contained in:
ntr 2018-10-23 16:51:12 +11:00
parent af9f2c479d
commit cf7501b442
4 changed files with 45 additions and 2 deletions

View File

@ -7,6 +7,7 @@ const Game = require('./game');
const addState = connect( const addState = connect(
function receiveState(state) { function receiveState(state) {
const { ws, game, account, activeSkill, activeIncoming } = state; const { ws, game, account, activeSkill, activeIncoming } = state;
function selectSkillTarget(targetTeamId) { function selectSkillTarget(targetTeamId) {
if (activeSkill) { if (activeSkill) {
return ws.sendGameSkill(game.id, activeSkill.crypId, targetTeamId, activeSkill.skill); return ws.sendGameSkill(game.id, activeSkill.crypId, targetTeamId, activeSkill.skill);

View File

@ -18,6 +18,8 @@ function errorToast(err) {
function createSocket(store) { function createSocket(store) {
let ws; let ws;
let gameStateTimeout;
function connect() { function connect() {
ws = new WebSocket('ws://localhost:40000'); ws = new WebSocket('ws://localhost:40000');
ws.binaryType = 'arraybuffer'; ws.binaryType = 'arraybuffer';
@ -71,6 +73,8 @@ function createSocket(store) {
function gameState(response) { function gameState(response) {
const [structName, game] = response; const [structName, game] = response;
clearInterval(gameStateTimeout);
gameStateTimeout = setTimeout(() => sendGameState(game.id), 1000);
store.dispatch(actions.setGame(game)); store.dispatch(actions.setGame(game));
} }
@ -109,6 +113,10 @@ function createSocket(store) {
send({ method: 'cryp_spawn', params: { name } }); send({ method: 'cryp_spawn', params: { name } });
} }
function sendGameState(id) {
send({ method: 'game_state', params: { id } });
}
function sendGamePve(id) { function sendGamePve(id) {
send({ method: 'game_pve', params: { id } }); send({ method: 'game_pve', params: { id } });
} }
@ -174,6 +182,7 @@ function createSocket(store) {
return { return {
sendAccountLogin, sendAccountLogin,
sendAccountRegister, sendAccountRegister,
sendGameState,
sendGamePve, sendGamePve,
sendGamePvp, sendGamePvp,
sendGameJoin, sendGameJoin,

View File

@ -8,7 +8,7 @@ use failure::Error;
use failure::err_msg; use failure::err_msg;
use account::Account; use account::Account;
use rpc::{GameSkillParams, GamePveParams, GamePvpParams, GameTargetParams, GameJoinParams}; use rpc::{GameStateParams, GameSkillParams, GamePveParams, GamePvpParams, GameTargetParams, GameJoinParams};
use cryp::{Cryp, cryp_get}; use cryp::{Cryp, cryp_get};
#[derive(Debug,Clone,Copy,PartialEq,Serialize,Deserialize)] #[derive(Debug,Clone,Copy,PartialEq,Serialize,Deserialize)]
@ -489,6 +489,10 @@ pub fn game_new(game: &Game, tx: &mut Transaction) -> Result<(), Error> {
return Ok(()); return Ok(());
} }
pub fn game_state(params: GameStateParams, tx: &mut Transaction, _account: &Account) -> Result<Game, Error> {
return game_get(tx, params.id)
}
pub fn game_get(tx: &mut Transaction, id: Uuid) -> Result<Game, Error> { pub fn game_get(tx: &mut Transaction, id: Uuid) -> Result<Game, Error> {
let query = " let query = "
SELECT * SELECT *

View File

@ -11,7 +11,7 @@ use failure::err_msg;
use net::Db; use net::Db;
use cryp::{Cryp, cryp_spawn}; use cryp::{Cryp, cryp_spawn};
use game::{Game, Skill, game_pve, game_pvp, game_join, game_skill, game_target}; use game::{Game, Skill, game_state, game_pve, game_pvp, game_join, game_skill, game_target};
use account::{Account, account_create, account_login, account_from_token, account_cryps}; use account::{Account, account_create, account_login, account_from_token, account_cryps};
use item::{Item, items_list, item_use}; use item::{Item, items_list, item_use};
@ -38,6 +38,7 @@ impl Rpc {
// match on that to determine what fn to call // match on that to determine what fn to call
let response = match v.method.as_ref() { let response = match v.method.as_ref() {
"cryp_spawn" => Rpc::cryp_spawn(data, &mut tx, account, client), "cryp_spawn" => Rpc::cryp_spawn(data, &mut tx, account, client),
"game_state" => Rpc::game_state(data, &mut tx, account, client),
"game_pve" => Rpc::game_pve(data, &mut tx, account, client), "game_pve" => Rpc::game_pve(data, &mut tx, account, client),
"game_pvp" => Rpc::game_pvp(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_join" => Rpc::game_join(data, &mut tx, account, client),
@ -68,6 +69,23 @@ impl Rpc {
} }
} }
fn game_state(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::<GameStateMsg>(&data).or(Err(err_msg("invalid params")))?;
let game_response = RpcResponse {
method: "game_state".to_string(),
params: RpcResult::GameState(game_state(msg.params, tx, &a)?)
};
return Ok(game_response);
}
fn game_pve(data: Vec<u8>, tx: &mut Transaction, account: Option<Account>, client: &mut WebSocket<TcpStream>) -> Result<RpcResponse, Error> { fn game_pve(data: Vec<u8>, tx: &mut Transaction, account: Option<Account>, client: &mut WebSocket<TcpStream>) -> Result<RpcResponse, Error> {
let a = match account { let a = match account {
Some(a) => a, Some(a) => a,
@ -275,6 +293,17 @@ pub struct CrypSpawnParams {
pub name: String, pub name: String,
} }
#[derive(Debug,Clone,Serialize,Deserialize)]
struct GameStateMsg {
method: String,
params: GameStateParams,
}
#[derive(Debug,Clone,Serialize,Deserialize)]
pub struct GameStateParams {
pub id: Uuid,
}
#[derive(Debug,Clone,Serialize,Deserialize)] #[derive(Debug,Clone,Serialize,Deserialize)]
struct GamePveMsg { struct GamePveMsg {
method: String, method: String,