From 31b9834a7c1967d56f5634329742c7bc9885a44f Mon Sep 17 00:00:00 2001 From: Christopher Date: Tue, 28 Feb 2023 13:53:38 +1300 Subject: [PATCH] work on nav command this involves architectural shifts --- 84_Super_Star_Trek/rust/src/main.rs | 62 +++++++++++++++++++-------- 84_Super_Star_Trek/rust/src/model.rs | 4 +- 84_Super_Star_Trek/rust/src/update.rs | 23 +++++++++- 84_Super_Star_Trek/rust/src/view.rs | 5 ++- 4 files changed, 73 insertions(+), 21 deletions(-) diff --git a/84_Super_Star_Trek/rust/src/main.rs b/84_Super_Star_Trek/rust/src/main.rs index 20cd7ea3..43e3045c 100644 --- a/84_Super_Star_Trek/rust/src/main.rs +++ b/84_Super_Star_Trek/rust/src/main.rs @@ -17,35 +17,63 @@ fn main() { } fn wait_for_command(game_status: &GameStatus) -> Message { - let stdin = stdin(); - let mut stdout = stdout(); loop { match game_status { - _ => { - print!("Command? "); - let _ = stdout.flush(); - - let mut buffer = String::new(); - if let Ok(_) = stdin.read_line(&mut buffer) { - let text = buffer.trim_end(); - if let Some(msg) = as_message(text, game_status) { - return msg - } - print_command_help(); + GameStatus::NeedDirectionForNav => { + let text = prompt("Course (1-9)?"); + if let Some(msg) = as_message(&text, game_status) { + return msg } + }, + GameStatus::NeedSpeedForNav(_) => { + let text = prompt("Warp Factor (0-8)?"); + if let Some(msg) = as_message(&text, game_status) { + return msg + } + }, + _ => { + let text = prompt("Command?"); + if let Some(msg) = as_message(&text, game_status) { + return msg + } + print_command_help(); } } } } -fn as_message(text: &str, game_status: &GameStatus) -> Option { - if text == "" { - return None +fn prompt(prompt: &str) -> String { + let stdin = stdin(); + let mut stdout = stdout(); + + print!("{prompt} "); + let _ = stdout.flush(); + + let mut buffer = String::new(); + if let Ok(_) = stdin.read_line(&mut buffer) { + return buffer.trim_end().into(); } + "".into() +} + +fn as_message(text: &str, game_status: &GameStatus) -> Option { match game_status { + GameStatus::NeedDirectionForNav => { + match text.parse::() { + Ok(n) if (n >= 1 && n <= 8) => Some(Message::DirectionForNav(n)), + _ => None + } + }, + GameStatus::NeedSpeedForNav(dir) => { + match text.parse::() { + Ok(n) if (n >= 1 && n <= 8) => Some(Message::DirectionAndSpeedForNav(*dir, n)), + _ => None + } + } _ => { match text { "SRS" => Some(Message::RequestShortRangeScan), + "NAV" => Some(Message::RequestNavigation), _ => None } } @@ -53,5 +81,5 @@ fn as_message(text: &str, game_status: &GameStatus) -> Option { } fn print_command_help() { - println!("valid commands are just SRS at the mo") + println!("valid commands are just SRS and NAV at the mo") } \ No newline at end of file diff --git a/84_Super_Star_Trek/rust/src/model.rs b/84_Super_Star_Trek/rust/src/model.rs index 4468e6f9..a05d8566 100644 --- a/84_Super_Star_Trek/rust/src/model.rs +++ b/84_Super_Star_Trek/rust/src/model.rs @@ -36,7 +36,9 @@ pub struct Enterprise { } pub enum GameStatus { - ShortRangeScan + ShortRangeScan, + NeedDirectionForNav, + NeedSpeedForNav(u8), } impl Galaxy { diff --git a/84_Super_Star_Trek/rust/src/update.rs b/84_Super_Star_Trek/rust/src/update.rs index cf30c83e..5788c778 100644 --- a/84_Super_Star_Trek/rust/src/update.rs +++ b/84_Super_Star_Trek/rust/src/update.rs @@ -1,7 +1,10 @@ use crate::model::{Galaxy, GameStatus}; pub enum Message { - RequestShortRangeScan + RequestShortRangeScan, + RequestNavigation, + DirectionForNav (u8), + DirectionAndSpeedForNav (u8, u8), } pub fn update(message: Message, model: Galaxy) -> Galaxy { @@ -10,6 +13,24 @@ pub fn update(message: Message, model: Galaxy) -> Galaxy { Galaxy { game_status: GameStatus::ShortRangeScan, ..model + }, + Message::RequestNavigation => { + Galaxy { + game_status: GameStatus::NeedDirectionForNav, + ..model } + }, + Message::DirectionForNav(dir) => { + Galaxy { + game_status: GameStatus::NeedSpeedForNav(dir), + ..model + } + }, + Message::DirectionAndSpeedForNav(dir, speed) => { + Galaxy { + game_status: GameStatus::ShortRangeScan, + ..model + } + } } } \ No newline at end of file diff --git a/84_Super_Star_Trek/rust/src/view.rs b/84_Super_Star_Trek/rust/src/view.rs index 4f0975cf..9527289d 100644 --- a/84_Super_Star_Trek/rust/src/view.rs +++ b/84_Super_Star_Trek/rust/src/view.rs @@ -5,8 +5,9 @@ pub fn view(model: &Galaxy) { match model.game_status { GameStatus::ShortRangeScan => { let quadrant = &model.quadrants[model.enterprise.sector.as_index()]; - render_quadrant(&model.enterprise.sector, quadrant) - } + render_quadrant(&model.enterprise.sector, quadrant); + }, + _ => () } }