consolidate views for auth and anon
This commit is contained in:
@@ -44,6 +44,7 @@ use http::{AUTH_CLEAR, TOKEN_HEADER};
|
||||
#[derive(Debug,Clone,Serialize)]
|
||||
pub enum RpcMessage {
|
||||
AccountState(Account),
|
||||
AccountAuthenticated(()),
|
||||
AccountConstructs(Vec<Construct>),
|
||||
AccountTeam(Vec<Construct>),
|
||||
AccountInstances(Vec<Instance>),
|
||||
@@ -61,6 +62,7 @@ pub enum RpcMessage {
|
||||
SubscriptionState(Option<Subscription>),
|
||||
|
||||
Pong(()),
|
||||
StartTutorial(()),
|
||||
|
||||
QueueRequested(()),
|
||||
QueueJoined(()),
|
||||
|
||||
@@ -49,9 +49,8 @@ impl User for Anonymous {
|
||||
info!("anonymous connection");
|
||||
|
||||
self.send(RpcMessage::AccountState(self.account.clone()), events, ws)?;
|
||||
self.send(RpcMessage::StartTutorial(()), events, ws)?;
|
||||
self.send(RpcMessage::ItemInfo(item_info()), events, ws)?;
|
||||
// self.send(RpcMessage::StartTutorial(()), events, ws)?;
|
||||
self.send(RpcMessage::InstanceState(pg::instance_demo(&self.account)?), events, ws)?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
@@ -59,70 +58,81 @@ impl User for Anonymous {
|
||||
fn receive(&mut self, data: Vec<u8>, _db: &Db, _begin: Instant, _events: &CbSender<Event>, _stripe: &StripeClient) -> Result<RpcMessage, Error> {
|
||||
match from_slice::<RpcRequest>(&data) {
|
||||
Ok(v) => {
|
||||
let mut instance = match self.instance {
|
||||
Some(ref i) => i.clone(),
|
||||
None => return Err(err_msg("instance missing")),
|
||||
let get_instance = || {
|
||||
match self.instance {
|
||||
Some(ref i) => Ok(i.clone()),
|
||||
None => return Err(err_msg("instance missing")),
|
||||
}
|
||||
};
|
||||
|
||||
let game = match self.game {
|
||||
Some(ref i) => Some(i.clone()),
|
||||
None => None
|
||||
let get_game = || {
|
||||
match self.game {
|
||||
Some(ref i) => Ok(i.clone()),
|
||||
None => return Err(err_msg("game missing")),
|
||||
}
|
||||
};
|
||||
|
||||
match v {
|
||||
RpcRequest::Ping {} => return Ok(RpcMessage::Pong(())),
|
||||
|
||||
|
||||
RpcRequest::InstancePractice {} =>
|
||||
Ok(RpcMessage::InstanceState(pg::instance_demo(&self.account)?)),
|
||||
|
||||
RpcRequest::InstanceReady { instance_id: _ } => {
|
||||
match instance.player_ready(self.account.id)? {
|
||||
match get_instance()?.player_ready(self.account.id)? {
|
||||
Some(g) => Ok(RpcMessage::GameState(g)),
|
||||
None => Ok(RpcMessage::InstanceState(instance)),
|
||||
None => Ok(RpcMessage::InstanceState(get_instance()?)),
|
||||
}
|
||||
},
|
||||
|
||||
RpcRequest::InstanceState { instance_id: _ } =>
|
||||
Ok(RpcMessage::InstanceState(instance)),
|
||||
Ok(RpcMessage::InstanceState(get_instance()?)),
|
||||
|
||||
RpcRequest::InstanceAbandon { instance_id: _ } =>
|
||||
Err(err_msg("don't give up!")),
|
||||
RpcRequest::InstanceAbandon { instance_id: _ } => {
|
||||
let mut instance = get_instance()?;
|
||||
instance.finish();
|
||||
Ok(RpcMessage::InstanceState(instance))
|
||||
},
|
||||
|
||||
RpcRequest::VboxBuy { instance_id: _, group, index, construct_id } =>
|
||||
Ok(RpcMessage::InstanceState(instance.vbox_buy(self.account.id, group, index, construct_id)?)),
|
||||
Ok(RpcMessage::InstanceState(get_instance()?.vbox_buy(self.account.id, group, index, construct_id)?)),
|
||||
|
||||
RpcRequest::VboxApply { instance_id: _, construct_id, index } =>
|
||||
Ok(RpcMessage::InstanceState(instance.vbox_apply(self.account.id, index, construct_id)?)),
|
||||
Ok(RpcMessage::InstanceState(get_instance()?.vbox_apply(self.account.id, index, construct_id)?)),
|
||||
|
||||
RpcRequest::VboxCombine { instance_id: _, inv_indices, vbox_indices } =>
|
||||
Ok(RpcMessage::InstanceState(instance.vbox_combine(self.account.id, inv_indices, vbox_indices)?)),
|
||||
Ok(RpcMessage::InstanceState(get_instance()?.vbox_combine(self.account.id, inv_indices, vbox_indices)?)),
|
||||
|
||||
RpcRequest::VboxRefill { instance_id: _ } =>
|
||||
Ok(RpcMessage::InstanceState(instance.vbox_refill(self.account.id)?)),
|
||||
Ok(RpcMessage::InstanceState(get_instance()?.vbox_refill(self.account.id)?)),
|
||||
|
||||
RpcRequest::VboxRefund { instance_id: _, index } =>
|
||||
Ok(RpcMessage::InstanceState(instance.vbox_refund(self.account.id, index)?)),
|
||||
Ok(RpcMessage::InstanceState(get_instance()?.vbox_refund(self.account.id, index)?)),
|
||||
|
||||
RpcRequest::VboxUnequip { instance_id: _, construct_id, target } =>
|
||||
Ok(RpcMessage::InstanceState(instance.vbox_unequip(self.account.id, target, construct_id, None)?)),
|
||||
Ok(RpcMessage::InstanceState(get_instance()?.vbox_unequip(self.account.id, target, construct_id, None)?)),
|
||||
|
||||
RpcRequest::VboxUnequipApply { instance_id: _, construct_id, target, target_construct_id } =>
|
||||
Ok(RpcMessage::InstanceState(instance.vbox_unequip(self.account.id, target, construct_id, Some(target_construct_id))?)),
|
||||
Ok(RpcMessage::InstanceState(get_instance()?.vbox_unequip(self.account.id, target, construct_id, Some(target_construct_id))?)),
|
||||
|
||||
RpcRequest::GameState { id: _ } =>
|
||||
Ok(RpcMessage::GameState(game.unwrap())),
|
||||
Ok(RpcMessage::GameState(get_game()?)),
|
||||
|
||||
RpcRequest::GameSkill { game_id: _, construct_id, target_construct_id, skill } => {
|
||||
let mut game = game.unwrap();
|
||||
let mut game = get_game()?;
|
||||
game.add_skill(self.account.id, construct_id, target_construct_id, skill)?;
|
||||
Ok(RpcMessage::GameState(game))
|
||||
},
|
||||
|
||||
RpcRequest::GameSkillClear { game_id: _ } => {
|
||||
let mut game = game.unwrap();
|
||||
let mut game = get_game()?;
|
||||
game.clear_skill(self.account.id)?;
|
||||
Ok(RpcMessage::GameState(game))
|
||||
},
|
||||
|
||||
RpcRequest::GameReady { id: _ } => {
|
||||
let mut game = game.unwrap();
|
||||
let mut game = get_game()?;
|
||||
game.player_ready(self.account.id)?;
|
||||
if game.skill_phase_finished() {
|
||||
game = game.resolve_phase_start();
|
||||
@@ -132,10 +142,10 @@ impl User for Anonymous {
|
||||
},
|
||||
|
||||
RpcRequest::GameConcede { game_id: _ } =>
|
||||
Ok(RpcMessage::GameState(game.unwrap().concede(self.account.id)?)),
|
||||
Ok(RpcMessage::GameState(get_game()?.concede(self.account.id)?)),
|
||||
|
||||
RpcRequest::GameOfferDraw { game_id: _ } =>
|
||||
Ok(RpcMessage::GameState(game.unwrap().offer_draw(self.account.id)?)),
|
||||
Ok(RpcMessage::GameState(get_game()?.offer_draw(self.account.id)?)),
|
||||
|
||||
_ => Err(format_err!("unhandled anonymous request request={:?}", v)),
|
||||
}
|
||||
|
||||
@@ -71,6 +71,7 @@ impl User for Authorised {
|
||||
info!("authenticated connection account={:?}", self.account);
|
||||
let a = &self.account;
|
||||
|
||||
ws.send(RpcMessage::AccountAuthenticated(()))?;
|
||||
// tell events we have connected
|
||||
events.send(Event::Connect(self.id, a.clone(), ws.clone()))?;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user