|
|
|
@ -1,5 +1,6 @@ |
|
|
|
use rhai::{Dynamic, EvalAltResult}; |
|
|
|
use serde::{Deserialize, Serialize}; |
|
|
|
use uuid::Uuid; |
|
|
|
|
|
|
|
use std::collections::HashMap; |
|
|
|
use std::fmt::Display; |
|
|
|
@ -116,8 +117,8 @@ impl Display for CardId { |
|
|
|
|
|
|
|
#[derive(Debug, Serialize, Deserialize, Clone)] |
|
|
|
pub struct Data { |
|
|
|
pub piles: HashMap<String, Pile>, |
|
|
|
pub player_piles: Vec<HashMap<String, Pile>>, |
|
|
|
pub piles: HashMap<String, RunningPile>, |
|
|
|
pub player_piles: Vec<HashMap<String, RunningPile>>, |
|
|
|
pub players: u32, |
|
|
|
#[serde(flatten)] |
|
|
|
pub other: HashMap<String, serde_json::Value>, |
|
|
|
@ -125,8 +126,8 @@ pub struct Data { |
|
|
|
|
|
|
|
impl Data { |
|
|
|
pub fn new( |
|
|
|
piles: HashMap<String, Pile>, |
|
|
|
player_piles: Vec<HashMap<String, Pile>>, |
|
|
|
piles: HashMap<String, RunningPile>, |
|
|
|
player_piles: Vec<HashMap<String, RunningPile>>, |
|
|
|
players: u32, |
|
|
|
) -> Self { |
|
|
|
Self { |
|
|
|
@ -137,3 +138,43 @@ impl Data { |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
#[derive(Serialize, Deserialize, Debug, Clone)] |
|
|
|
pub struct RunningPile { |
|
|
|
pub cards: Vec<(String, Uuid)>, |
|
|
|
pub other: HashMap<String, serde_json::Value>, |
|
|
|
} |
|
|
|
|
|
|
|
impl From<Pile> for RunningPile { |
|
|
|
fn from(p: Pile) -> Self { |
|
|
|
Self { |
|
|
|
cards: p.cards.into_iter().map(|x| (x, Uuid::new_v4())).collect(), |
|
|
|
other: p.other |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
impl RunningPile { |
|
|
|
pub fn from_rhai_map(map: rhai::Map) -> Result<Self, Box<rhai::EvalAltResult>> { |
|
|
|
// println!("{}", map.get("cards")
|
|
|
|
// .ok_or("Pile doesn't have property cards")?.type_name());
|
|
|
|
let cards: Vec<(String, Uuid)> = |
|
|
|
rhai::serde::from_dynamic(map.get("cards").ok_or("Pile doesn't have property cards")?)?; |
|
|
|
|
|
|
|
let other_fallible: Vec<Result<(String, serde_json::Value), Box<rhai::EvalAltResult>>> = |
|
|
|
map.into_iter() |
|
|
|
.map(|(x, v)| (x.to_string(), v)) |
|
|
|
.filter(|(s, _)| s != &"cards".to_string()) |
|
|
|
.map(|(k, v)| Ok((k, rhai::serde::from_dynamic::<serde_json::Value>(&v)?))) |
|
|
|
.collect(); |
|
|
|
|
|
|
|
let mut other = HashMap::new(); |
|
|
|
|
|
|
|
for x in other_fallible { |
|
|
|
let (k, v) = x?; |
|
|
|
other.insert(k, v); |
|
|
|
} |
|
|
|
|
|
|
|
Ok(Self { cards, other }) |
|
|
|
} |
|
|
|
} |
|
|
|
|