abandon rpc

This commit is contained in:
Mashy 2019-09-11 15:29:52 +10:00
parent 9874334fc3
commit 60bd7f65ba
7 changed files with 61 additions and 11 deletions

View File

@ -27,8 +27,13 @@ const addState = connect(
return ws.sendGameSkillClear(game.id); return ws.sendGameSkillClear(game.id);
} }
function sendAbandon() {
return ws.sendInstanceAbandon(game.instance);
}
return { return {
game, game,
sendAbandon,
sendGameSkillClear, sendGameSkillClear,
sendReady, sendReady,
account, account,
@ -51,6 +56,7 @@ const addState = connect(
function Controls(args) { function Controls(args) {
const { const {
account, account,
sendAbandon,
game, game,
animating, animating,
sendGameSkillClear, sendGameSkillClear,
@ -104,7 +110,7 @@ function Controls(args) {
<div class="controls"> <div class="controls">
<PlayerBox player={opponent}/> <PlayerBox player={opponent}/>
{game.phase === 'Finish' ? quitBtn : readyBtn} {game.phase === 'Finish' ? quitBtn : readyBtn}
<PlayerBox player={player} isPlayer={true} isGame={true} clear={sendGameSkillClear}/> <PlayerBox player={player} isPlayer={true} isGame={true} clear={sendGameSkillClear} abandon={sendAbandon}/>
</div> </div>
</aside> </aside>
); );

View File

@ -20,7 +20,12 @@ const addState = connect(
return false; return false;
} }
function sendAbandon() {
return ws.sendInstanceAbandon(instance.id);
}
return { return {
sendAbandon,
instance, instance,
sendReady, sendReady,
account, account,
@ -41,6 +46,7 @@ const addState = connect(
function Controls(args) { function Controls(args) {
const { const {
account, account,
sendAbandon,
instance, instance,
sendReady, sendReady,
leave, leave,
@ -77,13 +83,18 @@ function Controls(args) {
</div> </div>
); );
const ready = instance.phase !== 'Finished'
? <button class="ready" onClick={() => sendReady()}>Ready</button>
: <button class="ready" onClick={leave}>Leave</button>
return ( return (
<aside> <aside>
{timer} {timer}
<div class="controls"> <div class="controls">
<PlayerBox player={opponent} /> <PlayerBox player={opponent} />
<button class="ready" onClick={() => sendReady()}>Ready</button> {ready}
<PlayerBox player={player} isPlayer={true} leave={leave}/> <PlayerBox player={player} isPlayer={true} abandon={sendAbandon}/>
</div> </div>
</aside> </aside>
); );

View File

@ -2,11 +2,11 @@ const preact = require('preact');
function Scoreboard(args) { function Scoreboard(args) {
const { const {
abandon,
isPlayer, isPlayer,
player, player,
isGame, isGame,
clear, clear,
leave,
} = args; } = args;
let scoreText = () => { let scoreText = () => {
@ -41,7 +41,7 @@ function Scoreboard(args) {
{(isPlayer && isGame) ? <button onClick={clear}>Clear</button> : null} {(isPlayer && isGame) ? <button onClick={clear}>Clear</button> : null}
</div> </div>
<div> <div>
{leave ? <button onClick={leave}>Leave</button> : null} {abandon ? <button onClick={abandon}>Abandon</button> : null}
</div> </div>
</div> </div>
); );

View File

@ -130,6 +130,10 @@ function createSocket(events) {
send(['InstanceReady', { instance_id: instanceId }]); send(['InstanceReady', { instance_id: instanceId }]);
} }
function sendInstanceAbandon(instanceId) {
send(['InstanceAbandon', { instance_id: instanceId }]);
}
function sendMtxApply(constructId, mtx, name) { function sendMtxApply(constructId, mtx, name) {
send(['MtxConstructApply', { construct_id: constructId, mtx, name }]); send(['MtxConstructApply', { construct_id: constructId, mtx, name }]);
if (mtx === 'Rename') { if (mtx === 'Rename') {
@ -205,7 +209,7 @@ function createSocket(events) {
let pongTimeout; let pongTimeout;
function onPong() { function onPong() {
events.setPing(Date.now() - ping); events.setPing(Date.now() - ping);
// pongTimeout = setTimeout(sendPing, 1000); pongTimeout = setTimeout(sendPing, 10000);
} }
// ------------- // -------------
@ -324,6 +328,7 @@ function createSocket(events) {
sendGameSkillClear, sendGameSkillClear,
sendGameTarget, sendGameTarget,
sendInstanceAbandon,
sendInstanceReady, sendInstanceReady,
sendInstancePractice, sendInstancePractice,
sendInstanceQueue, sendInstanceQueue,

View File

@ -316,10 +316,6 @@ impl Instance {
} }
fn finish_condition(&mut self) -> bool { fn finish_condition(&mut self) -> bool {
if self.rounds.len() < 4 {
return false;
}
// tennis // tennis
for player in self.players.iter() { for player in self.players.iter() {
if player.score == Score::Win { if player.score == Score::Win {
@ -327,6 +323,10 @@ impl Instance {
return true; return true;
} }
} }
// Game defaults to lose otherwise
if self.rounds.len() < 4 {
return false;
}
// both players afk // both players afk
if self.players.iter().all(|p| p.score == Score::Zero) { if self.players.iter().all(|p| p.score == Score::Zero) {
@ -458,6 +458,13 @@ impl Instance {
.ok_or(err_msg("account not in instance")) .ok_or(err_msg("account not in instance"))
} }
fn account_opponent(&mut self, account: Uuid) -> Result<&mut Player, Error> {
self.players
.iter_mut()
.find(|p| p.id != account)
.ok_or(err_msg("opponent not in instance"))
}
pub fn vbox_action_allowed(&self, account: Uuid) -> Result<(), Error> { pub fn vbox_action_allowed(&self, account: Uuid) -> Result<(), Error> {
if self.players.iter().find(|p| p.id == account).is_none() { if self.players.iter().find(|p| p.id == account).is_none() {
return Err(err_msg("player not in this instance")); return Err(err_msg("player not in this instance"));
@ -739,6 +746,14 @@ pub fn pvp(tx: &mut Transaction, a: &Account, b: &Account) -> Result<Instance, E
instance_update(tx, instance) instance_update(tx, instance)
} }
pub fn instance_abandon(tx: &mut Transaction, account: &Account, instance_id: Uuid) -> Result<RpcMessage, Error> {
let mut instance = instance_get(tx, instance_id)?;
instance.account_player(account.id)?.set_lose();
instance.account_opponent(account.id)?.set_win();
instance.next_round();
Ok(RpcMessage::InstanceState(instance_update(tx, instance)?))
}
pub fn instance_ready(tx: &mut Transaction, account: &Account, instance_id: Uuid) -> Result<RpcMessage, Error> { pub fn instance_ready(tx: &mut Transaction, account: &Account, instance_id: Uuid) -> Result<RpcMessage, Error> {
let mut instance = instance_get(tx, instance_id)?; let mut instance = instance_get(tx, instance_id)?;

View File

@ -99,6 +99,16 @@ impl Player {
self self
} }
pub fn set_win(&mut self) -> &mut Player {
self.score = Score::Win;
self
}
pub fn set_lose(&mut self) -> &mut Player {
self.score = Score::Lose;
self
}
pub fn construct_get(&mut self, id: Uuid) -> Result<&mut Construct, Error> { pub fn construct_get(&mut self, id: Uuid) -> Result<&mut Construct, Error> {
self.constructs.iter_mut().find(|c| c.id == id).ok_or(err_msg("construct not found")) self.constructs.iter_mut().find(|c| c.id == id).ok_or(err_msg("construct not found"))
} }

View File

@ -21,7 +21,7 @@ use account;
use construct::{Construct}; use construct::{Construct};
use events::{Event}; use events::{Event};
use game::{Game, game_state, game_skill, game_skill_clear, game_ready}; use game::{Game, game_state, game_skill, game_skill_clear, game_ready};
use instance::{Instance, instance_state, instance_practice, instance_ready}; use instance::{Instance, instance_state, instance_practice, instance_ready, instance_abandon};
use item::{Item, ItemInfoCtr, item_info}; use item::{Item, ItemInfoCtr, item_info};
use mtx; use mtx;
use mail; use mail;
@ -88,6 +88,7 @@ enum RpcRequest {
InstanceQueue {}, InstanceQueue {},
InstancePractice {}, InstancePractice {},
InstanceAbandon { instance_id: Uuid },
InstanceReady { instance_id: Uuid }, InstanceReady { instance_id: Uuid },
InstanceState { instance_id: Uuid }, InstanceState { instance_id: Uuid },
@ -184,6 +185,8 @@ impl Connection {
Ok(instance_ready(&mut tx, account, instance_id)?), Ok(instance_ready(&mut tx, account, instance_id)?),
RpcRequest::InstanceState { instance_id } => RpcRequest::InstanceState { instance_id } =>
Ok(instance_state(&mut tx, instance_id)?), Ok(instance_state(&mut tx, instance_id)?),
RpcRequest::InstanceAbandon { instance_id } =>
Ok(instance_abandon(&mut tx, account, instance_id)?),
RpcRequest::VboxAccept { instance_id, group, index } => RpcRequest::VboxAccept { instance_id, group, index } =>
Ok(RpcMessage::InstanceState(vbox_accept(&mut tx, account, instance_id, group, index)?)), Ok(RpcMessage::InstanceState(vbox_accept(&mut tx, account, instance_id, group, index)?)),