528 lines
24 KiB
Rust
Raw Normal View History

use libpso::packet::ship::*;
2020-04-22 07:14:59 -06:00
use libpso::packet::messages::*;
use entity::gateway::EntityGateway;
use entity::item::Meseta;
2020-04-22 07:14:59 -06:00
use crate::common::serverstate::ClientId;
2023-11-10 23:48:06 -07:00
use stats::leveltable::LEVEL_TABLE;
2022-10-18 04:46:21 -06:00
use crate::ship::ship::{SendShipPacket, ShipError, Clients, ItemDropLocation};
use crate::ship::room::Rooms;
2020-06-06 08:46:02 -06:00
use crate::ship::location::{ClientLocation, ClientLocationError};
2022-07-18 19:25:47 -06:00
use crate::ship::items::ClientItemId;
2020-05-14 23:38:18 -06:00
use crate::ship::packet::builder;
2022-04-30 11:41:24 -06:00
use crate::ship::items::state::ItemState;
2023-02-12 02:13:58 +00:00
use crate::ship::items::tasks::{drop_item, drop_partial_item, drop_meseta, equip_item, unequip_item, sort_inventory, use_item, feed_mag, sell_item, take_meseta, floor_item_limit_reached};
2020-04-22 07:14:59 -06:00
2022-10-18 04:46:21 -06:00
pub async fn request_exp<EG>(id: ClientId,
request_exp: RequestExp,
entity_gateway: &mut EG,
client_location: &ClientLocation,
clients: &Clients,
rooms: &Rooms)
2023-01-28 20:12:20 -07:00
-> Result<Vec<(ClientId, SendShipPacket)>, anyhow::Error>
2022-10-18 04:46:21 -06:00
where
EG: EntityGateway + Clone + 'static,
{
2022-09-18 21:01:32 -06:00
let area_client = client_location.get_local_client(id).await.map_err(|err| -> ClientLocationError { err.into() })?;
let room_id = client_location.get_room(id).await.map_err(|err| -> ClientLocationError { err.into() })?;
2020-06-05 22:12:44 -06:00
2022-10-18 04:46:21 -06:00
let enemy_id = request_exp.enemy_id as usize;
let enemy_exp = rooms.with(room_id, |room| Box::pin(async move {
let monster = room.maps.enemy_by_id(enemy_id)?;
2022-10-18 17:55:47 -06:00
let monster_stats = room.monster_stats.get(&monster.monster).ok_or_else(|| ShipError::UnknownMonster(monster.monster))?;
2023-01-28 20:12:20 -07:00
Ok::<_, anyhow::Error>(monster_stats.exp)
2022-10-18 04:46:21 -06:00
})).await??;
2020-06-05 22:12:44 -06:00
2020-06-05 22:19:20 -06:00
let exp_gain = if request_exp.last_hitter == 1 {
2022-10-18 04:46:21 -06:00
enemy_exp
2020-06-05 22:19:20 -06:00
}
else {
2022-10-18 04:46:21 -06:00
((enemy_exp as f32) * 0.8) as u32
2020-06-05 22:19:20 -06:00
};
2022-09-18 21:01:32 -06:00
let clients_in_area = client_location.get_clients_in_room(room_id).await.map_err(|err| -> ClientLocationError { err.into() })?;
2020-06-05 22:19:20 -06:00
let gain_exp_pkt = builder::message::character_gained_exp(area_client, exp_gain);
2022-10-18 04:46:21 -06:00
let mut exp_pkts: Vec<_> = clients_in_area.clone().into_iter()
2020-06-05 22:12:44 -06:00
.map(move |c| {
(c.client, SendShipPacket::Message(Message::new(GameMessage::GiveCharacterExp(gain_exp_pkt.clone()))))
2022-10-18 04:46:21 -06:00
})
.collect();
2020-06-05 22:12:44 -06:00
2022-10-18 04:46:21 -06:00
let (char_class, exp) = clients.with(id, |client| Box::pin(async move {
(client.character.char_class, client.character.exp)
})).await?;
let before_level = LEVEL_TABLE.get_level_from_exp(char_class, exp);
let after_level = LEVEL_TABLE.get_level_from_exp(char_class, exp + exp_gain);
2020-06-05 22:12:44 -06:00
let level_up = before_level != after_level;
if level_up {
2022-10-18 04:46:21 -06:00
let (_, before_stats) = LEVEL_TABLE.get_stats_from_exp(char_class, exp);
let (after_level, after_stats) = LEVEL_TABLE.get_stats_from_exp(char_class, exp + exp_gain);
2020-06-05 22:12:44 -06:00
2023-01-29 16:34:29 -07:00
let level_up_pkt = builder::message::character_leveled_up(area_client, after_level-1, before_stats, after_stats);
2022-10-18 04:46:21 -06:00
exp_pkts.extend(clients_in_area.into_iter()
.map(move |c| {
(c.client, SendShipPacket::Message(Message::new(GameMessage::PlayerLevelUp(level_up_pkt.clone()))))
}));
2020-06-05 22:12:44 -06:00
}
2022-10-18 04:46:21 -06:00
clients.with_mut(id, |client| {
let mut entity_gateway = entity_gateway.clone();
Box::pin(async move {
client.character.exp += exp_gain;
entity_gateway.save_character(&client.character).await
})}).await??;
2020-06-05 22:12:44 -06:00
Ok(exp_pkts)
2020-04-22 07:14:59 -06:00
}
2020-06-02 18:51:18 -06:00
pub async fn player_drop_item<EG>(id: ClientId,
2022-10-18 04:46:21 -06:00
player_drop_item: PlayerDropItem,
entity_gateway: &mut EG,
2022-04-30 11:41:24 -06:00
client_location: &ClientLocation,
2022-10-18 04:46:21 -06:00
clients: &Clients,
rooms: &Rooms,
2022-04-30 11:41:24 -06:00
item_state: &mut ItemState)
2023-01-28 20:12:20 -07:00
-> Result<Vec<(ClientId, SendShipPacket)>, anyhow::Error>
where
2022-10-18 04:46:21 -06:00
EG: EntityGateway + Clone + 'static,
{
2022-09-18 21:01:32 -06:00
let room_id = client_location.get_room(id).await.map_err(|err| -> ClientLocationError { err.into() })?;
2022-10-18 04:46:21 -06:00
let map_area = rooms.with(room_id, |room| Box::pin(async move {
room.map_areas.get_area_map(player_drop_item.map_area)
})).await??;
clients.with(id, |client| {
let mut entity_gateway = entity_gateway.clone();
let mut item_state = item_state.clone();
Box::pin(async move {
drop_item(&mut item_state, &mut entity_gateway, &client.character, &ClientItemId(player_drop_item.item_id), map_area, (player_drop_item.x, player_drop_item.y, player_drop_item.z)).await
})}).await??;
2022-09-18 21:01:32 -06:00
let clients_in_area = client_location.get_clients_in_room(room_id).await.map_err(|err| -> ClientLocationError { err.into() })?;
let pdi = player_drop_item.clone();
2022-10-18 04:46:21 -06:00
Ok(clients_in_area.into_iter()
.map(move |c| {
(c.client, SendShipPacket::Message(Message::new(GameMessage::PlayerDropItem(pdi.clone()))))
})
.collect())
}
2020-05-14 23:38:18 -06:00
2022-09-18 21:01:32 -06:00
pub async fn drop_coordinates(id: ClientId,
2022-10-18 04:46:21 -06:00
drop_coordinates: DropCoordinates,
2022-09-18 21:01:32 -06:00
client_location: &ClientLocation,
2022-10-18 04:46:21 -06:00
clients: &Clients,
2022-09-18 21:01:32 -06:00
rooms: &Rooms)
2023-01-28 20:12:20 -07:00
-> Result<Vec<(ClientId, SendShipPacket)>, anyhow::Error>
2020-05-14 23:38:18 -06:00
{
2022-09-18 21:01:32 -06:00
let room_id = client_location.get_room(id).await.map_err(|err| -> ClientLocationError { err.into() })?;
2022-10-18 04:46:21 -06:00
let map_area = rooms.with(room_id, |room| Box::pin(async move {
room.map_areas.get_area_map(drop_coordinates.map_area)
})).await??;
clients.with_mut(id, |client| Box::pin(async move {
client.item_drop_location = Some(ItemDropLocation {
map_area,
x: drop_coordinates.x,
z: drop_coordinates.z,
item_id: ClientItemId(drop_coordinates.item_id),
});
})).await?;
Ok(Vec::new()) // TODO: do we need to send a packet here?
2020-05-14 23:38:18 -06:00
}
2020-12-03 15:04:48 -07:00
pub async fn no_longer_has_item<EG>(id: ClientId,
2022-10-18 04:46:21 -06:00
no_longer_has_item: PlayerNoLongerHasItem,
entity_gateway: &mut EG,
2020-12-03 15:04:48 -07:00
client_location: &ClientLocation,
2022-10-18 04:46:21 -06:00
clients: &Clients,
2022-04-30 21:40:46 -06:00
item_state: &mut ItemState)
2023-01-28 20:12:20 -07:00
-> Result<Vec<(ClientId, SendShipPacket)>, anyhow::Error>
2020-05-14 23:38:18 -06:00
where
2022-10-18 04:46:21 -06:00
EG: EntityGateway + Clone + 'static,
2020-05-14 23:38:18 -06:00
{
2022-10-18 04:46:21 -06:00
//let client = clients.get_mut(&id).ok_or(ShipError::ClientNotFound(id))?;
2022-09-18 21:01:32 -06:00
let area_client = client_location.get_local_client(id).await.map_err(|err| -> ClientLocationError { err.into() })?;
let room_id = client_location.get_room(id).await.map_err(|err| -> ClientLocationError { err.into() })?;
2022-10-18 04:46:21 -06:00
let (drop_location, tek) = clients.with(id, |client| Box::pin(async move {
(client.item_drop_location, client.tek)
})).await?;
if let Some(drop_location) = drop_location {
2020-12-03 15:04:48 -07:00
if drop_location.item_id.0 != no_longer_has_item.item_id {
2023-01-28 20:12:20 -07:00
return Err(ShipError::DropInvalidItemId(no_longer_has_item.item_id).into());
2020-12-03 15:04:48 -07:00
}
if no_longer_has_item.item_id == 0xFFFFFFFF {
2022-10-18 04:46:21 -06:00
let dropped_meseta = clients.with_mut(id, |client| {
let mut entity_gateway = entity_gateway.clone();
let mut item_state = item_state.clone();
Box::pin(async move {
client.item_drop_location = None;
drop_meseta(&mut item_state, &mut entity_gateway, &client.character, drop_location.map_area, (drop_location.x, drop_location.z), no_longer_has_item.amount).await
})}).await??;
2020-12-03 15:04:48 -07:00
let dropped_meseta_pkt = builder::message::drop_split_meseta_stack(area_client, &dropped_meseta)?;
2022-10-18 17:55:47 -06:00
let no_longer_has_meseta_pkt = builder::message::player_no_longer_has_meseta(area_client, no_longer_has_item.amount);
2020-12-03 15:04:48 -07:00
2022-09-18 21:01:32 -06:00
let clients_in_area = client_location.get_clients_in_room(room_id).await.map_err(|err| -> ClientLocationError { err.into() })?;
2022-10-18 04:46:21 -06:00
Ok(clients_in_area.into_iter()
.flat_map(move |c| {
std::iter::once((c.client, SendShipPacket::Message(Message::new(GameMessage::DropSplitStack(dropped_meseta_pkt.clone())))))
.chain(
if c.client != id {
Box::new(std::iter::once(
(c.client, SendShipPacket::Message(Message::new(GameMessage::PlayerNoLongerHasItem(no_longer_has_meseta_pkt.clone()))))
)) as Box<dyn Iterator<Item = _> + Send>
}
else {
Box::new(std::iter::empty()) as Box<dyn Iterator<Item = _> + Send>
}
)
})
.collect()
)
2020-12-03 15:04:48 -07:00
}
else {
2022-10-18 04:46:21 -06:00
let dropped_item = clients.with_mut(id, |client| {
let mut entity_gateway = entity_gateway.clone();
let mut item_state = item_state.clone();
Box::pin(async move {
client.item_drop_location = None;
drop_partial_item(&mut item_state,
&mut entity_gateway,
&client.character,
&drop_location.item_id,
drop_location.map_area,
(drop_location.x, drop_location.z),
no_longer_has_item.amount)
.await
})}).await??;
2022-04-30 21:40:46 -06:00
let dropped_item_pkt = builder::message::drop_split_stack(area_client, &dropped_item)?;
2020-12-03 15:04:48 -07:00
2022-09-18 21:01:32 -06:00
let clients_in_area = client_location.get_clients_in_room(room_id).await.map_err(|err| -> ClientLocationError { err.into() })?;
2022-10-18 04:46:21 -06:00
Ok(clients_in_area.into_iter()
.map(move |c| {
(c.client, SendShipPacket::Message(Message::new(GameMessage::DropSplitStack(dropped_item_pkt.clone()))))
})
.collect())
2020-12-03 15:04:48 -07:00
}
2020-05-14 23:38:18 -06:00
}
2022-10-18 04:46:21 -06:00
else if let Some(_tek) = tek {
2022-09-18 21:01:32 -06:00
let neighbors = client_location.get_client_neighbors(id).await.map_err(|err| -> ClientLocationError { err.into() })?;
2020-12-03 15:04:48 -07:00
let no_longer_has_item = no_longer_has_item.clone();
2022-10-18 04:46:21 -06:00
Ok(neighbors.into_iter()
.map(move |c| {
(c.client, SendShipPacket::Message(Message::new(GameMessage::PlayerNoLongerHasItem(no_longer_has_item.clone()))))
})
.collect())
2020-05-14 23:38:18 -06:00
}
else {
2023-01-28 20:12:20 -07:00
Err(ShipError::InvalidItem(ClientItemId(no_longer_has_item.item_id)).into())
2020-05-14 23:38:18 -06:00
}
}
2020-06-07 18:33:36 -03:00
2022-09-18 21:01:32 -06:00
pub async fn update_player_position(id: ClientId,
2022-10-18 04:46:21 -06:00
message: Message,
clients: &Clients,
2022-09-18 21:01:32 -06:00
client_location: &ClientLocation,
rooms: &Rooms)
2023-01-28 20:12:20 -07:00
-> Result<Vec<(ClientId, SendShipPacket)>, anyhow::Error> {
2022-09-18 21:01:32 -06:00
if let Ok(room_id) = client_location.get_room(id).await.map_err(|err| -> ClientLocationError { err.into() }) {
2022-10-18 04:46:21 -06:00
let msg = message.msg.clone();
clients.with_mut(id, |client| {
let rooms = rooms.clone();
Box::pin(async move {
match msg {
GameMessage::PlayerChangedMap(p) => {
client.x = p.x;
client.y = p.y;
client.z = p.z;
},
GameMessage::PlayerChangedMap2(p) => {
client.area = rooms.with(room_id, |room| Box::pin(async move {
room.map_areas.get_area_map(p.map_area).ok()
})).await?;
},
GameMessage::TellOtherPlayerMyLocation(p) => {
client.x = p.x;
client.y = p.y;
client.z = p.z;
client.area = rooms.with(room_id, |room| Box::pin(async move {
room.map_areas.get_area_map(p.map_area).ok()
})).await?;
},
GameMessage::PlayerWarpingToFloor(p) => {
client.area = rooms.with(room_id, |room| Box::pin(async move {
room.map_areas.get_area_map(p.area as u16).ok()
})).await?;
},
GameMessage::PlayerTeleported(p) => {
client.x = p.x;
client.y = p.y;
client.z = p.z;
},
GameMessage::PlayerStopped(p) => {
client.x = p.x;
client.y = p.y;
client.z = p.z;
},
GameMessage::PlayerLoadedIn(p) => {
client.x = p.x;
client.y = p.y;
client.z = p.z;
},
GameMessage::PlayerWalking(p) => {
client.x = p.x;
client.z = p.z;
},
GameMessage::PlayerRunning(p) => {
client.x = p.x;
client.z = p.z;
},
GameMessage::PlayerWarped(p) => {
client.x = p.x;
client.y = p.y;
},
// GameMessage::PlayerChangedFloor(p) => {client.area = MapArea::from_value(&room.mode.episode(), p.map).ok();},
GameMessage::InitializeSpeechNpc(p) => {
client.x = p.x;
client.z = p.z;
}
_ => {},
}
2023-01-28 20:12:20 -07:00
Ok::<_, anyhow::Error>(())
2022-10-18 17:55:47 -06:00
})}).await??;
2022-10-18 04:46:21 -06:00
}
Ok(client_location.get_client_neighbors(id).await?.into_iter()
.map(move |client| {
(client.client, SendShipPacket::Message(message.clone()))
})
.collect())
}
pub async fn charge_attack<EG>(id: ClientId,
2022-10-18 04:46:21 -06:00
charge: ChargeAttack,
entity_gateway: &mut EG,
2022-07-18 19:25:47 -06:00
client_location: &ClientLocation,
2022-10-18 04:46:21 -06:00
clients: &Clients,
2022-07-18 19:25:47 -06:00
item_state: &mut ItemState)
2023-01-28 20:12:20 -07:00
-> Result<Vec<(ClientId, SendShipPacket)>, anyhow::Error>
where
2022-10-18 04:46:21 -06:00
EG: EntityGateway + Clone + 'static,
{
2022-10-18 04:46:21 -06:00
let meseta = charge.meseta;
clients.with(id, |client| {
let mut entity_gateway = entity_gateway.clone();
let mut item_state = item_state.clone();
Box::pin(async move {
// TODO: should probably validate this to be a legit number, I'd just hardcode 200 but vjaya
take_meseta(&mut item_state, &mut entity_gateway, &client.character.id, Meseta(meseta)).await
})}).await??;
Ok(client_location.get_client_neighbors(id).await.unwrap().into_iter()
.map(move |client| {
(client.client, SendShipPacket::Message(Message::new(GameMessage::ChargeAttack(charge.clone()))))
})
.collect())
}
2022-05-27 01:38:49 -06:00
pub async fn player_uses_item<EG>(id: ClientId,
2022-10-18 04:46:21 -06:00
player_use_tool: PlayerUseItem,
entity_gateway: &mut EG,
client_location: &ClientLocation,
2022-10-18 04:46:21 -06:00
clients: &Clients,
item_state: &mut ItemState)
2023-01-28 20:12:20 -07:00
-> Result<Vec<(ClientId, SendShipPacket)>, anyhow::Error>
where
2022-10-18 04:46:21 -06:00
EG: EntityGateway + Clone + 'static,
{
let neighbors = client_location.get_all_clients_by_client(id).await?.into_iter();
let area_client = client_location.get_local_client(id).await?;
Ok(clients.with_mut(id, |client| {
2022-10-18 04:46:21 -06:00
let mut entity_gateway = entity_gateway.clone();
let mut item_state = item_state.clone();
Box::pin(async move {
use_item(&mut item_state, &mut entity_gateway, &mut client.character, area_client, &ClientItemId(player_use_tool.item_id), 1).await
})}).await??
.into_iter()
.flat_map(move |pkt| {
let player_use_tool = player_use_tool.clone();
neighbors.clone().map(move |client| {
vec![(client.client, SendShipPacket::Message(Message::new(GameMessage::PlayerUseItem(player_use_tool.clone())))), (client.client, pkt.clone())]
})
})
.flatten()
.collect::<Vec<_>>()
)
}
2020-08-11 17:17:24 -03:00
pub async fn player_used_medical_center<EG>(id: ClientId,
2022-10-18 04:46:21 -06:00
pumc: PlayerUsedMedicalCenter,
entity_gateway: &mut EG,
2022-07-18 19:25:47 -06:00
client_location: &ClientLocation,
2022-10-18 04:46:21 -06:00
clients: &Clients,
2022-07-18 19:25:47 -06:00
item_state: &mut ItemState)
2023-01-28 20:12:20 -07:00
-> Result<Vec<(ClientId, SendShipPacket)>, anyhow::Error>
2020-08-11 17:17:24 -03:00
where
2022-10-18 04:46:21 -06:00
EG: EntityGateway + Clone + 'static,
2020-08-11 17:17:24 -03:00
{
2022-10-18 04:46:21 -06:00
clients.with(id, |client| {
let mut entity_gateway = entity_gateway.clone();
let mut item_state = item_state.clone();
Box::pin(async move {
take_meseta(&mut item_state, &mut entity_gateway, &client.character.id, Meseta(10)).await
})}).await??;
2022-07-18 19:25:47 -06:00
let pumc = pumc.clone();
2022-10-18 04:46:21 -06:00
Ok(client_location.get_client_neighbors(id).await.unwrap().into_iter()
.map(move |client| {
(client.client, SendShipPacket::Message(Message::new(GameMessage::PlayerUsedMedicalCenter(pumc.clone()))))
})
.collect())
2020-08-31 23:46:15 -06:00
}
pub async fn player_feed_mag<EG>(id: ClientId,
2022-10-18 04:46:21 -06:00
mag_feed: PlayerFeedMag,
entity_gateway: &mut EG,
2020-08-31 23:46:15 -06:00
client_location: &ClientLocation,
clients: &Clients,
2022-06-20 15:40:30 -06:00
item_state: &mut ItemState)
2023-01-28 20:12:20 -07:00
-> Result<Vec<(ClientId, SendShipPacket)>, anyhow::Error>
2020-08-31 23:46:15 -06:00
where
2022-10-18 04:46:21 -06:00
EG: EntityGateway + Clone + 'static,
2020-08-31 23:46:15 -06:00
{
2022-10-18 04:46:21 -06:00
let cmag_feed = mag_feed.clone();
clients.with(id, |client| {
let mut entity_gateway = entity_gateway.clone();
let mut item_state = item_state.clone();
Box::pin(async move {
feed_mag(&mut item_state, &mut entity_gateway, &client.character, &ClientItemId(cmag_feed.mag_id), &ClientItemId(cmag_feed.item_id)).await
})}).await??;
Ok(client_location.get_client_neighbors(id).await.unwrap().into_iter()
.map(move |client| {
(client.client, SendShipPacket::Message(Message::new(GameMessage::PlayerFeedMag(mag_feed.clone()))))
})
.collect())
2020-08-31 23:46:15 -06:00
}
2020-10-06 21:35:20 -03:00
pub async fn player_equips_item<EG>(id: ClientId,
2022-10-18 04:46:21 -06:00
pkt: PlayerEquipItem,
entity_gateway: &mut EG,
2020-10-06 21:35:20 -03:00
clients: &Clients,
2022-05-15 00:34:06 -06:00
item_state: &mut ItemState)
2023-01-28 20:12:20 -07:00
-> Result<Vec<(ClientId, SendShipPacket)>, anyhow::Error>
2020-10-06 21:35:20 -03:00
where
2022-10-18 04:46:21 -06:00
EG: EntityGateway + Clone + 'static,
2020-10-06 21:35:20 -03:00
{
2020-11-09 16:47:35 -07:00
let equip_slot = if pkt.sub_menu > 0 {
((pkt.sub_menu & 0x7) - 1) % 4
}
else {
0
};
2022-10-18 04:46:21 -06:00
clients.with(id, |client| {
let mut entity_gateway = entity_gateway.clone();
let mut item_state = item_state.clone();
Box::pin(async move {
equip_item(&mut item_state, &mut entity_gateway, &client.character, &ClientItemId(pkt.item_id), equip_slot).await
})}).await??;
Ok(Vec::new()) // TODO: tell other players you equipped an item
2020-10-06 21:35:20 -03:00
}
pub async fn player_unequips_item<EG>(id: ClientId,
2022-10-18 04:46:21 -06:00
pkt: PlayerUnequipItem,
entity_gateway: &mut EG,
clients: &Clients,
item_state: &mut ItemState)
2023-01-28 20:12:20 -07:00
-> Result<Vec<(ClientId, SendShipPacket)>, anyhow::Error>
2020-10-06 21:35:20 -03:00
where
2022-10-18 04:46:21 -06:00
EG: EntityGateway + Clone + 'static,
2020-10-06 21:35:20 -03:00
{
2022-10-18 04:46:21 -06:00
clients.with(id, |client| {
let mut entity_gateway = entity_gateway.clone();
let mut item_state = item_state.clone();
Box::pin(async move {
unequip_item(&mut item_state, &mut entity_gateway, &client.character, &ClientItemId(pkt.item_id)).await
})}).await??;
Ok(Vec::new()) // TODO: tell other players if you unequip an item
2020-10-26 00:02:48 -06:00
}
pub async fn player_sorts_items<EG>(id: ClientId,
2022-10-18 04:46:21 -06:00
pkt: SortItems,
entity_gateway: &mut EG,
clients: &Clients,
2022-05-15 18:59:49 -06:00
item_state: &mut ItemState)
2023-01-28 20:12:20 -07:00
-> Result<Vec<(ClientId, SendShipPacket)>, anyhow::Error>
where
2022-10-18 04:46:21 -06:00
EG: EntityGateway + Clone + 'static,
{
2022-05-15 18:59:49 -06:00
let item_ids = pkt.item_ids
.iter()
.filter_map(|item_id| {
if *item_id != 0 {
Some(ClientItemId(*item_id))
}
else {
None
}
})
.collect();
2022-10-18 04:46:21 -06:00
clients.with(id, |client| {
let mut entity_gateway = entity_gateway.clone();
let mut item_state = item_state.clone();
Box::pin(async move {
sort_inventory(&mut item_state, &mut entity_gateway, &client.character, item_ids).await
})}).await??;
Ok(Vec::new()) // TODO: clients probably care about each others item orders
}
pub async fn player_sells_item<EG> (id: ClientId,
2022-10-18 04:46:21 -06:00
sold_item: PlayerSoldItem,
entity_gateway: &mut EG,
clients: &Clients,
2022-06-21 00:09:52 -06:00
item_state: &mut ItemState)
2023-01-28 20:12:20 -07:00
-> Result<Vec<(ClientId, SendShipPacket)>, anyhow::Error>
where
2022-10-18 04:46:21 -06:00
EG: EntityGateway + Clone + 'static,
{
2022-10-18 04:46:21 -06:00
clients.with(id, |client| {
let mut entity_gateway = entity_gateway.clone();
let mut item_state = item_state.clone();
Box::pin(async move {
sell_item(&mut item_state, &mut entity_gateway, &client.character, ClientItemId(sold_item.item_id), sold_item.amount as u32).await
})}).await??;
Ok(Vec::new()) // TODO: send the packet to other clients
2020-11-05 16:36:39 -07:00
}
2023-02-12 02:13:58 +00:00
pub async fn floor_item_limit_deletion<EG> (id: ClientId,
floor_item_limit_delete: FloorItemLimitItemDeletion,
entity_gateway: &mut EG,
client_location: &ClientLocation,
clients: &Clients,
rooms: &Rooms,
item_state: &mut ItemState)
-> Result<Vec<(ClientId, SendShipPacket)>, anyhow::Error>
where
EG: EntityGateway + Clone + 'static,
{
let room_id = client_location.get_room(id).await.map_err(|err| -> ClientLocationError { err.into() })?;
let map_area = rooms.with(room_id, |room| Box::pin(async move {
room.map_areas.get_area_map(floor_item_limit_delete.map_area)
})).await??;
clients.with(id, |client| {
let mut entity_gateway = entity_gateway.clone();
2023-02-12 14:32:12 +00:00
let item_state = item_state.clone();
2023-02-12 02:13:58 +00:00
Box::pin(async move {
2023-02-12 14:32:12 +00:00
floor_item_limit_reached(&item_state, &mut entity_gateway, &client.character, &ClientItemId(floor_item_limit_delete.item_id), map_area).await
2023-02-12 02:13:58 +00:00
})}).await??;
Ok(Vec::new())
2023-02-12 14:32:12 +00:00
}