filthy timeout
This commit is contained in:
parent
af9f2c479d
commit
cf7501b442
@ -7,6 +7,7 @@ const Game = require('./game');
|
||||
const addState = connect(
|
||||
function receiveState(state) {
|
||||
const { ws, game, account, activeSkill, activeIncoming } = state;
|
||||
|
||||
function selectSkillTarget(targetTeamId) {
|
||||
if (activeSkill) {
|
||||
return ws.sendGameSkill(game.id, activeSkill.crypId, targetTeamId, activeSkill.skill);
|
||||
|
||||
@ -18,6 +18,8 @@ function errorToast(err) {
|
||||
function createSocket(store) {
|
||||
let ws;
|
||||
|
||||
let gameStateTimeout;
|
||||
|
||||
function connect() {
|
||||
ws = new WebSocket('ws://localhost:40000');
|
||||
ws.binaryType = 'arraybuffer';
|
||||
@ -71,6 +73,8 @@ function createSocket(store) {
|
||||
|
||||
function gameState(response) {
|
||||
const [structName, game] = response;
|
||||
clearInterval(gameStateTimeout);
|
||||
gameStateTimeout = setTimeout(() => sendGameState(game.id), 1000);
|
||||
store.dispatch(actions.setGame(game));
|
||||
}
|
||||
|
||||
@ -109,6 +113,10 @@ function createSocket(store) {
|
||||
send({ method: 'cryp_spawn', params: { name } });
|
||||
}
|
||||
|
||||
function sendGameState(id) {
|
||||
send({ method: 'game_state', params: { id } });
|
||||
}
|
||||
|
||||
function sendGamePve(id) {
|
||||
send({ method: 'game_pve', params: { id } });
|
||||
}
|
||||
@ -174,6 +182,7 @@ function createSocket(store) {
|
||||
return {
|
||||
sendAccountLogin,
|
||||
sendAccountRegister,
|
||||
sendGameState,
|
||||
sendGamePve,
|
||||
sendGamePvp,
|
||||
sendGameJoin,
|
||||
|
||||
@ -8,7 +8,7 @@ use failure::Error;
|
||||
use failure::err_msg;
|
||||
|
||||
use account::Account;
|
||||
use rpc::{GameSkillParams, GamePveParams, GamePvpParams, GameTargetParams, GameJoinParams};
|
||||
use rpc::{GameStateParams, GameSkillParams, GamePveParams, GamePvpParams, GameTargetParams, GameJoinParams};
|
||||
use cryp::{Cryp, cryp_get};
|
||||
|
||||
#[derive(Debug,Clone,Copy,PartialEq,Serialize,Deserialize)]
|
||||
@ -489,6 +489,10 @@ pub fn game_new(game: &Game, tx: &mut Transaction) -> Result<(), Error> {
|
||||
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> {
|
||||
let query = "
|
||||
SELECT *
|
||||
|
||||
@ -11,7 +11,7 @@ use failure::err_msg;
|
||||
|
||||
use net::Db;
|
||||
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 item::{Item, items_list, item_use};
|
||||
|
||||
@ -38,6 +38,7 @@ impl Rpc {
|
||||
// match on that to determine what fn to call
|
||||
let response = match v.method.as_ref() {
|
||||
"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_pvp" => Rpc::game_pvp(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> {
|
||||
let a = match account {
|
||||
Some(a) => a,
|
||||
@ -275,6 +293,17 @@ pub struct CrypSpawnParams {
|
||||
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)]
|
||||
struct GamePveMsg {
|
||||
method: String,
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user