2022-04-19 23:20:50 -06:00
|
|
|
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 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};
|
2022-04-27 20:57:47 -06:00
|
|
|
use crate::entity::item::mag::Mag;
|
2022-04-19 23:20:50 -06:00
|
|
|
use crate::ship::drops::ItemDrop;
|
|
|
|
|
|
|
|
|
|
|
|
#[derive(thiserror::Error, Debug)]
|
2022-04-27 20:57:47 -06:00
|
|
|
pub enum ItemStateError {
|
2022-04-19 23:20:50 -06:00
|
|
|
#[error("character {0} not found")]
|
|
|
|
NoCharacter(CharacterEntityId),
|
|
|
|
#[error("room {0} not found")]
|
|
|
|
NoRoom(RoomId),
|
|
|
|
#[error("floor item {0} not found")]
|
|
|
|
NoFloorItem(ClientItemId),
|
|
|
|
|
|
|
|
#[error("inventory error {0}")]
|
|
|
|
InventoryError(#[from] InventoryError),
|
|
|
|
|
|
|
|
#[error("invalid drop? {0:?} (this shouldn't occur)")]
|
|
|
|
BadItemDrop(ItemDrop),
|
|
|
|
|
|
|
|
#[error("idk")]
|
|
|
|
Dummy,
|
|
|
|
|
|
|
|
#[error("gateway")]
|
2022-04-20 18:35:24 -06:00
|
|
|
GatewayError(#[from] GatewayError),
|
2022-04-19 23:20:50 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#[async_trait::async_trait]
|
2022-04-27 20:57:47 -06:00
|
|
|
pub trait ItemAction {
|
2022-04-19 23:20:50 -06:00
|
|
|
type Input;
|
|
|
|
type Output;
|
|
|
|
type Start;
|
|
|
|
type Error;
|
|
|
|
|
|
|
|
async fn action(&self, s: Self::Start, i: Self::Input) -> Result<(Self::Start, Self::Output), Self::Error>;
|
|
|
|
async fn commit(&self, v: Self::Start) -> Result<(Self::Start, Self::Output), Self::Error>;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-04-27 20:57:47 -06:00
|
|
|
pub struct ItemStateAction<T, S, E> {
|
2022-04-19 23:20:50 -06:00
|
|
|
_t: std::marker::PhantomData<T>,
|
|
|
|
_s: std::marker::PhantomData<S>,
|
|
|
|
_e: std::marker::PhantomData<E>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T, S, E> Default for ItemStateAction<T, S, E> {
|
|
|
|
fn default() -> ItemStateAction<T, S, E> {
|
|
|
|
ItemStateAction {
|
|
|
|
_t: std::marker::PhantomData,
|
|
|
|
_s: std::marker::PhantomData,
|
|
|
|
_e: std::marker::PhantomData,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T, S, E> ItemStateAction<T, S, E>
|
|
|
|
where
|
|
|
|
T: Send + Sync,
|
|
|
|
S: Send + Sync,
|
|
|
|
E: Send + Sync,
|
|
|
|
{
|
2022-04-27 20:56:39 -06:00
|
|
|
pub fn act<O, F, Fut>(self, f: F) -> ItemActionStage<O, ItemStateAction<T, S, E>, F, Fut, S, E>
|
2022-04-19 23:20:50 -06:00
|
|
|
where
|
2022-04-27 20:56:39 -06:00
|
|
|
F: Fn(S, ()) -> Fut + Send + Sync,
|
|
|
|
Fut: Future<Output=Result<(S, O), E>> + Send
|
2022-04-19 23:20:50 -06:00
|
|
|
{
|
|
|
|
ItemActionStage {
|
|
|
|
_s: Default::default(),
|
|
|
|
_e: std::marker::PhantomData,
|
|
|
|
prev: self,
|
|
|
|
actionf: f,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-27 20:56:39 -06:00
|
|
|
pub struct ItemActionStage<O, P, F, Fut, S, E>
|
2022-04-19 23:20:50 -06:00
|
|
|
where
|
|
|
|
P: ItemAction,
|
2022-04-27 20:56:39 -06:00
|
|
|
F: Fn(S, P::Output) -> Fut + Send + Sync,
|
|
|
|
Fut: Future<Output=Result<(S, O) , E>> + Send,
|
2022-04-19 23:20:50 -06:00
|
|
|
{
|
|
|
|
_s: std::marker::PhantomData<S>,
|
|
|
|
_e: std::marker::PhantomData<E>,
|
|
|
|
prev: P,
|
|
|
|
actionf: F,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[async_trait::async_trait]
|
2022-04-27 20:56:39 -06:00
|
|
|
impl<O, P: ItemAction, F, Fut, S, E> ItemAction for ItemActionStage<O, P, F, Fut, S, E>
|
2022-04-19 23:20:50 -06:00
|
|
|
where
|
|
|
|
P: ItemAction + ItemAction<Start = S, Error = E> + Send + Sync,
|
2022-04-27 20:56:39 -06:00
|
|
|
F: Fn(S, P::Output) -> Fut + Send + Sync,
|
|
|
|
Fut: Future<Output=Result<(S, O), E>> + Send,
|
2022-04-19 23:20:50 -06:00
|
|
|
S: Send + Sync,
|
|
|
|
P::Output: Send + Sync,
|
|
|
|
E: Send + Sync,
|
|
|
|
O: Send + Sync,
|
|
|
|
P::Error: Send + Sync,
|
|
|
|
{
|
|
|
|
type Input = P::Output;
|
|
|
|
type Output = O;
|
|
|
|
type Start = S;
|
|
|
|
type Error = P::Error;
|
|
|
|
|
|
|
|
async fn action(&self, s: Self::Start, i: Self::Input) -> Result<(Self::Start, Self::Output), Self::Error> {
|
|
|
|
(self.actionf)(s, i).await
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn commit(&self, i: Self::Start) -> Result<(Self::Start, Self::Output), Self::Error> {
|
|
|
|
let (i, prev) = self.prev.commit(i).await?;
|
|
|
|
self.action(i, prev).await
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-27 20:56:39 -06:00
|
|
|
impl<O, P: ItemAction, F, Fut, S, E> ItemActionStage<O, P, F, Fut, S, E>
|
2022-04-19 23:20:50 -06:00
|
|
|
where
|
|
|
|
P: ItemAction<Start = S, Error = E> + Send + Sync,
|
2022-04-27 20:56:39 -06:00
|
|
|
F: Fn(S, P::Output) -> Fut + Send + Sync,
|
|
|
|
Fut: Future<Output=Result<(S, O), E>> + Send,
|
2022-04-19 23:20:50 -06:00
|
|
|
S: Send + Sync,
|
|
|
|
P::Output: Send + Sync,
|
|
|
|
E: Send + Sync,
|
|
|
|
O: Send + Sync,
|
|
|
|
P::Error: Send + Sync,
|
|
|
|
{
|
2022-04-27 20:56:39 -06:00
|
|
|
pub fn act<O2, G, GFut>(self, g: G) -> ItemActionStage<O2, ItemActionStage<O, P, F, Fut, S, E>, G, GFut, S, E>
|
2022-04-19 23:20:50 -06:00
|
|
|
where
|
|
|
|
S: Send + Sync,
|
2022-04-27 20:56:39 -06:00
|
|
|
G: Fn(S, <ItemActionStage<O, P, F, Fut, S, E> as ItemAction>::Output) -> GFut + Send + Sync,
|
|
|
|
GFut: Future<Output=Result<(S, O2), E>> + Send,
|
2022-04-19 23:20:50 -06:00
|
|
|
O2: Send + Sync,
|
|
|
|
{
|
|
|
|
ItemActionStage {
|
|
|
|
_s: Default::default(),
|
|
|
|
_e: Default::default(),
|
|
|
|
prev: self,
|
|
|
|
actionf: g,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[async_trait::async_trait]
|
|
|
|
impl<T, S, E> ItemAction for ItemStateAction<T, S, E>
|
|
|
|
where
|
|
|
|
T: Send + Sync,
|
|
|
|
S: Send + Sync,
|
|
|
|
E: Send + Sync,
|
|
|
|
{
|
|
|
|
type Input = T;
|
|
|
|
type Output = ();
|
|
|
|
type Start = T;
|
|
|
|
type Error = E;
|
|
|
|
|
|
|
|
async fn action(&self, s: Self::Start, i: Self::Input) -> Result<(Self::Start, Self::Output), Self::Error> {
|
|
|
|
Ok((s, ()))
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn commit(&self, i: Self::Start) -> Result<(Self::Start, Self::Output), Self::Error> {
|
|
|
|
Ok((i, ()))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-04-27 20:57:47 -06:00
|
|
|
#[derive(Clone, Debug)]
|
|
|
|
pub struct IndividualItemDetail {
|
2022-04-19 23:20:50 -06:00
|
|
|
entity_id: ItemEntityId,
|
|
|
|
item: ItemDetail,
|
|
|
|
}
|
|
|
|
|
2022-04-27 20:57:47 -06:00
|
|
|
#[derive(Clone, Debug)]
|
2022-04-19 23:20:50 -06:00
|
|
|
pub struct StackedItemDetail {
|
|
|
|
pub entity_ids: Vec<ItemEntityId>,
|
|
|
|
pub tool: Tool,
|
|
|
|
}
|
|
|
|
|
2022-04-27 20:57:47 -06:00
|
|
|
#[derive(Clone, Debug)]
|
|
|
|
pub enum InventoryItemDetail {
|
2022-04-19 23:20:50 -06:00
|
|
|
Individual(IndividualItemDetail),
|
|
|
|
Stacked(StackedItemDetail),
|
|
|
|
}
|
|
|
|
|
|
|
|
impl InventoryItemDetail {
|
|
|
|
fn stacked<'a>(&'a self) -> Option<&'a StackedItemDetail> {
|
|
|
|
match self {
|
|
|
|
InventoryItemDetail::Stacked(sitem) => Some(sitem),
|
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fn stacked_mut <'a>(&'a mut self) -> Option<&'a mut StackedItemDetail> {
|
|
|
|
match self {
|
|
|
|
InventoryItemDetail::Stacked(sitem) => Some(sitem),
|
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-04-27 20:57:47 -06:00
|
|
|
#[derive(Clone, Debug)]
|
|
|
|
pub struct InventoryItem {
|
2022-04-19 23:20:50 -06:00
|
|
|
item_id: ClientItemId,
|
|
|
|
item: InventoryItemDetail,
|
|
|
|
}
|
|
|
|
|
2022-04-27 20:57:47 -06:00
|
|
|
impl InventoryItem {
|
|
|
|
pub async fn with_entity_id<F, Fut, T>(&self, mut param: T, mut func: F) -> T
|
|
|
|
where
|
|
|
|
F: FnMut(T, ItemEntityId) -> Fut,
|
|
|
|
Fut: Future<Output=T>,
|
|
|
|
{
|
|
|
|
match &self.item {
|
|
|
|
InventoryItemDetail::Individual(individual_item) => {
|
|
|
|
param = func(param, individual_item.entity_id.clone()).await;
|
|
|
|
},
|
|
|
|
InventoryItemDetail::Stacked(stacked_item) => {
|
|
|
|
for entity_id in &stacked_item.entity_ids {
|
|
|
|
param = func(param, entity_id.clone()).await;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
param
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-19 23:20:50 -06:00
|
|
|
#[derive(Clone)]
|
2022-04-27 20:57:47 -06:00
|
|
|
pub enum FloorItemDetail {
|
2022-04-19 23:20:50 -06:00
|
|
|
Individual(IndividualItemDetail),
|
|
|
|
Stacked(StackedItemDetail),
|
|
|
|
Meseta(Meseta),
|
|
|
|
}
|
|
|
|
|
|
|
|
impl FloorItemDetail {
|
|
|
|
fn stacked<'a>(&'a self) -> Option<&'a StackedItemDetail> {
|
|
|
|
match self {
|
|
|
|
FloorItemDetail::Stacked(sitem) => Some(sitem),
|
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone)]
|
2022-04-27 20:57:47 -06:00
|
|
|
pub struct FloorItem {
|
2022-04-19 23:20:50 -06:00
|
|
|
item_id: ClientItemId,
|
|
|
|
item: FloorItemDetail,
|
|
|
|
}
|
|
|
|
|
2022-04-27 20:57:47 -06:00
|
|
|
impl FloorItem {
|
|
|
|
pub async fn with_entity_id<F, Fut, T>(&self, mut param: T, mut func: F) -> T
|
|
|
|
where
|
|
|
|
F: FnMut(T, ItemEntityId) -> Fut,
|
|
|
|
Fut: Future<Output=T>,
|
|
|
|
{
|
|
|
|
match &self.item {
|
|
|
|
FloorItemDetail::Individual(individual_item) => {
|
|
|
|
param = func(param, individual_item.entity_id.clone()).await;
|
|
|
|
},
|
|
|
|
FloorItemDetail::Stacked(stacked_item) => {
|
|
|
|
for entity_id in &stacked_item.entity_ids {
|
|
|
|
param = func(param, entity_id.clone()).await;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
FloorItemDetail::Meseta(_meseta) => {},
|
|
|
|
}
|
|
|
|
|
|
|
|
param
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn with_mag<F, Fut, T>(&self, mut param: T, mut func: F) -> T
|
|
|
|
where
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
|
|
|
|
param
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-19 23:20:50 -06:00
|
|
|
|
2022-04-27 20:57:47 -06:00
|
|
|
#[derive(Clone, Debug)]
|
|
|
|
pub struct Inventory(Vec<InventoryItem>);
|
|
|
|
|
|
|
|
impl Inventory {
|
|
|
|
pub fn as_inventory_entity(&self, _character_id: &CharacterEntityId) -> InventoryEntity {
|
|
|
|
InventoryEntity {
|
|
|
|
items: self.0.iter()
|
|
|
|
.map(|item| {
|
|
|
|
match &item.item {
|
|
|
|
InventoryItemDetail::Individual(item) => {
|
|
|
|
InventoryItemEntity::Individual(ItemEntity {
|
|
|
|
id: item.entity_id,
|
|
|
|
item: item.item.clone(),
|
|
|
|
})
|
|
|
|
},
|
|
|
|
InventoryItemDetail::Stacked(items) => {
|
|
|
|
InventoryItemEntity::Stacked(items.entity_ids.iter()
|
|
|
|
.map(|id| {
|
|
|
|
ItemEntity {
|
|
|
|
id: *id,
|
|
|
|
item: ItemDetail::Tool(items.tool)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.collect())
|
|
|
|
},
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.collect()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-04-19 23:20:50 -06:00
|
|
|
|
|
|
|
|
|
|
|
#[derive(thiserror::Error, Debug)]
|
2022-04-27 20:57:47 -06:00
|
|
|
pub enum InventoryError {
|
2022-04-19 23:20:50 -06:00
|
|
|
#[error("inventory full")]
|
|
|
|
InventoryFull,
|
|
|
|
#[error("stack full")]
|
|
|
|
StackFull,
|
|
|
|
#[error("meseta full")]
|
|
|
|
MesetaFull,
|
|
|
|
}
|
|
|
|
|
2022-04-27 20:57:47 -06:00
|
|
|
pub enum AddItemResult {
|
2022-04-19 23:20:50 -06:00
|
|
|
NewItem,
|
|
|
|
AddToStack,
|
|
|
|
Meseta,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone)]
|
2022-04-27 20:57:47 -06:00
|
|
|
pub struct LocalFloor(Vec<FloorItem>);
|
2022-04-19 23:20:50 -06:00
|
|
|
#[derive(Clone)]
|
2022-04-27 20:57:47 -06:00
|
|
|
pub struct SharedFloor(Vec<FloorItem>);
|
2022-04-19 23:20:50 -06:00
|
|
|
#[derive(Clone)]
|
2022-04-27 20:57:47 -06:00
|
|
|
pub struct RoomFloorItems(Vec<FloorItem>);
|
2022-04-19 23:20:50 -06:00
|
|
|
|
2022-04-27 20:57:47 -06:00
|
|
|
pub struct InventoryState {
|
2022-04-19 23:20:50 -06:00
|
|
|
character_id: CharacterEntityId,
|
2022-04-27 20:57:47 -06:00
|
|
|
pub inventory: Inventory,
|
2022-04-19 23:20:50 -06:00
|
|
|
meseta: Meseta,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl InventoryState {
|
2022-04-27 20:57:47 -06:00
|
|
|
pub fn add_floor_item(&mut self, item: FloorItem) -> Result<AddItemResult, InventoryError> {
|
2022-04-19 23:20:50 -06:00
|
|
|
match item.item {
|
|
|
|
FloorItemDetail::Individual(iitem) => {
|
|
|
|
if self.inventory.0.len() >= 30 {
|
|
|
|
return Err(InventoryError::InventoryFull)
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
self.inventory.0.push(InventoryItem {
|
|
|
|
item_id: item.item_id,
|
|
|
|
item: InventoryItemDetail::Individual(iitem)
|
|
|
|
});
|
|
|
|
Ok(AddItemResult::NewItem)
|
|
|
|
}
|
|
|
|
},
|
|
|
|
FloorItemDetail::Stacked(sitem) => {
|
|
|
|
let existing_stack = self.inventory.0
|
|
|
|
.iter_mut()
|
|
|
|
.filter_map(|item| item.item.stacked_mut())
|
|
|
|
.find(|item| {
|
|
|
|
item.tool == sitem.tool
|
|
|
|
});
|
|
|
|
match existing_stack {
|
|
|
|
Some(existing_stack) => {
|
|
|
|
if existing_stack.entity_ids.len() + sitem.entity_ids.len() > sitem.tool.max_stack() {
|
|
|
|
return Err(InventoryError::StackFull)
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
existing_stack.entity_ids.append(&mut sitem.entity_ids.clone());
|
|
|
|
Ok(AddItemResult::AddToStack)
|
|
|
|
}
|
|
|
|
},
|
|
|
|
None => {
|
|
|
|
if self.inventory.0.len() >= 30 {
|
|
|
|
return Err(InventoryError::InventoryFull)
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
self.inventory.0.push(InventoryItem {
|
|
|
|
item_id: item.item_id,
|
|
|
|
item: InventoryItemDetail::Stacked(sitem)
|
|
|
|
});
|
|
|
|
Ok(AddItemResult::NewItem)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
},
|
|
|
|
FloorItemDetail::Meseta(meseta) => {
|
|
|
|
if self.meseta == Meseta(999999) {
|
|
|
|
Err(InventoryError::MesetaFull)
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
self.meseta.0 = std::cmp::min(self.meseta.0 + meseta.0 ,999999);
|
|
|
|
Ok(AddItemResult::Meseta)
|
|
|
|
}
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-27 20:57:47 -06:00
|
|
|
pub struct FloorState {
|
2022-04-19 23:20:50 -06:00
|
|
|
character_id: CharacterEntityId,
|
|
|
|
local: LocalFloor,
|
|
|
|
shared: SharedFloor,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl FloorState {
|
2022-04-27 20:57:47 -06:00
|
|
|
pub fn take_item(&mut self, item_id: &ClientItemId) -> Option<FloorItem> {
|
2022-04-19 23:20:50 -06:00
|
|
|
let item = self.local.0
|
|
|
|
.drain_filter(|item| {
|
|
|
|
item.item_id == *item_id
|
|
|
|
})
|
|
|
|
.next();
|
|
|
|
item.or_else(|| {
|
|
|
|
self.shared.0
|
|
|
|
.drain_filter(|item| {
|
|
|
|
item.item_id == *item_id
|
|
|
|
})
|
|
|
|
.next()
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-04-27 20:57:47 -06:00
|
|
|
pub struct ItemState {
|
2022-04-19 23:20:50 -06:00
|
|
|
character_inventory: HashMap<CharacterEntityId, Inventory>,
|
|
|
|
//character_bank: HashMap<CharacterEntityId, Bank>,
|
|
|
|
character_meseta: HashMap<CharacterEntityId, Meseta>,
|
|
|
|
//bank_meseta: HashMap<CharacterEntityId, Meseta>,
|
|
|
|
|
|
|
|
character_room: HashMap<CharacterEntityId, RoomId>,
|
|
|
|
character_floor: HashMap<CharacterEntityId, LocalFloor>,
|
|
|
|
room_floor: HashMap<RoomId, SharedFloor>,
|
|
|
|
|
|
|
|
//room_item_id_counter: Arc<RefCell<HashMap<RoomId, Box<dyn FnMut() -> ClientItemId + Send + Sync>>>>,
|
|
|
|
room_item_id_counter: u32,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for ItemState {
|
|
|
|
fn default() -> ItemState {
|
|
|
|
ItemState {
|
|
|
|
character_inventory: HashMap::new(),
|
|
|
|
character_meseta: HashMap::new(),
|
|
|
|
character_room: HashMap::new(),
|
|
|
|
character_floor: HashMap::new(),
|
|
|
|
room_floor: HashMap::new(),
|
|
|
|
room_item_id_counter: 0x00810000,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
struct ProxiedItemState {
|
|
|
|
character_inventory: HashMap<CharacterEntityId, Inventory>,
|
|
|
|
//character_bank: HashMap<CharacterEntityId, RefCell<Bank>>,
|
|
|
|
character_meseta: HashMap<CharacterEntityId, Meseta>,
|
|
|
|
//bank_meseta: HashMap<CharacterEntityId, RefCell<Meseta>>,
|
|
|
|
|
|
|
|
character_room: HashMap<CharacterEntityId, RoomId>,
|
|
|
|
character_floor: HashMap<CharacterEntityId, LocalFloor>,
|
|
|
|
room_floor: HashMap<RoomId, SharedFloor>,
|
|
|
|
|
|
|
|
//room_item_id_counter: HashMap<RoomId, Box<dyn FnMut() -> ClientItemId + Send>>,
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for ProxiedItemState {
|
|
|
|
fn default() -> Self {
|
|
|
|
ProxiedItemState {
|
|
|
|
character_inventory: HashMap::new(),
|
|
|
|
//character_bank: HashMap::new(),
|
|
|
|
character_meseta: HashMap::new(),
|
|
|
|
//bank_meseta: HashMap::new(),
|
|
|
|
character_floor: HashMap::new(),
|
|
|
|
character_room: HashMap::new(),
|
|
|
|
room_floor: HashMap::new(),
|
|
|
|
//room_item_id_counter: HashMap::new(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-27 20:57:47 -06:00
|
|
|
pub struct ItemStateProxy<'a> {
|
2022-04-19 23:20:50 -06:00
|
|
|
item_state: &'a mut ItemState,
|
|
|
|
proxied_state: ProxiedItemState,
|
|
|
|
}
|
|
|
|
|
2022-04-27 20:57:47 -06:00
|
|
|
impl<'a> ItemStateProxy<'a> {
|
|
|
|
pub fn commit(self) {
|
|
|
|
self.item_state.character_inventory.extend(self.proxied_state.character_inventory.clone());
|
|
|
|
self.item_state.character_meseta.extend(self.proxied_state.character_meseta.clone());
|
|
|
|
self.item_state.character_room.extend(self.proxied_state.character_room.clone());
|
|
|
|
self.item_state.character_floor.extend(self.proxied_state.character_floor.clone());
|
|
|
|
self.item_state.room_floor.extend(self.proxied_state.room_floor.clone());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-19 23:20:50 -06:00
|
|
|
|
|
|
|
fn get_or_clone<K, V>(master: &HashMap<K, V>, proxy: &mut HashMap<K, V>, key: K, err: fn(K) -> ItemStateError) -> Result<V, ItemStateError>
|
|
|
|
where
|
|
|
|
K: Eq + std::hash::Hash + Copy,
|
|
|
|
V: Clone
|
|
|
|
{
|
|
|
|
let existing_element = master.get(&key).ok_or_else(|| err(key))?;
|
|
|
|
Ok(proxy.entry(key)
|
|
|
|
.or_insert(existing_element.clone()).clone())
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> ItemStateProxy<'a> {
|
2022-04-27 21:16:04 -06:00
|
|
|
pub fn new(item_state: &'a mut ItemState) -> Self {
|
2022-04-19 23:20:50 -06:00
|
|
|
ItemStateProxy {
|
|
|
|
item_state,
|
|
|
|
proxied_state: Default::default(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-27 20:57:47 -06:00
|
|
|
pub fn inventory(&mut self, character_id: &CharacterEntityId) -> Result<InventoryState, ItemStateError> {
|
2022-04-19 23:20:50 -06:00
|
|
|
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))?,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-04-27 20:57:47 -06:00
|
|
|
pub fn set_inventory(&mut self, inventory: InventoryState) {
|
2022-04-19 23:20:50 -06:00
|
|
|
self.proxied_state.character_inventory.insert(inventory.character_id, inventory.inventory);
|
|
|
|
self.proxied_state.character_meseta.insert(inventory.character_id, inventory.meseta);
|
|
|
|
}
|
|
|
|
|
2022-04-27 20:57:47 -06:00
|
|
|
pub fn floor(&mut self, character_id: &CharacterEntityId) -> Result<FloorState, ItemStateError> {
|
2022-04-19 23:20:50 -06:00
|
|
|
let room_id = get_or_clone(&self.item_state.character_room, &mut self.proxied_state.character_room, *character_id, |c| ItemStateError::NoCharacter(c))?;
|
|
|
|
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))?,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-04-27 20:57:47 -06:00
|
|
|
pub fn set_floor(&mut self, floor: FloorState) {
|
2022-04-19 23:20:50 -06:00
|
|
|
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();
|
|
|
|
self.proxied_state.character_floor.insert(floor.character_id, floor.local);
|
|
|
|
self.proxied_state.room_floor.insert(room_id, floor.shared);
|
|
|
|
}
|
|
|
|
|
2022-04-27 20:57:47 -06:00
|
|
|
pub fn new_item_id(&mut self) -> Result<ClientItemId, ItemStateError> {
|
2022-04-19 23:20:50 -06:00
|
|
|
self.item_state.room_item_id_counter += 1;
|
|
|
|
Ok(ClientItemId(self.item_state.room_item_id_counter))
|
|
|
|
}
|
|
|
|
}
|