Compare commits

3 Commits

Author SHA1 Message Date
adriangl 2d8dd0f58f moved validation logic to seperate call 2024-03-30 22:39:09 +01:00
adriangl 7ea62870d0 patched crash from negative points 2024-03-24 07:09:28 +01:00
adriangl 76dd868163 added build and module options to flake
Co-authored-by: oysteikt <oysteikt@pvv.ntnu.no>
2024-03-24 03:55:38 +01:00
8 changed files with 289 additions and 36 deletions
+1
View File
@@ -0,0 +1 @@
/nix/store/9dql8fkg1cf434x95wccky3g34jkz49l-devenv-profile
+2
View File
@@ -1 +1,3 @@
/target /target
/results
Generated
+158 -12
View File
@@ -26,6 +26,15 @@ dependencies = [
"memchr", "memchr",
] ]
[[package]]
name = "ansi_term"
version = "0.12.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d52a9bb7ec0cf484c551830a7ce27bd20d67eac647e1befb56b0be4ee39a55d2"
dependencies = [
"winapi",
]
[[package]] [[package]]
name = "async-stream" name = "async-stream"
version = "0.3.5" version = "0.3.5"
@@ -45,7 +54,7 @@ checksum = "16e62a023e7c117e27523144c5d2459f4397fcc3cab0085af8e2224f643a0193"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
"quote", "quote",
"syn", "syn 2.0.48",
] ]
[[package]] [[package]]
@@ -56,7 +65,7 @@ checksum = "c980ee35e870bd1a4d2c8294d4c04d0499e67bca1e4b5cefcc693c2fa00caea9"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
"quote", "quote",
"syn", "syn 2.0.48",
] ]
[[package]] [[package]]
@@ -74,6 +83,17 @@ dependencies = [
"bytemuck", "bytemuck",
] ]
[[package]]
name = "atty"
version = "0.2.14"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d9b39be18770d11421cdb1b9947a45dd3f37e93092cbf377614828a319d5fee8"
dependencies = [
"hermit-abi 0.1.19",
"libc",
"winapi",
]
[[package]] [[package]]
name = "autocfg" name = "autocfg"
version = "1.1.0" version = "1.1.0"
@@ -140,6 +160,21 @@ version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
[[package]]
name = "clap"
version = "2.34.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a0610544180c38b88101fecf2dd634b174a62eef6946f84dfc6a7127512b381c"
dependencies = [
"ansi_term",
"atty",
"bitflags 1.3.2",
"strsim",
"textwrap",
"unicode-width",
"vec_map",
]
[[package]] [[package]]
name = "cookie" name = "cookie"
version = "0.18.0" version = "0.18.0"
@@ -190,7 +225,7 @@ dependencies = [
"proc-macro2", "proc-macro2",
"proc-macro2-diagnostics", "proc-macro2-diagnostics",
"quote", "quote",
"syn", "syn 2.0.48",
] ]
[[package]] [[package]]
@@ -376,6 +411,24 @@ version = "0.14.3"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "290f1a1d9242c78d09ce40a5e87e7554ee637af1351968159f4952f028f75604" checksum = "290f1a1d9242c78d09ce40a5e87e7554ee637af1351968159f4952f028f75604"
[[package]]
name = "heck"
version = "0.3.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6d621efb26863f0e9924c6ac577e8275e5e6b77455db64ffa6c65c904e9e132c"
dependencies = [
"unicode-segmentation",
]
[[package]]
name = "hermit-abi"
version = "0.1.19"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "62b467343b94ba476dcb2500d242dadbb39557df889310ac77c5d99100aaac33"
dependencies = [
"libc",
]
[[package]] [[package]]
name = "hermit-abi" name = "hermit-abi"
version = "0.3.4" version = "0.3.4"
@@ -463,7 +516,7 @@ version = "0.4.10"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0bad00257d07be169d870ab665980b06cdb366d792ad690bf2e76876dc503455" checksum = "0bad00257d07be169d870ab665980b06cdb366d792ad690bf2e76876dc503455"
dependencies = [ dependencies = [
"hermit-abi", "hermit-abi 0.3.4",
"rustix", "rustix",
"windows-sys 0.52.0", "windows-sys 0.52.0",
] ]
@@ -600,7 +653,7 @@ version = "1.16.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4161fcb6d602d4d2081af7c3a45852d875a03dd337a6bfdd6e06407b61342a43" checksum = "4161fcb6d602d4d2081af7c3a45852d875a03dd337a6bfdd6e06407b61342a43"
dependencies = [ dependencies = [
"hermit-abi", "hermit-abi 0.3.4",
"libc", "libc",
] ]
@@ -633,6 +686,7 @@ dependencies = [
"rocket", "rocket",
"serde", "serde",
"serde_json", "serde_json",
"structopt",
"uuid", "uuid",
] ]
@@ -679,7 +733,7 @@ dependencies = [
"proc-macro2", "proc-macro2",
"proc-macro2-diagnostics", "proc-macro2-diagnostics",
"quote", "quote",
"syn", "syn 2.0.48",
] ]
[[package]] [[package]]
@@ -712,6 +766,30 @@ version = "0.2.17"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5b40af805b3121feab8a3c29f04d8ad262fa8e0561883e7653e024ae4479e6de" checksum = "5b40af805b3121feab8a3c29f04d8ad262fa8e0561883e7653e024ae4479e6de"
[[package]]
name = "proc-macro-error"
version = "1.0.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "da25490ff9892aab3fcf7c36f08cfb902dd3e71ca0f9f9517bea02a73a5ce38c"
dependencies = [
"proc-macro-error-attr",
"proc-macro2",
"quote",
"syn 1.0.109",
"version_check",
]
[[package]]
name = "proc-macro-error-attr"
version = "1.0.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a1be40180e52ecc98ad80b184934baf3d0d29f979574e439af5a55274b35f869"
dependencies = [
"proc-macro2",
"quote",
"version_check",
]
[[package]] [[package]]
name = "proc-macro2" name = "proc-macro2"
version = "1.0.78" version = "1.0.78"
@@ -729,7 +807,7 @@ checksum = "af066a9c399a26e020ada66a034357a868728e72cd426f3adcd35f80d88d88c8"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
"quote", "quote",
"syn", "syn 2.0.48",
"version_check", "version_check",
"yansi", "yansi",
] ]
@@ -799,7 +877,7 @@ checksum = "5fddb4f8d99b0a2ebafc65a87a69a7b9875e4b1ae1f00db265d300ef7f28bccc"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
"quote", "quote",
"syn", "syn 2.0.48",
] ]
[[package]] [[package]]
@@ -897,7 +975,7 @@ dependencies = [
"proc-macro2", "proc-macro2",
"quote", "quote",
"rocket_http", "rocket_http",
"syn", "syn 2.0.48",
"unicode-xid", "unicode-xid",
"version_check", "version_check",
] ]
@@ -990,7 +1068,7 @@ checksum = "33c85360c95e7d137454dc81d9a4ed2b8efd8fbe19cee57357b32b9771fccb67"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
"quote", "quote",
"syn", "syn 2.0.48",
] ]
[[package]] [[package]]
@@ -1080,6 +1158,47 @@ dependencies = [
"loom", "loom",
] ]
[[package]]
name = "strsim"
version = "0.8.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8ea5119cdb4c55b55d432abb513a0429384878c15dde60cc77b1c99de1a95a6a"
[[package]]
name = "structopt"
version = "0.3.26"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0c6b5c64445ba8094a6ab0c3cd2ad323e07171012d9c98b0b15651daf1787a10"
dependencies = [
"clap",
"lazy_static",
"structopt-derive",
]
[[package]]
name = "structopt-derive"
version = "0.4.18"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dcb5ae327f9cc13b68763b5749770cb9e048a99bd9dfdfa58d0cf05d5f64afe0"
dependencies = [
"heck",
"proc-macro-error",
"proc-macro2",
"quote",
"syn 1.0.109",
]
[[package]]
name = "syn"
version = "1.0.109"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "72b64191b275b66ffe2469e8af2c1cfe3bafa67b529ead792a6d0160888b4237"
dependencies = [
"proc-macro2",
"quote",
"unicode-ident",
]
[[package]] [[package]]
name = "syn" name = "syn"
version = "2.0.48" version = "2.0.48"
@@ -1104,6 +1223,15 @@ dependencies = [
"windows-sys 0.52.0", "windows-sys 0.52.0",
] ]
[[package]]
name = "textwrap"
version = "0.11.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d326610f408c7a4eb6f51c37c330e496b08506c9457c9d34287ecc38809fb060"
dependencies = [
"unicode-width",
]
[[package]] [[package]]
name = "thread_local" name = "thread_local"
version = "1.1.7" version = "1.1.7"
@@ -1169,7 +1297,7 @@ checksum = "5b8a1e28f2deaa14e508979454cb3a223b10b938b45af148bc0986de36f1923b"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
"quote", "quote",
"syn", "syn 2.0.48",
] ]
[[package]] [[package]]
@@ -1256,7 +1384,7 @@ checksum = "34704c8d6ebcbc939824180af020566b01a7c01f80641264eba0999f6c2b6be7"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
"quote", "quote",
"syn", "syn 2.0.48",
] ]
[[package]] [[package]]
@@ -1329,6 +1457,18 @@ version = "1.0.12"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b" checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b"
[[package]]
name = "unicode-segmentation"
version = "1.11.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d4c87d22b6e3f4a18d4d40ef354e97c90fcb14dd91d7dc0aa9d8a1172ebf7202"
[[package]]
name = "unicode-width"
version = "0.1.11"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e51733f11c9c4f72aa0c160008246859e340b00807569a0da0e7a1079b27ba85"
[[package]] [[package]]
name = "unicode-xid" name = "unicode-xid"
version = "0.2.4" version = "0.2.4"
@@ -1351,6 +1491,12 @@ version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "830b7e5d4d90034032940e4ace0d9a9a057e7a45cd94e6c007832e39edb82f6d" checksum = "830b7e5d4d90034032940e4ace0d9a9a057e7a45cd94e6c007832e39edb82f6d"
[[package]]
name = "vec_map"
version = "0.8.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f1bddf1187be692e79c5ffeab891132dfb0f236ed36a43c7ed39f1165ee20191"
[[package]] [[package]]
name = "version_check" name = "version_check"
version = "0.9.4" version = "0.9.4"
+1
View File
@@ -8,6 +8,7 @@ edition = "2021"
[dependencies] [dependencies]
rocket = { version = "0.5.0", features = ["json", "uuid"] } rocket = { version = "0.5.0", features = ["json", "uuid"] }
serde = "1.0.196" serde = "1.0.196"
structopt = "0.3"
rand = "0.8.5" rand = "0.8.5"
serde_json = "1.0.113" serde_json = "1.0.113"
View File
+59 -3
View File
@@ -2,16 +2,18 @@
inputs = { inputs = {
nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable"; nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
devenv.url = "github:cachix/devenv"; devenv.url = "github:cachix/devenv";
fenix.url = "github:nix-community/fenix"; fenix.url = "github:nix-community/fenix";
fenix.inputs.nixpkgs.follows = "nixpkgs"; fenix.inputs.nixpkgs.follows = "nixpkgs";
}; };
outputs = { self, nixpkgs, devenv, ... }@inputs: outputs = { self, fenix, nixpkgs, devenv, ... }@inputs:
let let
toolchain = fenix.packages.x86_64-linux.minimal.toolchain;
pkgs = import nixpkgs { pkgs = import nixpkgs {
system = "x86_64-linux"; system = "x86_64-linux";
}; };
lib = pkgs.lib;
types = lib.types;
in { in {
devShell.x86_64-linux = devenv.lib.mkShell { devShell.x86_64-linux = devenv.lib.mkShell {
inherit inputs pkgs; inherit inputs pkgs;
@@ -24,5 +26,59 @@
} }
]; ];
}; };
packages.x86_64-linux.default = (pkgs.makeRustPlatform {
cargo = toolchain;
rustc = toolchain;
}).buildRustPackage {
pname = "ozai";
version = "0.1.0";
src = ./.;
cargoLock.lockFile = ./Cargo.lock;
};
nixosModules = {
ozai = { config, pkgs, ... }: {
options.services.ozai = {
enable = lib.mkEnableOption "Ozai Azul server";
host = lib.mkOption {
type = types.str;
default = "127.0.0.1";
description = "The host to run the server on";
};
port = lib.mkOption {
type = types.int;
default = 8000;
description = "The port to run the server on";
};
};
config = lib.mkIf config.services.ozai.enable {
systemd.services.ozai = {
description = "Ozai Azul server";
after = [ "network.target" ];
wantedBy = [ "multi-user.target" ];
serviceConfig = {
ExecStart = "${self.packages.x86_64-linux.default}/bin/ozai --port ${toString config.services.ozai.port} --host '${config.services.ozai.host}'";
Restart = "always";
};
};
};
};
};
nixosConfigurations = {
ozai = nixpkgs.lib.nixosSystem {
system = "x86_64-linux";
modules = [
self.nixosModules.ozai
{
system.stateVersion = "23.11";
boot.isContainer = true;
services.ozai.enable = true;
}
];
};
};
}; };
} }
+48 -20
View File
@@ -219,7 +219,7 @@ impl GameState {
} }
} }
let negative = match player.floor.len() { let negative = match player.floor.len() {
0 => 0, 0 => 0,
1 => 1, 1 => 1,
2 => 2, 2 => 2,
@@ -229,7 +229,8 @@ impl GameState {
6 => 11, 6 => 11,
_ => 14, _ => 14,
}; };
player.points -= negative; // check if the absolute value of negative is less than the points
player.points = player.points.saturating_sub(negative);
if player.floor.start == 1 { if player.floor.start == 1 {
self.starting_player = player_name.clone(); self.starting_player = player_name.clone();
@@ -254,7 +255,14 @@ impl GameState {
return Err(MoveErr::Other("Game is over")); return Err(MoveErr::Other("Game is over"));
} }
self.do_move(game_move).map_err(|e| e)?; self.do_move(game_move.clone()).map_err(|e| e)?;
match game_move.policy {
//if validate return ok and do not iterate
Policy::Validate => return Ok(()),
//else continue the function
_ => (),
}
if self.is_empty() { if self.is_empty() {
self.score_unchecked().map_err(|e| MoveErr::Other(e))?; self.score_unchecked().map_err(|e| MoveErr::Other(e))?;
@@ -284,30 +292,18 @@ impl GameState {
match game_move.policy { match game_move.policy {
Policy::Strict => self.do_move_strict(game_move), Policy::Strict => self.do_move_strict(game_move),
Policy::Loose => self.do_move_loose(game_move), Policy::Loose => self.do_move_loose(game_move),
Policy::Validate => self.do_move_validate(game_move),
} }
} }
/// Does a move, if the move is placed where it can't fit, places it on the floor
fn do_move_loose(&mut self, game_move: GameMove) -> Result<(), MoveErr> {
match self.do_move_strict(game_move.clone()) {
Ok(()) => Ok(()),
Err(MoveErr::Dst(_)) => {
let mut new_game_move = game_move;
new_game_move.destination = Destination::Floor;
self.do_move_strict(new_game_move)
}
e => e,
}
}
/// Does a move, errors out if the move is weird in any way
fn do_move_strict(&mut self, game_move: GameMove) -> Result<(), MoveErr> { /// Validates a move, errors out if the move is weird in any way
fn do_move_validate(&mut self, game_move: GameMove) -> Result<(), MoveErr> {
if self.current_player != game_move.player { if self.current_player != game_move.player {
return Err(MoveErr::Player("Not this player's turn")); return Err(MoveErr::Player("Not this player's turn"));
} }
// let player = &mut self.players[current_player];
let color = game_move.color; let color = game_move.color;
let src = game_move.source; let src = game_move.source;
let dst = game_move.destination; let dst = game_move.destination;
@@ -346,6 +342,32 @@ impl GameState {
"That pattern line and color is already filled on the wall", "That pattern line and color is already filled on the wall",
)) ))
} }
_ => Ok(()),
}
}
/// Does a move, if the move is placed where it can't fit, places it on the floor
fn do_move_loose(&mut self, game_move: GameMove) -> Result<(), MoveErr> {
match self.do_move_strict(game_move.clone()) {
Ok(()) => Ok(()),
Err(MoveErr::Dst(_)) => {
let mut new_game_move = game_move;
new_game_move.destination = Destination::Floor;
self.do_move_strict(new_game_move)
}
e => e,
}
}
/// Does a move, errors out if the move is weird in any way
fn do_move_strict(&mut self, game_move: GameMove) -> Result<(), MoveErr> {
self.do_move_validate(game_move.clone())?;
let color = game_move.color;
let src = game_move.source;
let dst = game_move.destination;
match (color, src, dst) {
(c, s, Destination::PatternLine(p)) => { (c, s, Destination::PatternLine(p)) => {
let amount = self.take_tiles(s, c); let amount = self.take_tiles(s, c);
@@ -377,6 +399,7 @@ impl GameState {
.add_color(c, amount as isize) .add_color(c, amount as isize)
.map_err(|e| MoveErr::Other(e))?; .map_err(|e| MoveErr::Other(e))?;
} }
_ => (),
}; };
self.current_player = self self.current_player = self
@@ -869,9 +892,14 @@ enum Policy {
/// Anything weird will return an error /// Anything weird will return an error
#[serde(rename = "strict")] #[serde(rename = "strict")]
Strict, Strict,
#[serde(rename = "loose")]
/// If you place tiles wrongly, they will fall on the floor, anything else will return an error /// If you place tiles wrongly, they will fall on the floor, anything else will return an error
#[serde(rename = "loose")]
Loose, Loose,
/// Will validate the move, but not execute it
#[serde(rename = "validate")]
Validate,
// Anything weird will instead play a random move // Anything weird will instead play a random move
// #[serde(rename = "random")] // #[serde(rename = "random")]
// Random, // Random,
+20 -1
View File
@@ -83,10 +83,29 @@ enum NewGameOptions {
Premade { game: azul::GameState }, Premade { game: azul::GameState },
} }
use structopt::StructOpt;
#[derive(StructOpt)]
struct Cli {
#[structopt(long = "host", default_value = "127.0.0.1")]
host: String,
#[structopt(long = "port", default_value = "8000")]
port: u16,
}
#[launch] #[launch]
fn rocket() -> _ { fn rocket() -> _ {
let args = Cli::from_args();
let host = args.host;
let port = args.port;
rocket::build() rocket::build()
.configure(rocket::Config {
address: host.parse().unwrap(),
port,
..Default::default()
})
.manage(SharedState::default()) .manage(SharedState::default())
.mount("/", routes![index]) .mount("/", routes![index])
.mount("/api/", routes![get_game, new_game, make_move]) .mount("/api/", routes![get_game, new_game, make_move])
} }