Browse Source

Add `other` property to cards

main
ThePerkinrex 5 years ago
parent
commit
3248560b73
No known key found for this signature in database GPG Key ID: FD81DE6D75E20917
  1. 2
      server/build.rs
  2. 2
      server/src/games/config.rs
  3. 2
      server/src/games/mod.rs
  4. 12
      server/src/games/run.rs
  5. 10
      server/src/games/run/engine.rs
  6. 6
      server/src/games/run/functions.rs
  7. 11
      server/src/games/run/types.rs

2
server/build.rs

@ -11,7 +11,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
}
}
}
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, &[<std::path::PathBuf as From<&str>>::from("../protobuf"), "../protobuf/include".into()]) {
eprintln!("{}", e);
return Err(e.into());
}

2
server/src/games/config.rs

@ -124,7 +124,7 @@ fn cache_image<P1: AsRef<Path>, P2: AsRef<Path>>(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,
}

2
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,

12
server/src/games/run.rs

@ -37,8 +37,8 @@ impl RunningGame {
.collect()
}
pub fn compile(path: std::path::PathBuf) -> RhaiResult<(AST, Vec<String>)> {
let e = setup_engine();
pub fn compile(path: std::path::PathBuf, conf: &Config) -> RhaiResult<(AST, Vec<String>)> {
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,

10
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
}

6
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,

11
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<String, serde_json::Value>
}
impl Card {
@ -171,13 +172,15 @@ impl Debug for Card {
pub struct RunningPile {
pub cards: Vec<Card>,
pub face_down: bool,
#[serde(flatten)]
pub other: HashMap<String, serde_json::Value>,
}
impl From<Pile> for RunningPile {
fn from(p: Pile) -> Self {
impl RunningPile {
pub fn from_pile(p: Pile, available_cards: &HashMap<String, super::super::config::Card>) -> 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,
}

Loading…
Cancel
Save