Struct veloren_common::trade::PendingTrade
source · pub struct PendingTrade {
pub parties: [Uid; 2],
pub offers: [HashMap<InvSlotId, u32>; 2],
pub phase: TradePhase,
pub accept_flags: [bool; 2],
}
Expand description
Items are not removed from the inventory during a PendingTrade: all the items are moved atomically (if there’s space and both parties agree) upon completion
Since this stores InvSlotId
s (i.e. references into inventories) instead of
items themselves, there aren’t any duplication/loss risks from things like
dropped connections or declines, since the server doesn’t have to move items
from a trade back into a player’s inventory.
On the flip side, since they are references to slots, if a player could
swap items in their inventory during a trade, they could mutate the trade,
enabling them to remove an item from the trade even after receiving the
counterparty’s phase2 accept. To prevent this, we disallow all
forms of inventory manipulation in server::events::inventory_manip
if
there’s a pending trade that’s past phase1 (in phase1, the trade should be
mutable anyway).
Inventory manipulation in phase1 may be beneficial to trade (e.g. splitting a stack of items, once that’s implemented), but should reset both phase1 accept flags to make the changes more visible.
Another edge case prevented by using InvSlotId
s is that it disallows
trading currently-equipped items (since EquipSlot
s are disjoint from
InvSlotId
s), which avoids the issues associated with trading equipped bags
that may still have contents.
Fields§
§parties: [Uid; 2]
parties[0]
is the entity that initiated the trade, parties[1] is the
other entity that’s being traded with
offers: [HashMap<InvSlotId, u32>; 2]
offers[i]
represents the items and quantities of the party i’s items
being offered
phase: TradePhase
The current phase of the trade
accept_flags: [bool; 2]
accept_flags
indicate that which parties wish to proceed to the next
phase of the trade
Implementations§
source§impl PendingTrade
impl PendingTrade
pub fn new(party: Uid, counterparty: Uid) -> PendingTrade
pub fn phase(&self) -> TradePhase
pub fn should_commit(&self) -> bool
pub fn which_party(&self, party: Uid) -> Option<usize>
pub fn is_empty_trade(&self) -> bool
sourcepub fn process_trade_action(
&mut self,
who: usize,
action: TradeAction,
inventories: &[&Inventory],
)
pub fn process_trade_action( &mut self, who: usize, action: TradeAction, inventories: &[&Inventory], )
Invariants:
- A party is never shown as offering more of an item than they own
- Offers with a quantity of zero get removed from the trade
- Modifications can only happen in phase 1
- Whenever a trade is modified, both accept flags get reset
- Accept flags only get set for the current phase
Trait Implementations§
source§impl Clone for PendingTrade
impl Clone for PendingTrade
source§fn clone(&self) -> PendingTrade
fn clone(&self) -> PendingTrade
1.0.0 · source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read moresource§impl Debug for PendingTrade
impl Debug for PendingTrade
source§impl<'de> Deserialize<'de> for PendingTrade
impl<'de> Deserialize<'de> for PendingTrade
source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
source§impl PartialEq for PendingTrade
impl PartialEq for PendingTrade
source§fn eq(&self, other: &PendingTrade) -> bool
fn eq(&self, other: &PendingTrade) -> bool
self
and other
values to be equal, and is used
by ==
.source§impl Serialize for PendingTrade
impl Serialize for PendingTrade
impl Eq for PendingTrade
impl StructuralPartialEq for PendingTrade
Auto Trait Implementations§
impl Freeze for PendingTrade
impl RefUnwindSafe for PendingTrade
impl Send for PendingTrade
impl Sync for PendingTrade
impl Unpin for PendingTrade
impl UnwindSafe for PendingTrade
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
source§default unsafe fn clone_to_uninit(&self, dst: *mut T)
default unsafe fn clone_to_uninit(&self, dst: *mut T)
clone_to_uninit
)§impl<C, M> ConvertSaveload<M> for C
impl<C, M> ConvertSaveload<M> for C
§type Error = Infallible
type Error = Infallible
§fn convert_into<F>(
&self,
_: F,
) -> Result<<C as ConvertSaveload<M>>::Data, <C as ConvertSaveload<M>>::Error>
fn convert_into<F>( &self, _: F, ) -> Result<<C as ConvertSaveload<M>>::Data, <C as ConvertSaveload<M>>::Error>
Data
) using
entity to marker mapping function§fn convert_from<F>(
data: <C as ConvertSaveload<M>>::Data,
_: F,
) -> Result<C, <C as ConvertSaveload<M>>::Error>
fn convert_from<F>( data: <C as ConvertSaveload<M>>::Data, _: F, ) -> Result<C, <C as ConvertSaveload<M>>::Error>
Data
) using
entity to marker mapping function§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key
and return true
if they are equal.§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
source§impl<T> IntoEither for T
impl<T> IntoEither for T
source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left
is true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moresource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left(&self)
returns true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read more