diff --git a/src/entity/gateway/entitygateway.rs b/src/entity/gateway/entitygateway.rs index 7c58842..a472973 100644 --- a/src/entity/gateway/entitygateway.rs +++ b/src/entity/gateway/entitygateway.rs @@ -158,7 +158,7 @@ pub trait EntityGateway: Send + Sync { #[async_trait::async_trait] pub trait EntityGatewayTransaction: Send + Sync { - fn gateway<'a>(&'a mut self) -> &'a mut dyn EntityGateway { + fn gateway(&mut self) -> &mut dyn EntityGateway { unimplemented!() } diff --git a/src/entity/gateway/inmemory.rs b/src/entity/gateway/inmemory.rs index 653f1bb..ca527e8 100644 --- a/src/entity/gateway/inmemory.rs +++ b/src/entity/gateway/inmemory.rs @@ -17,7 +17,7 @@ pub struct InMemoryGatewayTransaction<'a> { #[async_trait::async_trait] impl<'a> EntityGatewayTransaction for InMemoryGatewayTransaction<'a> { - fn gateway<'b>(&'b mut self) -> &'b mut dyn EntityGateway { + fn gateway(&mut self) -> &mut dyn EntityGateway { &mut self.working_gateway } @@ -220,7 +220,7 @@ impl EntityGateway for InMemoryGateway { original_gateway: self, }); - let (mut transaction, result) = func(transaction).await?; + let (transaction, result) = func(transaction).await?; transaction.commit().await?; Ok(result) diff --git a/src/entity/gateway/postgres/postgres.rs b/src/entity/gateway/postgres/postgres.rs index 34f4ab3..b6f9cc9 100644 --- a/src/entity/gateway/postgres/postgres.rs +++ b/src/entity/gateway/postgres/postgres.rs @@ -25,7 +25,7 @@ pub struct PostgresTransaction<'t> { #[async_trait::async_trait] impl<'t> EntityGatewayTransaction for PostgresTransaction<'t> { - fn gateway<'b>(&'b mut self) -> &'b mut dyn EntityGateway { + fn gateway(&mut self) -> &mut dyn EntityGateway { self } @@ -573,10 +573,10 @@ impl EntityGateway for PostgresGateway { R: Send, E: From, { - let mut transaction = Box::new(PostgresTransaction { + let transaction = Box::new(PostgresTransaction { pgtransaction: self.pool.begin().await.map_err(|_| ()).unwrap() }); - let (mut transaction, result) = func(transaction).await.map_err(|_| ()).unwrap(); + let (transaction, result) = func(transaction).await.map_err(|_| ()).unwrap(); transaction.commit().await.map_err(|_| ()).unwrap(); Ok(result) } diff --git a/src/lib.rs b/src/lib.rs index 9934022..d908e0b 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -3,7 +3,6 @@ #![feature(drain_filter)] #![feature(try_blocks)] -#[macro_use] extern crate fix_hidden_lifetime_bug; diff --git a/src/ship/items/actions.rs b/src/ship/items/actions.rs index 934fac7..4f2fdad 100644 --- a/src/ship/items/actions.rs +++ b/src/ship/items/actions.rs @@ -1,43 +1,17 @@ -use std::collections::HashMap; use crate::ship::items::ClientItemId; -use crate::entity::item::{Meseta, ItemEntityId, ItemDetail, ItemEntity, ItemType, InventoryEntity, InventoryItemEntity, EquippedEntity, ItemNote}; -use std::cell::{RefMut, RefCell}; -use std::ops::{Deref, DerefMut}; -use std::convert::{From, Into}; +use crate::entity::item::ItemNote; use std::future::Future; use std::pin::Pin; -use async_std::sync::{Arc, Mutex}; -use std::borrow::BorrowMut; -use crate::ship::location::{AreaClient, RoomId}; -use crate::entity::character::{CharacterEntity, CharacterEntityId, TechLevel}; -use crate::entity::gateway::{EntityGateway, GatewayError}; -use crate::entity::gateway::entitygateway::EntityGatewayTransaction; -use crate::entity::item::tool::{Tool, ToolType}; -use crate::ship::items::state::{ItemState, ItemStateProxy, ItemStateAction, ItemAction, ItemStateError, FloorItem, InventoryItem, AddItemResult}; +use crate::entity::character::{CharacterEntity, CharacterEntityId}; +use crate::entity::gateway::EntityGateway; +use crate::ship::items::state::{ItemState, ItemStateProxy, ItemStateAction, ItemAction, ItemStateError, FloorItem, AddItemResult}; pub enum TriggerCreateItem {ItemAction, Yes, No } -/* -fn take_item_from_floor(character_id: CharacterEntityId, item_id: ClientItemId) - -> impl for<'a> Fn((ItemStateProxy<'a>, Box), ()) - -> Pin), FloorItem), ItemStateError>> + Send + 'a>> -{ - move |(mut item_state, transaction), _| { - Box::pin(async move { - let mut floor = item_state.floor(&character_id)?; - let item = floor.take_item(&item_id).ok_or(ItemStateError::NoFloorItem(item_id))?; - item_state.set_floor(floor); - - Ok(((item_state, transaction), item)) - }) - } -} -*/ - fn take_item_from_floor(character_id: CharacterEntityId, item_id: ClientItemId) -> impl for<'a> Fn((ItemStateProxy<'a>, Box), ()) -> Pin), FloorItem), ItemStateError>> + Send + 'a>> @@ -53,71 +27,6 @@ fn take_item_from_floor(character_id: CharacterEntityId, item_id: ClientItemId) } } -/* -fn take_item_from_floor<'a, F, Fut>(character_id: CharacterEntityId, item_id: ClientItemId) -> F -where - F: Fn((ItemStateProxy<'a>, Box), ()) -> Fut, - Fut: Future, Box), FloorItem), ItemStateError>> + Send - //Fut: Result, Box), FloorItem)> + Send, ItemStateError> - -{ - async move |(mut item_state, transaction): (ItemStateProxy<'a>, Box), _: ()| { - let mut floor = item_state.floor(&character_id)?; - let item = floor.take_item(&item_id).ok_or(ItemStateError::NoFloorItem(item_id))?; - item_state.set_floor(floor); - - Ok(((item_state, transaction), item)) - } -} -*/ - -/* -fn add_floor_item_to_inventory<'a, Fut>(character: &CharacterEntity) - -> impl Fn((ItemStateProxy<'a>, Box), FloorItem) -> Fut -where - Fut: Future, Box), TriggerCreateItem), ItemStateError>> + Send -{ - let character = character.clone(); - move |(mut item_state, transaction): (ItemStateProxy<'a>, Box), floor_item: FloorItem| { - let character = character.clone(); - async move { - let mut inventory = item_state.inventory(&character.id)?; - - let character_id = character.id; - let transaction = floor_item.with_entity_id(Ok(transaction), |mut transaction: Result<_, ItemStateError>, entity_id| { - async move { - if let Ok(transaction) = &mut transaction { - transaction.gateway().add_item_note(&entity_id, ItemNote::Pickup { - character_id - }).await?; - } - transaction - }}).await?; - - let mut transaction = floor_item.with_mag(Ok(transaction), |mut transaction: Result<_, ItemStateError>, entity_id, _mag| { - let character = character.clone(); - async move { - if let Ok(transaction) = &mut transaction { - transaction.gateway().change_mag_owner(&entity_id, &character).await?; - } - transaction - }}).await?; - - let add_result = inventory.add_floor_item(floor_item)?; - transaction.gateway().set_character_inventory(&character.id, &inventory.inventory.as_inventory_entity(&character.id)).await?; - item_state.set_inventory(inventory); - - Ok(((item_state, transaction), - match add_result { - AddItemResult::NewItem => TriggerCreateItem::Yes, - AddItemResult::AddToStack => TriggerCreateItem::No, - AddItemResult::Meseta => TriggerCreateItem::No, - })) - } - } -} -*/ - fn add_floor_item_to_inventory(character: &CharacterEntity) -> impl for<'a> Fn((ItemStateProxy<'a>, Box), FloorItem) -> Pin), TriggerCreateItem), ItemStateError>> + Send + 'a>> @@ -162,109 +71,6 @@ fn add_floor_item_to_inventory(character: &CharacterEntity) } } -/* -fn add_floor_item_to_inventory(character: &CharacterEntity) - -> impl for<'a> Fn((ItemStateProxy<'a>, Box), FloorItem) - -> Pin), TriggerCreateItem), ItemStateError>> + Send + 'a>> -{ - let character = character.clone(); - move |(mut item_state, transaction), floor_item| { - let character = character.clone(); - Box::pin(async move { - let mut inventory = item_state.inventory(&character.id)?; - - let character_id = character.id; - let transaction = floor_item.with_entity_id(Ok(transaction), |mut transaction: Result<_, ItemStateError>, entity_id| { - async move { - if let Ok(transaction) = &mut transaction { - transaction.gateway().add_item_note(&entity_id, ItemNote::Pickup { - character_id - }).await?; - } - transaction - }}).await?; - - let mut transaction = floor_item.with_mag(Ok(transaction), |mut transaction: Result<_, ItemStateError>, entity_id, _mag| { - let character = character.clone(); - async move { - if let Ok(transaction) = &mut transaction { - transaction.gateway().change_mag_owner(&entity_id, &character).await?; - } - transaction - }}).await?; - - let add_result = inventory.add_floor_item(floor_item)?; - transaction.gateway().set_character_inventory(&character.id, &inventory.inventory.as_inventory_entity(&character.id)).await?; - item_state.set_inventory(inventory); - - Ok(((item_state, transaction), - match add_result { - AddItemResult::NewItem => TriggerCreateItem::Yes, - AddItemResult::AddToStack => TriggerCreateItem::No, - AddItemResult::Meseta => TriggerCreateItem::No, - })) - }) - } -} -*/ - -/* -fn take_item_from_inventory(character_id: CharacterEntityId, item_id: ClientItemId) - -> impl for<'a> Fn((ItemStateProxy<'a>, Box), ()) - -> Pin), InventoryItem), ItemStateError>> + Send + 'a>> -{ - move |(mut item_state, transaction), _| { - Box::pin(async move { - let mut inventory = item_state.inventory(&character_id)?; - let item = inventory.take_item(&item_id); - - transaction.gateway().set_character_inventory(&character_id, &inventory.inventory.as_inventory_entity(&character_id)).await?; - item_state.set_inventory(inventory); - Ok((item_state, transaction), item) - }) - } -} - - -fn add_inventory_item_to_shared_floor(character: &CharacterEntity) - -> impl for<'a> Fn((ItemStateProxy<'a>, Box), InventoryItem) - -> Pin), ()), ItemStateError>> + Send + 'a>> -{ - let character = character.clone(); - move |(mut item_state, transaction), inventory_item| { - let character = character.clone(); - Box::pin(async move { - - }) - } -} -*/ - -/* -pub async fn pick_up_item<'a, EG>( - item_state: &'a mut ItemState, - entity_gateway: &mut EG, - character: &CharacterEntity, - item_id: &ClientItemId) - -> Result -where - //'a: 'static, - EG: EntityGateway, -{ - let result: Result = entity_gateway.with_transaction(|transaction| async move { - let item_state_proxy = ItemStateProxy::new(item_state); - let ((item_state_proxy, transaction), result) = ItemStateAction::default() - .act(take_item_from_floor(character.id, *item_id)) - .act(add_floor_item_to_inventory(character)) - .commit((item_state_proxy, transaction)) - .await?; - item_state_proxy.commit(); - Ok((transaction, result)) - }).await; - Ok(result?) -} -*/ - pub async fn pick_up_item( item_state: &mut ItemState, entity_gateway: &mut EG, @@ -284,7 +90,7 @@ where item_state_proxy.commit(); Ok((transaction, result)) }).await; - Ok(result?) + result } pub async fn drop_item( @@ -294,7 +100,6 @@ pub async fn drop_item( item_id: &ClientItemId) -> Result<(), ItemStateError> where - //'a: 'static, EG: EntityGateway, { /* diff --git a/src/ship/items/state.rs b/src/ship/items/state.rs index 6b78a8a..7bdc42e 100644 --- a/src/ship/items/state.rs +++ b/src/ship/items/state.rs @@ -1,19 +1,13 @@ use std::collections::HashMap; use crate::ship::items::ClientItemId; -use crate::entity::item::{Meseta, ItemEntityId, ItemDetail, ItemEntity, ItemType, InventoryEntity, InventoryItemEntity, EquippedEntity, ItemNote}; -use std::cell::{RefMut, RefCell}; -use std::ops::{Deref, DerefMut}; -use std::convert::{From, Into}; +use crate::entity::item::{Meseta, ItemEntityId, ItemDetail, ItemEntity, InventoryEntity, InventoryItemEntity}; use std::future::Future; -use std::pin::Pin; -use async_std::sync::{Arc, Mutex}; -use std::borrow::BorrowMut; -use crate::ship::location::{AreaClient, RoomId}; -use crate::entity::character::{CharacterEntity, CharacterEntityId, TechLevel}; -use crate::entity::gateway::{EntityGateway, GatewayError}; +use crate::ship::location::RoomId; +use crate::entity::character::CharacterEntityId; +use crate::entity::gateway::GatewayError; use crate::entity::gateway::entitygateway::EntityGatewayTransaction; -use crate::entity::item::tool::{Tool, ToolType}; +use crate::entity::item::tool::Tool; use crate::entity::item::mag::Mag; use crate::ship::drops::ItemDrop; @@ -139,6 +133,7 @@ where O: Send + Sync, P::Error: Send + Sync, { + #[allow(clippy::type_complexity)] pub fn act(self, g: G) -> ItemActionStage, G, GFut, S, E> where S: Send + Sync, @@ -196,13 +191,13 @@ pub enum InventoryItemDetail { } impl InventoryItemDetail { - fn stacked<'a>(&'a self) -> Option<&'a StackedItemDetail> { + fn stacked(&self) -> Option<&StackedItemDetail> { match self { InventoryItemDetail::Stacked(sitem) => Some(sitem), _ => None, } } - fn stacked_mut <'a>(&'a mut self) -> Option<&'a mut StackedItemDetail> { + fn stacked_mut(&mut self) -> Option<&mut StackedItemDetail> { match self { InventoryItemDetail::Stacked(sitem) => Some(sitem), _ => None, @@ -225,11 +220,11 @@ impl InventoryItem { { match &self.item { InventoryItemDetail::Individual(individual_item) => { - param = func(param, individual_item.entity_id.clone()).await; + param = func(param, individual_item.entity_id).await; }, InventoryItemDetail::Stacked(stacked_item) => { for entity_id in &stacked_item.entity_ids { - param = func(param, entity_id.clone()).await; + param = func(param, *entity_id).await; } } } @@ -246,7 +241,7 @@ pub enum FloorItemDetail { } impl FloorItemDetail { - fn stacked<'a>(&'a self) -> Option<&'a StackedItemDetail> { + fn stacked(&self) -> Option<&StackedItemDetail> { match self { FloorItemDetail::Stacked(sitem) => Some(sitem), _ => None, @@ -268,11 +263,11 @@ impl FloorItem { { match &self.item { FloorItemDetail::Individual(individual_item) => { - param = func(param, individual_item.entity_id.clone()).await; + param = func(param, individual_item.entity_id).await; }, FloorItemDetail::Stacked(stacked_item) => { for entity_id in &stacked_item.entity_ids { - param = func(param, entity_id.clone()).await; + param = func(param, *entity_id).await; } }, FloorItemDetail::Meseta(_meseta) => {}, @@ -286,18 +281,11 @@ impl FloorItem { F: FnMut(T, ItemEntityId, Mag) -> Fut, Fut: Future, { - match &self.item { - FloorItemDetail::Individual(individual_item) => { - match &individual_item.item { - ItemDetail::Mag(mag) => { - param = func(param, individual_item.entity_id.clone(), mag.clone()).await; - } - _ => {} - } - }, - _ => {} + if let FloorItemDetail::Individual(individual_item) = &self.item { + if let ItemDetail::Mag(mag) = &individual_item.item { + param = func(param, individual_item.entity_id, mag.clone()).await; + } } - param } } @@ -370,7 +358,7 @@ impl InventoryState { match item.item { FloorItemDetail::Individual(iitem) => { if self.inventory.0.len() >= 30 { - return Err(InventoryError::InventoryFull) + Err(InventoryError::InventoryFull) } else { self.inventory.0.push(InventoryItem { @@ -390,7 +378,7 @@ impl InventoryState { match existing_stack { Some(existing_stack) => { if existing_stack.entity_ids.len() + sitem.entity_ids.len() > sitem.tool.max_stack() { - return Err(InventoryError::StackFull) + Err(InventoryError::StackFull) } else { existing_stack.entity_ids.append(&mut sitem.entity_ids.clone()); @@ -399,7 +387,7 @@ impl InventoryState { }, None => { if self.inventory.0.len() >= 30 { - return Err(InventoryError::InventoryFull) + Err(InventoryError::InventoryFull) } else { self.inventory.0.push(InventoryItem { @@ -477,6 +465,7 @@ impl Default for ItemState { } +#[derive(Default)] struct ProxiedItemState { character_inventory: HashMap, //character_bank: HashMap>, @@ -491,6 +480,7 @@ struct ProxiedItemState { } +/* impl Default for ProxiedItemState { fn default() -> Self { ProxiedItemState { @@ -505,6 +495,7 @@ impl Default for ProxiedItemState { } } } +*/ pub struct ItemStateProxy<'a> { item_state: &'a mut ItemState, @@ -529,7 +520,7 @@ where { let existing_element = master.get(&key).ok_or_else(|| err(key))?; Ok(proxy.entry(key) - .or_insert(existing_element.clone()).clone()) + .or_insert_with(|| existing_element.clone()).clone()) } @@ -544,8 +535,8 @@ impl<'a> ItemStateProxy<'a> { pub fn inventory(&mut self, character_id: &CharacterEntityId) -> Result { Ok(InventoryState { character_id: *character_id, - inventory: get_or_clone(&self.item_state.character_inventory, &mut self.proxied_state.character_inventory, *character_id, |c| ItemStateError::NoCharacter(c))?, - meseta: get_or_clone(&self.item_state.character_meseta, &mut self.proxied_state.character_meseta, *character_id, |c| ItemStateError::NoCharacter(c))?, + inventory: get_or_clone(&self.item_state.character_inventory, &mut self.proxied_state.character_inventory, *character_id, ItemStateError::NoCharacter)?, + meseta: get_or_clone(&self.item_state.character_meseta, &mut self.proxied_state.character_meseta, *character_id, ItemStateError::NoCharacter)?, }) } @@ -555,16 +546,16 @@ impl<'a> ItemStateProxy<'a> { } pub fn floor(&mut self, character_id: &CharacterEntityId) -> Result { - let room_id = get_or_clone(&self.item_state.character_room, &mut self.proxied_state.character_room, *character_id, |c| ItemStateError::NoCharacter(c))?; + let room_id = get_or_clone(&self.item_state.character_room, &mut self.proxied_state.character_room, *character_id, ItemStateError::NoCharacter)?; Ok(FloorState { character_id: *character_id, - local: get_or_clone(&self.item_state.character_floor, &mut self.proxied_state.character_floor, *character_id, |c| ItemStateError::NoCharacter(c))?, - shared: get_or_clone(&self.item_state.room_floor, &mut self.proxied_state.room_floor, room_id, |r| ItemStateError::NoRoom(r))?, + local: get_or_clone(&self.item_state.character_floor, &mut self.proxied_state.character_floor, *character_id, ItemStateError::NoCharacter)?, + shared: get_or_clone(&self.item_state.room_floor, &mut self.proxied_state.room_floor, room_id, ItemStateError::NoRoom)?, }) } pub fn set_floor(&mut self, floor: FloorState) { - let room_id = get_or_clone(&self.item_state.character_room, &mut self.proxied_state.character_room, floor.character_id, |c| ItemStateError::NoCharacter(c)).unwrap(); + let room_id = get_or_clone(&self.item_state.character_room, &mut self.proxied_state.character_room, floor.character_id, ItemStateError::NoCharacter).unwrap(); self.proxied_state.character_floor.insert(floor.character_id, floor.local); self.proxied_state.room_floor.insert(room_id, floor.shared); }