Browse Source

lint src/ship/shop/tool.rs

pull/42/head
jake 4 years ago
parent
commit
d42491095e
  1. 2
      src/ship/ship.rs
  2. 21
      src/ship/shops/tool.rs

2
src/ship/ship.rs

@ -302,7 +302,7 @@ impl ItemShops {
ItemShops { ItemShops {
weapon_shop: weapon_shop, weapon_shop: weapon_shop,
tool_shop: ToolShop::new(),
tool_shop: ToolShop::default(),
armor_shop: ArmorShop::default(), armor_shop: ArmorShop::default(),
} }
} }

21
src/ship/shops/tool.rs

@ -53,7 +53,7 @@ impl ShopItem for ToolShopItem {
fn price(&self) -> usize { fn price(&self) -> usize {
match self { match self {
ToolShopItem::Tool(tool) => { ToolShopItem::Tool(tool) => {
TOOL_STATS.get(&tool)
TOOL_STATS.get(tool)
.map(|tool_stats| { .map(|tool_stats| {
tool_stats.price tool_stats.price
}) })
@ -146,7 +146,7 @@ fn load_tool_table() -> ToolTable {
let mut table: HashMap<String, Vec<ToolType>> = toml::from_str(s.as_str()).unwrap(); let mut table: HashMap<String, Vec<ToolType>> = toml::from_str(s.as_str()).unwrap();
ToolTable(table.remove("tools".into()).unwrap())
ToolTable(table.remove("tools").unwrap())
} }
fn load_tech_table() -> TechTable { fn load_tech_table() -> TechTable {
@ -156,7 +156,7 @@ fn load_tech_table() -> TechTable {
f.read_to_string(&mut s).unwrap(); f.read_to_string(&mut s).unwrap();
let mut table: HashMap<String, Vec<TechTierDeserialize>> = toml::from_str(s.as_str()).unwrap(); let mut table: HashMap<String, Vec<TechTierDeserialize>> = toml::from_str(s.as_str()).unwrap();
let techniques = table.remove("techniques".into()).unwrap();
let techniques = table.remove("techniques").unwrap();
let techniques = techniques.into_iter() let techniques = techniques.into_iter()
.map(|tech_tier| { .map(|tech_tier| {
TechTier { TechTier {
@ -194,15 +194,18 @@ pub struct ToolShop<R: Rng + SeedableRng> {
rng: R, rng: R,
} }
impl<R: Rng + SeedableRng> ToolShop<R> {
pub fn new() -> ToolShop<R> {
impl<R: Rng + SeedableRng> Default for ToolShop<R> {
fn default() -> ToolShop<R> {
ToolShop { ToolShop {
tools: load_tool_table(), tools: load_tool_table(),
techs: load_tech_table(), techs: load_tech_table(),
rng: R::from_entropy(), rng: R::from_entropy(),
} }
} }
}
impl<R: Rng + SeedableRng> ToolShop<R> {
fn generate_tech_types(&mut self, character_level: usize) -> Vec<Technique> { fn generate_tech_types(&mut self, character_level: usize) -> Vec<Technique> {
let tier = self.techs.0.iter() let tier = self.techs.0.iter()
.filter(|t| t.level <= character_level) .filter(|t| t.level <= character_level)
@ -233,7 +236,7 @@ impl<R: Rng + SeedableRng> ToolShop<R> {
let tech_choice = WeightedIndex::new(tier.iter().map(|(_, e)| e.probability)).unwrap(); let tech_choice = WeightedIndex::new(tier.iter().map(|(_, e)| e.probability)).unwrap();
while techs.len() < number_of_techs { while techs.len() < number_of_techs {
let tech_detail = tier.get(tech_choice.sample(&mut self.rng)).unwrap(); let tech_detail = tier.get(tech_choice.sample(&mut self.rng)).unwrap();
if techs.iter().find(|t| *t == tech_detail.0).is_none() {
if !techs.iter().any(|t| t == tech_detail.0) {
techs.push(*tech_detail.0); techs.push(*tech_detail.0);
} }
} }
@ -269,7 +272,7 @@ impl<R: Rng + SeedableRng> ToolShop<R> {
pub fn generate_tool_list(&mut self, character_level: usize) -> Vec<ToolShopItem> { pub fn generate_tool_list(&mut self, character_level: usize) -> Vec<ToolShopItem> {
let mut tools = Vec::new().into_iter() let mut tools = Vec::new().into_iter()
.chain(self.tools.0.clone().into_iter().map(|t| ToolShopItem::Tool(t)))
.chain(self.tools.0.clone().into_iter().map(ToolShopItem::Tool))
.chain(self.generate_techs(character_level).into_iter()) .chain(self.generate_techs(character_level).into_iter())
.collect::<Vec<_>>(); .collect::<Vec<_>>();
tools.sort(); tools.sort();
@ -284,12 +287,12 @@ mod test {
#[test] #[test]
fn test_loading_tool_shop() { fn test_loading_tool_shop() {
ToolShop::<rand_chacha::ChaCha20Rng>::new();
ToolShop::<rand_chacha::ChaCha20Rng>::default();
} }
#[test] #[test]
fn test_generating_some_tools() { fn test_generating_some_tools() {
let mut ts = ToolShop::<rand_chacha::ChaCha20Rng>::new();
let mut ts = ToolShop::<rand_chacha::ChaCha20Rng>::default();
for i in 0..200 { for i in 0..200 {
ts.generate_tool_list(i); ts.generate_tool_list(i);
} }

Loading…
Cancel
Save