|
|
|
@ -8,98 +8,104 @@ use uuid::Uuid; |
|
|
|
|
|
|
|
#[derive(Clone)] |
|
|
|
pub struct Game { |
|
|
|
pub name: String, |
|
|
|
pub version: String, |
|
|
|
pub authors: Vec<String>, |
|
|
|
conf: config::Config, |
|
|
|
folder: std::path::PathBuf, |
|
|
|
ast: rhai::AST, |
|
|
|
fns: Vec<String>, |
|
|
|
pub name: String, |
|
|
|
pub version: String, |
|
|
|
pub authors: Vec<String>, |
|
|
|
conf: config::Config, |
|
|
|
folder: std::path::PathBuf, |
|
|
|
ast: rhai::AST, |
|
|
|
fns: Vec<String>, |
|
|
|
} |
|
|
|
|
|
|
|
impl Game { |
|
|
|
pub fn load<P: AsRef<std::path::Path> + std::fmt::Debug>(folder: P) -> Self { |
|
|
|
// config::setup();
|
|
|
|
pub fn load<P: AsRef<std::path::Path> + std::fmt::Debug>(folder: P) -> Self { |
|
|
|
// config::setup();
|
|
|
|
|
|
|
|
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"); |
|
|
|
Self { |
|
|
|
conf: conf.clone(), |
|
|
|
name: conf.name, |
|
|
|
version: conf.version, |
|
|
|
authors: conf.authors, |
|
|
|
folder: folder.as_ref().to_path_buf(), |
|
|
|
ast, |
|
|
|
fns, |
|
|
|
} |
|
|
|
} |
|
|
|
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"); |
|
|
|
Self { |
|
|
|
conf: conf.clone(), |
|
|
|
name: conf.name, |
|
|
|
version: conf.version, |
|
|
|
authors: conf.authors, |
|
|
|
folder: folder.as_ref().to_path_buf(), |
|
|
|
ast, |
|
|
|
fns, |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
// pub fn name(&self) -> String {
|
|
|
|
// self.name.clone()
|
|
|
|
// }
|
|
|
|
// pub fn version(&self) -> String {
|
|
|
|
// self.version.clone()
|
|
|
|
// }
|
|
|
|
// pub fn authors(&self) -> Vec<String> {
|
|
|
|
// self.authors.clone()
|
|
|
|
// }
|
|
|
|
// pub fn name(&self) -> String {
|
|
|
|
// self.name.clone()
|
|
|
|
// }
|
|
|
|
// pub fn version(&self) -> String {
|
|
|
|
// self.version.clone()
|
|
|
|
// }
|
|
|
|
// pub fn authors(&self) -> Vec<String> {
|
|
|
|
// self.authors.clone()
|
|
|
|
// }
|
|
|
|
|
|
|
|
pub fn run(&self, players: &[Uuid]) -> run::RunningGame { |
|
|
|
// let ast = rhai::Engine::new().compile_file(self.folder.join(&self.conf.script)).unwrap();
|
|
|
|
run::RunningGame::new( |
|
|
|
self.name.clone(), |
|
|
|
self.ast.clone(), |
|
|
|
&self.fns, |
|
|
|
&self.conf, |
|
|
|
players, |
|
|
|
) |
|
|
|
} |
|
|
|
pub fn run(&self, players: &[Uuid]) -> run::RunningGame { |
|
|
|
// let ast = rhai::Engine::new().compile_file(self.folder.join(&self.conf.script)).unwrap();
|
|
|
|
run::RunningGame::new( |
|
|
|
self.name.clone(), |
|
|
|
self.ast.clone(), |
|
|
|
&self.fns, |
|
|
|
&self.conf, |
|
|
|
players, |
|
|
|
) |
|
|
|
} |
|
|
|
|
|
|
|
pub fn get_card_path(&self, image: &str) -> std::path::PathBuf { |
|
|
|
self.folder |
|
|
|
.join(&self.conf.available_cards[image].image) |
|
|
|
.to_path_buf() |
|
|
|
} |
|
|
|
pub fn get_card_paths(&self, image: &str) -> (std::path::PathBuf, std::path::PathBuf) { |
|
|
|
let card = &self.conf.available_cards[image]; |
|
|
|
let front = self.folder.join(&card.image).to_path_buf(); |
|
|
|
let back = self.folder.join( |
|
|
|
&card |
|
|
|
.back_image |
|
|
|
.as_ref() |
|
|
|
.unwrap_or(self.conf.default_back.as_ref().unwrap()), |
|
|
|
); |
|
|
|
(front, back) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
impl std::fmt::Display for Game { |
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
|
|
|
write!( |
|
|
|
f, |
|
|
|
"{} [{}]{}", |
|
|
|
self.name, |
|
|
|
self.version, |
|
|
|
if self.authors.is_empty() { |
|
|
|
String::new() |
|
|
|
} else { |
|
|
|
format!(" by {}", self.authors.join(", ")) |
|
|
|
} |
|
|
|
) |
|
|
|
} |
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
|
|
|
write!( |
|
|
|
f, |
|
|
|
"{} [{}]{}", |
|
|
|
self.name, |
|
|
|
self.version, |
|
|
|
if self.authors.is_empty() { |
|
|
|
String::new() |
|
|
|
} else { |
|
|
|
format!(" by {}", self.authors.join(", ")) |
|
|
|
} |
|
|
|
) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
impl std::fmt::Debug for Game { |
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
|
|
|
write!(f, "{}", self) |
|
|
|
} |
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
|
|
|
write!(f, "{}", self) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
pub fn load_games() -> Vec<Game> { |
|
|
|
config::setup(); |
|
|
|
let mut games = Vec::new(); |
|
|
|
for file in read_dir("games").unwrap() { |
|
|
|
if let Ok(folder) = file { |
|
|
|
if folder.path().is_dir() { |
|
|
|
for file in read_dir(folder.path()).unwrap() { |
|
|
|
if let Ok(file) = file { |
|
|
|
if file.file_name().to_str().unwrap() == "game.json" { |
|
|
|
games.push(Game::load(folder.path())) |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
games |
|
|
|
config::setup(); |
|
|
|
let mut games = Vec::new(); |
|
|
|
for file in read_dir("games").unwrap() { |
|
|
|
if let Ok(folder) = file { |
|
|
|
if folder.path().is_dir() { |
|
|
|
for file in read_dir(folder.path()).unwrap() { |
|
|
|
if let Ok(file) = file { |
|
|
|
if file.file_name().to_str().unwrap() == "game.json" { |
|
|
|
games.push(Game::load(folder.path())) |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
games |
|
|
|
} |
|
|
|
|