switched to bincode for world data
This commit is contained in:
parent
5ba83793b5
commit
d9a80f2a2b
4 changed files with 46 additions and 25 deletions
|
@ -8,6 +8,7 @@ edition = "2021"
|
|||
[dependencies]
|
||||
anyhow = "1.0.75"
|
||||
axum = "0.7.2"
|
||||
bincode = "1.3.3"
|
||||
bit-struct = "0.3.2"
|
||||
const_format = "0.2.32"
|
||||
erased-serde = "0.4.1"
|
||||
|
|
|
@ -1,13 +1,14 @@
|
|||
DEVPORT=1505
|
||||
DEVFILE="offline-state.json"
|
||||
DEVPORT=1507
|
||||
DEVFILE="dev"
|
||||
PRODPORT=48228
|
||||
PRODFILE="state.json"
|
||||
PRODFILE="prod"
|
||||
|
||||
MANIFEST=../Cargo.toml
|
||||
|
||||
.PHONY: local global
|
||||
local: surnames.txt names.txt ipaddr.txt
|
||||
echo '"localhost"' > ipaddr.txt
|
||||
mkdir -p dev
|
||||
cargo run $(DEVPORT) $(DEVFILE)
|
||||
|
||||
global: surnames.txt names.txt ipaddr.txt
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
#![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 axum::{
|
||||
|
@ -73,7 +73,7 @@ impl LiveState {
|
|||
}
|
||||
|
||||
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>>;
|
||||
|
||||
|
@ -85,24 +85,11 @@ async fn main() -> Result<(), Error> {
|
|||
None => 48228,
|
||||
})?;
|
||||
SAVE.set(match args.next() {
|
||||
Some(file) => file,
|
||||
None => "state.json".to_string(),
|
||||
Some(file) => file.into(),
|
||||
None => "save".into(),
|
||||
})?;
|
||||
|
||||
let state = match tokio::fs::OpenOptions::new()
|
||||
.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 = read_from_disk().await?;
|
||||
|
||||
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<()> {
|
||||
let json = serde_json::to_string_pretty(&state)?;
|
||||
tokio::fs::write(SAVE.get().unwrap(), json).await?;
|
||||
let json = serde_json::to_string_pretty(&state.turtles)?;
|
||||
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(())
|
||||
}
|
||||
|
||||
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(
|
||||
State(state): State<SharedControl>,
|
||||
Json(req): Json<turtle::TurtleRegister>,
|
||||
|
|
|
@ -274,7 +274,8 @@ impl TurtleCommander {
|
|||
if let TurtleCommandResponse::Failure = state.ret {
|
||||
if let TurtleCommand::Backward(_) = command {
|
||||
// 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;
|
||||
}
|
||||
|
@ -305,13 +306,13 @@ pub(crate) async fn process_turtle_update(
|
|||
|
||||
if turtle.fuel > update.fuel {
|
||||
let diff = turtle.fuel - update.fuel;
|
||||
turtle.fuel = update.fuel;
|
||||
|
||||
let delta = turtle.queued_movement * diff as i32;
|
||||
|
||||
turtle.position.pos += delta;
|
||||
turtle.queued_movement = Vec3::zeros();
|
||||
}
|
||||
turtle.fuel = update.fuel;
|
||||
|
||||
let above = Block {
|
||||
name: update.above.clone(),
|
||||
|
|
Loading…
Reference in a new issue