You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

219 lines
11 KiB

use thiserror::Error;
use crate::entity::gateway::{EntityGateway, GatewayError};
use crate::entity::character::CharacterEntity;
use crate::entity::item::mag::MagCell;
use crate::entity::item::tool::ToolType;
use crate::entity::item::ItemDetail;
use crate::ship::items::state::{ItemStateProxy, InventoryState, InventoryItem, InventoryItemDetail};
#[derive(Error, Debug)]
pub enum ApplyItemError {
#[error("no character")]
NoCharacter,
#[error("item not equipped")]
ItemNotEquipped,
#[error("invalid item")]
InvalidItem,
#[error("gateway error {0}")]
GatewayError(#[from] GatewayError),
}
// TODO: make all these functions not-pub
pub async fn power_material<EG: EntityGateway + ?Sized>(entity_gateway: &mut EG, character: &mut CharacterEntity) -> Result<(), ApplyItemError> {
character.materials.power += 1;
entity_gateway.save_character(character).await?;
Ok(())
}
pub async fn mind_material<EG: EntityGateway + ?Sized>(entity_gateway: &mut EG, character: &mut CharacterEntity) -> Result<(), ApplyItemError> {
character.materials.mind += 1;
entity_gateway.save_character(character).await.unwrap();
Ok(())
}
pub async fn evade_material<EG: EntityGateway + ?Sized>(entity_gateway: &mut EG, character: &mut CharacterEntity) -> Result<(), ApplyItemError> {
character.materials.evade += 1;
entity_gateway.save_character(character).await.unwrap();
Ok(())
}
pub async fn def_material<EG: EntityGateway + ?Sized>(entity_gateway: &mut EG, character: &mut CharacterEntity) -> Result<(), ApplyItemError> {
character.materials.def += 1;
entity_gateway.save_character(character).await.unwrap();
Ok(())
}
pub async fn luck_material<EG: EntityGateway + ?Sized>(entity_gateway: &mut EG, character: &mut CharacterEntity) -> Result<(), ApplyItemError> {
character.materials.luck += 1;
entity_gateway.save_character(character).await.unwrap();
Ok(())
}
pub async fn hp_material<EG: EntityGateway + ?Sized>(entity_gateway: &mut EG, character: &mut CharacterEntity) -> Result<(), ApplyItemError> {
character.materials.hp += 1;
entity_gateway.save_character(character).await.unwrap();
Ok(())
}
pub async fn tp_material<EG: EntityGateway + ?Sized>(entity_gateway: &mut EG, character: &mut CharacterEntity) -> Result<(), ApplyItemError> {
character.materials.tp += 1;
entity_gateway.save_character(character).await.unwrap();
Ok(())
}
/*
async fn mag_cell<EG: EntityGateway>(entity_gateway: &mut EG, used_cell: &ConsumedItem, inventory: &mut CharacterInventory, mag_cell_type: MagCell) -> Result<(), ApplyItemError> {
let mut mag_handle = inventory.get_equipped_mag_handle().ok_or(ApplyItemError::ItemNotEquipped)?;
let mag_item = mag_handle.item_mut()
.ok_or(ApplyItemError::InvalidItem)?;
let actual_mag = mag_item
.individual_mut()
.ok_or(ApplyItemError::InvalidItem)?
.mag_mut()
.ok_or(ApplyItemError::InvalidItem)?;
actual_mag.apply_mag_cell(mag_cell_type);
for mag_entity_id in mag_item.entity_ids() {
for cell_entity_id in used_cell.entity_ids() {
entity_gateway.use_mag_cell(&mag_entity_id, &cell_entity_id).await.unwrap();
}
}
Ok(())
}
pub async fn cell_of_mag_502<EG: EntityGateway>(entity_gateway: &mut EG, used_cell: &ConsumedItem, inventory: &mut CharacterInventory) -> Result<(), ApplyItemError> {
mag_cell(entity_gateway, used_cell, inventory, MagCell::CellOfMag502).await
}
pub async fn cell_of_mag_213<EG: EntityGateway>(entity_gateway: &mut EG, used_cell: &ConsumedItem, inventory: &mut CharacterInventory) -> Result<(), ApplyItemError> {
mag_cell(entity_gateway, used_cell, inventory, MagCell::CellOfMag213).await
}
pub async fn parts_of_robochao<EG: EntityGateway>(entity_gateway: &mut EG, used_cell: &ConsumedItem, inventory: &mut CharacterInventory) -> Result<(), ApplyItemError> {
mag_cell(entity_gateway, used_cell, inventory, MagCell::PartsOfRobochao).await
}
pub async fn heart_of_opaopa<EG: EntityGateway>(entity_gateway: &mut EG, used_cell: &ConsumedItem, inventory: &mut CharacterInventory) -> Result<(), ApplyItemError> {
mag_cell(entity_gateway, used_cell, inventory, MagCell::HeartOfOpaOpa).await
}
pub async fn heart_of_pian<EG: EntityGateway>(entity_gateway: &mut EG, used_cell: &ConsumedItem, inventory: &mut CharacterInventory) -> Result<(), ApplyItemError> {
mag_cell(entity_gateway, used_cell, inventory, MagCell::HeartOfPian).await
}
pub async fn heart_of_chao<EG: EntityGateway>(entity_gateway: &mut EG, used_cell: &ConsumedItem, inventory: &mut CharacterInventory) -> Result<(), ApplyItemError> {
mag_cell(entity_gateway, used_cell, inventory, MagCell::HeartOfChao).await
}
pub async fn heart_of_angel<EG: EntityGateway>(entity_gateway: &mut EG, used_cell: &ConsumedItem, inventory: &mut CharacterInventory) -> Result<(), ApplyItemError> {
mag_cell(entity_gateway, used_cell, inventory, MagCell::HeartOfAngel).await
}
pub async fn kit_of_hamburger<EG: EntityGateway>(entity_gateway: &mut EG, used_cell: &ConsumedItem, inventory: &mut CharacterInventory) -> Result<(), ApplyItemError> {
mag_cell(entity_gateway, used_cell, inventory, MagCell::KitOfHamburger).await
}
pub async fn panthers_spirit<EG: EntityGateway>(entity_gateway: &mut EG, used_cell: &ConsumedItem, inventory: &mut CharacterInventory) -> Result<(), ApplyItemError> {
mag_cell(entity_gateway, used_cell, inventory, MagCell::PanthersSpirit).await
}
pub async fn kit_of_mark3<EG: EntityGateway>(entity_gateway: &mut EG, used_cell: &ConsumedItem, inventory: &mut CharacterInventory) -> Result<(), ApplyItemError> {
mag_cell(entity_gateway, used_cell, inventory, MagCell::KitOfMark3).await
}
pub async fn kit_of_master_system<EG: EntityGateway>(entity_gateway: &mut EG, used_cell: &ConsumedItem, inventory: &mut CharacterInventory) -> Result<(), ApplyItemError> {
mag_cell(entity_gateway, used_cell, inventory, MagCell::KitOfMasterSystem).await
}
pub async fn kit_of_genesis<EG: EntityGateway>(entity_gateway: &mut EG, used_cell: &ConsumedItem, inventory: &mut CharacterInventory) -> Result<(), ApplyItemError> {
mag_cell(entity_gateway, used_cell, inventory, MagCell::KitOfGenesis).await
}
pub async fn kit_of_sega_saturn<EG: EntityGateway>(entity_gateway: &mut EG, used_cell: &ConsumedItem, inventory: &mut CharacterInventory) -> Result<(), ApplyItemError> {
mag_cell(entity_gateway, used_cell, inventory, MagCell::KitOfSegaSaturn).await
}
pub async fn kit_of_dreamcast<EG: EntityGateway>(entity_gateway: &mut EG, used_cell: &ConsumedItem, inventory: &mut CharacterInventory) -> Result<(), ApplyItemError> {
mag_cell(entity_gateway, used_cell, inventory, MagCell::KitOfDreamcast).await
}
pub async fn tablet<EG: EntityGateway>(entity_gateway: &mut EG, used_cell: &ConsumedItem, inventory: &mut CharacterInventory) -> Result<(), ApplyItemError> {
mag_cell(entity_gateway, used_cell, inventory, MagCell::Tablet).await
}
pub async fn dragon_scale<EG: EntityGateway>(entity_gateway: &mut EG, used_cell: &ConsumedItem, inventory: &mut CharacterInventory) -> Result<(), ApplyItemError> {
mag_cell(entity_gateway, used_cell, inventory, MagCell::DragonScale).await
}
pub async fn heaven_striker_coat<EG: EntityGateway>(entity_gateway: &mut EG, used_cell: &ConsumedItem, inventory: &mut CharacterInventory) -> Result<(), ApplyItemError> {
mag_cell(entity_gateway, used_cell, inventory, MagCell::HeavenStrikerCoat).await
}
pub async fn pioneer_parts<EG: EntityGateway>(entity_gateway: &mut EG, used_cell: &ConsumedItem, inventory: &mut CharacterInventory) -> Result<(), ApplyItemError> {
mag_cell(entity_gateway, used_cell, inventory, MagCell::PioneerParts).await
}
pub async fn amities_memo<EG: EntityGateway>(entity_gateway: &mut EG, used_cell: &ConsumedItem, inventory: &mut CharacterInventory) -> Result<(), ApplyItemError> {
mag_cell(entity_gateway, used_cell, inventory, MagCell::AmitiesMemo).await
}
pub async fn heart_of_morolian<EG: EntityGateway>(entity_gateway: &mut EG, used_cell: &ConsumedItem, inventory: &mut CharacterInventory) -> Result<(), ApplyItemError> {
mag_cell(entity_gateway, used_cell, inventory, MagCell::HeartOfMorolian).await
}
pub async fn rappys_beak<EG: EntityGateway>(entity_gateway: &mut EG, used_cell: &ConsumedItem, inventory: &mut CharacterInventory) -> Result<(), ApplyItemError> {
mag_cell(entity_gateway, used_cell, inventory, MagCell::RappysBeak).await
}
pub async fn yahoos_engine<EG: EntityGateway>(entity_gateway: &mut EG, used_cell: &ConsumedItem, inventory: &mut CharacterInventory) -> Result<(), ApplyItemError> {
mag_cell(entity_gateway, used_cell, inventory, MagCell::YahoosEngine).await
}
pub async fn d_photon_core<EG: EntityGateway>(entity_gateway: &mut EG, used_cell: &ConsumedItem, inventory: &mut CharacterInventory) -> Result<(), ApplyItemError> {
mag_cell(entity_gateway, used_cell, inventory, MagCell::DPhotonCore).await
}
pub async fn liberta_kit<EG: EntityGateway>(entity_gateway: &mut EG, used_cell: &ConsumedItem, inventory: &mut CharacterInventory) -> Result<(), ApplyItemError> {
mag_cell(entity_gateway, used_cell, inventory, MagCell::LibertaKit).await
}
*/
async fn apply_tool<'a, EG: EntityGateway + ?Sized>(item_state: &mut ItemStateProxy<'a>, entity_gateway: &mut EG, character: &mut CharacterEntity, tool: ToolType) -> Result<(), ApplyItemError> {
match tool {
ToolType::PowerMaterial => power_material(entity_gateway, character).await,
ToolType::MindMaterial => mind_material(entity_gateway, character).await,
ToolType::EvadeMaterial => evade_material(entity_gateway, character).await,
ToolType::DefMaterial => def_material(entity_gateway, character).await,
ToolType::LuckMaterial => luck_material(entity_gateway, character).await,
ToolType::HpMaterial => hp_material(entity_gateway, character).await,
ToolType::TpMaterial => tp_material(entity_gateway, character).await,
ToolType::Monomate => Ok(()),
ToolType::Dimate => Ok(()),
ToolType::Trimate => Ok(()),
ToolType::Monofluid => Ok(()),
ToolType::Difluid => Ok(()),
ToolType::Trifluid => Ok(()),
ToolType::HuntersReport => Ok(()),
// TODO: rest of these
_ => Err(ApplyItemError::InvalidItem)
}
}
pub async fn apply_item<'a, EG: EntityGateway + ?Sized>(item_state: &mut ItemStateProxy<'a>, entity_gateway: &mut EG, character: &mut CharacterEntity, item: InventoryItem) -> Result<(), ApplyItemError> {
let item_detail = match item.item {
InventoryItemDetail::Individual(individual_item) => {
individual_item.item
},
InventoryItemDetail::Stacked(stacked_item) => {
ItemDetail::Tool(stacked_item.tool)
},
};
match item_detail {
ItemDetail::Tool(tool) => apply_tool(item_state, entity_gateway, character, tool.tool).await,
_ => Err(ApplyItemError::InvalidItem)
}
}