2021-02-12 10:51:32 +00:00
|
|
|
use crate::{events::group_manip::handle_invite, Server};
|
2021-02-11 19:35:36 +00:00
|
|
|
use common::{
|
2021-02-12 10:51:32 +00:00
|
|
|
comp::{
|
|
|
|
group::InviteKind,
|
|
|
|
inventory::{Inventory, slot::InvSlotId},
|
|
|
|
},
|
2021-02-12 02:53:25 +00:00
|
|
|
trade::{PendingTrade, TradeActionMsg, Trades},
|
2021-02-11 19:35:36 +00:00
|
|
|
uid::Uid,
|
|
|
|
};
|
|
|
|
use common_net::{msg::ServerGeneral, sync::WorldSyncExt};
|
|
|
|
use specs::{world::WorldExt, Entity as EcsEntity};
|
|
|
|
use tracing::warn;
|
|
|
|
|
|
|
|
pub fn handle_initiate_trade(server: &mut Server, interactor: EcsEntity, counterparty: EcsEntity) {
|
|
|
|
if let Some(uid) = server.state_mut().ecs().uid_from_entity(counterparty) {
|
|
|
|
handle_invite(server, interactor, uid, InviteKind::Trade);
|
|
|
|
} else {
|
|
|
|
warn!("Entity tried to trade with an entity that lacks an uid");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-12 02:53:25 +00:00
|
|
|
pub fn handle_process_trade_action(
|
|
|
|
server: &mut Server,
|
|
|
|
entity: EcsEntity,
|
|
|
|
trade_id: usize,
|
|
|
|
msg: TradeActionMsg,
|
|
|
|
) {
|
2021-02-11 19:35:36 +00:00
|
|
|
if let Some(uid) = server.state.ecs().uid_from_entity(entity) {
|
|
|
|
let mut trades = server.state.ecs().write_resource::<Trades>();
|
|
|
|
if let TradeActionMsg::Decline = msg {
|
|
|
|
let to_notify = trades.decline_trade(trade_id, uid);
|
|
|
|
to_notify
|
|
|
|
.and_then(|u| server.state.ecs().entity_from_uid(u.0))
|
|
|
|
.map(|e| server.notify_client(e, ServerGeneral::DeclinedTrade));
|
|
|
|
} else {
|
2021-02-12 10:51:32 +00:00
|
|
|
if let Some(inv) = server.state.ecs().read_component::<Inventory>().get(entity) {
|
|
|
|
trades.process_trade_action(trade_id, uid, msg, inv);
|
|
|
|
}
|
2021-02-11 19:35:36 +00:00
|
|
|
if let Some(trade) = trades.trades.get(&trade_id) {
|
|
|
|
if trade.should_commit() {
|
|
|
|
// TODO: inventory manip
|
2021-02-12 10:51:32 +00:00
|
|
|
}
|
|
|
|
// send the updated state to both parties
|
|
|
|
for party in trade.parties.iter() {
|
|
|
|
server.state.ecs().entity_from_uid(party.0).map(|e| {
|
|
|
|
server.notify_client(
|
|
|
|
e,
|
|
|
|
ServerGeneral::UpdatePendingTrade(trade_id, trade.clone()),
|
|
|
|
)
|
|
|
|
});
|
2021-02-11 19:35:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|