Move client message processing into client agent
This commit is contained in:
parent
b6c3c69777
commit
1fdea3e454
2 changed files with 29 additions and 19 deletions
|
@ -6,11 +6,26 @@ use super::*;
|
||||||
|
|
||||||
pub struct ClientAgent {
|
pub struct ClientAgent {
|
||||||
server: Handle,
|
server: Handle,
|
||||||
|
player_id: PlayerId,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ClientAgent {
|
impl ClientAgent {
|
||||||
pub fn new(server: Handle) -> ClientAgent {
|
pub fn new(server: Handle) -> ClientAgent {
|
||||||
ClientAgent { server }
|
ClientAgent {
|
||||||
|
server,
|
||||||
|
player_id: 0,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn process_message(&self, msg: ClientMessage) {
|
||||||
|
trace!("Client#{} message: {:?}", self.player_id, &msg);
|
||||||
|
|
||||||
|
match msg {
|
||||||
|
ClientMessage::Input { local_input } => self.server.lock().await.input(self.player_id, local_input),
|
||||||
|
// for now, anybody can set the state
|
||||||
|
ClientMessage::SetState { new_state } => self.server.lock().await.set_state(new_state),
|
||||||
|
ClientMessage::GetState { .. } => {}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn run(
|
pub async fn run(
|
||||||
|
@ -23,7 +38,7 @@ impl ClientAgent {
|
||||||
let (sender, mut receiver) = channel(CHANNEL_BUFFER);
|
let (sender, mut receiver) = channel(CHANNEL_BUFFER);
|
||||||
|
|
||||||
// register player
|
// register player
|
||||||
let player_id = self
|
self.player_id = self
|
||||||
.server
|
.server
|
||||||
.lock()
|
.lock()
|
||||||
.await
|
.await
|
||||||
|
@ -37,7 +52,7 @@ impl ClientAgent {
|
||||||
client_message = socket.next() => {
|
client_message = socket.next() => {
|
||||||
match client_message {
|
match client_message {
|
||||||
Some(msg) =>
|
Some(msg) =>
|
||||||
self.server.lock().await.process_message(player_id, msg?),
|
self.process_message(msg?).await,
|
||||||
None => break Ok(()),
|
None => break Ok(()),
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
@ -51,7 +66,7 @@ impl ClientAgent {
|
||||||
.await;
|
.await;
|
||||||
|
|
||||||
// deregister player, whether normally or due to error
|
// deregister player, whether normally or due to error
|
||||||
self.server.lock().await.remove_player(player_id);
|
self.server.lock().await.remove_player(self.player_id);
|
||||||
|
|
||||||
result
|
result
|
||||||
}
|
}
|
||||||
|
|
|
@ -96,22 +96,17 @@ impl Server {
|
||||||
Ok(player_id)
|
Ok(player_id)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn process_message(&mut self, player: PlayerId, msg: ClientMessage) {
|
pub fn input(&mut self, player: PlayerId, local_input: Value) {
|
||||||
trace!("Client#{} message: {:?}", player, &msg);
|
|
||||||
|
|
||||||
match msg {
|
|
||||||
ClientMessage::Input { local_input } => {
|
|
||||||
if let Some(Some(player)) = self.players.get_mut(player) {
|
if let Some(Some(player)) = self.players.get_mut(player) {
|
||||||
player.input = local_input;
|
player.input = local_input;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// for now, anybody can set the state
|
|
||||||
ClientMessage::SetState { new_state } => self.broadcast(ServerMessage::SetState {
|
pub fn set_state(&mut self, new_state: Value) {
|
||||||
|
self.broadcast(ServerMessage::SetState {
|
||||||
player_id: None,
|
player_id: None,
|
||||||
state: new_state,
|
state: new_state,
|
||||||
}),
|
})
|
||||||
ClientMessage::GetState { .. } => {}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn tick(&mut self, tick: Instant) {
|
fn tick(&mut self, tick: Instant) {
|
||||||
|
|
Loading…
Reference in a new issue