/
09-25.clj
96 lines (62 loc) · 2.91 KB
/
09-25.clj
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
(require '[clojure.data.json :as json]
'[clojure.string :as str])
(def pokeapi-base-url "https://pokeapi.co/api/v2/type/")
(defn oxford-commaize
"Joins the list in an oxford-comma way. If only one element, return that"
[xs] (if (= 1 (count xs))
(first xs)
(str (str/join ", " (butlast xs)) ", and " (last xs))))
(defn strenghts-weaknesses
"Given a pokemon type, returns what that type is weak against, and strong against."
[type]
(let [damages (get type "damage_relations")
strong (map #(get % "name") (get damages "double_damage_to"))
weak (map #(get % "name") (get damages "double_damage_from"))]
(str "Weak against " (oxford-commaize weak) ". Strong against " (oxford-commaize strong))))
(defn fetch-type-info
"Fetches the type information from the pokeapi, and parses it into a map"
[type] (json/read-str (slurp (str pokeapi-base-url type))))
(def not-found-msgs
["%s is not an existing type, dingus!"
"What do you mean %s? That's not a type!"
"I'm sorry, what? %s? Never heard of it"
"Don't know about %s, but I bet it's weak against common sense."
"Are you sure you don't mean fighting? %s sounds kinda weird"
"I think I once ate %s, but not sure"])
(defn not-found
"Prints a random message from the list of not-found messages, with the type formatted in"
[type]
(format (rand-nth not-found-msgs) type))
(defn type-matchup
"Fetches the type information, then presents the information in a nice way, or a not found message if not found."
[type]
(try
(strenghts-weaknesses (fetch-type-info type))
(catch Exception _e (not-found type))))
(comment
(type-matchup "fairy") ; => "Weak against poison, and steel. Strong against fighting, dragon, and dark"
(type-matchup "fighting") ; => "Weak against flying, psychic, and fairy. Strong against normal, rock, steel, ice, and dark"
(type-matchup "dan") ; => "What do you mean dan? That's not a type!"
(def fire (fetch-type-info "fire"))
(def fighting (fetch-type-info "fighting"))
(def fweak (get-in fighting ["damage_relations" "double_damage_from" "name"]))
(get-in fighting ["damage_relations" "double_damage_from"])
(map #(get % "name") fweak)
(fetch-type-info "asdd")
(type-matchup (fetch-type-info "asd"))
(oxford-commaize ["test" "asd"])
(select-keys fighting ["damage_relations"])
(select-keys (select-keys fighting ["damage_relations"]) ["double_damage_from"])
(let [damages (get fighting "damage_relations")
strong (map #(get % "name") (get damages "double_damage_to"))
weak (map #(get % "name") (get damages "double_damage_from"))]
{:strong strong
:weak weak})
(def types ["normal" "rock" "steel" "ice" "dark"])
(oxford-commaize types)
(oxford-commaize ["x"])
(str/join ", " types)
(->> (get-in fighting ["damage_relations" "double_damage_from"])
(map #(get % "name")))
(json/read-str (slurp pokeapi-base-url))
)