Skip to content

Commit

Permalink
Merge branch 'main' into backend
Browse files Browse the repository at this point in the history
  • Loading branch information
ettolrach authored Mar 3, 2024
2 parents a41418e + 584db21 commit 7fba6f9
Show file tree
Hide file tree
Showing 36 changed files with 715 additions and 193 deletions.
55 changes: 55 additions & 0 deletions .github/workflows/build-game-server.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
name: "Build CosmicKube game server"
on:
push:
branches:
- main
jobs:
build-server:
name: "Build Game Server"
runs-on: ubuntu-20.04
permissions:
packages: write
steps:
- name: Checkout
uses: actions/checkout@v4

- name: Install Rust toolchain
uses: actions-rs/toolchain@v1
with:
toolchain: stable
default: true
override: true

- name: Build
run: |
cd backend
cargo build --all --release && strip target/release/cosmic_kube && mv target/release/cosmic_kube target/release/cosmic_kube_amd64
- name: Upload Artifact
uses: actions/upload-artifact@v1
with:
name: server
path: backend/target/release/cosmic_kube_amd64


- 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: server.Dockerfile
push: true
tags: |
ghcr.io/pilksoc/cosmickube:dev-latest
ghcr.io/pilksoc/cosmickube:dev-${{ github.run_number }}
5 changes: 3 additions & 2 deletions backend/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,13 @@ edition = "2021"
openai_api_rust = "0.1.8"
diesel = { version = "2.1.0", features = ["postgres"] }
dotenvy = "0.15"
uuid = { version = "1.7.0", features = ["v5", "fast-rng", "macro-diagnostics"] }
uuid = { version = "1.7.0", features = ["serde", "v5", "fast-rng", "macro-diagnostics"] }
tokio = { version = "1", features = ["full"] }
tokio-stream = "0.1.6"
async-trait = "0.1.77"
thiserror = "1.0.57"
warp = "0.3"
serde = { version = "1.0", features = ["derive"]}
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
futures = { version = "0.3", default-features=false}
serde_repr = "0.1"
22 changes: 0 additions & 22 deletions backend/src/cache.rs

This file was deleted.

20 changes: 0 additions & 20 deletions backend/src/cache_test.rs

This file was deleted.

9 changes: 7 additions & 2 deletions backend/src/kube.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use uuid::Uuid;
use serde::{Deserialize, Serialize};

#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Clone)]
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Clone, Serialize, Deserialize)]
pub struct KubeId {
uuid: Uuid,
}
Expand All @@ -17,9 +18,13 @@ impl KubeId {
pub fn as_u128(&self) -> u128 {
self.uuid.as_u128()
}

pub fn uuid(&self) -> Uuid {
self.uuid
}
}

#[derive(PartialEq, Debug, Clone)]
#[derive(PartialEq, Debug, Clone, Serialize, Deserialize)]
pub struct Kube {
pub id: KubeId,
pub name: String,
Expand Down
1 change: 0 additions & 1 deletion backend/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,5 @@ pub mod space;
pub mod kube;
pub mod player;
pub mod llm;
pub mod cache;

type Coordinate = [u64; 2];
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;
warp::serve(routes).run(([127, 0, 0, 1], 8000)).await; //PORT 8000 localhost
}

fn with_clients(clients: Clients) -> impl Filter<Extract = (Clients,), Error = Infallible> + Clone {
Expand Down
33 changes: 0 additions & 33 deletions backend/src/schema.rs

This file was deleted.

14 changes: 0 additions & 14 deletions backend/src/websocketstructs.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1 @@

// these are the structs intended for use when communicating via websockets

// this is the data we expect to recieve from the player
pub struct PlayerInfo {
player: Player, //the player requesting the data
coordinates: [u64; 2], //current player coordinates
action: String, //PLACEHOLDER! we need to know what the player is doing.
}

// this is the data we expect to send to the player
pub struct GameState {
grid: String, //PLACEHOLDER! This will be the partial grid state type
}

29 changes: 15 additions & 14 deletions backend/src/ws.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
use crate::{Client, Clients};
use crate::{ws::gen_json::create_response, Client, Clients};
use futures::{FutureExt, StreamExt};
use tokio::sync::mpsc;
use tokio_stream::wrappers::UnboundedReceiverStream;
use uuid::Uuid;
use warp::ws::{Message, WebSocket};
mod gen_json;

pub async fn client_connection(ws: WebSocket, clients: Clients) {
println!("establishing client connection... {:?}", ws); //debug
Expand All @@ -26,7 +27,8 @@ pub async fn client_connection(ws: WebSocket, clients: Clients) {
}));

// creating a new uuid to use as the key in the 'clients' hashmap, and a new instance of a 'client'
let uuid = Uuid::new_v4().simple().to_string();
// this might be clapped
let uuid = Uuid::new_v5(&Uuid::NAMESPACE_DNS, "client".as_bytes()).simple().to_string();

let new_client = Client {
client_id: uuid.clone(),
Expand Down Expand Up @@ -54,7 +56,7 @@ pub async fn client_connection(ws: WebSocket, clients: Clients) {
println!("{} disconnected", uuid); //debug
}

// example function to respond to a clients message, this just responds to 'ping!' with 'pong!', but later we will replace this with;

// ->recieve client game info <- send back client game state
// wwwwwwwwwwwwwwwwwwwww i am so tired
async fn client_msg(client_id: &str, msg: Message, clients: &Clients) {
Expand All @@ -65,17 +67,16 @@ async fn client_msg(client_id: &str, msg: Message, clients: &Clients) {
Err(_) => return,
};

if message == "ping" || message == "ping\n" {
let locked = clients.lock().await;
match locked.get(client_id) {
Some(v) => {
if let Some(sender) = &v.sender {
println!("sending pong");
let _ = sender.send(Ok(Message::text("pong")));
}
println!("{}", message);

let locked = clients.lock().await;
match locked.get(client_id) {
Some(v) => {
if let Some(sender) = &v.sender {
let _ = sender.send(Ok(Message::text(create_response(message))));
}
None => return,
}
return;
};
None => return,
}
return;
}
61 changes: 61 additions & 0 deletions backend/src/ws/gen_json.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
use core::fmt;
use cosmic_kube::kube::Kube;
use serde::{Deserialize, Serialize};
use serde_json::json;
use serde_repr::{Serialize_repr, Deserialize_repr};

// this is the data we expect to recieve from the player
#[derive(Serialize, Deserialize)]
pub struct PlayerInfo {
player: String, //Player, //the player requesting the data
coordinates: [u64; 2], //current player coordinates
action: Option<Action>, // 0, block picked up 1, block placed
}

#[derive(Serialize_repr, Deserialize_repr)]
#[repr(u8)]
enum ActionType {
Pickup = 0,
Place = 1,
}

impl fmt::Display for ActionType {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
ActionType::Pickup => write!(f, "pickup"),
ActionType::Place => write!(f, "place"),
}
}
}

#[derive(Serialize, Deserialize)]
pub struct Action {
kind: ActionType,
kube: Kube,
}

fn recalculate_game(state: PlayerInfo) -> String {
// debug: log of event to server console
println!("{} @ (x:{}, y:{})", state.player, state.coordinates[0], state.coordinates[1]);
match state.action {
Some(p) => println!("{}: {}", p.kind, p.kube.name),
None => println!(""),
}

//send action to database to get result !!!<----

let resp = json!({
"grid" : "edited grid"
});

resp.to_string()
}

pub fn create_response(message: &str) -> String {

// Parse the string of data into serde_json::Value.
let state: PlayerInfo = serde_json::from_str(message).expect("something went wrong in json parse");

recalculate_game(state)
}

34 changes: 34 additions & 0 deletions design/initial_crafting_sketch.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
simplest building blocks, (you cannot craft these);
-hydrogen
-oxygen
-nitrogen
-calcium
-iron
-aluminium
-uranium
-hydrogen
-sodium
-chlorine
-light
-time
-silicon

hardcoded recipes:
- hydrogen + oxygen = water
- water + chlorine = tap water
- sodium + chlorine = salt
- water + salt = sea water
- nitrogen + oxygen = air
- iron + water = rust
- silicon + aluminium = feldspar
- feldspar + silicon = sand
- sand + water = dirt
- sand + sea water = beach
- dirt + water = earth
- earth + air = life
- life + time = age
- uranium + water = energy
- sand + time = rock
- rock + energy = fire
- fire + sand = glass

Binary file added game-source/assets/box.aseprite
Binary file not shown.
Binary file added game-source/assets/box.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
34 changes: 34 additions & 0 deletions game-source/assets/box.png.import
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
[remap]

importer="texture"
type="CompressedTexture2D"
uid="uid://c6jm42o02gsfr"
path="res://.godot/imported/box.png-52cc4e41fddadef836f9ad4dbe7936fa.ctex"
metadata={
"vram_texture": false
}

[deps]

source_file="res://assets/box.png"
dest_files=["res://.godot/imported/box.png-52cc4e41fddadef836f9ad4dbe7936fa.ctex"]

[params]

compress/mode=0
compress/high_quality=false
compress/lossy_quality=0.7
compress/hdr_compression=1
compress/normal_map=0
compress/channel_pack=0
mipmaps/generate=false
mipmaps/limit=-1
roughness/mode=0
roughness/src_normal=""
process/fix_alpha_border=true
process/premult_alpha=false
process/normal_map_invert_y=false
process/hdr_as_srgb=false
process/hdr_clamp_exposure=false
process/size_limit=0
detect_3d/compress_to=1
Binary file added game-source/assets/button-tile.aseprite
Binary file not shown.
Binary file added game-source/assets/lil_guy.aseprite
Binary file not shown.
Binary file added game-source/assets/lil_guy.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit 7fba6f9

Please sign in to comment.