not sure what is going on anymore, a lot of changes as im working on the chat thing. currently simple chat seems to work

This commit is contained in:
Rusty Striker 2024-10-01 20:53:19 +03:00
parent dd34317d14
commit acf96db186
Signed by: RustyStriker
GPG key ID: 87E4D691632DFF15
11 changed files with 170 additions and 52 deletions

View file

@ -1,34 +1,34 @@
use serde::{Deserialize, Serialize};
use crate::game::{action::GameEntry, chat_message::ChatMessage};
use crate::game::{entry::GameEntry, chat_message::ChatMessage};
#[derive(Serialize, Deserialize)]
pub enum PEntry {
pub enum Entry {
Weapon(Weapon),
Consumable(Consumable),
Spell(Spell),
}
impl GameEntry for PEntry {
impl GameEntry for Entry {
fn display_name(&self) -> String {
match self {
PEntry::Weapon(weapon) => weapon.name.clone(),
PEntry::Consumable(consumable) => consumable.name.clone(),
PEntry::Spell(spell) => spell.name.clone(),
Entry::Weapon(weapon) => weapon.name.clone(),
Entry::Consumable(consumable) => consumable.name.clone(),
Entry::Spell(spell) => spell.name.clone(),
}
}
fn load(entry: &str) -> Option<PEntry> {
fn load(entry: &str) -> Option<Entry> {
println!("loading {}", entry);
let json = std::fs::read_to_string(format!("assets/pf2r/{}.json", entry)).ok()?;
println!("{}", &json);
if entry.starts_with("weapon/") {
serde_json::from_str::<Weapon>(&json).map(|w| PEntry::Weapon(w)).ok()
serde_json::from_str::<Weapon>(&json).map(|w| Entry::Weapon(w)).ok()
}
else if entry.starts_with("spell/") {
serde_json::from_str::<Spell>(&json).map(|s| PEntry::Spell(s)).ok()
serde_json::from_str::<Spell>(&json).map(|s| Entry::Spell(s)).ok()
}
else if entry.starts_with("consumable/") {
serde_json::from_str::<Consumable>(&json).map(|s| PEntry::Consumable(s)).ok()
serde_json::from_str::<Consumable>(&json).map(|s| Entry::Consumable(s)).ok()
}
else {
None
@ -43,12 +43,22 @@ impl GameEntry for PEntry {
actions: None,
source: String::from(source),
targets: None,
id: 0,
}
}
fn all(_filter: Option<&str>) -> Vec<String> {
todo!()
}
fn categories() -> Vec<String> {
vec![
"weapon", "consumable", "spell"
]
.iter()
.map(|s| s.to_string())
.collect()
}
}
#[derive(Serialize, Deserialize, Default)]

View file

@ -1,9 +1,9 @@
use crate::game::{action::{ActionDefinition, ActionResult, GameEntry}, character_sheet::*, chat_message::{ChatMessage, RollDialogOption}};
use crate::game::{entry::{ActionDefinition, ActionResult, GameEntry}, character_sheet::*, chat_message::{ChatMessage, RollDialogOption}};
use serde::Serialize;
use tavern_macros::CharacterSheet;
pub mod entry;
use entry::{PEntry, Weapon};
use entry::{Entry, Weapon};
#[derive(Default, CharacterSheet, Serialize)]
pub struct Pathfinder2rCharacterSheet {
@ -50,14 +50,14 @@ pub struct Pathfinder2rCharacterSheet {
impl Pathfinder2rCharacterSheet {
fn set_items(&mut self, entry: EntryType) {
if let EntryType::Array(a) = entry {
let ws: Vec<PEntry> = a
let ws: Vec<Entry> = a
.iter()
.map(|e| PEntry::load(&e.as_text()))
.map(|e| Entry::load(&e.as_text()))
.flatten()
.collect();
self.weapon = [None, None];
for w in ws {
if let PEntry::Weapon(w) = w {
if let Entry::Weapon(w) = w {
if self.weapon[0].is_none() {
self.weapon[0] = Some(w);
} else if self.weapon[1].is_none() {
@ -69,7 +69,7 @@ impl Pathfinder2rCharacterSheet {
}
else {
let s = entry.as_text();
if let Some(PEntry::Weapon(w)) = PEntry::load(&s) {
if let Some(Entry::Weapon(w)) = Entry::load(&s) {
self.weapon[0] = Some(w);
self.weapon[1] = None;
}
@ -85,18 +85,19 @@ impl Pathfinder2rCharacterSheet {
)
}
}
impl Character<PEntry> for Pathfinder2rCharacterSheet {
fn use_action(&mut self, entry: &PEntry, action: &ActionResult) -> ChatMessage {
impl Character<Entry> for Pathfinder2rCharacterSheet {
fn use_action(&mut self, entry: &Entry, action: &ActionResult) -> ChatMessage {
match entry {
PEntry::Weapon(_) => ChatMessage {
Entry::Weapon(_) => ChatMessage {
text: String::from("Attack"),
roll: Some(vec![RollDialogOption { name: String::from("pierce"), dice_type: 4, dice_amount: 1, constant: 0, extra: String::new(), enabled: true }]),
roll_target: Some(10),
actions: Some(vec!["damage".to_string(), "double".to_string()]),
source: self.name.clone(),
targets: None,
id: 0,
},
PEntry::Consumable(_consumable) => if action.name == "consume" {
Entry::Consumable(_consumable) => if action.name == "consume" {
ChatMessage {
text: "Heal".to_string(),
roll: Some(vec![RollDialogOption { name: "heal".to_string(), dice_type: 6, dice_amount: 1, constant: 0, extra: String::new(), enabled: true }]),
@ -104,21 +105,22 @@ impl Character<PEntry> for Pathfinder2rCharacterSheet {
actions: Some(vec!["heal".to_string()]),
source: self.name.clone(),
targets: None,
id: 0,
}
} else { todo!() },
PEntry::Spell(_spell) => todo!(),
Entry::Spell(_spell) => todo!(),
}
}
fn actions(&self, entry: &PEntry) -> Vec<ActionDefinition> {
fn actions(&self, entry: &Entry) -> Vec<ActionDefinition> {
let v;
match entry {
PEntry::Weapon(_) =>
Entry::Weapon(_) =>
// should technically check if the item is in the user's packpack or something
// but for now just return a constant list
v = vec![ ("wield", 0), ("attack", 1), ("drop", 0), ("stow", 0) ],
PEntry::Consumable(_) => v = vec![("consume", 0), ("stow", 0), ("drop", 0)],
PEntry::Spell(_) => v = vec![("cast", 1)],
Entry::Consumable(_) => v = vec![("consume", 0), ("stow", 0), ("drop", 0)],
Entry::Spell(_) => v = vec![("cast", 1)],
};
v.iter()
.map(|s| ActionDefinition { name: s.0.to_string(), targets: s.1 })