Browse Source

add impl From<> for shops. get_sell_price returns Result now.

pull/47/head
andy 3 years ago
parent
commit
61e7e8b39f
  1. 50
      src/ship/items/inventory.rs
  2. 7
      src/ship/items/manager.rs
  3. 34
      src/ship/shops/armor.rs
  4. 22
      src/ship/shops/tool.rs
  5. 11
      src/ship/shops/weapon.rs

50
src/ship/items/inventory.rs

@ -6,7 +6,7 @@ use crate::entity::item::{ItemEntityId, ItemDetail, ItemEntity, ItemType, ItemLo
use crate::entity::item::tool::{Tool, ToolType}; use crate::entity::item::tool::{Tool, ToolType};
use crate::entity::item::mag::Mag; use crate::entity::item::mag::Mag;
use crate::entity::item::weapon::Weapon; use crate::entity::item::weapon::Weapon;
use crate::ship::items::{ClientItemId, BankItem, BankItemHandle};
use crate::ship::items::{ClientItemId, BankItem, BankItemHandle, ItemManagerError};
use crate::ship::items::floor::{IndividualFloorItem, StackedFloorItem}; use crate::ship::items::floor::{IndividualFloorItem, StackedFloorItem};
use crate::ship::shops::{ShopItem, ArmorShopItem, ToolShopItem, WeaponShopItem}; use crate::ship::shops::{ShopItem, ArmorShopItem, ToolShopItem, WeaponShopItem};
@ -222,59 +222,75 @@ impl InventoryItem {
} }
} }
pub fn get_sell_price(&self) -> Option<u32> {
// pub fn get_sell_price(&self) -> Option<u32> {
pub fn get_sell_price(&self) -> Result<u32, ItemManagerError> {
match self { match self {
InventoryItem::Individual(individual_item) => { InventoryItem::Individual(individual_item) => {
match &individual_item.item { match &individual_item.item {
// TODO: can wrapped items be sold? // TODO: can wrapped items be sold?
ItemDetail::Weapon(w) => { ItemDetail::Weapon(w) => {
if !w.tekked { if !w.tekked {
return Some(1u32)
// return Some(1u32)
return Ok(1u32)
} }
if w.is_rare_item() { if w.is_rare_item() {
return Some(10u32)
// return Some(10u32)
return Ok(10u32)
} }
// other item factors? // other item factors?
Some((WeaponShopItem::weapon_from_item(w).price() / 8) as u32)
// Some((WeaponShopItem::from(w).price() / 8) as u32)
Ok((WeaponShopItem::from(w).price() / 8) as u32)
}, },
ItemDetail::Armor(a) => { ItemDetail::Armor(a) => {
if a.is_rare_item() { if a.is_rare_item() {
return Some(10u32)
// return Some(10u32)
return Ok(10u32)
} }
Some((ArmorShopItem::armor_from_item(a).price() / 8) as u32)
// Some((ArmorShopItem::from(a).price() / 8) as u32)
return Ok((ArmorShopItem::from(a).price() / 8) as u32)
}, },
ItemDetail::Shield(s) => { ItemDetail::Shield(s) => {
if s.is_rare_item() { if s.is_rare_item() {
return Some(10u32)
// return Some(10u32)
return Ok(10u32)
} }
Some((ArmorShopItem::shield_from_item(s).price() / 8) as u32)
// Some((ArmorShopItem::from(s).price() / 8) as u32)
return Ok((ArmorShopItem::from(s).price() / 8) as u32)
}, },
ItemDetail::Unit(u) => { ItemDetail::Unit(u) => {
if u.is_rare_item() { if u.is_rare_item() {
return Some(10u32)
// return Some(10u32)
return Ok(10u32)
} }
Some((ArmorShopItem::unit_from_item(u).price() / 8) as u32)
// Some((ArmorShopItem::from(u).price() / 8) as u32)
return Ok((ArmorShopItem::from(u).price() / 8) as u32)
}, },
ItemDetail::Tool(t) => { ItemDetail::Tool(t) => {
if !matches!(t.tool, ToolType::PhotonDrop | ToolType::PhotonSphere | ToolType::PhotonCrystal) && t.is_rare_item() { if !matches!(t.tool, ToolType::PhotonDrop | ToolType::PhotonSphere | ToolType::PhotonCrystal) && t.is_rare_item() {
return Some(10u32)
// return Some(10u32)
return Ok(10u32)
} }
Some((ToolShopItem::tool_from_item(t).price() / 8) as u32)
// Some((ToolShopItem::from(t).price() / 8) as u32)
return Ok((ToolShopItem::from(t).price() / 8) as u32)
}, },
ItemDetail::TechniqueDisk(d) => { // TODO: are all techs the same? ItemDetail::TechniqueDisk(d) => { // TODO: are all techs the same?
Some((ToolShopItem::tech_from_item(d).price() / 8) as u32)
// Some((ToolShopItem::from(d).price() / 8) as u32)
return Ok((ToolShopItem::from(d).price() / 8) as u32)
}, },
ItemDetail::Mag(_m) => { //TODO: error. mags are not sellable ItemDetail::Mag(_m) => { //TODO: error. mags are not sellable
None
// None
return Err(ItemManagerError::ItemNotSellable(self.clone()))
}, },
ItemDetail::ESWeapon(_e) => { ItemDetail::ESWeapon(_e) => {
Some(10u32) // TODO: check price
// Some(10u32) // TODO: check price
return Ok(10u32)
}, },
} }
}, },
// the number of stacked items sold is handled by the caller. this is just the price of 1 // the number of stacked items sold is handled by the caller. this is just the price of 1
InventoryItem::Stacked(stacked_item) => { InventoryItem::Stacked(stacked_item) => {
Some((ToolShopItem::tool_from_item(&stacked_item.tool).price() / 8) as u32)
// Some((ToolShopItem::from(&stacked_item.tool).price() / 8) as u32)
return Ok((ToolShopItem::from(&stacked_item.tool).price() / 8) as u32)
}, },
} }
} }

7
src/ship/items/manager.rs

@ -56,7 +56,7 @@ pub enum ItemManagerError {
NoArmorEquipped, NoArmorEquipped,
GatewayError(#[from] crate::entity::gateway::GatewayError), GatewayError(#[from] crate::entity::gateway::GatewayError),
StackedItemError(Vec<ItemEntity>), StackedItemError(Vec<ItemEntity>),
ItemNotSellable, // TODO: capture what item was attempted to be sold
ItemNotSellable(InventoryItem), // TODO: capture what item was attempted to be sold
WalletFull, WalletFull,
} }
@ -856,7 +856,8 @@ impl ItemManager {
let inventory = self.character_inventory.get_mut(&character.id).ok_or(ItemManagerError::NoCharacter(character.id))?; let inventory = self.character_inventory.get_mut(&character.id).ok_or(ItemManagerError::NoCharacter(character.id))?;
let sold_item_handle = inventory.get_item_handle_by_id(item_id).ok_or(ItemManagerError::NoSuchItemId(item_id))?; let sold_item_handle = inventory.get_item_handle_by_id(item_id).ok_or(ItemManagerError::NoSuchItemId(item_id))?;
if let Some(item_sold) = sold_item_handle.item() { if let Some(item_sold) = sold_item_handle.item() {
if let Some(unit_price) = item_sold.get_sell_price() { // -> Option<u32> u32 = meseta, or None if error
// if let Some(unit_price) = item_sold.get_sell_price() { // -> Option<u32> u32 = meseta, or None if error
let unit_price = item_sold.get_sell_price()?; { // -> Result<u32, ItemManagerError> // ItemMangerError::InvalidSale
let total_sale = unit_price * amount as u32; let total_sale = unit_price * amount as u32;
if character.meseta + total_sale <= 999999 { if character.meseta + total_sale <= 999999 {
character.meseta += total_sale; character.meseta += total_sale;
@ -880,8 +881,6 @@ impl ItemManager {
} else { } else {
return Err(ItemManagerError::WalletFull.into()); return Err(ItemManagerError::WalletFull.into());
} }
} else {
return Err(ItemManagerError::ItemNotSellable.into());
} }
} else { } else {
return Err(ItemManagerError::ItemIdNotInInventory(item_id).into()) return Err(ItemManagerError::ItemIdNotInInventory(item_id).into())

34
src/ship/shops/armor.rs

@ -14,7 +14,7 @@ use crate::ship::item_stats::{ARMOR_STATS, SHIELD_STATS, UNIT_STATS};
#[derive(Debug)] #[derive(Debug)]
pub enum ArmorShopItem { pub enum ArmorShopItem {
Frame(ArmorType, usize),
Frame(ArmorType, usize), // slots
Barrier(ShieldType), Barrier(ShieldType),
Unit(UnitType), Unit(UnitType),
} }
@ -90,17 +90,35 @@ impl ShopItem for ArmorShopItem {
} }
} }
impl ArmorShopItem {
pub fn armor_from_item(a: &Armor) -> ArmorShopItem {
ArmorShopItem::Frame(a.armor, a.slots as usize)
// impl ArmorShopItem {
// pub fn armor_from_item(a: &Armor) -> ArmorShopItem {
// ArmorShopItem::Frame(a.armor, a.slots as usize)
// }
// pub fn shield_from_item(s: &Shield) -> ArmorShopItem {
// ArmorShopItem::Barrier(s.shield)
// }
// pub fn unit_from_item(u: &Unit) -> ArmorShopItem {
// ArmorShopItem::Unit(u.unit)
// }
// }
impl From<&Armor> for ArmorShopItem {
fn from(armor: &Armor) -> ArmorShopItem {
ArmorShopItem::Frame(armor.armor, armor.slots as usize)
} }
}
pub fn shield_from_item(s: &Shield) -> ArmorShopItem {
ArmorShopItem::Barrier(s.shield)
impl From<&Shield> for ArmorShopItem {
fn from(shield: &Shield) -> ArmorShopItem {
ArmorShopItem::Barrier(shield.shield)
} }
}
pub fn unit_from_item(u: &Unit) -> ArmorShopItem {
ArmorShopItem::Unit(u.unit)
impl From<&Unit> for ArmorShopItem {
fn from(unit: &Unit) -> ArmorShopItem {
ArmorShopItem::Unit(unit.unit)
} }
} }

22
src/ship/shops/tool.rs

@ -96,13 +96,25 @@ impl ShopItem for ToolShopItem {
} }
} }
impl ToolShopItem {
pub fn tool_from_item(t: &Tool) -> ToolShopItem {
ToolShopItem::Tool(t.tool)
// impl ToolShopItem {
// pub fn tool_from_item(t: &Tool) -> ToolShopItem {
// ToolShopItem::Tool(t.tool)
// }
// pub fn tech_from_item(d: &TechniqueDisk) -> ToolShopItem {
// ToolShopItem::Tech(*d)
// }
// }
impl From<&Tool> for ToolShopItem {
fn from(tool: &Tool) -> ToolShopItem {
ToolShopItem::Tool(tool.tool)
} }
}
pub fn tech_from_item(d: &TechniqueDisk) -> ToolShopItem {
ToolShopItem::Tech(*d)
impl From<&TechniqueDisk> for ToolShopItem {
fn from(techdisk: &TechniqueDisk) -> ToolShopItem {
ToolShopItem::Tech(*techdisk)
} }
} }

11
src/ship/shops/weapon.rs

@ -53,6 +53,17 @@ impl PartialOrd for WeaponShopItem {
} }
} }
impl From<&Weapon> for WeaponShopItem {
fn from(weapon: &Weapon) -> WeaponShopItem {
WeaponShopItem {
weapon: weapon.weapon,
special: weapon.special,
grind: weapon.grind as usize,
attributes: weapon.attrs,
}
}
}
fn special_stars(special: &WeaponSpecial) -> usize { fn special_stars(special: &WeaponSpecial) -> usize {
match special { match special {

Loading…
Cancel
Save