Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
145 changes: 128 additions & 17 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "lisnake"
version = "1.0.3"
version = "2.0.0"
description = "Snake for Project Lighthouse"
edition = "2021"
license = "MIT"
Expand All @@ -13,8 +13,8 @@ anyhow = "1.0.81"
clap = { version = "4.5.3", features = ["derive", "env"] }
dotenvy = "0.15.7"
futures = "0.3.30"
lighthouse-client = "3.0.1"
rand = "0.8.5"
lighthouse-client = "5.0.1"
rand = "0.9.0"
tokio = { version = "1.36.0", features = ["rt", "macros", "time"] }
tracing = "0.1.40"
tracing-subscriber = { version = "0.3.18", features = ["env-filter", "std"] }
50 changes: 26 additions & 24 deletions src/controller.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,38 +2,40 @@ use std::sync::Arc;

use anyhow::Result;
use futures::{lock::Mutex, prelude::*, Stream};
use lighthouse_client::protocol::{Delta, Model, ServerMessage};
use lighthouse_client::protocol::{Delta, GamepadButtonEvent, GamepadControlEvent, GamepadEvent, InputEvent, KeyEvent, ServerMessage};
use tracing::debug;

use crate::model::State;

pub async fn run(mut stream: impl Stream<Item = lighthouse_client::Result<ServerMessage<Model>>> + Unpin, shared_state: Arc<Mutex<State>>) -> Result<()> {
pub async fn run(mut stream: impl Stream<Item = lighthouse_client::Result<ServerMessage<InputEvent>>> + Unpin, shared_state: Arc<Mutex<State>>) -> Result<()> {
while let Some(msg) = stream.next().await {
if let Model::InputEvent(event) = msg?.payload {
if event.is_down {
// Map the key code to a direction vector
let opt_dir = match event.key {
Some(37) => Some(Delta::LEFT),
Some(38) => Some(Delta::UP),
Some(39) => Some(Delta::RIGHT),
Some(40) => Some(Delta::DOWN),
_ => None,
}.or(match event.button {
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
Some(12) => Some(Delta::UP),
Some(13) => Some(Delta::DOWN),
Some(14) => Some(Delta::LEFT),
Some(15) => Some(Delta::RIGHT),
12 => Some(Delta::UP),
13 => Some(Delta::DOWN),
14 => Some(Delta::LEFT),
15 => Some(Delta::RIGHT),
_ => None,
});

// Update the snake's direction
if let Some(dir) = opt_dir {
debug!("Rotating snake head to {:?}", dir);
let mut state = shared_state.lock().await;
state.snake.rotate_head(dir);
}
},
_ => None,
}
_ => None,
};

// Update the snake's direction
if let Some(dir) = opt_dir {
debug!("Rotating snake head to {:?}", dir);
let mut state = shared_state.lock().await;
state.snake.rotate_head(dir);
}
}

Expand Down
6 changes: 3 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,13 @@ async fn main() -> Result<()> {
let auth = Authentication::new(&args.username, &args.token);
let state = Arc::new(Mutex::new(State::new()));

let mut lh = Lighthouse::connect_with_tokio_to(&args.url, auth).await?;
let lh = Lighthouse::connect_with_tokio_to(&args.url, auth).await?;
info!("Connected to the Lighthouse server");

let stream = lh.stream_model().await?;
let input = lh.stream_input().await?;

let updater_handle = task::spawn(updater::run(lh, state.clone()));
let controller_handle = task::spawn(controller::run(stream, state));
let controller_handle = task::spawn(controller::run(input, state));

updater_handle.await.unwrap()?;
controller_handle.await.unwrap()?;
Expand Down
Loading