372 lines
17 KiB
Rust
372 lines
17 KiB
Rust
use libpso::packet::ship::*;
|
|
use libpso::packet::messages::*;
|
|
use crate::entity::gateway::EntityGateway;
|
|
use crate::entity::item::{ItemType};
|
|
use crate::common::serverstate::ClientId;
|
|
use crate::common::leveltable::CharacterLevelTable;
|
|
use crate::ship::ship::{SendShipPacket, ShipError, Rooms, Clients, ItemDropLocation};
|
|
use crate::ship::location::{ClientLocation, ClientLocationError};
|
|
use crate::ship::items::{ItemManager, ClientItemId, InventoryItem};
|
|
use crate::ship::packet::builder;
|
|
|
|
pub async fn request_exp<EG: EntityGateway>(id: ClientId,
|
|
request_exp: &RequestExp,
|
|
entity_gateway: &mut EG,
|
|
client_location: &ClientLocation,
|
|
clients: &mut Clients,
|
|
rooms: &mut Rooms,
|
|
level_table: &CharacterLevelTable)
|
|
-> Result<Box<dyn Iterator<Item = (ClientId, SendShipPacket)> + Send>, ShipError> {
|
|
let client = clients.get_mut(&id).ok_or(ShipError::ClientNotFound(id))?;
|
|
let area_client = client_location.get_local_client(id).map_err(|err| -> ClientLocationError { err.into() })?;
|
|
let room_id = client_location.get_room(id).map_err(|err| -> ClientLocationError { err.into() })?;
|
|
let room = rooms.get_mut(room_id.0)
|
|
.ok_or_else(|| ShipError::InvalidRoom(room_id.0 as u32))?
|
|
.as_mut()
|
|
.ok_or_else(|| ShipError::InvalidRoom(room_id.0 as u32))?;
|
|
|
|
let monster = room.maps.enemy_by_id(request_exp.enemy_id as usize)?;
|
|
let monster_stats = room.monster_stats.get(&monster.monster).unwrap();
|
|
|
|
let exp_gain = if request_exp.last_hitter == 1 {
|
|
monster_stats.exp
|
|
}
|
|
else {
|
|
((monster_stats.exp as f32) * 0.8) as u32
|
|
};
|
|
|
|
let clients_in_area = client_location.get_clients_in_room(room_id).map_err(|err| -> ClientLocationError { err.into() })?;
|
|
let gain_exp_pkt = builder::message::character_gained_exp(area_client, exp_gain);
|
|
let mut exp_pkts: Box<dyn Iterator<Item = _> + Send> = Box::new(clients_in_area.clone().into_iter()
|
|
.map(move |c| {
|
|
(c.client, SendShipPacket::Message(Message::new(GameMessage::GiveCharacterExp(gain_exp_pkt.clone()))))
|
|
}));
|
|
|
|
let before_level = level_table.get_level_from_exp(client.character.char_class, client.character.exp);
|
|
let after_level = level_table.get_level_from_exp(client.character.char_class, client.character.exp + exp_gain);
|
|
let level_up = before_level != after_level;
|
|
|
|
if level_up {
|
|
let (_, before_stats) = level_table.get_stats_from_exp(client.character.char_class, client.character.exp);
|
|
let (after_level, after_stats) = level_table.get_stats_from_exp(client.character.char_class, client.character.exp + exp_gain);
|
|
|
|
let level_up_pkt = builder::message::character_leveled_up(area_client, after_level, before_stats, after_stats);
|
|
exp_pkts = Box::new(exp_pkts.chain(clients_in_area.into_iter()
|
|
.map(move |c| {
|
|
(c.client, SendShipPacket::Message(Message::new(GameMessage::PlayerLevelUp(level_up_pkt.clone()))))
|
|
})))
|
|
}
|
|
|
|
client.character.exp += exp_gain;
|
|
entity_gateway.save_character(&client.character).await?;
|
|
|
|
Ok(exp_pkts)
|
|
}
|
|
|
|
pub async fn player_drop_item<EG>(id: ClientId,
|
|
player_drop_item: &PlayerDropItem,
|
|
entity_gateway: &mut EG,
|
|
client_location: &ClientLocation,
|
|
clients: &mut Clients,
|
|
rooms: &mut Rooms,
|
|
item_manager: &mut ItemManager)
|
|
-> Result<Box<dyn Iterator<Item = (ClientId, SendShipPacket)> + Send>, ShipError>
|
|
where
|
|
EG: EntityGateway
|
|
{
|
|
let client = clients.get_mut(&id).ok_or(ShipError::ClientNotFound(id))?;
|
|
let room_id = client_location.get_room(id).map_err(|err| -> ClientLocationError { err.into() })?;
|
|
let room = rooms.get_mut(room_id.0)
|
|
.ok_or_else(|| ShipError::InvalidRoom(room_id.0 as u32))?
|
|
.as_mut()
|
|
.ok_or_else(|| ShipError::InvalidRoom(room_id.0 as u32))?;
|
|
let area = room.map_areas.get_area_map(player_drop_item.map_area)?;
|
|
item_manager.player_drop_item_on_shared_floor(entity_gateway, &client.character, ClientItemId(player_drop_item.item_id), (*area, player_drop_item.x, player_drop_item.y, player_drop_item.z)).await?;
|
|
let clients_in_area = client_location.get_clients_in_room(room_id).map_err(|err| -> ClientLocationError { err.into() })?;
|
|
let pdi = player_drop_item.clone();
|
|
Ok(Box::new(clients_in_area.into_iter()
|
|
.map(move |c| {
|
|
(c.client, SendShipPacket::Message(Message::new(GameMessage::PlayerDropItem(pdi.clone()))))
|
|
})))
|
|
}
|
|
|
|
pub fn drop_coordinates(id: ClientId,
|
|
drop_coordinates: &DropCoordinates,
|
|
client_location: &ClientLocation,
|
|
clients: &mut Clients,
|
|
rooms: &Rooms)
|
|
-> Result<Box<dyn Iterator<Item = (ClientId, SendShipPacket)> + Send>, ShipError>
|
|
{
|
|
let client = clients.get_mut(&id).ok_or(ShipError::ClientNotFound(id))?;
|
|
let room_id = client_location.get_room(id).map_err(|err| -> ClientLocationError { err.into() })?;
|
|
let room = rooms.get(room_id.0)
|
|
.ok_or_else(|| ShipError::InvalidRoom(room_id.0 as u32))?
|
|
.as_ref()
|
|
.ok_or_else(|| ShipError::InvalidRoom(room_id.0 as u32))?;
|
|
|
|
client.item_drop_location = Some(ItemDropLocation {
|
|
map_area: *room.map_areas.get_area_map(drop_coordinates.map_area)?,
|
|
x: drop_coordinates.x,
|
|
z: drop_coordinates.z,
|
|
item_id: ClientItemId(drop_coordinates.item_id),
|
|
});
|
|
|
|
Ok(Box::new(None.into_iter()))
|
|
}
|
|
|
|
pub async fn split_item_stack<EG>(id: ClientId,
|
|
no_longer_has_item: &PlayerNoLongerHasItem,
|
|
entity_gateway: &mut EG,
|
|
client_location: &ClientLocation,
|
|
clients: &mut Clients,
|
|
item_manager: &mut ItemManager)
|
|
-> Result<Box<dyn Iterator<Item = (ClientId, SendShipPacket)> + Send>, ShipError>
|
|
where
|
|
EG: EntityGateway
|
|
{
|
|
let client = clients.get_mut(&id).ok_or(ShipError::ClientNotFound(id))?;
|
|
let area_client = client_location.get_local_client(id).map_err(|err| -> ClientLocationError { err.into() })?;
|
|
let room_id = client_location.get_room(id).map_err(|err| -> ClientLocationError { err.into() })?;
|
|
let drop_location = client.item_drop_location.ok_or(ShipError::ItemDropLocationNotSet)?;
|
|
|
|
if drop_location.item_id.0 != no_longer_has_item.item_id {
|
|
return Err(ShipError::DropInvalidItemId(no_longer_has_item.item_id));
|
|
}
|
|
|
|
if no_longer_has_item.item_id == 0xFFFFFFFF {
|
|
let dropped_meseta = item_manager.player_drops_meseta_on_shared_floor(entity_gateway, &mut client.character, drop_location, no_longer_has_item.amount as u32).await?;
|
|
|
|
let dropped_meseta_pkt = builder::message::drop_split_meseta_stack(area_client, &dropped_meseta)?;
|
|
client.item_drop_location = None;
|
|
|
|
let clients_in_area = client_location.get_clients_in_room(room_id).map_err(|err| -> ClientLocationError { err.into() })?;
|
|
Ok(Box::new(clients_in_area.into_iter()
|
|
.map(move |c| {
|
|
(c.client, SendShipPacket::Message(Message::new(GameMessage::DropSplitStack(dropped_meseta_pkt.clone()))))
|
|
})))
|
|
}
|
|
else {
|
|
let dropped_item = item_manager.player_drops_partial_stack_on_shared_floor(entity_gateway, &client.character, drop_location.item_id, drop_location, no_longer_has_item.amount as usize).await?;
|
|
|
|
let dropped_item_pkt = builder::message::drop_split_stack(area_client, dropped_item)?;
|
|
client.item_drop_location = None;
|
|
|
|
let clients_in_area = client_location.get_clients_in_room(room_id).map_err(|err| -> ClientLocationError { err.into() })?;
|
|
Ok(Box::new(clients_in_area.into_iter()
|
|
.map(move |c| {
|
|
(c.client, SendShipPacket::Message(Message::new(GameMessage::DropSplitStack(dropped_item_pkt.clone()))))
|
|
})))
|
|
}
|
|
}
|
|
|
|
pub fn update_player_position(id: ClientId,
|
|
message: &Message,
|
|
clients: &mut Clients,
|
|
client_location: &ClientLocation,
|
|
rooms: &Rooms)
|
|
-> Result<Box<dyn Iterator<Item = (ClientId, SendShipPacket)> + Send>, ShipError> {
|
|
let client = clients.get_mut(&id).ok_or(ShipError::ClientNotFound(id))?;
|
|
if let Ok(room_id) = client_location.get_room(id).map_err(|err| -> ClientLocationError { err.into() }) {
|
|
let room = rooms.get(room_id.0)
|
|
.ok_or_else(|| ShipError::InvalidRoom(room_id.0 as u32))?
|
|
.as_ref()
|
|
.ok_or_else(|| ShipError::InvalidRoom(room_id.0 as u32))?;
|
|
|
|
match &message.msg {
|
|
GameMessage::PlayerChangedMap(p) => {
|
|
client.x = p.x;
|
|
client.y = p.y;
|
|
client.z = p.z;
|
|
},
|
|
GameMessage::PlayerChangedMap2(p) => {
|
|
client.area = room.map_areas.get_area_map(p.map_area).ok().cloned();
|
|
},
|
|
GameMessage::TellOtherPlayerMyLocation(p) => {
|
|
client.x = p.x;
|
|
client.y = p.y;
|
|
client.z = p.z;
|
|
client.area = room.map_areas.get_area_map(p.map_area).ok().cloned();
|
|
},
|
|
GameMessage::PlayerWarpingToFloor(p) => {
|
|
client.area = room.map_areas.get_area_map(p.area as u16).ok().cloned();
|
|
},
|
|
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;
|
|
}
|
|
_ => {},
|
|
}
|
|
} else {}
|
|
let m = message.clone();
|
|
Ok(Box::new(client_location.get_client_neighbors(id).unwrap().into_iter()
|
|
.map(move |client| {
|
|
(client.client, SendShipPacket::Message(m.clone()))
|
|
})))
|
|
}
|
|
|
|
pub async fn charge_attack<EG>(id: ClientId,
|
|
charge: &ChargeAttack,
|
|
clients: &mut Clients,
|
|
entity_gateway: &mut EG)
|
|
-> Result<Box<dyn Iterator<Item = (ClientId, SendShipPacket)> + Send>, ShipError>
|
|
where
|
|
EG: EntityGateway
|
|
{
|
|
let client = clients.get_mut(&id).ok_or(ShipError::ClientNotFound(id))?;
|
|
if client.character.meseta >= charge.meseta {
|
|
client.character.meseta -= charge.meseta;
|
|
entity_gateway.save_character(&client.character).await?;
|
|
Ok(Box::new(None.into_iter()))
|
|
} else {
|
|
Err(ShipError::NotEnoughMeseta(id, client.character.meseta))
|
|
}
|
|
}
|
|
|
|
pub async fn use_item<EG>(id: ClientId,
|
|
player_use_tool: &PlayerUseItem,
|
|
entity_gateway: &mut EG,
|
|
_client_location: &ClientLocation,
|
|
clients: &mut Clients,
|
|
item_manager: &mut ItemManager)
|
|
-> Result<Box<dyn Iterator<Item = (ClientId, SendShipPacket)> + Send>, ShipError>
|
|
where
|
|
EG: EntityGateway
|
|
{
|
|
let client = clients.get_mut(&id).ok_or(ShipError::ClientNotFound(id))?;
|
|
let item_used_type = item_manager.player_consumes_tool(entity_gateway, &client.character, ClientItemId(player_use_tool.item_id), 1).await?;
|
|
|
|
item_manager.use_item(item_used_type, entity_gateway, &mut client.character).await?;
|
|
Ok(Box::new(None.into_iter()))
|
|
}
|
|
|
|
pub async fn player_used_medical_center<EG>(id: ClientId,
|
|
_pumc: &PlayerUsedMedicalCenter, // not needed?
|
|
entity_gateway: &mut EG,
|
|
clients: &mut Clients)
|
|
-> Result<Box<dyn Iterator<Item = (ClientId, SendShipPacket)> + Send>, ShipError>
|
|
where
|
|
EG: EntityGateway
|
|
{
|
|
let client = clients.get_mut(&id).ok_or(ShipError::ClientNotFound(id))?;
|
|
if client.character.meseta >= 10 {
|
|
client.character.meseta -= 10;
|
|
entity_gateway.save_character(&client.character).await?;
|
|
Ok(Box::new(None.into_iter()))
|
|
} else {
|
|
Err(ShipError::NotEnoughMeseta(id, client.character.meseta))
|
|
}
|
|
}
|
|
|
|
|
|
pub async fn player_feed_mag<EG>(id: ClientId,
|
|
mag_feed: &PlayerFeedMag,
|
|
entity_gateway: &mut EG,
|
|
client_location: &ClientLocation,
|
|
clients: &Clients,
|
|
item_manager: &mut ItemManager)
|
|
-> Result<Box<dyn Iterator<Item = (ClientId, SendShipPacket)> + Send>, ShipError>
|
|
where
|
|
EG: EntityGateway
|
|
{
|
|
let client = clients.get(&id).ok_or(ShipError::ClientNotFound(id))?;
|
|
item_manager.player_feeds_mag_item(entity_gateway, &client.character, ClientItemId(mag_feed.mag_id), ClientItemId(mag_feed.item_id)).await?;
|
|
|
|
let mag_feed = mag_feed.clone();
|
|
Ok(Box::new(client_location.get_client_neighbors(id).unwrap().into_iter()
|
|
.map(move |client| {
|
|
(client.client, SendShipPacket::Message(Message::new(GameMessage::PlayerFeedMag(mag_feed.clone()))))
|
|
})))
|
|
}
|
|
|
|
pub async fn player_equips_item<EG>(id: ClientId,
|
|
pkt: &PlayerEquipItem,
|
|
entity_gateway: &mut EG,
|
|
clients: &Clients,
|
|
item_manager: &mut ItemManager)
|
|
-> Result<Box<dyn Iterator<Item = (ClientId, SendShipPacket)> + Send>, ShipError>
|
|
where
|
|
EG: EntityGateway
|
|
{
|
|
let client = clients.get(&id).ok_or(ShipError::ClientNotFound(id))?;
|
|
item_manager.player_equips_item(entity_gateway, &client.character, ClientItemId(pkt.item_id), pkt.sub_menu).await?;
|
|
Ok(Box::new(None.into_iter()))
|
|
}
|
|
|
|
pub async fn player_unequips_item<EG>(id: ClientId,
|
|
pkt: &PlayerUnequipItem,
|
|
entity_gateway: &mut EG,
|
|
clients: &Clients,
|
|
item_manager: &mut ItemManager)
|
|
-> Result<Box<dyn Iterator<Item = (ClientId, SendShipPacket)> + Send>, ShipError>
|
|
where
|
|
EG: EntityGateway
|
|
{
|
|
let client = clients.get(&id).ok_or(ShipError::ClientNotFound(id))?;
|
|
let equipped_unit_ids: Vec<ClientItemId> = {
|
|
item_manager.player_unequips_item(entity_gateway, &client.character, ClientItemId(pkt.item_id)).await?;
|
|
let inventory = item_manager.get_character_inventory(&client.character).unwrap();
|
|
let ue_item = inventory.get_item_by_id(ClientItemId(pkt.item_id)).ok_or(ShipError::ItemError)?;
|
|
if let ItemType::Armor(_) = ue_item.item_type() {
|
|
inventory.items()
|
|
.iter()
|
|
.filter(|inv_item| {
|
|
if let ItemType::Unit(_) = inv_item.item_type() {
|
|
return inv_item.equipped()
|
|
}
|
|
false
|
|
})
|
|
.map(|u| u.item_id())
|
|
.collect()
|
|
} else {
|
|
Vec::new()
|
|
}
|
|
};
|
|
|
|
for unit_id in equipped_unit_ids {
|
|
item_manager.player_unequips_item(entity_gateway, &client.character, unit_id).await;
|
|
}
|
|
|
|
Ok(Box::new(None.into_iter()))
|
|
}
|
|
|
|
pub async fn player_sorts_items<EG>(id: ClientId,
|
|
pkt: &SortItems,
|
|
entity_gateway: &mut EG,
|
|
clients: &Clients,
|
|
item_manager: &mut ItemManager)
|
|
-> Result<Box<dyn Iterator<Item = (ClientId, SendShipPacket)> + Send>, ShipError>
|
|
where
|
|
EG: EntityGateway
|
|
{
|
|
let client = clients.get(&id).ok_or(ShipError::ClientNotFound(id))?;
|
|
item_manager.player_sorts_items(entity_gateway, &client.character, pkt.item_ids).await?;
|
|
Ok(Box::new(None.into_iter())) // Do clients care about the order of other clients items?
|
|
} |