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