Skip to content

Commit

Permalink
refactor: game and reduce code footprint
Browse files Browse the repository at this point in the history
  • Loading branch information
hampfh committed Jul 14, 2024
1 parent 4bb1356 commit 5bd3dff
Show file tree
Hide file tree
Showing 13 changed files with 274 additions and 332 deletions.
65 changes: 48 additions & 17 deletions apps/server/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions apps/server/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,4 @@ libsqlite3-sys = { version = "0.9.1", features = ["bundled"] }
gif = "0.12.0"
clap = { version = "4.5.8", features = ["derive", "cargo"] }
terminate-thread = "0.3.1"
mlua = { version = "0.9.9", features = ["lua54", "send"] }
93 changes: 74 additions & 19 deletions apps/server/src/game/game.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
use std::sync::{Arc, Mutex};

use mlua::Lua;

use crate::game::ascii_graphics::draw_game_in_terminal;
use crate::game::game_state::{ErrorType, Game, Move, MAP_SIZE, MAX_TURNS};
use crate::game::player::{Player, PlayerType};
Expand All @@ -6,23 +10,66 @@ use crate::game::turn;
use super::board::Tile;
use super::game_state::GameResult;
use super::load_script::load_script;
use super::load_script_with_validation::load_script_with_validation;
use super::sandbox::sandbox_executor::{assert_lua_core_functions, execute_lua_in_sandbox};

impl Game {
pub(crate) fn start(
&mut self,
program1: String,
program2: String,
) -> (GameResult, Vec<Vec<Tile>>, Vec<Move>) {
match load_script_with_validation(&self.player_one_sandbox, program1, PlayerType::Flipped) {
match assert_lua_core_functions(program1.clone(), PlayerType::Flipped) {
Ok(_) => (),
Err(error) => {
return (
GameResult::Error(error),
self.turns.clone(),
self.logger.clone(),
)
}
}
match assert_lua_core_functions(program2.clone(), PlayerType::Regular) {
Ok(_) => (),
Err(error) => {
return (
GameResult::Error(error),
self.turns.clone(),
self.logger.clone(),
)
}
}

match execute_lua_in_sandbox(
self.player_one_sandbox.clone(),
program1,
PlayerType::Flipped,
false,
) {
Ok(_) => (),
Err(err) => return (err, self.turns.clone(), self.logger.clone()),
Err(err) => {
return (
GameResult::Error(err),
self.turns.clone(),
self.logger.clone(),
)
}
}
load_script(&self.player_one_sandbox, self.std.clone());

match load_script_with_validation(&self.player_two_sandbox, program2, PlayerType::Regular) {
match execute_lua_in_sandbox(
self.player_two_sandbox.clone(),
program2,
PlayerType::Regular,
false,
) {
Ok(_) => (),
Err(err) => return (err, self.turns.clone(), self.logger.clone()),
Err(err) => {
return (
GameResult::Error(err),
self.turns.clone(),
self.logger.clone(),
)
}
}
load_script(&self.player_two_sandbox, self.std.clone());

Expand Down Expand Up @@ -73,21 +120,29 @@ impl Game {
round += 1;
}
}
}

/**
* Returns a tuple, the first player is always the active one
* the second is the non-active player
*/
pub(crate) fn get_active_player(game: &mut Game) -> (&mut Player, &Player) {
if game.player_one_turn {
return (&mut game.player_one, &game.player_two);
pub(crate) fn get_active_sandbox(&self) -> Arc<Mutex<Lua>> {
if self.player_one_turn {
return self.player_one_sandbox.clone();
} else {
return self.player_two_sandbox.clone();
}
}
return (&mut game.player_two, &game.player_one);
}
pub fn get_active_player_type(player_one_turn: bool) -> PlayerType {
if player_one_turn {
return PlayerType::Flipped;
/**
* Returns a tuple, the first player is always the active one
* the second is the non-active player
*/
pub(crate) fn get_active_player(&mut self) -> (&mut Player, &Player) {
if self.player_one_turn {
return (&mut self.player_one, &self.player_two);
}
return (&mut self.player_two, &self.player_one);
}

pub(crate) fn get_active_player_type(&self) -> PlayerType {
if self.player_one_turn {
return PlayerType::Flipped;
}
return PlayerType::Regular;
}
return PlayerType::Regular;
}
6 changes: 3 additions & 3 deletions apps/server/src/game/game_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::{
time::Duration,
};

use rlua::Lua;
use mlua::Lua;

use super::{
board::Tile,
Expand Down Expand Up @@ -60,8 +60,8 @@ pub(crate) struct Game {

pub(crate) walls: Vec<Wall>,

pub(crate) player_one_sandbox: Arc<Mutex<rlua::Lua>>,
pub(crate) player_two_sandbox: Arc<Mutex<rlua::Lua>>,
pub(crate) player_one_sandbox: Arc<Mutex<mlua::Lua>>,
pub(crate) player_two_sandbox: Arc<Mutex<mlua::Lua>>,
pub(crate) player_one_turn: bool,
pub(crate) last_move: Option<Move>,
pub(crate) std: String, // Standard library
Expand Down
8 changes: 2 additions & 6 deletions apps/server/src/game/load_script.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
use std::sync::{Arc, Mutex};

use rlua::Lua;
use mlua::Lua;

pub(crate) fn load_script(sandbox: &Arc<Mutex<Lua>>, script: String) {
sandbox
.lock()
.unwrap()
.context(|ctx| ctx.load(&script).exec())
.unwrap();
sandbox.lock().unwrap().load(&script).exec().unwrap();
}
Loading

0 comments on commit 5bd3dff

Please sign in to comment.