~deciduously/fcalc

ref: 5ba32ecfb93e39f3b11e2abaa2ba6d5c7c848544 fcalc/src/recipe.rs -rw-r--r-- 3.4 KiB
5ba32ecfBen Lovy Recipe lookup a month ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
//! A recipe tracks the cost to produce an item.

use crate::{item::*, FcalcError};
use lazy_static::lazy_static;
use serde::Deserialize;
use std::{collections::HashMap, fmt, str::FromStr};

lazy_static! {
    pub static ref RECIPES: Recipes = Recipes::new();
}

/// A formula for building a product
#[derive(Clone, Debug, Deserialize)]
pub struct RawRecipe {
    inputs: Vec<String>,
    output: String,
    time: f64,
    building_type: String,
}

/// A formula for building a product
#[derive(Clone, Debug, Deserialize)]
pub struct Recipe {
    pub inputs: Vec<Item>,
    pub output: Product,
    time: f64,
    building_type: BuildingType,
}

impl From<RawRecipe> for Recipe {
    fn from(raw: RawRecipe) -> Self {
        let inputs = raw
            .inputs
            .iter()
            .map(|el| ITEMS.lookup(el).expect("Could not find input product"))
            .collect();
        let output = ITEMS
            .lookup(&raw.output)
            .expect("Could not find output product.")
            .as_product()
            .expect("Output product was a raw material");
        Self {
            inputs,
            output,
            time: raw.time,
            building_type: BuildingType::from_str(&raw.building_type).unwrap(),
        }
    }
}

impl fmt::Display for Recipe {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(
            f,
            "Inputs: {:?}, Time: {}, Building Type: {:?}",
            self.inputs, self.time, self.building_type
        )
    }
}

/// Each recipe is made in a type of building
#[derive(Clone, Copy, Debug, Deserialize)]
pub enum BuildingType {
    Assembler,
    Furnace,
}

impl FromStr for BuildingType {
    type Err = FcalcError;
    fn from_str(s: &str) -> Result<Self, Self::Err> {
        match s {
            "Furnace" => Ok(BuildingType::Furnace),
            "Assembler" => Ok(BuildingType::Assembler),
            _ => Err(FcalcError::InvalidBuildingType),
        }
    }
}

/// Each building type has several tiers
#[derive(Debug, Deserialize)]
pub struct Building {
    building_type: BuildingType,
    name: String,
    crafting_speed: f64,
}

/// Top-level to read recipes.toml
#[derive(Debug, Deserialize)]
pub struct RawRecipes {
    buildings: Vec<Building>,
    recipes: Vec<RawRecipe>,
}

impl RawRecipes {
    fn new() -> Self {
        Self::default()
    }
}

impl Default for RawRecipes {
    fn default() -> Self {
        let input = include_str!("recipes.toml");
        toml::from_str(input).expect("Could not read recipes input file.")
    }
}

/// Recipe lookup
#[derive(Debug)]
pub struct Recipes {
    recipes: HashMap<Product, Recipe>,
}

impl From<RawRecipes> for Recipes {
    fn from(raw: RawRecipes) -> Self {
        let mut recipes = HashMap::new();
        for raw in &raw.recipes {
            // TODO convert all Strings to Items in input and output
            let recipe = Recipe::from(raw.clone());
            recipes.insert(recipe.output.clone(), recipe);
        }
        Self { recipes }
    }
}

impl Recipes {
    fn new() -> Self {
        let raw = RawRecipes::new();
        Self::from(raw)
    }

    pub fn lookup(&self, product: &Product) -> Option<&Recipe> {
        self.recipes.get(product)
    }
}

#[cfg(test)]
mod test {
    use super::*;
    use pretty_assertions::assert_eq;
    #[test]
    fn it_loads_all_recipes() {
        assert_eq!(RECIPES.recipes.len(), 1);
    }
}