diff options
| author | troido <troido@protonmail.com> | 2020-04-06 10:37:53 +0200 |
|---|---|---|
| committer | troido <troido@protonmail.com> | 2020-04-06 10:37:53 +0200 |
| commit | e8d3e3c4f69fc5bab2b32b16b7c8c2c4a8a89a4b (patch) | |
| tree | 67986bb0c0af1abe61dc9846d1a6c8c9b8e2c4b0 /src/playerstate.rs | |
| parent | ff457701ff56072914acb8a7160cd02c2a07095a (diff) | |
made parseerrors their own thing
Diffstat (limited to 'src/playerstate.rs')
| -rw-r--r-- | src/playerstate.rs | 44 |
1 files changed, 23 insertions, 21 deletions
diff --git a/src/playerstate.rs b/src/playerstate.rs index 9dec879..acc49ff 100644 --- a/src/playerstate.rs +++ b/src/playerstate.rs @@ -28,7 +28,9 @@ use crate::{ Sprite, Encyclopedia, Pos, - hashmap + hashmap, + PResult, + perr }; #[derive(Debug, Clone)] @@ -99,44 +101,44 @@ impl PlayerState { }) } - pub fn from_json(val: &Value) -> Result<Self> { - let inventory = val.get("inventory").ok_or(aerr!("player json does not have inventory"))?; + pub fn from_json(val: &Value) -> PResult<Self> { + let inventory = val.get("inventory").ok_or(perr!("player json does not have inventory"))?; let items = inventory .get("items") - .ok_or(aerr!("inventory does not have items"))? + .ok_or(perr!("inventory does not have items"))? .as_array() - .ok_or(aerr!("inventory items not an array"))? + .ok_or(perr!("inventory items not an array"))? .into_iter() .map(|entry| { let itemid = ItemId( entry .get(0) - .ok_or(aerr!("item does not have name"))? + .ok_or(perr!("item does not have name"))? .as_str() - .ok_or(aerr!("item name not a string"))? + .ok_or(perr!("item name not a string"))? .to_string() ); let is_equipped = entry .get(1) - .ok_or(aerr!("item does not have equipped flag"))? + .ok_or(perr!("item does not have equipped flag"))? .as_bool() - .ok_or(aerr!("item is_equipped not a bool"))?; + .ok_or(perr!("item is_equipped not a bool"))?; Ok((itemid, is_equipped)) }) - .collect::<Result<Vec<(ItemId, bool)>>>()?; + .collect::<PResult<Vec<(ItemId, bool)>>>()?; Ok(Self { - id: PlayerId{name: val.get("name").ok_or(aerr!("player json does not have name"))?.as_str().ok_or(aerr!("player name not a string"))?.to_string()}, - room: match val.get("roomname").ok_or(aerr!("player json does not have room name"))? { + id: PlayerId{name: val.get("name").ok_or(perr!("player json does not have name"))?.as_str().ok_or(perr!("player name not a string"))?.to_string()}, + room: match val.get("roomname").ok_or(perr!("player json does not have room name"))? { Value::String(name) => Some(RoomId::from_str(name)), _ => None }, pos: RoomPos::Unknown, inventory: items, - health: val.get("health").ok_or(aerr!("player json does not have health"))?.as_i64().ok_or(aerr!("player health not a number"))?, - inventory_capacity: inventory.get("capacity").ok_or(aerr!("inventory does no have capacity"))?.as_i64().ok_or(aerr!("inventory capacity not a number"))? as usize, - maximum_health: val.get("maxhealth").ok_or(aerr!("player json does not have maxhealth"))?.as_i64().ok_or(aerr!("maxhealth not a number"))?, + health: val.get("health").ok_or(perr!("player json does not have health"))?.as_i64().ok_or(perr!("player health not a number"))?, + inventory_capacity: inventory.get("capacity").ok_or(perr!("inventory does not have capacity"))?.as_i64().ok_or(perr!("inventory capacity not a number"))? as usize, + maximum_health: val.get("maxhealth").ok_or(perr!("player json does not have maxhealth"))?.as_i64().ok_or(perr!("maxhealth not a number"))?, equipment: HashMap::new() }) } @@ -147,15 +149,15 @@ impl PlayerState { self.health = self.maximum_health / 2; } - pub fn construct(&self, encyclopedia: &Encyclopedia) -> PreEntity { - vec![ + pub fn construct(&self, encyclopedia: &Encyclopedia) -> Result<PreEntity> { + Ok(vec![ ComponentWrapper::Visible(Visible{sprite: Sprite{name: "player".to_string()}, height: 1.75, name: self.id.name.clone()}), ComponentWrapper::Player(Player::new(self.id.clone())), ComponentWrapper::Inventory(Inventory{ items: self.inventory.iter().map( |(itemid, is_equipped)| { - let item = encyclopedia.get_item(itemid).unwrap(); - InventoryEntry{itemid: itemid.clone(), item, is_equipped: *is_equipped} - }).collect(), + let item = encyclopedia.get_item(itemid).ok_or(aerr!("failed to load item '{:?} in inventory of player {:?}", itemid, self))?; + Ok(InventoryEntry{itemid: itemid.clone(), item, is_equipped: *is_equipped}) + }).collect::<Result<Vec<InventoryEntry>>>()?, capacity: self.inventory_capacity }), ComponentWrapper::Health(Health{health: self.health, maxhealth: self.maximum_health}), @@ -166,6 +168,6 @@ impl PlayerState { ComponentWrapper::Faction(Faction::Good), ComponentWrapper::Equipment(Equipment{slots: vec!(Slot::Hand, Slot::Body)}), ComponentWrapper::Ear(Ear::default()) - ] + ]) } } |
