Geoffrey-rs/geoffrey_bot/src/bot/formatters.rs

83 lines
2.0 KiB
Rust

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_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(|item| {
format!(
"**{}**, {} for {}D",
item.item.name, item.count_per_price, item.price
)
})
.collect::<Vec<String>>()
.join("\n")
)
} else {
"".to_string()
}
}
_ => "".to_string(),
};
format!("{}\n{}", display_loc(loc), info)
}