Ogare65z

Untitled

Jul 21st, 2021
230
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.16 KB | None | 0 0
  1. import itertools as it
  2. import json
  3. import pathlib
  4. from dataclasses import dataclass
  5.  
  6.  
  7. @dataclass
  8. class Skills:
  9.     id: int
  10.     name: str
  11.  
  12.     @staticmethod
  13.     def populate(file_name):
  14.         filepath_json = pathlib.Path.cwd() / f"{file_name}"
  15.         json_data = {}
  16.         if filepath_json.exists():
  17.             with filepath_json.open("r", encoding="utf-8") as f:
  18.                 try:
  19.                     json_data = json.load(f)
  20.                 except json.JSONDecodeError:
  21.                     pass
  22.             _skills = []
  23.             for i in json_data:
  24.                 _skills.append(Skills(int(i["id"]), i["name"]))
  25.             return _skills
  26.         else:
  27.             with filepath_json. open("w", encoding="utf-8"):
  28.                 pass
  29.             return []
  30.  
  31.  
  32. skills = Skills.populate("skills.json")
  33.  
  34.  
  35. def get_skill_name(_id):
  36.     for v in skills:
  37.         if v.id == _id:
  38.             return v.name
  39.  
  40.  
  41. @dataclass
  42. class FoodStates:
  43.     name: str
  44.     modifier: int
  45.  
  46.     @staticmethod
  47.     def populate(file_name):
  48.         filepath_json = pathlib.Path.cwd() / f"{file_name}"
  49.         json_data = {}
  50.         if filepath_json.exists():
  51.             with filepath_json.open("r", encoding="utf-8") as f:
  52.                 try:
  53.                     json_data = json.load(f)
  54.                 except json.JSONDecodeError:
  55.                     pass
  56.             _states = []
  57.             for i in json_data:
  58.                 _states.append(Skills(i["name"], i["modifier"]))
  59.             return _states
  60.         else:
  61.             with filepath_json. open("w", encoding="utf-8"):
  62.                 pass
  63.             return []
  64.  
  65.  
  66. class Recipe:
  67.  
  68.     # init method or constructor
  69.     def __init__(self, file_name):
  70.         self.fileName = file_name
  71.         self.filepathJson = pathlib.Path.cwd() / self.fileName / f"{self.fileName}.json"
  72.         self.matches = {}
  73.         self.match_recipes = []
  74.         if self.filepathJson.exists():
  75.             with self.filepathJson.open("r", encoding="utf-8") as f:
  76.                 try:
  77.                     self.matches = json.load(f)
  78.                 except json.JSONDecodeError:
  79.                     pass
  80.  
  81.         else:
  82.             if (not pathlib.Path(pathlib.Path.cwd() / self.fileName).exists() or
  83.                     not pathlib.Path(pathlib.Path.cwd() / self.fileName).is_dir()):
  84.                 pathlib.Path(pathlib.Path.cwd() / self.fileName).mkdir()
  85.  
  86.         self.recipe_length = 0
  87.         for values in self.matches.values():
  88.             if len(values) > self.recipe_length:
  89.                 self.recipe_length = len(values)
  90.  
  91.     def simulate(self, ingredients):
  92.         simulated_recipes = list(it.product(*ingredients))
  93.         for recipe in simulated_recipes:
  94.             _sum = 0
  95.             for ingredient in recipe:
  96.                 _sum = _sum + ingredient[1]
  97.             affinity_num = (_sum % 138) + 1
  98.             if str(affinity_num) in self.matches.keys():
  99.                 continue
  100.             if len(recipe) > self.recipe_length:
  101.                 self.recipe_length = len(recipe)
  102.             self.matches[str(affinity_num)] = recipe
  103.         # print(f"Size:{len(self.matches.keys())}   {self.matches.keys()}")
  104.  
  105.         with self.filepathJson.open("w", encoding="utf-8") as f:
  106.             json.dump(self.matches, f)
  107.  
  108.         filepath_csv = pathlib.Path.cwd() / self.fileName / f"{self.fileName}.csv"
  109.         with filepath_csv.open("w", encoding="utf-8") as f:
  110.             for index in range(1, 139):
  111.                 if str(index) not in self.matches.keys():
  112.                     f.write(f"{get_skill_name(index)}, {index}\n")
  113.                 else:
  114.                     v = self.matches[str(index)]
  115.                     csv_str = ""
  116.                     for value in v:
  117.                         csv_str = csv_str + f"{value[0]}, {value[1]}, "
  118.                     csv_str = csv_str + f"{get_skill_name(index)}, {index}\n"
  119.                     f.write(csv_str)
  120.  
  121.     def get_options_affinity(self, ingredients, affinity_num):
  122.         simulated_recipes = list(it.product(*ingredients))
  123.         for recipe in simulated_recipes:
  124.             ingredient_sum = 0
  125.             for ingredient in recipe:
  126.                 ingredient_sum = ingredient_sum + ingredient[1]
  127.  
  128.             _affinity_num = (ingredient_sum % 138) + 1
  129.             if _affinity_num != affinity_num:
  130.                 continue
  131.             self.match_recipes.append(recipe)
  132.         filepath_match = (pathlib.Path.cwd() / self.fileName /
  133.                           f"{get_skill_name(affinity_num)}_{affinity_num}.csv")
  134.         with filepath_match.open("w", encoding="utf-8") as f:
  135.             v = self.match_recipes
  136.             csv_str = ""
  137.             for value in v:
  138.                 csv_str = csv_str + f"{value[0]}, {value[1]}, "
  139.             csv_str = csv_str + f"\n"
  140.             f.write(csv_str)
  141.  
  142.  
  143. def unfermented_moonshine():
  144.     toon = [["Ogare", 92]]
  145.     cookers = [["oven", 178]]
  146.     containers = [["cauldron", 351]]
  147.     water = [["water", 6]]
  148.     sugar = [["sugar", 47]]
  149.     grain = [["wheat", 25], ["barley", 23], ["oat", 25], ["rye", 23]]
  150.     pea = [["roasted pea", 62], ["fried pea", 59]]
  151.     corn = [["roasted corn", 48], ["fried corn", 45]]
  152.     garlic = [["roasted garlic", 96], ["fried garlic", 93]]
  153.     tomato = [["roasted tomato", 47], ["fried tomato", 44]]
  154.     pea_pods = [["roasted pea pods", 50], ["fried pea pods", 47]]
  155.     carrot = [["roasted carrot", 45], ["fried carrot", 42]]
  156.     cucumber = [["roasted cucumber", 21], ["fried cucumber", 18]]
  157.     onion = [["roasted onion", 95], ["fried onion", 92]]
  158.     potato = [["roasted potato", 51], ["fried potato", 48]]
  159.     lettuce = [["roasted lettuce", 49], ["fried lettuce", 46]]
  160.     pumpkin = [["roasted pumpkin", 49], ["fried pumpkin", 46]]
  161.     cabbage = [["roasted cabbage", 46], ["fried cabbage", 43]]
  162.     ingredients = [toon, cookers, containers, water, sugar, grain, grain, grain, grain, pea, corn, garlic, tomato,
  163.                    pea_pods, carrot, carrot, cucumber, onion, onion, potato, potato, lettuce, pumpkin, pumpkin, cabbage]
  164.     unf_moonshine_recipes = Recipe("unf moonshine")
  165.     unf_moonshine_recipes.simulate(ingredients)
  166.  
  167.  
  168. if __name__ == '__main__':
  169.     unfermented_moonshine()
Add Comment
Please, Sign In to add comment