Skip to content

Commit

Permalink
Improvements and add hill gamemode support
Browse files Browse the repository at this point in the history
  • Loading branch information
outkine committed Mar 31, 2024
1 parent c500758 commit 412955a
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 10 deletions.
24 changes: 19 additions & 5 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use tokio::{
use wasmer_cache::{Cache, FileSystemCache};
use wasi_process2::WasiProcess;
use wasmer_wasi::{WasiFunctionEnv, WasiVersion, WasiEnv};
use wasmer::{AsStoreMut, AsStoreRef, Instance, FunctionEnv};
use wasmer::AsStoreMut;

use logic::{GameMode, MainOutput, RobotRunner};

Expand Down Expand Up @@ -72,6 +72,9 @@ enum Run {
/// It will then start receiving newline-delimited `ProgramInput` json object. It must respond to
/// each one with a `ProgramOutput` json object followed by a newline. The match is over when stdin is closed, and
/// the process may be forcefully terminated after that.
///
/// Please note: `run term` will not display runtime errors. In order to see these, please use
/// `run web`
#[structopt(verbatim_doc_comment)]
Term {
#[structopt(parse(from_os_str))]
Expand All @@ -86,14 +89,17 @@ enum Run {
raw: bool,
/// Show only the blue robot's logs
#[structopt(long)]
no_logs: bool,
/// Show only the blue robot's logs
#[structopt(long)]
blue_logs_only: bool,
/// Show only the red robot's logs
#[structopt(long)]
red_logs_only: bool,
/// Only show the results of the battle
#[structopt(long)]
results_only: bool,
/// Choose the gamemode. Current supported: "Normal"
/// Choose the gamemode. Current supported: "Normal" (default) | "Hill"
#[structopt(long, parse(from_os_str))]
game_mode: Option<OsString>,
/// Specify a random seed for robot spawning. It can be of any length.
Expand Down Expand Up @@ -127,6 +133,9 @@ enum Run {
/// The network port to listen to.
#[structopt(short, long, env = "PORT")]
port: Option<u16>,
/// Choose the gamemode. Current supported: "Normal" (default) | "Hill"
#[structopt(long, parse(from_os_str))]
game_mode: Option<OsString>,
},
}

Expand Down Expand Up @@ -376,6 +385,7 @@ async fn try_main() -> anyhow::Result<()> {
redbot,
turn_num,
raw,
no_logs,
blue_logs_only,
red_logs_only,
results_only,
Expand All @@ -392,6 +402,7 @@ async fn try_main() -> anyhow::Result<()> {
},
game_mode,
!raw && !results_only,
no_logs,
red_logs_only,
blue_logs_only,
)
Expand All @@ -414,7 +425,7 @@ async fn try_main() -> anyhow::Result<()> {
while let Some(line) = stdin.next_line().await.unwrap() {
match serde_json::from_str(&line) {
Ok(game_spec) => {
let out = run_game(game_spec, game_mode, false, false, false).await?;
let out = run_game(game_spec, game_mode, false, false, false, false).await?;

let mut value = serde_json::to_value(&out).unwrap();
if let serde_json::Value::Object(v) = &mut value {
Expand All @@ -432,12 +443,14 @@ async fn try_main() -> anyhow::Result<()> {
robots,
address,
port,
game_mode: game_mode_string
} => {
let ids = robots
.iter()
.map(|id| RobotId::parse(id))
.collect::<Result<Vec<_>, _>>()?;
server::serve(ids, address, port).await?;
let game_mode = parse_game_mode(game_mode_string);
server::serve(ids, game_mode, address, port).await?;
}
},

Expand Down Expand Up @@ -802,6 +815,7 @@ async fn run_game(
spec: GameSpec,
game_mode: GameMode,
display_turns: bool,
no_logs: bool,
red_logs_only: bool,
blue_logs_only: bool,
) -> anyhow::Result<MainOutput> {
Expand All @@ -827,7 +841,7 @@ async fn run_game(
runners,
|turn_state| {
if display_turns {
display::display_turn(turn_state, !red_logs_only, !blue_logs_only)
display::display_turn(turn_state, !no_logs && !red_logs_only, !no_logs && !blue_logs_only)
.expect("printing failed");
}
},
Expand Down
13 changes: 8 additions & 5 deletions src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,25 @@ use super::{RobotId, Runner};
struct Context {
r1: OwningRef<Arc<Vec<RobotId>>, RobotId>,
ids: Arc<Vec<RobotId>>,
game_mode: logic::GameMode
}

pub async fn serve(ids: Vec<RobotId>, address: String, port: Option<u16>) -> anyhow::Result<()> {
pub async fn serve(ids: Vec<RobotId>, game_mode: logic::GameMode, address: String, port: Option<u16>) -> anyhow::Result<()> {
let ids = Arc::new(ids);
let r1 = OwningRef::new(ids.clone()).map(|v| v.first().unwrap());

let ctx = Context { r1, ids };
let ctx = Context { r1, ids, game_mode };
let ctx = warp::any().map(move || ctx.clone());

let route = warp::path("getflags")
.and(ctx.clone())
.and(warp::get())
.map(move |Context { r1, .. }| {
.map(move |Context { r1, game_mode, .. }| {
let (user1, robot1) = r1.display_id();
let body = serde_json::json!({
"user": user1,
"robot": robot1,
"gameMode": game_mode
});
warp::reply::json(&body)
})
Expand Down Expand Up @@ -86,6 +88,7 @@ pub async fn serve(ids: Vec<RobotId>, address: String, port: Option<u16>) -> any

webbrowser::open(&url).ok();
println!("Website running at {}", url);
println!("Robots are available in the opponent select menu");
eprintln!("Press Enter to stop");

let listener = tokio_stream::wrappers::TcpListenerStream::new(listener);
Expand All @@ -106,7 +109,7 @@ struct RunParams {
}

async fn run(
Context { r1, ids }: Context,
Context { r1, ids, game_mode }: Context,
params: RunParams,
) -> Result<impl warp::Reply, warp::Rejection> {
let r2 = OwningRef::new(ids).try_map(|ids| ids.get(params.id).ok_or_else(|| warp::reject()))?;
Expand Down Expand Up @@ -140,7 +143,7 @@ async fn run(
params.turns,
true,
None,
logic::GameMode::Normal,
game_mode,
None,
)
.await;
Expand Down

0 comments on commit 412955a

Please sign in to comment.