added CHANGE command

This commit is contained in:
2026-05-14 21:24:28 +01:00
parent a55053dc97
commit a18928c673
11 changed files with 82 additions and 11 deletions
BIN
View File
Binary file not shown.
+17 -2
View File
@@ -4,6 +4,7 @@ use crate::
// internal code
character,
// libraries
json,
HashMap,
Arc,
Mutex,
@@ -11,6 +12,7 @@ use crate::
mpsc::Sender,
info,
debug,
warn,
Serialize,
Deserialize,
};
@@ -60,8 +62,21 @@ pub async fn api_process
.and(characters_filter)
.map(|name: String, characters: Arc<Mutex<HashMap<String, character::Character>>>|
{
let map = characters.lock().unwrap(); // TODO eh
let reply = map.get(&name).unwrap(); // TODO eh
let characters = match characters.lock()
{
Ok(data) => data,
Err(poisoned) =>
{
warn!("Character Mutex is poisoned");
poisoned.into_inner()
},
};
let Some(reply) = characters.get(&name)
else
{
warn!("Client requested character that does not exist");
return warp::reply::json(&json!({"reply": "invalid character"}));
};
debug!("GET: name: {name}");
warp::reply::json(&reply)
}).boxed();
+28
View File
@@ -31,6 +31,34 @@ pub struct Character
bottom_clothing: String,
shoes: String,
}
impl Character {
pub fn set_field(&mut self, field: &str, value: &str) -> Result<(), ()> {
match field {
"name" => self.name = value.to_string(),
"gender" => self.gender = value.to_string(),
"eye_color" => self.eye_color = value.to_string(),
"pronoun_subject" => self.pronoun_subject = value.to_string(),
"pronoun_object" => self.pronoun_object = value.to_string(),
"pronoun_deppos" => self.pronoun_deppos = value.to_string(),
"pronoun_indpos" => self.pronoun_indpos = value.to_string(),
"pronoun_reflex" => self.pronoun_reflex = value.to_string(),
"animation" => self.animation = value.to_string(),
"head" => self.head = value.to_string(),
"hair" => self.hair = value.to_string(),
"torso" => self.torso = value.to_string(),
"arm" => self.arm = value.to_string(),
"leg" => self.leg = value.to_string(),
"hair_color" => self.hair_color = value.to_string(),
"top_clothing" => self.top_clothing = value.to_string(),
"bottom_clothing" => self.bottom_clothing = value.to_string(),
"shoes" => self.shoes = value.to_string(),
_ => return Err(()),
}
Ok(())
}
}
pub fn character_parse(archive: &mut ZipArchive<File>)
-> Result<Arc<Mutex<HashMap<String, Character>>>,String>
+1
View File
@@ -19,6 +19,7 @@ use serde::
Deserialize,
Serialize
};
use serde_json::json;
use zip::
{
ZipArchive,
+29 -3
View File
@@ -165,7 +165,7 @@ fn character_parse
// Ensure the index is valid (the index is not beyond the vector)
let token = tokens
.get(sum_index)
.ok_or_else(|| "File reached termination point".to_string())?;
.ok_or_else(|| "File unexpectedly reached termination point".to_string())?;
match token.to_lowercase().as_str()
{
// The character is saying something, so grab the text and pass it
@@ -173,7 +173,7 @@ fn character_parse
"says" =>
{
info!("SAYS command with character {character_name}");
match strings::extract_quoted(&tokens[sum_index+1..]) // TODO increment to after the string
match strings::extract_quoted(&tokens[sum_index+1..])
{
Some((output_string, counter)) =>
{
@@ -185,9 +185,35 @@ fn character_parse
data.character = character_name;
data.choices = vec![];
},
None => return Err(String::from("Unable to read string")),
None => return Err("Unable to read output string".to_string()),
}
},
// Change the property of the selected character eg @tim CHANGE name "Bill Buffins"
// will change the character with ID tim to "Bill Buffins"; a character's ID cannot change
"change" =>
{
sum_index += 1;
let feature = tokens
.get(sum_index)
.ok_or_else(|| "File unexpectedly reached termination point".to_string())?;
let output_string: String;
(output_string, sum_index) = match strings::extract_quoted(&tokens[sum_index+1..])
{
Some((string,counter)) => (string,sum_index+counter),
None => return Err("Unable to parse property to change character".to_string()),
};
info!("CHANGE command with character {character_name} feature {feature}");
let mut characters = characters.lock().expect("Data cannot be unlocked");
if let Some(character) = characters.get_mut(&character_name)
{
if character.set_field(feature, &output_string)
.is_err() { warn!("Feature {feature} does not exist") };
}
let mut data = data_to_send.lock().unwrap();
data.action_type = String::from("change");
data.content = String::new();
data.character = character_name;
},
// Catch all condition, if the instruction is unrecognised as a
// character command
_ => return Err(format!("Invalid command: {token}")),