diff --git a/Cargo.lock b/Cargo.lock index 0beff5e..5340e4a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -661,9 +661,9 @@ checksum = "875b3680cb2f8f71bdcf9a30f38d48282f5d3c95cbf9b3fa57269bb5d5c06828" [[package]] name = "lighthouse-client" -version = "5.0.1" +version = "5.0.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b45cbc92c5f1bc3bec7f803f9b8b41480a9b4ca2e4b680ed1cf8826417781960" +checksum = "18691375aad352822d0a1839a167378db47a59fb2c65aef29b2c1e567bf25203" dependencies = [ "async-tungstenite", "futures", @@ -680,9 +680,9 @@ dependencies = [ [[package]] name = "lighthouse-protocol" -version = "5.0.1" +version = "5.0.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6697f71915df7d895bb21968c8bd81eeb759e3f03ab8d6b633c8ef29ed67b44c" +checksum = "d1f5b333204f358e5a1c72edd5c0b92d06a886982abe0e229cb2a3d2f1ecd9ca" dependencies = [ "rand 0.8.5", "rmpv", diff --git a/Cargo.toml b/Cargo.toml index c960642..aa0dfa3 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -13,7 +13,7 @@ anyhow = "1.0.81" clap = { version = "4.5.3", features = ["derive", "env"] } dotenvy = "0.15.7" futures = "0.3.30" -lighthouse-client = "5.0.1" +lighthouse-client = "5.0.2" rand = "0.9.0" tokio = { version = "1.36.0", features = ["rt", "macros", "time"] } tracing = "0.1.40" diff --git a/src/controller.rs b/src/controller.rs index 4ecf795..0002d23 100644 --- a/src/controller.rs +++ b/src/controller.rs @@ -2,40 +2,19 @@ use std::sync::Arc; use anyhow::Result; use futures::{lock::Mutex, prelude::*, Stream}; -use lighthouse_client::protocol::{Delta, GamepadButtonEvent, GamepadControlEvent, GamepadEvent, InputEvent, KeyEvent, ServerMessage}; +use lighthouse_client::protocol::{InputEvent, ServerMessage}; use tracing::debug; use crate::model::State; pub async fn run(mut stream: impl Stream>> + Unpin, shared_state: Arc>) -> Result<()> { while let Some(msg) = stream.next().await { - let opt_dir = match msg?.payload { - InputEvent::Key(KeyEvent { code, down, .. }) if down => match code.as_str() { - "ArrowLeft" => Some(Delta::LEFT), - "ArrowUp" => Some(Delta::UP), - "ArrowRight" => Some(Delta::RIGHT), - "ArrowDown" => Some(Delta::DOWN), - _ => None, - } - InputEvent::Gamepad(GamepadEvent { control, .. }) => match control { - GamepadControlEvent::Button(GamepadButtonEvent { index, down, .. }) if down => match index { - // Per https://w3c.github.io/gamepad/#remapping - 12 => Some(Delta::UP), - 13 => Some(Delta::DOWN), - 14 => Some(Delta::LEFT), - 15 => Some(Delta::RIGHT), - _ => None, - }, - _ => None, - } - _ => None, - }; - + let input_event = msg?.payload; // Update the snake's direction - if let Some(dir) = opt_dir { + if let Some(dir) = input_event.direction() { debug!("Rotating snake head to {:?}", dir); let mut state = shared_state.lock().await; - state.snake.rotate_head(dir); + state.snake.rotate_head(dir.into()); } }