Skip to content

Commit

Permalink
Merge pull request #28 from pilksoc/sf-be-websocketsdone
Browse files Browse the repository at this point in the history
client server communication is now set up
  • Loading branch information
stephanie-flower authored Mar 3, 2024
2 parents 5f35801 + 5f1f7b4 commit c65af60
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 17 deletions.
2 changes: 1 addition & 1 deletion backend/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ pub mod player;
pub mod llm;
pub mod local_grid;

type Coordinate = [u64; 2];
pub type Coordinate = [u64; 2];
73 changes: 57 additions & 16 deletions backend/src/ws/gen_json.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,24 @@
use core::fmt;
use cosmic_kube::grid::Grid;
use cosmic_kube::kube::Kube;
use cosmic_kube::local_grid::LocalGrid;
use cosmic_kube::space::{Space, SpaceKind};
use cosmic_kube::Coordinate;
use core::fmt;
use rand::Rng;
use serde::{Deserialize, Serialize};
use serde_json::{json, Value};
use serde_repr::{Serialize_repr, Deserialize_repr};
use rand::Rng;
use serde_repr::{Deserialize_repr, Serialize_repr};

//example valid json:
// { "initialised": true, "player": "charlie zoot", "coordinates": [10, 10], "action": { "kind": 1, "kube": { "id": {"uuid": "f7993723-2529-50c4-950d-ba104d29b5df" }, "name": "dirt" }, "coordinates": [10,11] } }


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

Expand All @@ -34,39 +42,72 @@ impl fmt::Display for ActionType {
pub struct Action {
kind: ActionType,
kube: Kube,
coordinates: Coordinate,
}

fn recalculate_game(state: PlayerInfo) -> String {
fn perform_action(mut grid: Grid, action: Action) -> Grid {
let kube_result: SpaceKind;
match action.kind {
ActionType::Pickup => kube_result = SpaceKind::EmptySpace,
ActionType::Place => kube_result = SpaceKind::Kube(action.kube),
}

let space_in_question: Space = Space::new(action.coordinates, kube_result);
grid.insert(space_in_question);
grid
}

fn debug_message(state: &PlayerInfo) {
// debug: log of event to server console
println!("{} @ (x:{}, y:{})", state.player, state.coordinates[0], state.coordinates[1]);
match state.action {
println!(
"{} @ (x:{}, y:{})",
state.player, state.coordinates[0], state.coordinates[1]
);
let mut _has_action: bool = true;
match &state.action {
Some(p) => println!("{}: {}", p.kind, p.kube.name),
None => println!(""),
None => _has_action = false,
}
}

fn recalculate_game(state: PlayerInfo) -> String {
debug_message(&state); //debug

// total_grid: Grid = get grid from GO db
let example_grid: Grid = Grid::new(2048, 2048);
let new_grid_to_send: Grid;

//send action to database to get result !!!<----
// then we want to update the grid by performing action
match state.action {
Some(p) => new_grid_to_send = perform_action(example_grid, p),
_ => new_grid_to_send = example_grid,
}

//store new_grid_to_send in the database

let new_grid: LocalGrid =
LocalGrid::from_grid_and_coord(&new_grid_to_send, state.coordinates, 48);
let resp: Value;

if state.initialised {
// if the player is not new to the game, continue game loop
resp = json!({
"grid" : "edited grid"
"grid" : new_grid,
});
} else {
let mut rng = rand::thread_rng();
resp = json!({
"coordinates" : [rng.gen_range(0..2048), rng.gen_range(0..2048)]
});
}


resp.to_string()
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");
let state: PlayerInfo =
serde_json::from_str(message).expect("something went wrong in json parse");

recalculate_game(state)
}

0 comments on commit c65af60

Please sign in to comment.