1
Fork 0

switched to bincode for world data

This commit is contained in:
Andy Killorin 2023-12-20 21:45:43 -06:00
parent 5ba83793b5
commit d9a80f2a2b
Signed by: ank
GPG key ID: B6241CA3B552BCA4
4 changed files with 46 additions and 25 deletions

View file

@ -8,6 +8,7 @@ edition = "2021"
[dependencies] [dependencies]
anyhow = "1.0.75" anyhow = "1.0.75"
axum = "0.7.2" axum = "0.7.2"
bincode = "1.3.3"
bit-struct = "0.3.2" bit-struct = "0.3.2"
const_format = "0.2.32" const_format = "0.2.32"
erased-serde = "0.4.1" erased-serde = "0.4.1"

View file

@ -1,13 +1,14 @@
DEVPORT=1505 DEVPORT=1507
DEVFILE="offline-state.json" DEVFILE="dev"
PRODPORT=48228 PRODPORT=48228
PRODFILE="state.json" PRODFILE="prod"
MANIFEST=../Cargo.toml MANIFEST=../Cargo.toml
.PHONY: local global .PHONY: local global
local: surnames.txt names.txt ipaddr.txt local: surnames.txt names.txt ipaddr.txt
echo '"localhost"' > ipaddr.txt echo '"localhost"' > ipaddr.txt
mkdir -p dev
cargo run $(DEVPORT) $(DEVFILE) cargo run $(DEVPORT) $(DEVFILE)
global: surnames.txt names.txt ipaddr.txt global: surnames.txt names.txt ipaddr.txt

View file

@ -1,6 +1,6 @@
#![feature(iter_map_windows)] #![feature(iter_map_windows)]
use std::{collections::VecDeque, io::ErrorKind, sync::Arc, env::args}; use std::{collections::VecDeque, io::ErrorKind, sync::Arc, env::args, path};
use anyhow::{Context, Error, Ok}; use anyhow::{Context, Error, Ok};
use axum::{ use axum::{
@ -73,7 +73,7 @@ impl LiveState {
} }
static PORT: OnceCell<u16> = OnceCell::const_new(); static PORT: OnceCell<u16> = OnceCell::const_new();
static SAVE: OnceCell<String> = OnceCell::const_new(); static SAVE: OnceCell<path::PathBuf> = OnceCell::const_new();
type SharedControl = Arc<RwLock<LiveState>>; type SharedControl = Arc<RwLock<LiveState>>;
@ -85,24 +85,11 @@ async fn main() -> Result<(), Error> {
None => 48228, None => 48228,
})?; })?;
SAVE.set(match args.next() { SAVE.set(match args.next() {
Some(file) => file, Some(file) => file.into(),
None => "state.json".to_string(), None => "save".into(),
})?; })?;
let state = match tokio::fs::OpenOptions::new() let state = read_from_disk().await?;
.read(true)
.open(SAVE.get().unwrap())
.await
{
tokio::io::Result::Ok(file) => serde_json::from_reader(file.into_std().await)?,
tokio::io::Result::Err(e) => match e.kind() {
ErrorKind::NotFound => SavedState {
turtles: Vec::new(),
world: RTree::new(),
},
_ => panic!(),
},
};
let state = LiveState::from_save(state); let state = LiveState::from_save(state);
@ -142,11 +129,42 @@ async fn flush(State(state): State<SharedControl>) -> &'static str {
} }
async fn write_to_disk(state: SavedState) -> anyhow::Result<()> { async fn write_to_disk(state: SavedState) -> anyhow::Result<()> {
let json = serde_json::to_string_pretty(&state)?; let json = serde_json::to_string_pretty(&state.turtles)?;
tokio::fs::write(SAVE.get().unwrap(), json).await?; let bincode = bincode::serialize(&state.world)?;
tokio::fs::write(SAVE.get().unwrap().join("turtles.json"), json).await?;
tokio::fs::write(SAVE.get().unwrap().join("world.bin"), bincode).await?;
Ok(()) Ok(())
} }
async fn read_from_disk() -> anyhow::Result<SavedState> {
let turtles = match tokio::fs::OpenOptions::new()
.read(true)
.open(SAVE.get().unwrap().join("turtles.json"))
.await
{
tokio::io::Result::Ok(file) => serde_json::from_reader(file.into_std().await)?,
tokio::io::Result::Err(e) => match e.kind() {
ErrorKind::NotFound => Vec::new(),
_ => panic!(),
},
};
let world = match tokio::fs::OpenOptions::new()
.read(true).open(SAVE.get().unwrap().join("world.bin")).await {
tokio::io::Result::Ok(file) => bincode::deserialize_from(file.into_std().await)?,
tokio::io::Result::Err(e) => match e.kind() {
ErrorKind::NotFound => RTree::new(),
_ => panic!(),
},
};
Ok(SavedState {
turtles,
world,
})
}
async fn create_turtle( async fn create_turtle(
State(state): State<SharedControl>, State(state): State<SharedControl>,
Json(req): Json<turtle::TurtleRegister>, Json(req): Json<turtle::TurtleRegister>,

View file

@ -274,7 +274,8 @@ impl TurtleCommander {
if let TurtleCommandResponse::Failure = state.ret { if let TurtleCommandResponse::Failure = state.ret {
if let TurtleCommand::Backward(_) = command { if let TurtleCommand::Backward(_) = command {
// turn around if you bump your rear on something // turn around if you bump your rear on something
self.goto(Position::new(recent.pos, recent.dir.left().left())).await? self.execute(TurtleCommand::Left).await;
recent = self.execute(TurtleCommand::Left).await.pos;
} }
break 'route; break 'route;
} }
@ -305,13 +306,13 @@ pub(crate) async fn process_turtle_update(
if turtle.fuel > update.fuel { if turtle.fuel > update.fuel {
let diff = turtle.fuel - update.fuel; let diff = turtle.fuel - update.fuel;
turtle.fuel = update.fuel;
let delta = turtle.queued_movement * diff as i32; let delta = turtle.queued_movement * diff as i32;
turtle.position.pos += delta; turtle.position.pos += delta;
turtle.queued_movement = Vec3::zeros(); turtle.queued_movement = Vec3::zeros();
} }
turtle.fuel = update.fuel;
let above = Block { let above = Block {
name: update.above.clone(), name: update.above.clone(),