Skip to content

Commit 412955a

Browse files
committed
Improvements and add hill gamemode support
1 parent c500758 commit 412955a

File tree

2 files changed

+27
-10
lines changed

2 files changed

+27
-10
lines changed

src/main.rs

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use tokio::{
1414
use wasmer_cache::{Cache, FileSystemCache};
1515
use wasi_process2::WasiProcess;
1616
use wasmer_wasi::{WasiFunctionEnv, WasiVersion, WasiEnv};
17-
use wasmer::{AsStoreMut, AsStoreRef, Instance, FunctionEnv};
17+
use wasmer::AsStoreMut;
1818

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

@@ -72,6 +72,9 @@ enum Run {
7272
/// It will then start receiving newline-delimited `ProgramInput` json object. It must respond to
7373
/// each one with a `ProgramOutput` json object followed by a newline. The match is over when stdin is closed, and
7474
/// the process may be forcefully terminated after that.
75+
///
76+
/// Please note: `run term` will not display runtime errors. In order to see these, please use
77+
/// `run web`
7578
#[structopt(verbatim_doc_comment)]
7679
Term {
7780
#[structopt(parse(from_os_str))]
@@ -86,14 +89,17 @@ enum Run {
8689
raw: bool,
8790
/// Show only the blue robot's logs
8891
#[structopt(long)]
92+
no_logs: bool,
93+
/// Show only the blue robot's logs
94+
#[structopt(long)]
8995
blue_logs_only: bool,
9096
/// Show only the red robot's logs
9197
#[structopt(long)]
9298
red_logs_only: bool,
9399
/// Only show the results of the battle
94100
#[structopt(long)]
95101
results_only: bool,
96-
/// Choose the gamemode. Current supported: "Normal"
102+
/// Choose the gamemode. Current supported: "Normal" (default) | "Hill"
97103
#[structopt(long, parse(from_os_str))]
98104
game_mode: Option<OsString>,
99105
/// Specify a random seed for robot spawning. It can be of any length.
@@ -127,6 +133,9 @@ enum Run {
127133
/// The network port to listen to.
128134
#[structopt(short, long, env = "PORT")]
129135
port: Option<u16>,
136+
/// Choose the gamemode. Current supported: "Normal" (default) | "Hill"
137+
#[structopt(long, parse(from_os_str))]
138+
game_mode: Option<OsString>,
130139
},
131140
}
132141

@@ -376,6 +385,7 @@ async fn try_main() -> anyhow::Result<()> {
376385
redbot,
377386
turn_num,
378387
raw,
388+
no_logs,
379389
blue_logs_only,
380390
red_logs_only,
381391
results_only,
@@ -392,6 +402,7 @@ async fn try_main() -> anyhow::Result<()> {
392402
},
393403
game_mode,
394404
!raw && !results_only,
405+
no_logs,
395406
red_logs_only,
396407
blue_logs_only,
397408
)
@@ -414,7 +425,7 @@ async fn try_main() -> anyhow::Result<()> {
414425
while let Some(line) = stdin.next_line().await.unwrap() {
415426
match serde_json::from_str(&line) {
416427
Ok(game_spec) => {
417-
let out = run_game(game_spec, game_mode, false, false, false).await?;
428+
let out = run_game(game_spec, game_mode, false, false, false, false).await?;
418429

419430
let mut value = serde_json::to_value(&out).unwrap();
420431
if let serde_json::Value::Object(v) = &mut value {
@@ -432,12 +443,14 @@ async fn try_main() -> anyhow::Result<()> {
432443
robots,
433444
address,
434445
port,
446+
game_mode: game_mode_string
435447
} => {
436448
let ids = robots
437449
.iter()
438450
.map(|id| RobotId::parse(id))
439451
.collect::<Result<Vec<_>, _>>()?;
440-
server::serve(ids, address, port).await?;
452+
let game_mode = parse_game_mode(game_mode_string);
453+
server::serve(ids, game_mode, address, port).await?;
441454
}
442455
},
443456

@@ -802,6 +815,7 @@ async fn run_game(
802815
spec: GameSpec,
803816
game_mode: GameMode,
804817
display_turns: bool,
818+
no_logs: bool,
805819
red_logs_only: bool,
806820
blue_logs_only: bool,
807821
) -> anyhow::Result<MainOutput> {
@@ -827,7 +841,7 @@ async fn run_game(
827841
runners,
828842
|turn_state| {
829843
if display_turns {
830-
display::display_turn(turn_state, !red_logs_only, !blue_logs_only)
844+
display::display_turn(turn_state, !no_logs && !red_logs_only, !no_logs && !blue_logs_only)
831845
.expect("printing failed");
832846
}
833847
},

src/server.rs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,23 +15,25 @@ use super::{RobotId, Runner};
1515
struct Context {
1616
r1: OwningRef<Arc<Vec<RobotId>>, RobotId>,
1717
ids: Arc<Vec<RobotId>>,
18+
game_mode: logic::GameMode
1819
}
1920

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

24-
let ctx = Context { r1, ids };
25+
let ctx = Context { r1, ids, game_mode };
2526
let ctx = warp::any().map(move || ctx.clone());
2627

2728
let route = warp::path("getflags")
2829
.and(ctx.clone())
2930
.and(warp::get())
30-
.map(move |Context { r1, .. }| {
31+
.map(move |Context { r1, game_mode, .. }| {
3132
let (user1, robot1) = r1.display_id();
3233
let body = serde_json::json!({
3334
"user": user1,
3435
"robot": robot1,
36+
"gameMode": game_mode
3537
});
3638
warp::reply::json(&body)
3739
})
@@ -86,6 +88,7 @@ pub async fn serve(ids: Vec<RobotId>, address: String, port: Option<u16>) -> any
8688

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

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

108111
async fn run(
109-
Context { r1, ids }: Context,
112+
Context { r1, ids, game_mode }: Context,
110113
params: RunParams,
111114
) -> Result<impl warp::Reply, warp::Rejection> {
112115
let r2 = OwningRef::new(ids).try_map(|ids| ids.get(params.id).ok_or_else(|| warp::reject()))?;
@@ -140,7 +143,7 @@ async fn run(
140143
params.turns,
141144
true,
142145
None,
143-
logic::GameMode::Normal,
146+
game_mode,
144147
None,
145148
)
146149
.await;

0 commit comments

Comments
 (0)