Skip to content

Commit

Permalink
Merge pull request #17 from pilksoc/backend
Browse files Browse the repository at this point in the history
Add local grid
  • Loading branch information
djpiper28 authored Mar 3, 2024
2 parents 6048367 + 6551006 commit 3498101
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 4 deletions.
40 changes: 40 additions & 0 deletions backend/src/grid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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<Coordinate> = Vec::new();
let mut stack: Vec<Coordinate> = 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<Coordinate> {
let coordinates: [[Option<u64>; 2]; 8] = [
[coordinate[0].checked_add(1), Some(coordinate[1])],
Expand Down Expand Up @@ -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::<Vec<&Space>>().sort(),
neighbours_2_away,
)
}
}
2 changes: 1 addition & 1 deletion backend/src/kube.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
1 change: 1 addition & 0 deletions backend/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@ pub mod space;
pub mod kube;
pub mod player;
pub mod llm;
pub mod local_grid;

type Coordinate = [u64; 2];
18 changes: 18 additions & 0 deletions backend/src/local_grid.rs
Original file line number Diff line number Diff line change
@@ -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<Space>,
}
impl LocalGrid {
pub fn from_grid_and_coord(grid: &Grid, coordinate: Coordinate, local_size: u64) -> LocalGrid {
let mut spaces: Vec<Space> = Vec::new();
for space in grid.get_neighbours_n_away(coordinate, local_size) {
spaces.push(space.clone())
}
LocalGrid { spaces }
}
}
4 changes: 3 additions & 1 deletion backend/src/player.rs
Original file line number Diff line number Diff line change
@@ -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,
Expand Down
6 changes: 4 additions & 2 deletions backend/src/space.rs
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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),
Expand Down

0 comments on commit 3498101

Please sign in to comment.