103 lines
2.5 KiB
Rust
103 lines
2.5 KiB
Rust
use chrono::{Duration, Utc};
|
|
use geoffrey_models::models::item::ItemListing;
|
|
use geoffrey_models::models::locations::{Location, LocationData};
|
|
use geoffrey_models::models::player::Player;
|
|
use geoffrey_models::models::Portal;
|
|
|
|
pub fn display_owners(owners: &[Player], limit: usize) -> String {
|
|
let mut plural = "";
|
|
let mut ellipses = "";
|
|
|
|
if owners.len() > 1 {
|
|
plural = "s"
|
|
}
|
|
|
|
let range = if owners.len() > limit {
|
|
ellipses = "...";
|
|
limit
|
|
} else {
|
|
owners.len()
|
|
};
|
|
|
|
format!(
|
|
"Owner{}: {}{}",
|
|
plural,
|
|
owners[0..range]
|
|
.iter()
|
|
.map(|owner| owner.name.clone())
|
|
.collect::<Vec<String>>()
|
|
.join(", "),
|
|
ellipses
|
|
)
|
|
}
|
|
|
|
pub fn display_portal(portal: &Portal) -> String {
|
|
format!(
|
|
"Portal: {} {} (x={}, z={})",
|
|
portal.direction(),
|
|
portal.tunnel_addr(),
|
|
portal.x,
|
|
portal.z
|
|
)
|
|
}
|
|
|
|
pub fn display_loc(loc: &Location) -> String {
|
|
let portal_str = match &loc.portal {
|
|
None => "".to_string(),
|
|
Some(p) => format!("{}, ", display_portal(p)),
|
|
};
|
|
|
|
format!(
|
|
"**{}**, {}, {}{}",
|
|
loc.name,
|
|
loc.position,
|
|
portal_str,
|
|
display_owners(&loc.owners, 3),
|
|
)
|
|
}
|
|
|
|
pub fn display_item_listing(listing: &ItemListing) -> String {
|
|
let stocked_diff = Utc::now() - listing.restocked_time;
|
|
|
|
let time_str = if stocked_diff < Duration::days(1) {
|
|
"today".to_string()
|
|
} else if stocked_diff < Duration::days(2) {
|
|
"1 day ago".to_string()
|
|
} else {
|
|
format!("{} days ago", stocked_diff.num_days())
|
|
};
|
|
|
|
let item_listing = format!(
|
|
"**{}**, {} for {}D. Restocked {}.",
|
|
listing.item.name, listing.count_per_price, listing.price, time_str
|
|
);
|
|
|
|
if listing.is_out_of_stock(1) {
|
|
format!("~~{}~~", item_listing)
|
|
} else {
|
|
item_listing
|
|
}
|
|
}
|
|
|
|
pub fn display_loc_full(loc: &Location) -> String {
|
|
let info = match &loc.loc_data {
|
|
LocationData::Shop(shop) => {
|
|
if !shop.item_listings.is_empty() {
|
|
format!(
|
|
"\n**Inventory**:\n{}",
|
|
shop.item_listings
|
|
.iter()
|
|
.map(display_item_listing)
|
|
.collect::<Vec<String>>()
|
|
.join("\n")
|
|
)
|
|
} else {
|
|
"".to_string()
|
|
}
|
|
}
|
|
_ => "".to_string(),
|
|
};
|
|
|
|
format!("{}\n{}", display_loc(loc), info)
|
|
}
|