abandon rpc
This commit is contained in:
+19
-4
@@ -316,10 +316,6 @@ impl Instance {
|
||||
}
|
||||
|
||||
fn finish_condition(&mut self) -> bool {
|
||||
if self.rounds.len() < 4 {
|
||||
return false;
|
||||
}
|
||||
|
||||
// tennis
|
||||
for player in self.players.iter() {
|
||||
if player.score == Score::Win {
|
||||
@@ -327,6 +323,10 @@ impl Instance {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
// Game defaults to lose otherwise
|
||||
if self.rounds.len() < 4 {
|
||||
return false;
|
||||
}
|
||||
|
||||
// both players afk
|
||||
if self.players.iter().all(|p| p.score == Score::Zero) {
|
||||
@@ -458,6 +458,13 @@ impl 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> {
|
||||
if self.players.iter().find(|p| p.id == account).is_none() {
|
||||
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)
|
||||
}
|
||||
|
||||
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> {
|
||||
let mut instance = instance_get(tx, instance_id)?;
|
||||
|
||||
@@ -99,6 +99,16 @@ impl Player {
|
||||
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> {
|
||||
self.constructs.iter_mut().find(|c| c.id == id).ok_or(err_msg("construct not found"))
|
||||
}
|
||||
|
||||
+4
-1
@@ -21,7 +21,7 @@ use account;
|
||||
use construct::{Construct};
|
||||
use events::{Event};
|
||||
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 mtx;
|
||||
use mail;
|
||||
@@ -88,6 +88,7 @@ enum RpcRequest {
|
||||
|
||||
InstanceQueue {},
|
||||
InstancePractice {},
|
||||
InstanceAbandon { instance_id: Uuid },
|
||||
InstanceReady { instance_id: Uuid },
|
||||
InstanceState { instance_id: Uuid },
|
||||
|
||||
@@ -184,6 +185,8 @@ impl Connection {
|
||||
Ok(instance_ready(&mut tx, account, instance_id)?),
|
||||
RpcRequest::InstanceState { 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 } =>
|
||||
Ok(RpcMessage::InstanceState(vbox_accept(&mut tx, account, instance_id, group, index)?)),
|
||||
|
||||
Reference in New Issue
Block a user