implement looking up chat id

This commit is contained in:
yuni 2024-04-12 23:03:46 +02:00
parent 253604e9aa
commit a572959df3
5 changed files with 85 additions and 28 deletions

View file

@ -1,45 +1,101 @@
extern crate yaml_rust;
use bevy::prelude::*;
use yaml_rust::{Yaml, YamlLoader};
use crate::{audio};
pub const CHATS: &[&str] = &[
include_str!("chats/serenity.yaml"),
include_str!("chats/startrans.yaml"),
];
pub const TOKEN_CHAT: &str = "chat";
pub struct ChatPlugin;
impl Plugin for ChatPlugin {
fn build(&self, app: &mut App) {
app.add_systems(Startup, load_chats);
app.add_systems(Update, (
handle_new_conversations,
));
app.add_event::<StartConversationEvent>();
app.insert_resource(ChatDB(Vec::new()));
}
}
#[derive(Resource)]
pub struct ChatDB(Vec<Yaml>);
impl ChatDB {
pub fn get_chat_by_id(&self, id: &String) -> Result<u32, String> {
let mut found: Option<u32> = None;
for (index, object_yaml) in self.0.iter().enumerate() {
if let Some(object_vec) = object_yaml.as_vec() {
if object_vec.len() == 0 {
continue;
}
let first_item = &object_vec[0];
if let Some(hash) = first_item.as_hash() {
if let Some(chat_id_yaml) = hash.get(&Yaml::String(TOKEN_CHAT.to_string())) {
if let Some(chat_id) = chat_id_yaml.as_str() {
if chat_id != id {
continue;
}
if found.is_some() {
return Err("Found multiple chats with the same id!".to_string());
}
found = Some(index as u32);
}
}
}
} else {
warn!("Non-list YAML object found while processing chat specs");
}
}
if let Some(result) = found {
return Ok(result);
}
return Err(format!("No chat with the conversation ID `{id}` was found."));
}
}
#[derive(Component)]
#[derive(Clone)]
pub struct Talker {
pub pronoun: String,
pub conv_id: String,
pub name: Option<String>,
pub pronoun: Option<String>,
}
impl Default for Talker { fn default() -> Self { Self {
pronoun: "they/them".to_string(),
conv_id: "undefined".to_string(),
}}}
#[derive(Event)]
pub struct StartConversationEvent {
pub talker: Talker,
}
#[derive(Resource)]
pub struct ChatDB(Vec<yaml_rust::Yaml>);
pub fn load_chats(mut chatdb: ResMut<ChatDB>) {
for chat_yaml in CHATS {
if let Ok(mut yaml_data) = yaml_rust::YamlLoader::load_from_str(chat_yaml) {
if let Ok(mut yaml_data) = YamlLoader::load_from_str(chat_yaml) {
chatdb.0.append(&mut yaml_data);
} else {
error!("Could not load chat definitions. Validate files in `src/chats/` path.");
}
}
}
pub fn handle_new_conversations(
//mut commands: Commands,
mut er_conv: EventReader<StartConversationEvent>,
mut ew_sfx: EventWriter<audio::PlaySfxEvent>,
chatdb: Res<ChatDB>,
//time: Res<Time>,
) {
for event in er_conv.read() {
match (*chatdb).get_chat_by_id(&event.talker.conv_id) {
Ok(chat_id) => {
ew_sfx.send(audio::PlaySfxEvent(audio::Sfx::Ping));
dbg!(chat_id);
}
Err(error) => {
error!("Error while looking for chat ID: {error}");
}
}
}
}

View file

@ -9,21 +9,13 @@
---
- chat: ClippyPizza
- chat: SubduedClippy
- At your service!
---
- chat: Rudy
- system: "Error: No response"
- system: Lifeform in cryostasis detected
---
- chat: Icarus
- if talkcount > 0:
- "Oh, hi again! How are you?"
@ -130,7 +122,7 @@
---
- chat: SpacePizza
- chat: SpacePizzaChef
- Welcome to Space Pizza™, best pizza around the rings!
- Great to see a customer, we don't get many lately
- Would you like to order today's special?

View file

@ -1,3 +1,11 @@
- chat: NPCinCryoStasis
- system: "Error: No response"
- system: Lifeform in cryostasis detected
---
- chat: ClippyTransSerenity
- Welcome at StarTrans Cargo Services! You have reached Serenity Station.
- "Ready for a trip? Available bus stops: Oscillation Station, Metis Prime Station"

View file

@ -693,7 +693,8 @@ fn spawn_entities(
if !state.chat.is_empty() {
actor.insert(chat::Talker {
conv_id: state.chat.clone(),
..default()
name: state.name.clone(),
pronoun: Some(state.pronoun.clone()),
});
}
if state.is_vehicle {

View file

@ -179,12 +179,12 @@ actor -3300 10 0 pizzeria
thrust 15 6 3 400 0.5
rotationy -0.5
scale 3
chatid pizzaclippy
chatid SubduedClippy
actor -35 0 0 suit
relativeto pizzeria
name "Space Pizza™"
chatid pizzeria
chatid SpacePizzaChef
armodel suit_ar_chefhat
alive yes
scale 2
@ -199,7 +199,7 @@ actor -3300 10 0 pizzeria
actor 60 -15 -40 suit
relativeto player
name Icarus
chatid hi_icarus
chatid Icarus
alive yes
scale 2
collider capsule 1 0.5
@ -215,7 +215,7 @@ actor -300 0 40 suit
relativeto player
id drifter
name "Drifter"
chatid drifter
chatid Drifter
oxygen 0.08
scale 2
collider capsule 1 0.5
@ -238,7 +238,7 @@ actor 100 -18000 2000 "orb_busstop"
thrust 15 6 3 400 0.5
rotationy -0.5
scale 3
chatid "busstopclippy"
chatid ClippyTransSerenity
actor 40 10 40 "orb_busstop"
name "Light Orb"
relativeto busstopclippy
@ -263,7 +263,7 @@ actor 100 -18000 2000 "orb_busstop"
thrust 1.2 1 1 400 1.5
scale 2
collider capsule 1 0.5
chatid "busstop1clippynpc1"
chatid NPCinCryoStasis
actor 147002e3 165001e3 336e6 "orb_busstop"
relativeto jupiter
@ -283,7 +283,7 @@ actor 147002e3 165001e3 336e6 "orb_busstop"
thrust 15 6 3 400 0.5
rotationy -0.5
scale 3
chatid "busstopclippy"
chatid ClippyTransOscillation
actor 40 10 40 "orb_busstop"
name "Light Orb"
relativeto busstopclippy2
@ -319,7 +319,7 @@ actor 27643e3 -44e3 -124434e3 "orb_busstop"
thrust 15 6 3 400 0.5
rotationy -0.5
scale 3
chatid "busstopclippy"
chatid ClippyTransMetis
actor 40 10 40 "orb_busstop"
name "Light Orb"
relativeto busstopclippy3