@ -18,6 +18,11 @@ use crate::ship::items::floor::*;
use crate ::ship ::items ::inventory ::* ;
use crate ::ship ::items ::use_tool ;
#[ derive(PartialEq, Eq) ]
pub enum FloorType {
Local ,
Shared ,
}
pub enum TriggerCreateItem {
Yes ,
@ -84,7 +89,7 @@ impl ItemManager {
}
// TODO: Result
pub async fn load_character < EG : EntityGateway > ( & mut self , entity_gateway : & mut EG , character : & CharacterEntity ) -> Result < ( ) , ItemManager Error> {
pub async fn load_character < EG : EntityGateway > ( & mut self , entity_gateway : & mut EG , character : & CharacterEntity ) -> Result < ( ) , anyhow ::Error > {
let inventory = entity_gateway . get_character_inventory ( & character . id ) . await ? ;
let bank = entity_gateway . get_character_bank ( & character . id , BankName ( "" . into ( ) ) ) . await ? ;
let equipped = entity_gateway . get_character_equips ( & character . id ) . await ? ;
@ -164,19 +169,19 @@ impl ItemManager {
self . character_floor . insert ( character . id , RoomFloorItems ::new ( ) ) ;
self . room_floor . entry ( room_id ) . or_insert ( RoomFloorItems ::new ( ) ) ;
let mut inc = 0xF000 0000 ;
let mut inc = 0x0081 0000 ;
self . room_item_id_counter . entry ( room_id ) . or_insert ( Box ::new ( move | | {
inc + = 1 ;
ClientItemId ( inc )
} ) ) ;
}
pub fn get_character_inventory ( & self , character : & CharacterEntity ) -> Result < & CharacterInventory , ItemManager Error> {
pub fn get_character_inventory ( & self , character : & CharacterEntity ) -> Result < & CharacterInventory , anyhow ::Error > {
Ok ( self . character_inventory . get ( & character . id )
. ok_or ( ItemManagerError ::NoCharacter ( character . id ) ) ? )
}
pub fn get_character_bank ( & self , character : & CharacterEntity ) -> Result < & CharacterBank , ItemManager Error> {
pub fn get_character_bank ( & self , character : & CharacterEntity ) -> Result < & CharacterBank , anyhow ::Error > {
Ok ( self . character_bank
. get ( & character . id )
. ok_or ( ItemManagerError ::NoCharacter ( character . id ) ) ? )
@ -205,20 +210,20 @@ impl ItemManager {
} ) ;
}
pub fn get_floor_item_by_id ( & self , character : & CharacterEntity , item_id : ClientItemId ) -> Result < & FloorItem , ItemManager Error> {
pub fn get_floor_item_by_id ( & self , character : & CharacterEntity , item_id : ClientItemId ) -> Result < ( & FloorItem , FloorType ) , anyhow ::Error > {
let local_floor = self . character_floor . get ( & character . id ) . ok_or ( ItemManagerError ::NoCharacter ( character . id ) ) ? ;
let room = self . character_room . get ( & character . id ) . ok_or ( ItemManagerError ::NoCharacter ( character . id ) ) ? ;
let shared_floor = self . room_floor . get ( room ) . ok_or ( ItemManagerError ::NoCharacter ( character . id ) ) ? ;
local_floor . get_item_by_id ( item_id )
local_floor . get_item_by_id ( item_id ) . map ( | item | ( item , FloorType ::Local ) )
. or_else ( | | {
shared_floor . get_item_by_id ( item_id )
shared_floor . get_item_by_id ( item_id ) . map ( | item | ( item , FloorType ::Shared ) )
} )
. ok_or ( ItemManagerError ::NoSuchItemId ( item_id ) )
. ok_or ( ItemManagerError ::NoSuchItemId ( item_id ) . into ( ) )
}
pub async fn character_picks_up_item < EG : EntityGateway > ( & mut self , entity_gateway : & mut EG , character : & mut CharacterEntity , item_id : ClientItemId )
-> Result < TriggerCreateItem , ItemManager Error> {
-> Result < TriggerCreateItem , anyhow ::Error > {
let local_floor = self . character_floor . 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 room_id = self . character_room . get ( & character . id ) . ok_or ( ItemManagerError ::NoCharacter ( character . id ) ) ? ;
@ -246,7 +251,7 @@ impl ItemManager {
}
} ,
None = > {
return Err ( ItemManagerError ::CouldNotAddToInventory ( item_id ) ) ;
return Err ( ItemManagerError ::CouldNotAddToInventory ( item_id ) . into ( ) ) ;
} ,
}
TriggerCreateItem ::Yes
@ -273,20 +278,20 @@ impl ItemManager {
}
} ,
None = > {
return Err ( ItemManagerError ::CouldNotAddToInventory ( item_id ) ) ;
return Err ( ItemManagerError ::CouldNotAddToInventory ( item_id ) . into ( ) ) ;
}
}
} ,
Some ( FloorItem ::Meseta ( meseta_floor_item ) ) = > {
if character . meseta > = 999999 {
return Err ( ItemManagerError ::CouldNotAddToInventory ( item_id ) ) ;
return Err ( ItemManagerError ::CouldNotAddToInventory ( item_id ) . into ( ) ) ;
}
character . meseta = std ::cmp ::min ( character . meseta + meseta_floor_item . meseta . 0 , 999999 ) ;
entity_gateway . save_character ( & character ) . await ? ;
TriggerCreateItem ::No
} ,
None = > {
return Err ( ItemManagerError ::CouldNotAddToInventory ( item_id ) ) ;
return Err ( ItemManagerError ::CouldNotAddToInventory ( item_id ) . into ( ) ) ;
}
} ;
@ -295,7 +300,7 @@ impl ItemManager {
Ok ( trigger_create_item )
}
pub async fn enemy_drop_item_on_local_floor < EG : EntityGateway > ( & mut self , entity_gateway : & mut EG , character : & CharacterEntity , item_drop : ItemDrop ) -> Result < & FloorItem , ItemManager Error> {
pub async fn enemy_drop_item_on_local_floor < EG : EntityGateway > ( & mut self , entity_gateway : & mut EG , character : & CharacterEntity , item_drop : ItemDrop ) -> Result < & FloorItem , anyhow ::Error > {
let room_id = self . character_room . get ( & character . id ) . ok_or ( ItemManagerError ::NoCharacter ( character . id ) ) ? ;
enum ItemOrMeseta {
@ -377,7 +382,7 @@ impl ItemManager {
self . character_floor . entry ( character . id ) . or_insert ( RoomFloorItems ::new ( ) ) . add_item ( floor_item ) ;
// TODO: make these real errors
self . character_floor . get ( & character . id ) . ok_or ( ItemManagerError ::Idunnoman ) ? . get_item_by_id ( item_id ) . ok_or ( ItemManagerError ::Idunnoman )
self . character_floor . get ( & character . id ) . ok_or ( ItemManagerError ::Idunnoman ) ? . get_item_by_id ( item_id ) . ok_or ( ItemManagerError ::Idunnoman . into ( ) )
}
pub async fn player_drop_item_on_shared_floor < EG : EntityGateway > ( & mut self ,
@ -386,7 +391,7 @@ impl ItemManager {
//inventory_item: InventoryItem,
item_id : ClientItemId ,
item_drop_location : ( MapArea , f32 , f32 , f32 ) )
-> Result < ( ) , ItemManager Error> {
-> Result < ( ) , anyhow ::Error > {
let inventory = self . character_inventory . get_mut ( & character . id ) . ok_or ( ItemManagerError ::NoCharacter ( character . id ) ) ? ;
let room_id = self . character_room . get ( & character . id ) . ok_or ( ItemManagerError ::NoCharacter ( character . id ) ) ? ;
let shared_floor = self . room_floor . get_mut ( & room_id ) . ok_or ( ItemManagerError ::NoCharacter ( character . id ) ) ? ;
@ -431,11 +436,11 @@ impl ItemManager {
character : & mut CharacterEntity ,
drop_location : ItemDropLocation ,
amount : u32 )
-> Result < FloorItem , ItemManager Error> {
-> Result < FloorItem , anyhow ::Error > {
let room_id = self . character_room . get ( & character . id ) . ok_or ( ItemManagerError ::NoCharacter ( character . id ) ) ? ;
let shared_floor = self . room_floor . get_mut ( & room_id ) . ok_or ( ItemManagerError ::NoCharacter ( character . id ) ) ? ;
if character . meseta < amount {
return Err ( ItemManagerError ::CouldNotDropMeseta )
return Err ( ItemManagerError ::CouldNotDropMeseta . into ( ) )
}
character . meseta - = amount ;
entity_gateway . save_character ( & character ) . await ? ;
@ -461,7 +466,7 @@ impl ItemManager {
item_id : ClientItemId ,
drop_location : ItemDropLocation ,
amount : usize )
-> Result < & StackedFloorItem , ItemManager Error> {
-> Result < & StackedFloorItem , anyhow ::Error > {
let inventory = self . character_inventory . get_mut ( & character . id ) . ok_or ( ItemManagerError ::NoCharacter ( character . id ) ) ? ;
let room_id = self . character_room . get ( & character . id ) . ok_or ( ItemManagerError ::NoCharacter ( character . id ) ) ? ;
let shared_floor = self . room_floor . get_mut ( & room_id ) . ok_or ( ItemManagerError ::NoCharacter ( character . id ) ) ? ;
@ -493,7 +498,7 @@ impl ItemManager {
character : & mut CharacterEntity ,
item_id : ClientItemId ,
amount : usize )
-> Result < ConsumedItem , ItemManager Error> {
-> Result < ConsumedItem , anyhow ::Error > {
let inventory = self . character_inventory . get_mut ( & character . id ) . ok_or ( ItemManagerError ::NoCharacter ( character . id ) ) ? ;
let used_item = inventory . get_item_handle_by_id ( item_id ) . ok_or ( ItemManagerError ::NoSuchItemId ( item_id ) ) ? ;
let consumed_item = used_item . consume ( amount ) ? ;
@ -518,7 +523,7 @@ impl ItemManager {
character : & CharacterEntity ,
item_id : ClientItemId ,
amount : usize )
-> Result < ( ) , ItemManager Error> {
-> Result < ( ) , anyhow ::Error > {
let inventory = self . character_inventory . get_mut ( & character . id ) . ok_or ( ItemManagerError ::NoCharacter ( character . id ) ) ? ;
let bank = self . character_bank
. get_mut ( & character . id )
@ -556,7 +561,7 @@ impl ItemManager {
character : & CharacterEntity ,
item_id : ClientItemId ,
amount : usize )
-> Result < & InventoryItem , ItemManager Error> {
-> Result < & InventoryItem , anyhow ::Error > {
let inventory = self . character_inventory . get_mut ( & character . id ) . ok_or ( ItemManagerError ::NoCharacter ( character . id ) ) ? ;
let bank = self . character_bank
@ -588,7 +593,7 @@ impl ItemManager {
entity_gateway . set_character_inventory ( & character . id , & inventory . as_inventory_entity ( & character . id ) ) . await ? ;
entity_gateway . set_character_bank ( & character . id , & bank . as_bank_entity ( & character . id , & BankName ( "" . into ( ) ) ) , BankName ( "" . into ( ) ) ) . await ? ;
inventory . slot ( inventory_item_slot ) . ok_or ( ItemManagerError ::Idunnoman )
inventory . slot ( inventory_item_slot ) . ok_or ( ItemManagerError ::Idunnoman . into ( ) )
}
pub async fn player_feeds_mag_item < EG : EntityGateway > ( & mut self ,
@ -596,7 +601,7 @@ impl ItemManager {
character : & CharacterEntity ,
mag_id : ClientItemId ,
tool_id : ClientItemId )
-> Result < ( ) , ItemManager Error> {
-> Result < ( ) , anyhow ::Error > {
let inventory = self . character_inventory . get_mut ( & character . id ) . ok_or ( ItemManagerError ::NoCharacter ( character . id ) ) ? ;
let consumed_tool = {
let item_to_feed = inventory . get_item_handle_by_id ( tool_id ) . ok_or ( ItemManagerError ::NoSuchItemId ( tool_id ) ) ? ;
@ -614,7 +619,7 @@ impl ItemManager {
let consumed_tool_type = match & consumed_tool {
ConsumedItem ::Stacked ( stacked_consumed_item ) = > stacked_consumed_item . tool . tool ,
_ = > return Err ( ItemManagerError ::WrongItemType ( tool_id ) )
_ = > return Err ( ItemManagerError ::WrongItemType ( tool_id ) . into ( ) )
} ;
mag . feed ( consumed_tool_type ) ;
@ -632,7 +637,7 @@ impl ItemManager {
pub async fn use_item < EG : EntityGateway > ( & mut self ,
used_item : ConsumedItem ,
entity_gateway : & mut EG ,
character : & mut CharacterEntity ) -> Result < ( ) , ItemManager Error> {
character : & mut CharacterEntity ) -> Result < ( ) , anyhow ::Error > {
let inventory = self . character_inventory . get_mut ( & character . id ) . ok_or ( ItemManagerError ::NoCharacter ( character . id ) ) ? ;
match & used_item . item ( ) {
ItemDetail ::Weapon ( _w ) = > {
@ -749,7 +754,7 @@ impl ItemManager {
shop_item : & ( dyn ShopItem + Send + Sync ) ,
item_id : ClientItemId ,
amount : usize )
-> Result < & InventoryItem , ItemManager Error> {
-> Result < & InventoryItem , anyhow ::Error > {
let inventory = self . character_inventory . get_mut ( & character . id ) . ok_or ( ItemManagerError ::NoCharacter ( character . id ) ) ? ;
let item_detail = shop_item . as_item ( ) ;
@ -848,7 +853,7 @@ impl ItemManager {
character : & CharacterEntity ,
item_id : ClientItemId ,
equip_slot : u8 )
-> Result < ( ) , ItemManager Error> {
-> Result < ( ) , anyhow ::Error > {
let inventory = self . character_inventory . get_mut ( & character . id ) . ok_or ( ItemManagerError ::NoCharacter ( character . id ) ) ? ;
inventory . equip ( & item_id , equip_slot ) ;
entity_gateway . set_character_equips ( & character . id , & inventory . as_equipped_entity ( ) ) . await ? ;
@ -859,7 +864,7 @@ impl ItemManager {
entity_gateway : & mut EG ,
character : & CharacterEntity ,
item_id : ClientItemId )
-> Result < ( ) , ItemManager Error> {
-> Result < ( ) , anyhow ::Error > {
let inventory = self . character_inventory . get_mut ( & character . id ) . ok_or ( ItemManagerError ::NoCharacter ( character . id ) ) ? ;
inventory . unequip ( & item_id ) ;
entity_gateway . set_character_equips ( & character . id , & inventory . as_equipped_entity ( ) ) . await ? ;
@ -870,7 +875,7 @@ impl ItemManager {
entity_gateway : & mut EG ,
character : & CharacterEntity ,
item_ids : [ u32 ; 30 ] )
-> Result < ( ) , ItemManager Error> {
-> Result < ( ) , anyhow ::Error > {
let inventory = self . character_inventory . get_mut ( & character . id ) . ok_or ( ItemManagerError ::NoCharacter ( character . id ) ) ? ;
let sorted_inventory_items : Vec < InventoryItem > = item_ids . iter ( )
. filter ( | & client_item_id | * client_item_id < 0xFFFFFFFF )