From c787e7caf424239d4672dfb71ddfd9bda4fd2e8b Mon Sep 17 00:00:00 2001 From: yuni Date: Sat, 26 Oct 2024 22:01:52 +0200 Subject: [PATCH] implement persistent phonebook --- src/chat.rs | 41 ++++++++++++++++++++++++++++++++--------- src/chats/phone.yaml | 9 +++++++-- src/chats/serenity.yaml | 1 + src/var.rs | 1 + 4 files changed, 41 insertions(+), 11 deletions(-) diff --git a/src/chat.rs b/src/chat.rs index 64417d3..945d2b0 100644 --- a/src/chat.rs +++ b/src/chat.rs @@ -26,6 +26,8 @@ pub const CHATS: &[&str] = &[ include_str!("chats/thebe.yaml"), ]; +pub const CONTACTS: &[&str] = &["icarus", "travel", "luna", "nox"]; + pub const TEXT_CONTINUE: &str = "Continue..."; pub const TOKEN_CHAT: &str = "chat"; @@ -897,6 +899,7 @@ pub fn handle_chat_scripts( mut ew_achievement: EventWriter, id2pos: Res, id2v: Res, + mut prefs: ResMut, ) { for script in er_chatscript.read() { // Parse the script string @@ -1020,6 +1023,14 @@ pub fn handle_chat_scripts( } } } + "registercontact" => { + if CONTACTS.contains(¶m1) { + prefs.contacts.push(param1.to_string()); + prefs.save(); + } else { + error!("Can't register contact `{param1}', it doesn't exist in the chat::CONTACTS constant."); + } + } _ => { error!("Error, undefined chat script {name}"); } @@ -1030,6 +1041,7 @@ pub fn handle_chat_scripts( pub fn update_chat_variables( mut vars: ResMut, settings: Res, + prefs: Res, q_player: Query<&actor::Suit, With>, ) { if let Ok(suit) = q_player.get_single() { @@ -1043,15 +1055,7 @@ pub fn update_chat_variables( "player_suit_health_percent", ((suit.integrity * 100.0).round() as u8).to_string(), ); - vars.set_in_scope( - "$", - "ar", - if settings.hud_active { - String::from("1") - } else { - String::from("0") - }, - ); + vars.set_in_scope("$", "ar", bool2chatvar(settings.hud_active)); let wears_chefhat = if let Some(ava) = hud::PLAYER_AR_AVATARS.get(settings.ar_avatar) { match ava.0 { hud::Avatar::ChefHat => 1, @@ -1061,5 +1065,24 @@ pub fn update_chat_variables( 0 }; vars.set_in_scope("$", "chefhat", wears_chefhat.to_string()); + + // Set phone variables + let mut any = false; + for contact in CONTACTS { + let value = prefs.contacts.contains(&contact.to_string()); + if value { + any = true; + } + vars.set_in_scope("phone", contact, bool2chatvar(value)); + } + vars.set_in_scope("phone", "any", bool2chatvar(any)); + } +} + +fn bool2chatvar(var: bool) -> String { + if var { + String::from("1") + } else { + String::from("0") } } diff --git a/src/chats/phone.yaml b/src/chats/phone.yaml index 3cc68cc..079a761 100644 --- a/src/chats/phone.yaml +++ b/src/chats/phone.yaml @@ -1,9 +1,14 @@ - chat: phone +- if ~phone$any: + - "Error: Phonebook empty." + - goto: EXIT - Select contact to call. -- FASTravel: +- if: phone$travel + FASTravel: - script: changename FASTravelâ„¢ Chatbot - This is FASTravel, how can I help you? -- Icarus: +- if: phone$icarus + Icarus: - script: changename Icarus - Well hi there! - "[Cancel]": diff --git a/src/chats/serenity.yaml b/src/chats/serenity.yaml index ef61284..545bd5e 100644 --- a/src/chats/serenity.yaml +++ b/src/chats/serenity.yaml @@ -14,6 +14,7 @@ - How are you doing? - goto: howru - Oh hey, you're awake! +- script: registercontact icarus - set: $met - I found you drifting out cold, and thought, I better watch over you. - Took us here behind that moonlet, to shield you from the micrometeorites. diff --git a/src/var.rs b/src/var.rs index fdf4f18..e8d08c5 100644 --- a/src/var.rs +++ b/src/var.rs @@ -477,6 +477,7 @@ pub struct Preferences { #[serde(default = "Preferences::default_flashlight_power")] pub flashlight_power: usize, // 0-2 pub thruster_boost: usize, // 0-2 + pub contacts: Vec, #[serde(skip)] pub source_file: Option,