Browse Source

refactored small amount of code

main
Thomas Johnson 6 months ago
parent
commit
74198b24eb
  1. 15
      src/lib.rs
  2. 32
      src/server.rs

15
src/lib.rs

@ -60,17 +60,21 @@ impl Game {
}
}
pub fn try_move(&mut self, pid: PlayerId, to: NodeIndex) -> bool {
pub fn try_move(
&mut self,
pid: PlayerId,
to: NodeIndex,
) -> Vec<(Option<PlayerId>, ClientUpdate)> {
if self
.cities
.find_edge(self.players[pid].location, to)
.is_none()
{
return false;
return vec![(Some(pid), ClientUpdate::InvalidNode)];
}
self.players[pid].location = to;
// TODO: active scan
true
vec![(None, ClientUpdate::WaitMove { by: pid })]
}
/// Collect intel and reveal anyone on the current node.
@ -190,7 +194,7 @@ impl Game {
}
pub fn wait(&mut self, pid: PlayerId) -> Vec<(Option<PlayerId>, ClientUpdate)> {
vec![(None, ClientUpdate::WaitMove { by: Some(pid) })]
vec![(None, ClientUpdate::WaitMove { by: pid })]
}
pub fn capture(&mut self, pid: PlayerId) -> Vec<(Option<PlayerId>, ClientUpdate)> {
@ -276,6 +280,7 @@ impl Game {
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum ClientUpdate {
InvalidNode,
Death {
by: PlayerId,
of: PlayerId,
@ -285,7 +290,7 @@ pub enum ClientUpdate {
at: Option<NodeIndex>,
},
WaitMove {
by: Option<PlayerId>,
by: PlayerId,
},
Capture {
by: PlayerId,

32
src/server.rs

@ -237,6 +237,10 @@ async fn do_action(
let mut guard = state.lock();
let mut gs = guard.games.get_mut(&gid).expect("no homie");
if pid >= gs.pid_channels.len() {
return HttpResponse::InternalServerError().body("bad player ID");
}
let seqno = gs.game.lock().updates[pid].len();
match body.as_ref() {
b"strike" => {
let upds = gs.game.lock().strike(pid);
@ -264,32 +268,28 @@ async fn do_action(
}
_ => match body.as_ref().split(|c| b':' == *c).collect::<Vec<_>>()[..] {
[b"move", to] => {
return HttpResponse::Ok().body(
// TODO: fix try_move to give events
gs.game
.lock()
.try_move(
pid,
NodeIndex::new(
std::str::from_utf8(to)
.expect("utf8")
.parse()
.expect("bad location"),
),
)
.to_string(),
let upds = gs.game.lock().try_move(
pid,
NodeIndex::new(
std::str::from_utf8(to)
.expect("utf8")
.parse()
.expect("bad location"),
),
);
distribute_updates(&mut gs, upds).await;
}
[b"reveal", who] => {
gs.game.lock().reveal(
let upds = gs.game.lock().reveal(
pid, // TODO
None,
);
distribute_updates(&mut gs, upds).await;
}
_ => return HttpResponse::InternalServerError().body("no such action"),
},
}
HttpResponse::Ok().body(())
HttpResponse::Ok().body(seqno.to_string())
}
#[actix_web::main]

Loading…
Cancel
Save