Skip to content
Closed
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 = "4.0.0"
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"] }
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ 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?;
Expand Down
14 changes: 7 additions & 7 deletions src/model/snake.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ use crate::constants::SNAKE_COLOR;

#[derive(Debug, PartialEq, Eq, Clone)]
pub struct Snake {
fields: VecDeque<Pos>,
dir: Delta,
fields: VecDeque<Pos<i32>>,
dir: Delta<i32>,
}

impl Snake {
pub fn from_initial_length(length: usize) -> Self {
let mut pos: Pos = LIGHTHOUSE_RECT.sample_random().unwrap();
let mut pos: Pos<i32> = LIGHTHOUSE_RECT.sample_random().unwrap();
let dir = Delta::random_cardinal();

let mut fields = VecDeque::new();
Expand All @@ -24,9 +24,9 @@ impl Snake {
Self { fields, dir }
}

pub fn head(&self) -> Pos { *self.fields.front().unwrap() }
pub fn head(&self) -> Pos<i32> { *self.fields.front().unwrap() }

pub fn back(&self) -> Pos { *self.fields.back().unwrap() }
pub fn back(&self) -> Pos<i32> { *self.fields.back().unwrap() }

pub fn grow(&mut self) {
self.fields.push_back(LIGHTHOUSE_RECT.wrap(self.back() - self.dir));
Expand All @@ -42,7 +42,7 @@ impl Snake {
self.fields.iter().collect::<HashSet<_>>().len() < self.fields.len()
}

pub fn rotate_head(&mut self, dir: Delta) {
pub fn rotate_head(&mut self, dir: Delta<i32>) {
self.dir = dir;
}

Expand All @@ -56,7 +56,7 @@ impl Snake {
self.fields.len()
}

pub fn random_fruit_pos(&self) -> Option<Pos> {
pub fn random_fruit_pos(&self) -> Option<Pos<i32>> {
let fields = self.fields.iter().collect::<HashSet<_>>();
if fields.len() >= LIGHTHOUSE_SIZE {
None
Expand Down
2 changes: 1 addition & 1 deletion src/model/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use super::Snake;
#[derive(Debug, PartialEq, Eq, Clone)]
pub struct State {
pub snake: Snake,
pub fruit: Pos,
pub fruit: Pos<i32>,
}

impl State {
Expand Down
2 changes: 1 addition & 1 deletion src/updater.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use tracing::debug;

use crate::{constants::UPDATE_INTERVAL, model::State};

pub async fn run(mut lh: Lighthouse<TokioWebSocket>, shared_state: Arc<Mutex<State>>) -> Result<()> {
pub async fn run(lh: Lighthouse<TokioWebSocket>, shared_state: Arc<Mutex<State>>) -> Result<()> {
loop {
// Update the snake and render it
let frame = {
Expand Down