diff --git a/backend/src/grid.rs b/backend/src/grid.rs index ef20ebf..4c9e115 100644 --- a/backend/src/grid.rs +++ b/backend/src/grid.rs @@ -3,6 +3,10 @@ use crate::space::Space; use crate::Coordinate; use thiserror::Error; +fn distance([a, b]: Coordinate, [x, y]: Coordinate) -> u64 { + (a.abs_diff(x)).max(b.abs_diff(y)) +} + /// The direction in which to grow. For example, going from 3 to 9 would give a `GrowDirection::Expand`. enum GrowDirection { Shrink, @@ -79,6 +83,32 @@ impl Grid { self.spaces.get(&coordinate) } + pub fn get_neighbours_n_away(&self, coordinate: Coordinate, n: u64) -> Vec<&Space> { + let mut coords: Vec = Vec::new(); + let mut stack: Vec = self.neighbour_coords_in_bounds(coordinate); + while let Some(coord) = stack.pop() { + if coords.contains(&coord) { + continue; + } + coords.push(coord); + stack.extend( + self.neighbour_coords_in_bounds(coord) + .iter() + .filter(|c| + distance(coordinate, **c) <= n && distance(coordinate, **c) > distance(coord, **c) + ) + ) + } + let mut to_return: Vec<&Space> = Vec::new(); + for coord in coords { + match self.spaces.get(&coord) { + Some(space) => to_return.push(space), + None => (), + } + } + to_return + } + fn neighbour_coords_in_bounds(&self, coordinate: Coordinate) -> Vec { let coordinates: [[Option; 2]; 8] = [ [coordinate[0].checked_add(1), Some(coordinate[1])], @@ -262,4 +292,14 @@ mod tests { let expand_res = grid.expand_grid(u64::MAX - 1); assert!(expand_res.is_err()); } + + #[test] + fn find_neighbours_2_away() { + let grid = grid_3x3(); + let neighbours_2_away = grid.get_neighbours_n_away([1, 1], 2).sort(); + assert_eq!( + grid.spaces.values().collect::>().sort(), + neighbours_2_away, + ) + } } diff --git a/backend/src/kube.rs b/backend/src/kube.rs index 4f7733e..e5f755b 100644 --- a/backend/src/kube.rs +++ b/backend/src/kube.rs @@ -24,7 +24,7 @@ impl KubeId { } } -#[derive(PartialEq, Debug, Clone, Serialize, Deserialize)] +#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)] pub struct Kube { pub id: KubeId, pub name: String, diff --git a/backend/src/lib.rs b/backend/src/lib.rs index 4a3e81a..ba733cf 100644 --- a/backend/src/lib.rs +++ b/backend/src/lib.rs @@ -3,5 +3,6 @@ pub mod space; pub mod kube; pub mod player; pub mod llm; +pub mod local_grid; type Coordinate = [u64; 2]; diff --git a/backend/src/local_grid.rs b/backend/src/local_grid.rs new file mode 100644 index 0000000..c986e25 --- /dev/null +++ b/backend/src/local_grid.rs @@ -0,0 +1,18 @@ +use serde::{Deserialize, Serialize}; +use crate::Coordinate; +use crate::grid::Grid; +use crate::space::Space; + +#[derive(Debug, Clone, Serialize, Deserialize)] +pub struct LocalGrid { + spaces: Vec, +} +impl LocalGrid { + pub fn from_grid_and_coord(grid: &Grid, coordinate: Coordinate, local_size: u64) -> LocalGrid { + let mut spaces: Vec = Vec::new(); + for space in grid.get_neighbours_n_away(coordinate, local_size) { + spaces.push(space.clone()) + } + LocalGrid { spaces } + } +} diff --git a/backend/src/player.rs b/backend/src/player.rs index 6ce81b0..13144a4 100644 --- a/backend/src/player.rs +++ b/backend/src/player.rs @@ -1,4 +1,6 @@ -#[derive(Debug, PartialEq, Clone)] +use serde::{Deserialize, Serialize}; + +#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Clone, Serialize, Deserialize)] pub struct Player { uuid: String, username: String, diff --git a/backend/src/space.rs b/backend/src/space.rs index 228ba8a..fb75e8e 100644 --- a/backend/src/space.rs +++ b/backend/src/space.rs @@ -1,8 +1,10 @@ +use serde::{Deserialize, Serialize}; + use crate::kube; use crate::player; use crate::Coordinate; -#[derive(Debug, PartialEq, Clone)] +#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Clone, Serialize, Deserialize)] pub struct Space { pub coordinate: Coordinate, pub contains: SpaceKind, @@ -16,7 +18,7 @@ impl Space { } } -#[derive(Debug, PartialEq, Clone)] +#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Clone, Serialize, Deserialize)] #[allow(clippy::module_name_repetitions)] pub enum SpaceKind { Kube(kube::Kube),