breakage inc
This commit is contained in:
parent
1940c437b0
commit
fd518ba4e2
@ -16,6 +16,7 @@ use pg::{Db, PgPool};
|
||||
use rpc::RpcMessage;
|
||||
use warden::{GameEvent};
|
||||
|
||||
pub type EventsTx = Sender<Event>;
|
||||
type Id = usize;
|
||||
|
||||
pub struct Events {
|
||||
@ -23,6 +24,7 @@ pub struct Events {
|
||||
rx: Receiver<Event>,
|
||||
|
||||
warden: Sender<GameEvent>,
|
||||
queue: Option<Uuid>,
|
||||
|
||||
clients: HashMap<Id, WsClient>,
|
||||
}
|
||||
@ -39,7 +41,7 @@ pub enum Event {
|
||||
Push(Uuid, RpcMessage),
|
||||
|
||||
// client events
|
||||
Queue(Id)
|
||||
Queue(Id, Uuid),
|
||||
}
|
||||
|
||||
struct WsClient {
|
||||
@ -55,6 +57,7 @@ impl Events {
|
||||
tx,
|
||||
rx,
|
||||
warden,
|
||||
queue: None,
|
||||
clients: HashMap::new(),
|
||||
}
|
||||
}
|
||||
@ -75,6 +78,13 @@ impl Events {
|
||||
}
|
||||
}
|
||||
|
||||
fn get_client(&mut self, id: Id) -> Result<&mut WsClient, Error> {
|
||||
match self.clients.get_mut(&id) {
|
||||
Some(c) => Ok(c),
|
||||
None => Err(format_err!("connection not found id={:?}", id)),
|
||||
}
|
||||
}
|
||||
|
||||
fn remove_client(&mut self, id: Id) {
|
||||
self.clients.remove(&id);
|
||||
}
|
||||
@ -152,8 +162,19 @@ impl Events {
|
||||
Ok(())
|
||||
},
|
||||
|
||||
Event::Queue(id) => {
|
||||
info!("queue id={:?}", id);
|
||||
Event::Queue(id, account) => {
|
||||
info!("queue id={:?} account={:?}", id, account);
|
||||
|
||||
self.queue = match self.queue {
|
||||
Some(id) => {
|
||||
info!("game queue pair a={:?} b={:?}", account, id);
|
||||
None
|
||||
},
|
||||
None => {
|
||||
info!("joined game queue id={:?} account={:?}", id, account);
|
||||
Some(account)
|
||||
},
|
||||
};
|
||||
|
||||
Ok(())
|
||||
},
|
||||
|
||||
@ -13,6 +13,7 @@ use chrono::Duration;
|
||||
|
||||
use account::Account;
|
||||
use account;
|
||||
use events::EventsTx;
|
||||
use player::{Player, player_create};
|
||||
use construct::{Construct, construct_get};
|
||||
use mob::{bot_player, instance_mobs};
|
||||
@ -746,6 +747,10 @@ pub fn instance_game_finished(tx: &mut Transaction, game: &Game, instance_id: Uu
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn instance_queue(events: &EventsTx, account: &Account) -> Result<RpcMessage, Error> {
|
||||
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
@ -6,15 +6,19 @@ use uuid::Uuid;
|
||||
use failure::Error;
|
||||
use failure::err_msg;
|
||||
|
||||
use crossbeam_channel::{Sender};
|
||||
|
||||
use account;
|
||||
use pg::{Db};
|
||||
use events::{Event};
|
||||
use construct::{Construct};
|
||||
use game::{Game, game_state, game_skill, game_ready};
|
||||
use account::{Account, account_constructs};
|
||||
use skill::{Skill, dev_resolve, Resolutions};
|
||||
use instance::{Instance, instance_state, instance_practice, instance_ready, instance_pvp};
|
||||
use instance::{Instance, instance_state, instance_practice, instance_ready, instance_queue};
|
||||
use vbox::{vbox_accept, vbox_apply, vbox_discard, vbox_combine, vbox_reclaim, vbox_unequip};
|
||||
use item::{Item, ItemInfoCtr, item_info};
|
||||
use websocket::{Connection};
|
||||
|
||||
use mtx;
|
||||
|
||||
@ -34,6 +38,10 @@ pub enum RpcMessage {
|
||||
|
||||
DevResolutions(Resolutions),
|
||||
|
||||
QueueRequested,
|
||||
QueueJoined,
|
||||
QueueCancelled,
|
||||
|
||||
Error(String),
|
||||
}
|
||||
|
||||
@ -57,7 +65,7 @@ enum RpcRequest {
|
||||
AccountConstructs {},
|
||||
AccountSetTeam { ids: Vec<Uuid> },
|
||||
|
||||
InstancePvp {},
|
||||
InstanceQueue {},
|
||||
InstancePractice {},
|
||||
InstanceReady { instance_id: Uuid },
|
||||
InstanceState { instance_id: Uuid },
|
||||
@ -70,7 +78,7 @@ enum RpcRequest {
|
||||
VboxReclaim { instance_id: Uuid, index: usize },
|
||||
}
|
||||
|
||||
pub fn receive(data: Vec<u8>, db: &Db, begin: Instant, account: &Option<Account>) -> Result<RpcMessage, Error> {
|
||||
pub fn receive(data: Vec<u8>, db: &Db, connection: &Connection, begin: Instant, account: &Option<Account>) -> Result<RpcMessage, Error> {
|
||||
// cast the msg to this type to receive method name
|
||||
match from_slice::<RpcRequest>(&data) {
|
||||
Ok(v) => {
|
||||
@ -119,8 +127,8 @@ pub fn receive(data: Vec<u8>, db: &Db, begin: Instant, account: &Option<Account>
|
||||
RpcRequest::GameReady { id } =>
|
||||
Ok(RpcMessage::GameState(game_ready(&mut tx, account, id)?)),
|
||||
|
||||
// RpcRequest::InstanceQueue {} =>
|
||||
// Ok(RpcMessage::QueueState(instance_queue(&mut tx, account)?)),
|
||||
RpcRequest::InstanceQueue {} =>
|
||||
Ok(RpcMessage::QueueRequested),
|
||||
RpcRequest::InstancePractice {} =>
|
||||
Ok(RpcMessage::InstanceState(instance_practice(&mut tx, account)?)),
|
||||
|
||||
|
||||
@ -73,7 +73,7 @@ impl Handler for Connection {
|
||||
let begin = Instant::now();
|
||||
let db_connection = self.pool.get().unwrap();
|
||||
|
||||
match rpc::receive(msg, &db_connection, begin, &self.account) {
|
||||
match rpc::receive(msg, &db_connection, &self, begin, &self.account) {
|
||||
Ok(reply) => {
|
||||
// if the user queries the state of something
|
||||
// we tell events to push updates to them
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user