simplify tidepool moves output json

This commit is contained in:
tali 2023-04-13 13:38:29 -04:00
parent 1c8af44523
commit 2219e96f48
2 changed files with 36 additions and 42 deletions

View File

@ -120,9 +120,7 @@ fn simulation(
pieces += 1;
if let Some(moves) = moves.as_mut() {
moves.push(output::Move {
location: placement.into(),
})
moves.push(output::Move { placement })
}
} else {
break;

View File

@ -1,4 +1,4 @@
use mino::srs::PieceType;
use mino::srs::Piece;
use serde::Serialize;
use std::time::Duration;
@ -19,42 +19,9 @@ pub struct Output {
#[derive(Serialize, Debug)]
pub struct Move {
pub location: Location,
// TODO: garbage added
}
#[derive(Serialize, Debug)]
pub struct Location {
#[serde(rename = "type", serialize_with = "ser::piece_type")]
pub ty: PieceType,
pub x: i16,
pub y: i16,
pub orientation: Orientation,
}
#[derive(Serialize, Debug)]
#[serde(rename_all = "lowercase")]
pub enum Orientation {
North,
East,
South,
West,
}
impl From<mino::srs::Piece> for Location {
fn from(pc: mino::srs::Piece) -> Self {
Self {
ty: pc.ty,
x: pc.loc.x,
y: pc.loc.y,
orientation: match pc.loc.r {
mino::Rot::N => Orientation::North,
mino::Rot::E => Orientation::East,
mino::Rot::S => Orientation::South,
mino::Rot::W => Orientation::West,
},
}
}
#[serde(flatten, serialize_with = "ser::placement")]
pub placement: Piece,
// TODO: spin?
}
#[derive(Serialize, Debug)]
@ -78,11 +45,40 @@ mod ser {
seed.to_string().serialize(serializer)
}
pub fn piece_type<S>(ty: &PieceType, serializer: S) -> Result<S::Ok, S::Error>
pub fn placement<S>(pc: &Piece, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
ty.name().serialize(serializer)
#[derive(Serialize)]
struct Placement {
#[serde(rename = "type")]
ty: char,
x: i16,
y: i16,
orientation: Orientation,
}
#[derive(Serialize)]
#[serde(rename_all = "lowercase")]
enum Orientation {
North,
East,
South,
West,
}
Placement {
ty: pc.ty.as_char(),
x: pc.loc.x,
y: pc.loc.y,
orientation: match pc.loc.r {
mino::Rot::N => Orientation::North,
mino::Rot::E => Orientation::East,
mino::Rot::S => Orientation::South,
mino::Rot::W => Orientation::West,
},
}
.serialize(serializer)
}
pub fn seconds<S>(dur: &Duration, serializer: S) -> Result<S::Ok, S::Error>