ready rpc

This commit is contained in:
ntr 2019-04-29 19:09:16 +10:00
parent 59a22b6122
commit d3c301a3ac
5 changed files with 32 additions and 7 deletions

View File

@ -19,6 +19,7 @@ function GamePanel(props) {
setActiveCryp, setActiveCryp,
selectSkillTarget, selectSkillTarget,
sendInstanceState, sendInstanceState,
sendGameReady,
activeCryp, activeCryp,
account, account,
showLog, showLog,
@ -69,6 +70,11 @@ function GamePanel(props) {
onClick={() => toggleLog(!showLog)}> onClick={() => toggleLog(!showLog)}>
Log Log
</button> </button>
<button
className="game-back-btn instance-btn instance-ui-btn right"
onClick={() => sendGameReady()}>
Ready
</button>
</div> </div>
); );

View File

@ -23,6 +23,10 @@ const addState = connect(
return false; return false;
} }
function sendGameReady() {
ws.sendGameReady(game.id);
}
function sendInstanceState(instanceId) { function sendInstanceState(instanceId) {
if (!instanceId) return false; if (!instanceId) return false;
return ws.sendInstanceState(instanceId); return ws.sendInstanceState(instanceId);
@ -42,6 +46,7 @@ const addState = connect(
activeCryp, activeCryp,
selectSkillTarget, selectSkillTarget,
sendInstanceState, sendInstanceState,
sendGameReady,
}; };
}, },

View File

@ -74,8 +74,8 @@ function createSocket(events) {
send({ method: 'game_state', params: { id } }); send({ method: 'game_state', params: { id } });
} }
function sendGameJoin(gameId, crypIds) { function sendGameReady(id) {
send({ method: 'game_join', params: { game_id: gameId, cryp_ids: crypIds } }); send({ method: 'game_ready', params: { id } });
} }
function sendSpecForget(id, spec) { function sendSpecForget(id, spec) {
@ -331,7 +331,7 @@ function createSocket(events) {
sendAccountInstances, sendAccountInstances,
sendAccountZone, sendAccountZone,
sendGameState, sendGameState,
sendGameJoin, sendGameReady,
sendGameSkill, sendGameSkill,
sendGameTarget, sendGameTarget,
sendCrypSpawn, sendCrypSpawn,

View File

@ -558,7 +558,7 @@ impl Game {
{ {
let winner = self.players.iter().find(|t| t.cryps.iter().any(|c| !c.is_ko())); let winner = self.players.iter().find(|t| t.cryps.iter().any(|c| !c.is_ko()));
match winner { match winner {
Some(w) => self.log.push(format!("Winner: {:}", w.id)), Some(w) => self.log.push(format!("Winner: {:}", w.name)),
None => self.log.push(format!("Game was drawn.")), None => self.log.push(format!("Game was drawn.")),
}; };
} }
@ -585,6 +585,7 @@ impl Game {
player.add_warning(); player.add_warning();
if player.warnings >= 3 { if player.warnings >= 3 {
player.forfeit(); player.forfeit();
self.log.push(format!("{:?} forfeited.", player.name));
} }
} }
} }
@ -818,8 +819,8 @@ pub fn game_skill(params: GameSkillParams, tx: &mut Transaction, account: &Accou
Ok(game) Ok(game)
} }
pub fn game_ready(params: GameSkillParams, tx: &mut Transaction, account: &Account) -> Result<Game, Error> { pub fn game_ready(params: GameStateParams, tx: &mut Transaction, account: &Account) -> Result<Game, Error> {
let mut game = game_get(tx, params.game_id)?; let mut game = game_get(tx, params.id)?;
game.player_ready(account.id)?; game.player_ready(account.id)?;

View File

@ -16,7 +16,7 @@ use failure::err_msg;
use net::Db; use net::Db;
use cryp::{Cryp, cryp_spawn}; use cryp::{Cryp, cryp_spawn};
use game::{Game, game_state, game_skill}; use game::{Game, game_state, game_skill, game_ready};
use account::{Account, account_create, account_login, account_from_token, account_cryps, account_instances}; use account::{Account, account_create, account_login, account_from_token, account_cryps, account_instances};
use skill::{Skill}; use skill::{Skill};
// use zone::{Zone, zone_create, zone_join, zone_close}; // use zone::{Zone, zone_create, zone_join, zone_close};
@ -71,6 +71,7 @@ impl Rpc {
"game_state" => Rpc::game_state(data, &mut tx, account.unwrap(), client), "game_state" => Rpc::game_state(data, &mut tx, account.unwrap(), client),
"game_skill" => Rpc::game_skill(data, &mut tx, account.unwrap(), client), "game_skill" => Rpc::game_skill(data, &mut tx, account.unwrap(), client),
"game_ready" => Rpc::game_ready(data, &mut tx, account.unwrap(), client),
"instance_join" => Rpc::instance_join(data, &mut tx, account.unwrap(), client), "instance_join" => Rpc::instance_join(data, &mut tx, account.unwrap(), client),
"instance_ready" => Rpc::instance_ready(data, &mut tx, account.unwrap(), client), "instance_ready" => Rpc::instance_ready(data, &mut tx, account.unwrap(), client),
@ -140,6 +141,18 @@ impl Rpc {
return Ok(game_response); return Ok(game_response);
} }
fn game_ready(data: Vec<u8>, tx: &mut Transaction, account: Account, _client: &mut WebSocket<TcpStream>) -> Result<RpcResponse, Error> {
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_ready(msg.params, tx, &account)?)
};
return Ok(game_response);
}
fn cryp_spawn(data: Vec<u8>, tx: &mut Transaction, account: Account, client: &mut WebSocket<TcpStream>) -> Result<RpcResponse, Error> { fn cryp_spawn(data: Vec<u8>, tx: &mut Transaction, account: Account, client: &mut WebSocket<TcpStream>) -> Result<RpcResponse, Error> {
let msg = from_slice::<CrypSpawnMsg>(&data).or(Err(err_msg("invalid params")))?; let msg = from_slice::<CrypSpawnMsg>(&data).or(Err(err_msg("invalid params")))?;