Browse Source

mostly appease clippy

pull/112/head
jake 2 years ago
committed by andy
parent
commit
5d6f9deb59
  1. 2
      src/entity/gateway/entitygateway.rs
  2. 4
      src/entity/gateway/inmemory.rs
  3. 6
      src/entity/gateway/postgres/postgres.rs
  4. 1
      src/lib.rs
  5. 205
      src/ship/items/actions.rs
  6. 69
      src/ship/items/state.rs

2
src/entity/gateway/entitygateway.rs

@ -148,7 +148,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!()
}

4
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
}
@ -204,7 +204,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)

6
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<GatewayError>,
{
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)
}

1
src/lib.rs

@ -3,7 +3,6 @@
#![feature(drain_filter)]
#![feature(try_blocks)]
#[macro_use]
extern crate fix_hidden_lifetime_bug;

205
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<dyn EntityGatewayTransaction>), ())
-> Pin<Box<dyn Future<Output=Result<((ItemStateProxy, Box<dyn EntityGatewayTransaction>), 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<dyn EntityGatewayTransaction + 'a>), ())
-> Pin<Box<dyn Future<Output=Result<((ItemStateProxy, Box<dyn EntityGatewayTransaction + 'a>), 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<dyn EntityGatewayTransaction>), ()) -> Fut,
Fut: Future<Output=Result<((ItemStateProxy<'a>, Box<dyn EntityGatewayTransaction>), FloorItem), ItemStateError>> + Send
//Fut: Result<impl Future<Output=((ItemStateProxy<'a>, Box<dyn EntityGatewayTransaction>), FloorItem)> + Send, ItemStateError>
{
async move |(mut item_state, transaction): (ItemStateProxy<'a>, Box<dyn EntityGatewayTransaction>), _: ()| {
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<dyn EntityGatewayTransaction>), FloorItem) -> Fut
where
Fut: Future<Output=Result<((ItemStateProxy<'a>, Box<dyn EntityGatewayTransaction>), TriggerCreateItem), ItemStateError>> + Send
{
let character = character.clone();
move |(mut item_state, transaction): (ItemStateProxy<'a>, Box<dyn EntityGatewayTransaction>), 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<dyn EntityGatewayTransaction + 'a>), FloorItem)
-> Pin<Box<dyn Future<Output=Result<((ItemStateProxy, Box<dyn EntityGatewayTransaction + 'a>), 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<dyn EntityGatewayTransaction>), FloorItem)
-> Pin<Box<dyn Future<Output=Result<((ItemStateProxy, Box<dyn EntityGatewayTransaction>), 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<dyn EntityGatewayTransaction>), ())
-> Pin<Box<dyn Future<Output=Result<((ItemStateProxy, Box<dyn EntityGatewayTransaction>), 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<dyn EntityGatewayTransaction>), InventoryItem)
-> Pin<Box<dyn Future<Output=Result<((ItemStateProxy, Box<dyn EntityGatewayTransaction>), ()), 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<TriggerCreateItem, ItemStateError>
where
//'a: 'static,
EG: EntityGateway,
{
let result: Result<TriggerCreateItem, ItemStateError> = 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<EG>(
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<EG>(
@ -294,7 +100,6 @@ pub async fn drop_item<EG>(
item_id: &ClientItemId)
-> Result<(), ItemStateError>
where
//'a: 'static,
EG: EntityGateway,
{
/*

69
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<O2, G, GFut>(self, g: G) -> ItemActionStage<O2, ItemActionStage<O, P, F, Fut, S, E>, 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<Output=T>,
{
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<CharacterEntityId, Inventory>,
//character_bank: HashMap<CharacterEntityId, RefCell<Bank>>,
@ -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<InventoryState, ItemStateError> {
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<FloorState, ItemStateError> {
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);
}

Loading…
Cancel
Save