Browse Source

trade meseta

pull/80/head
jake 3 years ago
parent
commit
72d72801e1
  1. 34
      src/ship/items/manager.rs
  2. 12
      src/ship/packet/builder/message.rs
  3. 58
      src/ship/packet/handler/trade.rs
  4. 7
      src/ship/trade.rs

34
src/ship/items/manager.rs

@ -15,7 +15,7 @@ use crate::ship::trade::TradeItem;
use crate::ship::drops::{ItemDrop, ItemDropType};
use crate::ship::location::{AreaClient, RoomId};
use crate::ship::shops::ShopItem;
use crate::ship::packet::handler::trade::TradeError;
use crate::ship::packet::handler::trade::{TradeError, OTHER_MESETA_ITEM_ID};
use crate::ship::items::bank::*;
use crate::ship::items::floor::*;
@ -938,8 +938,8 @@ impl ItemManager {
pub async fn trade_items<EG: EntityGateway>(&mut self,
entity_gateway: &mut EG,
room_id: RoomId,
p1: (&AreaClient, &CharacterEntity, &Vec<TradeItem>),
p2: (&AreaClient, &CharacterEntity, &Vec<TradeItem>))
p1: (&AreaClient, &CharacterEntity, &Vec<TradeItem>, usize),
p2: (&AreaClient, &CharacterEntity, &Vec<TradeItem>, usize))
-> Result<Vec<ItemToTrade>, anyhow::Error> {
let it = ItemTransaction::new(&self, (p1, p2, room_id))
.act(|it, (p1, p2, room_id)| -> Result<_, anyhow::Error> {
@ -980,9 +980,6 @@ impl ItemManager {
Err(TradeError::NoInventorySpace)
},
}
},
TradeItem::Meseta(..) => {
Ok(acc)
}
}
})
@ -1038,17 +1035,27 @@ impl ItemManager {
}),
))
}
},
TradeItem::Meseta(amount) => {
Some((None, // is there a packet that informs other clients about meseta changes?
}
}
})
.chain(
if src_client.3 > 0 {
Box::new(std::iter::once(Some(
(Some(ItemToTrade {
add_to: *dest_client.0,
remove_from: *src_client.0,
current_item_id: OTHER_MESETA_ITEM_ID,
new_item_id: OTHER_MESETA_ITEM_ID,
item_detail: ItemToTradeDetail::Meseta(src_client.3)
}),
Box::new(TradeMeseta {
src_character_id: src_client.1.id,
dest_character_id: dest_client.1.id,
amount: *amount,
}),
))
}
amount: src_client.3,
}) as Box<dyn ItemAction<EG>>)))) as Box<dyn Iterator<Item = _>>
}
else {
Box::new(std::iter::empty()) as Box<dyn Iterator<Item = _>>
})
.collect::<Option<Vec<_>>>()
});
@ -1082,6 +1089,7 @@ impl ItemManager {
pub enum ItemToTradeDetail {
Individual(ItemDetail),
Stacked(Tool, usize),
Meseta(usize),
}
#[derive(Debug)]

12
src/ship/packet/builder/message.rs

@ -53,6 +53,18 @@ pub fn create_stacked_item(area_client: AreaClient, item_id: ClientItemId, tool:
})
}
pub fn create_meseta(area_client: AreaClient, amount: usize) -> CreateItem {
let bytes: [u8; 12] = [4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
CreateItem {
client: area_client.local_client.id(),
target: 0,
item_data: bytes,
item_id: 0xFFFFFFFF,
item_data2: u32::to_le_bytes(amount as u32),
unknown: 0,
}
}
pub fn create_withdrawn_inventory_item(area_client: AreaClient, item: &InventoryItem) -> Result<CreateItem, ShipError> {
let bytes = item.as_client_bytes();
Ok(CreateItem {

58
src/ship/packet/handler/trade.rs

@ -18,6 +18,10 @@ use libpso::utf8_to_utf16_array;
use crate::ship::packet::builder;
use crate::ship::shops::{ShopItem, ToolShopItem, ArmorShopItem};
pub const MESETA_ITEM_ID: ClientItemId = ClientItemId(0xFFFFFF01);
pub const OTHER_MESETA_ITEM_ID: ClientItemId = ClientItemId(0xFFFFFFFF);
#[derive(thiserror::Error, Debug, PartialEq, Eq)]
pub enum TradeError {
#[error("no partner")]
@ -36,6 +40,8 @@ pub enum TradeError {
NoInventorySpace,
#[error("no space in stack")]
NoStackSpace,
#[error("invalid meseta amount")]
InvalidMeseta,
}
@ -60,7 +66,7 @@ where
let trade_partner = client_location.get_client_neighbors(id)?
.into_iter()
.filter(|ac| {
ac.local_client.id() == trade_request.client
ac.local_client.id() == target as u8 //trade_request.client
})
.next()
.ok_or(TradeError::CouldNotFindTradePartner)?;
@ -107,6 +113,10 @@ where
if this.status == TradeStatus::Trading && other.status == TradeStatus::Trading {
let client = clients.get(&this.client()).ok_or(ShipError::ClientNotFound(this.client()))?;
let inventory = item_manager.get_character_inventory(&client.character)?;
if ClientItemId(item_id) == MESETA_ITEM_ID {
this.meseta += amount as usize;
}
else {
let item = inventory.get_item_by_id(ClientItemId(item_id)).ok_or(ItemManagerError::NoSuchItemId(ClientItemId(item_id)))?;
match item {
@ -120,6 +130,7 @@ where
this.items.push(TradeItem::Stacked(ClientItemId(item_id), amount as usize));
},
}
}
let trade_request = trade_request.clone();
Ok(Box::new(client_location.get_all_clients_by_client(id)?.into_iter()
@ -148,6 +159,10 @@ where
if this.status == TradeStatus::Trading && other.status == TradeStatus::Trading {
let client = clients.get(&this.client())?; //.ok_or(ShipError::ClientNotFound(id)).ok()?;
let inventory = item_manager.get_character_inventory(&client.character).ok()?;
if ClientItemId(item_id) == MESETA_ITEM_ID {
this.meseta -= amount as usize;
}
else {
let item = inventory.get_item_by_id(ClientItemId(item_id))?;
match item {
@ -176,6 +191,7 @@ where
}
},
}
}
let trade_request = trade_request.clone();
Some(Box::new(client_location.get_all_clients_by_client(id).unwrap().into_iter()
.filter(move |client| client.local_client.id() == target as u8)
@ -277,7 +293,6 @@ where
Ok(trades
.with(&id, |this, other| -> Result<Box<dyn Iterator<Item = (ClientId, SendShipPacket)> + Send>, anyhow::Error> {
if status_is_not(&this.status, &[TradeStatus::FinalConfirm]) || status_is_not(&other.status, &[TradeStatus::FinalConfirm, TradeStatus::ItemsChecked]) {
//if this.status != TradeStatus::FinalConfirm || (other.status != TradeStatus::FinalConfirm || other.status != TradeStatus::ItemsChecked) {
return Err(TradeError::MismatchedStatus.into())
}
@ -285,9 +300,19 @@ where
let inventory = item_manager.get_character_inventory(&client.character)?;
let item_blobs = items_to_trade.items.iter().take(items_to_trade.count as usize);
let trade_items = item_blobs
item_blobs
.map(|item| {
// TOOD: meseta?
if ClientItemId(item.item_id) == OTHER_MESETA_ITEM_ID {
if item.item_data[0] != 4 {
return Err(TradeError::InvalidItemId(ClientItemId(item.item_id)).into())
}
let amount = u32::from_le_bytes(item.item_data2);
if amount > client.character.meseta {
return Err(TradeError::InvalidMeseta.into())
}
Ok(())
}
else {
let real_item = inventory.get_item_by_id(ClientItemId(item.item_id))
.ok_or(ItemManagerError::NoSuchItemId(ClientItemId(item.item_id)))?;
let trade_item_bytes: [u8; 16] = item.item_data.iter()
@ -298,7 +323,7 @@ where
match real_item {
InventoryItem::Individual(individual_inventory_item) => {
if real_item.as_client_bytes() == trade_item_bytes {
Ok(TradeItem::Individual(individual_inventory_item.item_id))
Ok(())
}
else {
Err(TradeError::ClientItemIdDidNotMatchItem(ClientItemId(item.item_id), trade_item_bytes).into())
@ -308,7 +333,7 @@ where
if real_item.as_client_bytes()[0..4] == trade_item_bytes[0..4] {
let amount = trade_item_bytes[5] as usize;
if amount <= stacked_inventory_item.entity_ids.len() {
Ok(TradeItem::Stacked(stacked_inventory_item.item_id, amount))
Ok(())
}
else {
Err(TradeError::InvalidStackAmount(stacked_inventory_item.item_id, amount).into())
@ -319,11 +344,20 @@ where
}
}
}
}
})
.collect::<Result<Vec<_>, anyhow::Error>>()?;
this.status = TradeStatus::ItemsChecked;
Ok(Box::new(std::iter::once((other.client(), SendShipPacket::AcknowledgeTrade(AcknowledgeTrade {})))))
if this.status == TradeStatus::ItemsChecked && other.status == TradeStatus::ItemsChecked {
Ok(Box::new(vec![
(this.client(), SendShipPacket::AcknowledgeTrade(AcknowledgeTrade {})),
(other.client(), SendShipPacket::AcknowledgeTrade(AcknowledgeTrade {})),
].into_iter()))
}
else {
Ok(Box::new(None.into_iter()))
}
})?
.unwrap_or_else(|err| {
log::warn!("trade error: {:?}", err);
@ -414,8 +448,8 @@ where
TradeReady::BothPlayers(room_id, (this_local_client, this_client, this), (other_local_client, other_client, other)) => {
let traded_items = item_manager.trade_items(entity_gateway,
room_id,
(&this_local_client, &this_client.character, &this.items),
(&other_local_client, &other_client.character, &other.items)).await?;
(&this_local_client, &this_client.character, &this.items, this.meseta),
(&other_local_client, &other_client.character, &other.items, other.meseta)).await?;
let clients_in_room = client_location.get_all_clients_by_client(id)?;
let traded_item_packets = traded_items
@ -434,6 +468,12 @@ where
GameMessage::PlayerNoLongerHasItem(builder::message::player_no_longer_has_item(item.remove_from, item.current_item_id, amount as u32))
]
},
ItemToTradeDetail::Meseta(amount) => {
[
GameMessage::CreateItem(builder::message::create_meseta(item.add_to, amount)),
GameMessage::PlayerNoLongerHasItem(builder::message::player_no_longer_has_item(item.remove_from, item.current_item_id, amount as u32))
]
},
}
})
.flatten()

7
src/ship/trade.rs

@ -5,13 +5,10 @@ use async_std::sync::{Arc, Mutex};
use crate::common::serverstate::ClientId;
use crate::ship::items;
pub const MESETA_ITEM_ID: items::ClientItemId = items::ClientItemId(0xFFFFFF01);
#[derive(Debug, Clone)]
pub enum TradeItem {
Individual(items::ClientItemId),
Stacked(items::ClientItemId, usize),
Meseta(usize),
}
impl TradeItem {
@ -26,7 +23,6 @@ impl TradeItem {
match self {
TradeItem::Individual(item_id) => *item_id,
TradeItem::Stacked(item_id, _) => *item_id,
TradeItem::Meseta(_) => MESETA_ITEM_ID,
}
}
}
@ -49,6 +45,7 @@ pub struct ClientTradeState {
client: ClientId,
other_client: ClientId,
pub items: Vec<TradeItem>,
pub meseta: usize,
pub status: TradeStatus,
}
@ -113,6 +110,7 @@ impl TradeState {
client: *sender,
other_client: *receiver,
items: Default::default(),
meseta: 0,
status: TradeStatus::SentRequest,
};
self.trades.insert(*sender, RefCell::new(state));
@ -121,6 +119,7 @@ impl TradeState {
client: *receiver,
other_client: *sender,
items: Default::default(),
meseta: 0,
status: TradeStatus::ReceivedRequest,
};
self.trades.insert(*receiver, RefCell::new(state));

Loading…
Cancel
Save