From 3248560b732178e89adce5f5cc225f271a78f04d Mon Sep 17 00:00:00 2001 From: ThePerkinrex Date: Mon, 19 Jul 2021 16:39:12 +0200 Subject: [PATCH] Add `other` property to cards --- server/build.rs | 2 +- server/src/games/config.rs | 2 +- server/src/games/mod.rs | 2 +- server/src/games/run.rs | 12 ++++++------ server/src/games/run/engine.rs | 10 ++++++++-- server/src/games/run/functions.rs | 6 ++++-- server/src/games/run/types.rs | 11 +++++++---- 7 files changed, 28 insertions(+), 17 deletions(-) diff --git a/server/build.rs b/server/build.rs index 0cb5149..238829b 100644 --- a/server/build.rs +++ b/server/build.rs @@ -11,7 +11,7 @@ fn main() -> Result<(), Box> { } } } - if let Err(e) = prost_build::Config::new().out_dir("src/server/protos").compile_protos(&files, &["../protobuf".into(), "../protobuf/include".into()]) { + if let Err(e) = prost_build::Config::new().out_dir("src/server/protos").compile_protos(&files, &[>::from("../protobuf"), "../protobuf/include".into()]) { eprintln!("{}", e); return Err(e.into()); } diff --git a/server/src/games/config.rs b/server/src/games/config.rs index cd873fe..38a8b1e 100644 --- a/server/src/games/config.rs +++ b/server/src/games/config.rs @@ -124,7 +124,7 @@ fn cache_image, P2: AsRef>(p: P1, folder: P2) { .expect(&format!("Error loading the image in {:?}", original)) .write_to(&mut face_buf, image::ImageOutputFormat::Png) .unwrap(); - match std::fs::create_dir(cached.parent().unwrap()) { + match std::fs::create_dir_all(cached.parent().unwrap()) { Err(e) if e.kind() == ErrorKind::AlreadyExists => Ok(()), // Ignore if folder already exists x => x, } diff --git a/server/src/games/mod.rs b/server/src/games/mod.rs index d7bebc8..1206234 100644 --- a/server/src/games/mod.rs +++ b/server/src/games/mod.rs @@ -24,7 +24,7 @@ impl Game { let conf = config::Config::load(folder.as_ref().join("game.json")); let (ast, fns) = - RunningGame::compile(folder.as_ref().join(&conf.script)).expect("Compile error"); + RunningGame::compile(folder.as_ref().join(&conf.script), &conf).expect("Compile error"); Self { conf: conf.clone(), name: conf.name, diff --git a/server/src/games/run.rs b/server/src/games/run.rs index e5a216a..c21bba5 100644 --- a/server/src/games/run.rs +++ b/server/src/games/run.rs @@ -37,8 +37,8 @@ impl RunningGame { .collect() } - pub fn compile(path: std::path::PathBuf) -> RhaiResult<(AST, Vec)> { - let e = setup_engine(); + pub fn compile(path: std::path::PathBuf, conf: &Config) -> RhaiResult<(AST, Vec)> { + let e = setup_engine(conf); let ast = e.compile_file(path)?; let fns = Self::get_fns(&ast); Ok((ast, fns)) @@ -52,12 +52,12 @@ impl RunningGame { current_players: &[Uuid], ) -> Self { // log::info!("Fns: {:?}", fns); - let functions = Functions::new(fns, ast.clone(), &name); - let engine = setup_engine(); + let functions = Functions::new(fns, ast.clone(), &name, conf); + let engine = setup_engine(conf); let setup = Func::<(Dynamic,), Dynamic>::create_from_ast(engine, ast, "setup"); - let piles = conf.piles.clone().into_iter().map(|(k,v)| (k, v.into())).collect(); - let player_piles = vec![conf.player_piles.clone().into_iter().map(|(k,v)| (k, v.into())).collect(); current_players.len()]; + let piles = conf.piles.clone().into_iter().map(|(k,v)| (k, RunningPile::from_pile(v, &conf.available_cards))).collect(); + let player_piles = vec![conf.player_piles.clone().into_iter().map(|(k,v)| (k, RunningPile::from_pile(v, &conf.available_cards))).collect(); current_players.len()]; let Data { piles, player_piles, diff --git a/server/src/games/run/engine.rs b/server/src/games/run/engine.rs index 9838bc6..40bafbb 100644 --- a/server/src/games/run/engine.rs +++ b/server/src/games/run/engine.rs @@ -6,13 +6,13 @@ use rhai::{ use std::fmt::{Debug, Display}; -use crate::games::run::types::Card; +use crate::games::{Config, run::types::Card}; use super::{ types::{CardId, Data, Player, RhaiResult, RunningPile}, }; // TODO Write somekind of documentation on functions available & stuff -pub fn setup_engine() -> Engine { +pub fn setup_engine(conf: &Config) -> Engine { let mut engine = Engine::new(); engine.set_max_expr_depths(0, 0); engine.register_result_fn("shuffle", shuffle_pile); @@ -65,6 +65,12 @@ pub fn setup_engine() -> Engine { .register_fn("debug", |p: &mut CardId| p.to_string()) .register_fn("+", |s: &str, p: CardId| format!("{}{}", s, p)) .register_fn("+", |p: CardId, s: &str| p.to_string().push_str(s)); + + let available_cards = conf.available_cards.clone(); + engine.register_result_fn("new_card", move |kind: &str| { + let card = available_cards.get(kind).unwrap(); + to_dynamic(Card { kind: kind.to_string(), uuid: uuid::Uuid::new_v4(), other: card.other.clone() }) + }); engine } diff --git a/server/src/games/run/functions.rs b/server/src/games/run/functions.rs index 6187ecf..3a7b043 100644 --- a/server/src/games/run/functions.rs +++ b/server/src/games/run/functions.rs @@ -1,5 +1,7 @@ use rhai::{Dynamic, Engine, Scope, AST}; +use crate::games::Config; + use super::engine::setup_engine; use super::types::{CardId, Player, RhaiResult}; @@ -16,7 +18,7 @@ pub struct Functions { } impl Functions { - pub fn new(fns: &[String], ast: AST, game_name: &str) -> Self { + pub fn new(fns: &[String], ast: AST, game_name: &str, conf: &Config) -> Self { if !fns.contains(&"turn_end".to_string()) { panic!( "Expected turn_end function in game code for the game \"{}\"", @@ -30,7 +32,7 @@ impl Functions { ) }; let has_turn_start = fns.contains(&"turn_start".to_string()); - let engine = setup_engine(); + let engine = setup_engine(conf); Self { engine, ast, diff --git a/server/src/games/run/types.rs b/server/src/games/run/types.rs index 0753155..9ffda68 100644 --- a/server/src/games/run/types.rs +++ b/server/src/games/run/types.rs @@ -142,7 +142,8 @@ impl Data { #[derive(Serialize, Deserialize, Clone)] pub struct Card { pub kind: String, - pub uuid: Uuid + pub uuid: Uuid, + pub other: HashMap } impl Card { @@ -171,13 +172,15 @@ impl Debug for Card { pub struct RunningPile { pub cards: Vec, pub face_down: bool, + #[serde(flatten)] pub other: HashMap, } -impl From for RunningPile { - fn from(p: Pile) -> Self { +impl RunningPile { + pub fn from_pile(p: Pile, available_cards: &HashMap) -> Self { + Self { - cards: p.cards.into_iter().map(|kind| Card {kind, uuid: Uuid::new_v4()}).collect(), + cards: p.cards.into_iter().map(|kind| {let other = available_cards.get(&kind).unwrap().other.clone(); Card {kind, uuid: Uuid::new_v4(), other}}).collect(), other: p.other, face_down: p.face_down, }