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 // internal code
character, character,
// libraries // libraries
json,
HashMap, HashMap,
Arc, Arc,
Mutex, Mutex,
@@ -11,6 +12,7 @@ use crate::
mpsc::Sender, mpsc::Sender,
info, info,
debug, debug,
warn,
Serialize, Serialize,
Deserialize, Deserialize,
}; };
@@ -60,8 +62,21 @@ pub async fn api_process
.and(characters_filter) .and(characters_filter)
.map(|name: String, characters: Arc<Mutex<HashMap<String, character::Character>>>| .map(|name: String, characters: Arc<Mutex<HashMap<String, character::Character>>>|
{ {
let map = characters.lock().unwrap(); // TODO eh let characters = match characters.lock()
let reply = map.get(&name).unwrap(); // TODO eh {
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}"); debug!("GET: name: {name}");
warp::reply::json(&reply) warp::reply::json(&reply)
}).boxed(); }).boxed();
+28
View File
@@ -31,6 +31,34 @@ pub struct Character
bottom_clothing: String, bottom_clothing: String,
shoes: 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>) pub fn character_parse(archive: &mut ZipArchive<File>)
-> Result<Arc<Mutex<HashMap<String, Character>>>,String> -> Result<Arc<Mutex<HashMap<String, Character>>>,String>
+1
View File
@@ -19,6 +19,7 @@ use serde::
Deserialize, Deserialize,
Serialize Serialize
}; };
use serde_json::json;
use zip:: use zip::
{ {
ZipArchive, ZipArchive,
+29 -3
View File
@@ -165,7 +165,7 @@ fn character_parse
// Ensure the index is valid (the index is not beyond the vector) // Ensure the index is valid (the index is not beyond the vector)
let token = tokens let token = tokens
.get(sum_index) .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() match token.to_lowercase().as_str()
{ {
// The character is saying something, so grab the text and pass it // The character is saying something, so grab the text and pass it
@@ -173,7 +173,7 @@ fn character_parse
"says" => "says" =>
{ {
info!("SAYS command with character {character_name}"); 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)) => Some((output_string, counter)) =>
{ {
@@ -185,9 +185,35 @@ fn character_parse
data.character = character_name; data.character = character_name;
data.choices = vec![]; 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 // Catch all condition, if the instruction is unrecognised as a
// character command // character command
_ => return Err(format!("Invalid command: {token}")), _ => return Err(format!("Invalid command: {token}")),
+6 -6
View File
@@ -1,14 +1,14 @@
{ {
"tim": { "tim": {
"name": "Timothy Sharpshooter", "name": "Timothy Sharpshooter",
"gender": "", "gender": "Male",
"skin_color": "", "skin_color": "",
"eye_color": "", "eye_color": "",
"pronoun_subject": "", "pronoun_subject": "He",
"pronoun_object": "", "pronoun_object": "Him",
"pronoun_deppos": "", "pronoun_deppos": "His",
"pronoun_indpos": "", "pronoun_indpos": "His",
"pronoun_reflex": "", "pronoun_reflex": "Himself",
"animation": "", "animation": "",
"head": "", "head": "",
"hair": "", "hair": "",
Binary file not shown.
Binary file not shown.
Binary file not shown.
+1
View File
@@ -1,4 +1,5 @@
@tim says "hello world, it's a good day" @tim says "hello world, it's a good day"
@tim change name "Timothy Fineshooter"
choice "choice numero uno" { choice "choice numero uno" {
@tim says "super sad" @tim says "super sad"
} }
BIN
View File
Binary file not shown.