Skip to content

Commit

Permalink
Merge branch 'main' of github.com:pilksoc/CosmicKube
Browse files Browse the repository at this point in the history
  • Loading branch information
djpiper28 committed Mar 3, 2024
2 parents 3ee4347 + 35fa791 commit b51481e
Show file tree
Hide file tree
Showing 12 changed files with 131 additions and 6 deletions.
53 changes: 53 additions & 0 deletions .github/workflows/build-cache-server.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
name: "Build CosmicKube cache server"
on:
push:
branches:
- main
jobs:
build-server:
name: "Build Cache Server"
runs-on: ubuntu-20.04
permissions:
packages: write
steps:
- name: Checkout
uses: actions/checkout@v4

- name: Download Go
uses: actions/setup-go@v5
with:
go-version: '^1.22'

- name: Build
run: |
cd kube_cache
go build && strip kube_cache
- name: Upload Artifact
uses: actions/upload-artifact@v1
with:
name: cache_server
path: kube_cache/kube_cache


- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v2
- name: Login to DockerHub
uses: docker/login-action@v2
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}

# this will push the docker images to the github container registry
# you will need to give actions permission to push there first
- name: Build and push
id: docker_build
uses: docker/build-push-action@v3
with:
context: .
file: kube_cache/cache.Dockerfile
push: true
tags: |
ghcr.io/pilksoc/kubecache:latest
ghcr.io/pilksoc/kubecache:dev-${{ github.run_number }}
2 changes: 1 addition & 1 deletion .github/workflows/build-game-server.yml
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ jobs:
uses: docker/build-push-action@v3
with:
context: .
file: server.Dockerfile
file: backend/server.Dockerfile
push: true
tags: |
ghcr.io/pilksoc/cosmickube:dev-latest
Expand Down
File renamed without changes.
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 }
}
}
2 changes: 1 addition & 1 deletion backend/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ async fn main() {

let routes = ws_route.with(warp::cors().allow_any_origin());
println!("starting server"); //debug
warp::serve(routes).run(([127, 0, 0, 1], 8000)).await; //PORT 8000 localhost
warp::serve(routes).run(([0, 0, 0, 0], 8000)).await; //PORT 8000 localhost
}

fn with_clients(clients: Clients) -> impl Filter<Extract = (Clients,), Error = Infallible> + Clone {
Expand Down
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
File renamed without changes.
9 changes: 9 additions & 0 deletions kube_cache/cache.Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
FROM debian:12.5-slim

RUN apt update && apt install ca-certificates -y && apt upgrade -y
WORKDIR /usr/local/kube_cache
COPY kube_cache/kube_cache /usr/local/bin/kube_cache

EXPOSE 8000

CMD ["kube_cache"]

0 comments on commit b51481e

Please sign in to comment.