2021-02-13 23:32:55 +00:00
|
|
|
use super::group_manip;
|
|
|
|
use crate::{client::Client, Server};
|
|
|
|
use common::{
|
|
|
|
comp::{
|
|
|
|
self,
|
2021-03-25 04:35:33 +00:00
|
|
|
agent::{Agent, AgentEvent},
|
2021-02-13 23:32:55 +00:00
|
|
|
group::GroupManager,
|
|
|
|
invite::{Invite, InviteKind, InviteResponse, PendingInvites},
|
2021-04-07 19:37:48 +00:00
|
|
|
ChatType,
|
2021-02-13 23:32:55 +00:00
|
|
|
},
|
|
|
|
trade::Trades,
|
|
|
|
uid::Uid,
|
|
|
|
};
|
|
|
|
use common_net::{
|
|
|
|
msg::{InviteAnswer, ServerGeneral},
|
|
|
|
sync::WorldSyncExt,
|
|
|
|
};
|
|
|
|
use specs::world::WorldExt;
|
|
|
|
use std::time::{Duration, Instant};
|
|
|
|
use tracing::{error, warn};
|
|
|
|
|
|
|
|
/// Time before invite times out
|
|
|
|
const INVITE_TIMEOUT_DUR: Duration = Duration::from_secs(31);
|
|
|
|
/// Reduced duration shown to the client to help alleviate latency issues
|
|
|
|
const PRESENTED_INVITE_TIMEOUT_DUR: Duration = Duration::from_secs(30);
|
|
|
|
|
|
|
|
pub fn handle_invite(
|
|
|
|
server: &mut Server,
|
|
|
|
inviter: specs::Entity,
|
|
|
|
invitee_uid: Uid,
|
|
|
|
kind: InviteKind,
|
|
|
|
) {
|
|
|
|
let max_group_size = server.settings().max_player_group_size;
|
|
|
|
let state = server.state_mut();
|
|
|
|
let clients = state.ecs().read_storage::<Client>();
|
|
|
|
let invitee = match state.ecs().entity_from_uid(invitee_uid.into()) {
|
|
|
|
Some(t) => t,
|
|
|
|
None => {
|
|
|
|
// Inform of failure
|
|
|
|
if let Some(client) = clients.get(inviter) {
|
|
|
|
client.send_fallible(ServerGeneral::server_msg(
|
|
|
|
ChatType::Meta,
|
|
|
|
"Invite failed, target does not exist.",
|
|
|
|
));
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
let uids = state.ecs().read_storage::<Uid>();
|
|
|
|
|
|
|
|
// Check if entity is trying to invite themselves
|
|
|
|
if uids
|
|
|
|
.get(inviter)
|
|
|
|
.map_or(false, |inviter_uid| *inviter_uid == invitee_uid)
|
|
|
|
{
|
|
|
|
warn!("Entity tried to invite themselves into a group/trade");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut pending_invites = state.ecs().write_storage::<PendingInvites>();
|
|
|
|
|
|
|
|
if let InviteKind::Group = kind {
|
|
|
|
if !group_manip::can_invite(
|
|
|
|
state,
|
|
|
|
&clients,
|
|
|
|
&mut pending_invites,
|
|
|
|
max_group_size,
|
|
|
|
inviter,
|
|
|
|
invitee,
|
|
|
|
) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
Implement /price_list (work in progress), stub for /buy and /sell
remove outdated economic simulation code
remove old values, document
add natural resources to economy
Remove NaturalResources from Place (now in Economy)
find closest site to each chunk
implement natural resources (the distance scale is wrong)
cargo fmt
working distance calculation
this collection of natural resources seem to make sense, too much Wheat though
use natural resources and controlled area to replenish goods
increase the amount of chunks controlled by one guard to 50
add new professions and goods to the list
implement multiple products per worker
remove the old code and rename the new code to the previous name
correctly determine which goods guards will give you access to
correctly estimate the amount of natural resources controlled
adapt to new server API
instrument tooltips
Now I just need to figure out how to store a (reference to) a closure
closures for tooltip content generation
pass site/cave id to the client
Add economic information to the client structure
(not yet exchanged with the server)
Send SiteId to the client, prepare messages for economy request
Make client::sites a HashMap
Specialize the Crafter into Brewer,Bladesmith and Blacksmith
working server request for economic info from within tooltip
fully operational economic tooltips
I need to fix the id clash between caves and towns though
fix overlapping ids between caves and sites
display stock amount
correctly handle invalid (cave) ids in the request
some initial balancing, turn off most info logging
less intrusive way of implementing the dynamic tool tips in map
further tooltip cleanup
further cleanup, dynamic tooltip not fully working as intended
correctly working tooltip visibility logic
cleanup, display labor value
separate economy info request in a separate translation unit
display values as well
nicer display format for economy
add last_exports and coin to the new economy
do not allocate natural resources to Dungeons (making town so much larger)
balancing attempt
print town size statistics
cargo fmt (dead code)
resource tweaks, csv debugging
output a more interesting town (and then all sites)
fix the labor value logic (now we have meaningful prices)
load professions from ron (WIP)
use assets manager in economy
loading professions works
use professions from ron file
fix Labor debug logic
count chunks per type separately
(preparing for better resource control)
better structured resource data
traders, more professions (WIP)
fix exception when starting the simulation
fix replenish function
TODO:
- use area_ratio for resource usage (chunks should be added to stock, ratio on usage?)
- fix trading
documentation clean up
fix merge artifact
Revise trader mechanic
start Coin with a reasonable default
remove the outdated economy code
preserve documentation from removed old structure
output neighboring sites (preparation)
pass list of neighbors to economy
add trade structures
trading stub
Description of purpose by zesterer on Discord
remember prices (needed for planning)
avoid growing the order vector unboundedly
into_iter doesn't clear the Vec, so clear it manually
use drain to process Vecs, avoid clone
fix the test server
implement a test stub (I need to get it faster than 30 seconds to be more useful)
enable info output in test
debug missing and extra goods
use the same logging extension as water, activate feature
update dependencies
determine good prices, good exchange goods
a working set of revisions
a cozy world which economy tests in 2s
first order planning version
fun with package version
buy according to value/priority, not missing amount
introduce min price constant, fix order priority
in depth debugging
with a correct sign the trading plans start to make sense
move the trade planning to a separate function
rename new function
reorganize code into subroutines (much cleaner)
each trading step now has its own function
cut down the number of debugging output
introduce RoadSecurity and Transportation
transport capacity bookkeeping
only plan to pay with valuable goods, you can no longer stockpile unused options
(which interestingly shows a huge impact, to be investigated)
Coin is now listed as a payment (although not used)
proper transportation estimation (although 0)
remove more left overs uncovered by viewing the code in a merge request
use the old default values, handle non-pileable stocks directly before increasing it
(as economy is based on last year's products)
don't order the missing good multiple times
also it uses coin to buy things!
fix warnings and use the transportation from stock again
cargo fmt
prepare evaluation of trade
don't count transportation multiple times
fix merge artifact
operational trade planning
trade itself is still misleading
make clippy happy
clean up
correct labor ratio of merchants (no need to multiply with amount produced)
incomplete merchant labor_value computation
correct last commit
make economy of scale more explicit
make clippy happy (and code cleaner)
more merchant tweaks (more pop=better)
beginning of real trading code
revert the update of dependencies
remove stale comments/unused code
trading implementation complete (but untested)
something is still strange ...
fix sign in trading
another sign fix
some bugfixes and plenty of debugging code
another bug fixed, more to go
fix another invariant (rounding will lead to very small negative value)
introduce Terrain and Territory
fix merge mistakes
2021-03-14 03:18:32 +00:00
|
|
|
let mut agents = state.ecs().write_storage::<comp::Agent>();
|
2021-02-13 23:32:55 +00:00
|
|
|
let mut invites = state.ecs().write_storage::<Invite>();
|
|
|
|
|
|
|
|
if invites.contains(invitee) {
|
|
|
|
// Inform inviter that there is already an invite
|
|
|
|
if let Some(client) = clients.get(inviter) {
|
|
|
|
client.send_fallible(ServerGeneral::server_msg(
|
|
|
|
ChatType::Meta,
|
|
|
|
"This player already has a pending invite.",
|
|
|
|
));
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut invite_sent = false;
|
|
|
|
// Returns true if insertion was succesful
|
|
|
|
let mut send_invite = || {
|
|
|
|
match invites.insert(invitee, Invite { inviter, kind }) {
|
|
|
|
Err(err) => {
|
|
|
|
error!("Failed to insert Invite component: {:?}", err);
|
|
|
|
false
|
|
|
|
},
|
|
|
|
Ok(_) => {
|
|
|
|
match pending_invites.entry(inviter) {
|
|
|
|
Ok(entry) => {
|
|
|
|
entry.or_insert_with(|| PendingInvites(Vec::new())).0.push((
|
|
|
|
invitee,
|
|
|
|
kind,
|
|
|
|
Instant::now() + INVITE_TIMEOUT_DUR,
|
|
|
|
));
|
|
|
|
invite_sent = true;
|
|
|
|
true
|
|
|
|
},
|
|
|
|
Err(err) => {
|
|
|
|
error!(
|
|
|
|
"Failed to get entry for pending invites component: {:?}",
|
|
|
|
err
|
|
|
|
);
|
|
|
|
// Cleanup
|
|
|
|
invites.remove(invitee);
|
|
|
|
false
|
|
|
|
},
|
|
|
|
}
|
|
|
|
},
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// If client comp
|
|
|
|
if let (Some(client), Some(inviter)) = (clients.get(invitee), uids.get(inviter).copied()) {
|
|
|
|
if send_invite() {
|
|
|
|
client.send_fallible(ServerGeneral::Invite {
|
|
|
|
inviter,
|
|
|
|
timeout: PRESENTED_INVITE_TIMEOUT_DUR,
|
|
|
|
kind,
|
|
|
|
});
|
|
|
|
}
|
Implement /price_list (work in progress), stub for /buy and /sell
remove outdated economic simulation code
remove old values, document
add natural resources to economy
Remove NaturalResources from Place (now in Economy)
find closest site to each chunk
implement natural resources (the distance scale is wrong)
cargo fmt
working distance calculation
this collection of natural resources seem to make sense, too much Wheat though
use natural resources and controlled area to replenish goods
increase the amount of chunks controlled by one guard to 50
add new professions and goods to the list
implement multiple products per worker
remove the old code and rename the new code to the previous name
correctly determine which goods guards will give you access to
correctly estimate the amount of natural resources controlled
adapt to new server API
instrument tooltips
Now I just need to figure out how to store a (reference to) a closure
closures for tooltip content generation
pass site/cave id to the client
Add economic information to the client structure
(not yet exchanged with the server)
Send SiteId to the client, prepare messages for economy request
Make client::sites a HashMap
Specialize the Crafter into Brewer,Bladesmith and Blacksmith
working server request for economic info from within tooltip
fully operational economic tooltips
I need to fix the id clash between caves and towns though
fix overlapping ids between caves and sites
display stock amount
correctly handle invalid (cave) ids in the request
some initial balancing, turn off most info logging
less intrusive way of implementing the dynamic tool tips in map
further tooltip cleanup
further cleanup, dynamic tooltip not fully working as intended
correctly working tooltip visibility logic
cleanup, display labor value
separate economy info request in a separate translation unit
display values as well
nicer display format for economy
add last_exports and coin to the new economy
do not allocate natural resources to Dungeons (making town so much larger)
balancing attempt
print town size statistics
cargo fmt (dead code)
resource tweaks, csv debugging
output a more interesting town (and then all sites)
fix the labor value logic (now we have meaningful prices)
load professions from ron (WIP)
use assets manager in economy
loading professions works
use professions from ron file
fix Labor debug logic
count chunks per type separately
(preparing for better resource control)
better structured resource data
traders, more professions (WIP)
fix exception when starting the simulation
fix replenish function
TODO:
- use area_ratio for resource usage (chunks should be added to stock, ratio on usage?)
- fix trading
documentation clean up
fix merge artifact
Revise trader mechanic
start Coin with a reasonable default
remove the outdated economy code
preserve documentation from removed old structure
output neighboring sites (preparation)
pass list of neighbors to economy
add trade structures
trading stub
Description of purpose by zesterer on Discord
remember prices (needed for planning)
avoid growing the order vector unboundedly
into_iter doesn't clear the Vec, so clear it manually
use drain to process Vecs, avoid clone
fix the test server
implement a test stub (I need to get it faster than 30 seconds to be more useful)
enable info output in test
debug missing and extra goods
use the same logging extension as water, activate feature
update dependencies
determine good prices, good exchange goods
a working set of revisions
a cozy world which economy tests in 2s
first order planning version
fun with package version
buy according to value/priority, not missing amount
introduce min price constant, fix order priority
in depth debugging
with a correct sign the trading plans start to make sense
move the trade planning to a separate function
rename new function
reorganize code into subroutines (much cleaner)
each trading step now has its own function
cut down the number of debugging output
introduce RoadSecurity and Transportation
transport capacity bookkeeping
only plan to pay with valuable goods, you can no longer stockpile unused options
(which interestingly shows a huge impact, to be investigated)
Coin is now listed as a payment (although not used)
proper transportation estimation (although 0)
remove more left overs uncovered by viewing the code in a merge request
use the old default values, handle non-pileable stocks directly before increasing it
(as economy is based on last year's products)
don't order the missing good multiple times
also it uses coin to buy things!
fix warnings and use the transportation from stock again
cargo fmt
prepare evaluation of trade
don't count transportation multiple times
fix merge artifact
operational trade planning
trade itself is still misleading
make clippy happy
clean up
correct labor ratio of merchants (no need to multiply with amount produced)
incomplete merchant labor_value computation
correct last commit
make economy of scale more explicit
make clippy happy (and code cleaner)
more merchant tweaks (more pop=better)
beginning of real trading code
revert the update of dependencies
remove stale comments/unused code
trading implementation complete (but untested)
something is still strange ...
fix sign in trading
another sign fix
some bugfixes and plenty of debugging code
another bug fixed, more to go
fix another invariant (rounding will lead to very small negative value)
introduce Terrain and Territory
fix merge mistakes
2021-03-14 03:18:32 +00:00
|
|
|
} else if let Some(agent) = agents.get_mut(invitee) {
|
|
|
|
if send_invite() {
|
|
|
|
if let Some(inviter) = uids.get(inviter) {
|
|
|
|
agent.inbox.push_front(AgentEvent::TradeInvite(*inviter));
|
|
|
|
invite_sent = true;
|
|
|
|
}
|
|
|
|
}
|
2021-02-13 23:32:55 +00:00
|
|
|
} else if let Some(client) = clients.get(inviter) {
|
|
|
|
client.send_fallible(ServerGeneral::server_msg(
|
|
|
|
ChatType::Meta,
|
|
|
|
"Can't invite, not a player or npc",
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Notify inviter that the invite is pending
|
|
|
|
if invite_sent {
|
|
|
|
if let Some(client) = clients.get(inviter) {
|
|
|
|
client.send_fallible(ServerGeneral::InvitePending(invitee_uid));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
pub fn handle_invite_response(
|
|
|
|
server: &mut Server,
|
|
|
|
entity: specs::Entity,
|
|
|
|
response: InviteResponse,
|
|
|
|
) {
|
|
|
|
match response {
|
|
|
|
InviteResponse::Accept => handle_invite_accept(server, entity),
|
|
|
|
InviteResponse::Decline => handle_invite_decline(server, entity),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn handle_invite_accept(server: &mut Server, entity: specs::Entity) {
|
2021-03-25 04:35:33 +00:00
|
|
|
let index = server.index.clone();
|
2021-02-13 23:32:55 +00:00
|
|
|
let state = server.state_mut();
|
|
|
|
let clients = state.ecs().read_storage::<Client>();
|
|
|
|
let uids = state.ecs().read_storage::<Uid>();
|
2021-03-29 14:47:42 +00:00
|
|
|
let mut agents = state.ecs().write_storage::<Agent>();
|
2021-02-13 23:32:55 +00:00
|
|
|
let mut invites = state.ecs().write_storage::<Invite>();
|
|
|
|
if let Some((inviter, kind)) = invites.remove(entity).and_then(|invite| {
|
|
|
|
let Invite { inviter, kind } = invite;
|
|
|
|
let mut pending_invites = state.ecs().write_storage::<PendingInvites>();
|
|
|
|
let pending = &mut pending_invites.get_mut(inviter)?.0;
|
|
|
|
// Check that inviter has a pending invite and remove it from the list
|
|
|
|
let invite_index = pending.iter().position(|p| p.0 == entity)?;
|
|
|
|
pending.swap_remove(invite_index);
|
|
|
|
// If no pending invites remain remove the component
|
|
|
|
if pending.is_empty() {
|
|
|
|
pending_invites.remove(inviter);
|
|
|
|
}
|
|
|
|
|
|
|
|
Some((inviter, kind))
|
|
|
|
}) {
|
|
|
|
if let (Some(client), Some(target)) = (clients.get(inviter), uids.get(entity).copied()) {
|
|
|
|
client.send_fallible(ServerGeneral::InviteComplete {
|
|
|
|
target,
|
|
|
|
answer: InviteAnswer::Accepted,
|
|
|
|
kind,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
match kind {
|
|
|
|
InviteKind::Group => {
|
|
|
|
let mut group_manager = state.ecs().write_resource::<GroupManager>();
|
|
|
|
group_manager.add_group_member(
|
|
|
|
inviter,
|
|
|
|
entity,
|
|
|
|
&state.ecs().entities(),
|
|
|
|
&mut state.ecs().write_storage(),
|
|
|
|
&state.ecs().read_storage(),
|
|
|
|
&uids,
|
|
|
|
|entity, group_change| {
|
|
|
|
clients
|
|
|
|
.get(entity)
|
|
|
|
.and_then(|c| {
|
|
|
|
group_change
|
|
|
|
.try_map(|e| uids.get(e).copied())
|
|
|
|
.map(|g| (g, c))
|
|
|
|
})
|
|
|
|
.map(|(g, c)| c.send(ServerGeneral::GroupUpdate(g)));
|
|
|
|
},
|
|
|
|
);
|
|
|
|
},
|
|
|
|
InviteKind::Trade => {
|
|
|
|
if let (Some(inviter_uid), Some(invitee_uid)) =
|
|
|
|
(uids.get(inviter).copied(), uids.get(entity).copied())
|
|
|
|
{
|
|
|
|
let mut trades = state.ecs().write_resource::<Trades>();
|
|
|
|
let id = trades.begin_trade(inviter_uid, invitee_uid);
|
|
|
|
let trade = trades.trades[&id].clone();
|
2021-03-29 14:47:42 +00:00
|
|
|
if let Some(agent) = agents.get_mut(inviter) {
|
|
|
|
agent
|
|
|
|
.inbox
|
|
|
|
.push_front(AgentEvent::TradeAccepted(invitee_uid));
|
|
|
|
}
|
2021-04-07 19:37:48 +00:00
|
|
|
let pricing = agents
|
2021-02-13 23:32:55 +00:00
|
|
|
.get(inviter)
|
2021-04-08 16:33:00 +00:00
|
|
|
.and_then(|a| {
|
|
|
|
a.behavior
|
|
|
|
.trade_site
|
|
|
|
.and_then(|id| index.get_site_prices(id))
|
|
|
|
})
|
2021-04-06 17:18:40 +00:00
|
|
|
.or_else(|| {
|
2021-04-07 19:37:48 +00:00
|
|
|
agents.get(entity).and_then(|a| {
|
2021-04-08 16:33:00 +00:00
|
|
|
a.behavior
|
|
|
|
.trade_site
|
|
|
|
.and_then(|id| index.get_site_prices(id))
|
2021-04-07 19:37:48 +00:00
|
|
|
})
|
2021-04-08 16:33:00 +00:00
|
|
|
});
|
2021-03-25 04:35:33 +00:00
|
|
|
clients.get(inviter).map(|c| {
|
|
|
|
c.send(ServerGeneral::UpdatePendingTrade(
|
|
|
|
id,
|
|
|
|
trade.clone(),
|
|
|
|
pricing.clone(),
|
|
|
|
))
|
|
|
|
});
|
2021-02-13 23:32:55 +00:00
|
|
|
clients
|
|
|
|
.get(entity)
|
2021-03-25 04:35:33 +00:00
|
|
|
.map(|c| c.send(ServerGeneral::UpdatePendingTrade(id, trade, pricing)));
|
2021-02-13 23:32:55 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn handle_invite_decline(server: &mut Server, entity: specs::Entity) {
|
|
|
|
let state = server.state_mut();
|
|
|
|
let clients = state.ecs().read_storage::<Client>();
|
|
|
|
let uids = state.ecs().read_storage::<Uid>();
|
|
|
|
let mut invites = state.ecs().write_storage::<Invite>();
|
|
|
|
if let Some((inviter, kind)) = invites.remove(entity).and_then(|invite| {
|
|
|
|
let Invite { inviter, kind } = invite;
|
|
|
|
let mut pending_invites = state.ecs().write_storage::<PendingInvites>();
|
|
|
|
let pending = &mut pending_invites.get_mut(inviter)?.0;
|
|
|
|
// Check that inviter has a pending invite and remove it from the list
|
|
|
|
let invite_index = pending.iter().position(|p| p.0 == entity)?;
|
|
|
|
pending.swap_remove(invite_index);
|
|
|
|
// If no pending invites remain remove the component
|
|
|
|
if pending.is_empty() {
|
|
|
|
pending_invites.remove(inviter);
|
|
|
|
}
|
|
|
|
|
|
|
|
Some((inviter, kind))
|
|
|
|
}) {
|
|
|
|
// Inform inviter of rejection
|
|
|
|
if let (Some(client), Some(target)) = (clients.get(inviter), uids.get(entity).copied()) {
|
|
|
|
client.send_fallible(ServerGeneral::InviteComplete {
|
|
|
|
target,
|
|
|
|
answer: InviteAnswer::Declined,
|
|
|
|
kind,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|