From 6a48231f6dbfed329bfc92d60877636079bf3876 Mon Sep 17 00:00:00 2001 From: Andy Killorin <37423245+Speedy6451@users.noreply.github.com> Date: Sat, 16 Dec 2023 15:45:20 -0600 Subject: [PATCH] turtle creation --- server/Cargo.toml | 2 + server/src/main.rs | 150 ++++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 143 insertions(+), 9 deletions(-) diff --git a/server/Cargo.toml b/server/Cargo.toml index fb14476..0925011 100644 --- a/server/Cargo.toml +++ b/server/Cargo.toml @@ -12,6 +12,8 @@ bit-struct = "0.3.2" feistel_rs = "0.1.0" rstar = "0.11.0" rustmatica = "0.1.1" +serde = "1.0.193" +serde_json = "1.0.108" tokio = { version = "1.0", features = ["full"] } tower = { version = "0.4", features = ["util", "timeout", "load-shed", "limit"] } tower-http = { version = "0.5.0", features = [ diff --git a/server/src/main.rs b/server/src/main.rs index f26ca48..b6f5066 100644 --- a/server/src/main.rs +++ b/server/src/main.rs @@ -1,29 +1,103 @@ -use std::env::args; +use std::{env::args, sync::Arc, fs, io::ErrorKind}; use axum::{ - routing::get, - Router, + routing::{get, post}, + Router, extract::{State, Path}, Json, }; use anyhow::Error; use rstar; -use rustmatica::BlockState; +use rustmatica::{BlockState, util::{UVec3, Vec3}}; mod names; +use names::Name; +use serde_json::Value; +use tokio::sync::{Mutex, RwLock}; +use serde::{Serialize, Deserialize}; +#[derive(Serialize, Deserialize)] +enum Direction { + North, + South, + East, + West, +} +impl Direction { + fn left(self) -> Self { + match self { + Direction::North => Direction::West, + Direction::South => Direction::East, + Direction::East => Direction::North, + Direction::West => Direction::South, + } + } + + fn right(self) -> Self { + match self { + Direction::North => Direction::East, + Direction::South => Direction::West, + Direction::East => Direction::South, + Direction::West => Direction::North, + } + } + fn unit(self) -> Vec3 { + match self { + Direction::North => Vec3::new(0, 0, -1), + Direction::South => Vec3::new(0, 0, 1), + Direction::East => Vec3::new(1, 0, 0), + Direction::West => Vec3::new(-1, 0, 0), + } + } +} + +#[derive(Serialize, Deserialize)] +struct Turtle { + name: Name, + fuel: usize, + /// movement vector of last given command + queued_movement: Vec3, + position: Vec3, + facing: Direction, +} + +impl Turtle { + fn new(id: u32, position: Vec3, facing: Direction, fuel: usize) -> Self { + Self { name: Name::from_num(id), fuel, queued_movement: Vec3::new(0, 0, 0), position, facing } + + } +} + +#[derive(Serialize, Deserialize)] struct ControlState { - //turtles: Vec, + turtles: Vec //world: unimplemented!(), //chunkloaders: unimplemented!(), - - } +type SharedControl = Arc>; + #[tokio::main] async fn main() -> Result<(), Error> { println!("{}", names::Name::from_num(args().nth(1).unwrap().parse().unwrap()).to_str()); - println!("{:?}", feistel_rs::feistel_encrypt(&[127,127], &[127,127], 1)); - let serv = Router::new().route("/", get(|| async { "Hello" })); + + let state = match fs::File::open("state.json") { + Ok(file) => { + serde_json::from_reader(file)? + }, + Err(e) => match e.kind() { + ErrorKind::NotFound => { + ControlState { turtles: Vec::new() } + }, + _ => panic!() + } + }; + + let state = SharedControl::new(RwLock::new(state)); + + let serv = Router::new() + .route("/turtle/new", post(create_turtle)) + .route("/turtle/update/:id", post(command)) + .with_state(state); let listener = tokio::net::TcpListener::bind("0.0.0.0:48228").await.unwrap(); @@ -31,3 +105,61 @@ async fn main() -> Result<(), Error> { Ok(()) } + +async fn create_turtle( + State(state): State, + Json(req): Json, + ) -> Json { + let turtles = &mut state.write().await.turtles; + let id = (turtles.len() + 1) as u32; + turtles.push(Turtle::new(id, req.position, req.facing, req.fuel)); + + Json(TurtleResponse {name: Name::from_num(id).to_str(), command: TurtleCommand::Wait}) +} + +async fn command( + Path(id): Path, + State(state): State, + Json(req): Json, + ) -> Json { + let turtles = &state.read().await.turtles; + println!("{id}"); + + + Json(TurtleCommand::Wait) +} + +#[derive(Serialize, Deserialize)] +enum TurtleCommand { + Wait, + Forward, + Backward, + Up, + Down, + Left, + Right, + Dig, + DigUp, + DigDown, + TakeInventory, +} + +#[derive(Serialize, Deserialize)] +struct TurtleUpdate { + fuel: usize, + /// Block name + ahead: String, +} + +#[derive(Serialize, Deserialize)] +struct TurtleRegister { + fuel: usize, + position: Vec3, + facing: Direction, +} + +#[derive(Serialize, Deserialize)] +struct TurtleResponse { + name: String, + command: TurtleCommand, +}