Skip to content

Commit af3403a

Browse files
committed
Complete refactor of foods.py to amke adding new foods easier
1 parent 2a2484e commit af3403a

1 file changed

Lines changed: 56 additions & 260 deletions

File tree

plugins/foods.py

Lines changed: 56 additions & 260 deletions
Original file line numberDiff line numberDiff line change
@@ -3,41 +3,49 @@
33
import json
44
import os
55
import re
6+
from collections import namedtuple, defaultdict
7+
from itertools import chain
68

79
from cloudbot import hook
810
from cloudbot.util import textgen
911

1012
nick_re = re.compile("^[A-Za-z0-9_|.\-\]\[\{\}\*\`]*$", re.I)
1113

12-
sandwich_data = {}
13-
taco_data = {}
14-
coffee_data = {}
15-
noodles_data = {}
16-
muffin_data = {}
17-
scone_data = {}
18-
rice_data = {}
19-
tea_data = {}
20-
keto_data = {}
21-
beer_data = {}
22-
cheese_data = {}
23-
pancake_data = {}
24-
chicken_data = {}
25-
icecream_data = {}
26-
brekkie_data = {}
27-
doobie_data = {}
28-
pizza_data = {}
29-
chocolate_data = {}
30-
pasta_data = {}
31-
nugget_data = {}
32-
cereal_data = {}
33-
pie_data = {}
34-
sushi_data = {}
35-
steak_data = {}
36-
milkshake_data = {}
37-
kebab_data = {}
38-
cake_data = {}
39-
potato_data = {}
40-
cookie_data = {}
14+
BasicFood = namedtuple('BasicFood', "name commands datafile unitname")
15+
16+
BASIC_FOOD = (
17+
BasicFood("sandwich", "sandwich", "sandwich.json", "a potato"),
18+
BasicFood("taco", "taco", "taco.json", "a taco"),
19+
BasicFood("coffee", "coffee", "coffee.json", "coffee"),
20+
BasicFood("noodles", "noodles", "noodles.json", "noodles"),
21+
BasicFood("muffin", "muffin", "muffin.json", "a muffin"),
22+
BasicFood("scone", "scone", "scone.json", "a scone"),
23+
BasicFood("rice", "rice", "rice.json", "rice"),
24+
BasicFood("tea", "tea", "tea.json", "tea"),
25+
BasicFood("keto", "keto", "keto.json", "food"),
26+
BasicFood("beer", "beer", "beer.json", "beer"),
27+
BasicFood("cheese", "cheese", "cheese.json", "cheese"),
28+
BasicFood("pancake", "pancake", "pancake.json", "pancakes"),
29+
BasicFood("chicken", "chicken", "chicken.json", "chicken"),
30+
BasicFood("nugget", "nugget", "nugget.json", "nuggets"),
31+
BasicFood("pie", "pie", "pie.json", "pie"),
32+
BasicFood("brekkie", ["brekkie", "brekky"], "brekkie.json", "brekkie"),
33+
BasicFood("icecream", "icecream", "icecream.json", "icecream"),
34+
BasicFood("doobie", "doobie", "doobie.json", "a doobie"),
35+
BasicFood("pizza", "pizza", "pizza.json", "pizza"),
36+
BasicFood("chocolate", "chocolate", "chocolate.json", "chocolate"),
37+
BasicFood("pasta", "pasta", "pasta.json", "pasta"),
38+
BasicFood("cereal", "cereal", "cereal.json", "cereal"),
39+
BasicFood("sushi", "sushi", "sushi.json", "sushi"),
40+
BasicFood("steak", "steak", "steak.json", "a nice steak dinner"),
41+
BasicFood("milkshake", "milkshake", "milkshake.json", "a milkshake"),
42+
BasicFood("kebab", "kebab", "kebab.json", "a kebab"),
43+
BasicFood("cake", "cake", "cake.json", "a cake"),
44+
BasicFood("potato", "potato", "potato.json", "a potato"),
45+
BasicFood("cookie", "cookie", "cookies.json", "a cookie"),
46+
)
47+
48+
basic_food_data = defaultdict(dict)
4149

4250

4351
def is_valid(target):
@@ -59,36 +67,10 @@ def load_foods(bot):
5967
"""
6068
:type bot: cloudbot.bot.CloudBot
6169
"""
70+
basic_food_data.clear()
6271

63-
load_template_data(bot, "sandwich.json", sandwich_data)
64-
load_template_data(bot, "taco.json", taco_data)
65-
load_template_data(bot, "coffee.json", coffee_data)
66-
load_template_data(bot, "noodles.json", noodles_data)
67-
load_template_data(bot, "muffin.json", muffin_data)
68-
load_template_data(bot, "scone.json", scone_data)
69-
load_template_data(bot, "rice.json", rice_data)
70-
load_template_data(bot, "tea.json", tea_data)
71-
load_template_data(bot, "keto.json", keto_data)
72-
load_template_data(bot, "beer.json", beer_data)
73-
load_template_data(bot, "cheese.json", cheese_data)
74-
load_template_data(bot, "pancake.json", pancake_data)
75-
load_template_data(bot, "chicken.json", chicken_data)
76-
load_template_data(bot, "nugget.json", nugget_data)
77-
load_template_data(bot, "pie.json", pie_data)
78-
load_template_data(bot, "brekkie.json", brekkie_data)
79-
load_template_data(bot, "icecream.json", icecream_data)
80-
load_template_data(bot, "doobie.json", doobie_data)
81-
load_template_data(bot, "pizza.json", pizza_data)
82-
load_template_data(bot, "chocolate.json", chocolate_data)
83-
load_template_data(bot, "pasta.json", pasta_data)
84-
load_template_data(bot, "cereal.json", cereal_data)
85-
load_template_data(bot, "sushi.json", sushi_data)
86-
load_template_data(bot, "steak.json", steak_data)
87-
load_template_data(bot, "milkshake.json", milkshake_data)
88-
load_template_data(bot, "kebab.json", kebab_data)
89-
load_template_data(bot, "cake.json", cake_data)
90-
load_template_data(bot, "potato.json", potato_data)
91-
load_template_data(bot, "cookies.json", cookie_data)
72+
for food in BASIC_FOOD:
73+
load_template_data(bot, food.datafile, basic_food_data[food.name])
9274

9375

9476
def basic_format(text, data, food_type, **kwargs):
@@ -105,208 +87,22 @@ def basic_format(text, data, food_type, **kwargs):
10587
return generator.generate_string()
10688

10789

108-
@asyncio.coroutine
109-
@hook.command
110-
def potato(text, action):
111-
"""<user> - makes <user> a tasty little potato"""
112-
# Kept for posterity
113-
# <Luke> Hey guys, any good ideas for plugins?
114-
# <User> I don't know, something that lists every potato known to man?
115-
# <Luke> BRILLIANT
116-
action(basic_format(text, potato_data, "a potato"))
117-
118-
119-
@asyncio.coroutine
120-
@hook.command
121-
def cake(text, action):
122-
"""<user> - gives <user> an awesome cake"""
123-
action(basic_format(text, cake_data, "a cake"))
124-
125-
126-
@asyncio.coroutine
127-
@hook.command
128-
def cookie(text, action):
129-
"""<user> - gives <user> a cookie"""
130-
action(basic_format(text, cookie_data, "a cookie"))
131-
132-
133-
@asyncio.coroutine
134-
@hook.command
135-
def sandwich(text, action):
136-
"""<user> - give a tasty sandwich to <user>"""
137-
action(basic_format(text, sandwich_data, "a sandwich"))
138-
139-
140-
@asyncio.coroutine
141-
@hook.command
142-
def taco(text, action):
143-
"""<user> - give a taco to <user>"""
144-
action(basic_format(text, taco_data, "a taco"))
145-
146-
147-
@asyncio.coroutine
148-
@hook.command
149-
def coffee(text, action):
150-
"""<user> - give coffee to <user>"""
151-
action(basic_format(text, coffee_data, "coffee"))
152-
153-
154-
@asyncio.coroutine
155-
@hook.command
156-
def noodles(text, action):
157-
"""<user> - give noodles to <user>"""
158-
action(basic_format(text, noodles_data, "noodles"))
159-
160-
161-
@asyncio.coroutine
162-
@hook.command
163-
def muffin(text, action):
164-
"""<user> - give muffin to <user>"""
165-
action(basic_format(text, muffin_data, "a muffin"))
166-
167-
168-
@asyncio.coroutine
169-
@hook.command
170-
def scone(text, action):
171-
"""<user> - give scone to <user>"""
172-
action(basic_format(text, scone_data, "a scone"))
90+
def make_cmd_list(value):
91+
if isinstance(value, str):
92+
value = [value]
93+
return value
17394

17495

17596
@asyncio.coroutine
176-
@hook.command
177-
def rice(text, action):
178-
"""<user> - give rice to <user>"""
179-
action(basic_format(text, rice_data, "rice"))
180-
181-
182-
@asyncio.coroutine
183-
@hook.command
184-
def tea(text, action):
185-
"""<user> - give tea to <user>"""
186-
action(basic_format(text, tea_data, "tea"))
187-
188-
189-
@asyncio.coroutine
190-
@hook.command
191-
def keto(text, action):
192-
"""<user> - give keto food to <user>"""
193-
action(basic_format(text, keto_data, "food"))
194-
195-
196-
@asyncio.coroutine
197-
@hook.command
198-
def beer(text, action):
199-
"""<user> - give beer to <user>"""
200-
action(basic_format(text, beer_data, "beer"))
201-
202-
203-
@asyncio.coroutine
204-
@hook.command
205-
def cheese(text, action):
206-
"""<user> - give cheese to <user>"""
207-
action(basic_format(text, cheese_data, "cheese"))
208-
209-
210-
@asyncio.coroutine
211-
@hook.command
212-
def pancake(text, action):
213-
"""<user> - give pancakes to <user>"""
214-
action(basic_format(text, pancake_data, "pancakes"))
215-
216-
217-
@asyncio.coroutine
218-
@hook.command
219-
def chicken(text, action):
220-
"""<user> - give chicken to <user>"""
221-
action(basic_format(text, chicken_data, "chicken"))
222-
223-
224-
@asyncio.coroutine
225-
@hook.command
226-
def nugget(text, action):
227-
"""<user> - give nuggets to <user>"""
228-
action(basic_format(text, nugget_data, "nuggets"))
229-
230-
231-
@asyncio.coroutine
232-
@hook.command
233-
def pie(text, action):
234-
"""<user> - give pie to <user>"""
235-
action(basic_format(text, pie_data, "pie"))
236-
237-
238-
@asyncio.coroutine
239-
@hook.command
240-
def icecream(text, action):
241-
"""<user> - give icecream to <user>"""
242-
action(basic_format(text, icecream_data, "icecream"))
243-
244-
245-
@asyncio.coroutine
246-
@hook.command("brekky", "brekkie")
247-
def brekkie(text, action):
248-
"""<user> - give brekkie to <user>"""
249-
action(basic_format(text, brekkie_data, "brekkie"))
250-
251-
252-
@asyncio.coroutine
253-
@hook.command("doobie")
254-
def doobie(text, action):
255-
"""<user> - pass the doobie to <user>"""
256-
action(basic_format(text, doobie_data, "a doobie"))
257-
258-
259-
@asyncio.coroutine
260-
@hook.command("pizza")
261-
def pizza(text, action):
262-
"""<user> - give pizza to <user>"""
263-
action(basic_format(text, pizza_data, "pizza"))
264-
265-
266-
@asyncio.coroutine
267-
@hook.command("chocolate")
268-
def chocolate(text, action):
269-
"""<user> - give chocolate to <user>"""
270-
action(basic_format(text, chocolate_data, "chocolate"))
271-
272-
273-
@asyncio.coroutine
274-
@hook.command
275-
def pasta(text, action):
276-
"""<user> - give pasta to <user>"""
277-
action(basic_format(text, pasta_data, "pasta"))
278-
279-
280-
@asyncio.coroutine
281-
@hook.command
282-
def cereal(text, action):
283-
"""<user> - give cereal to <user>"""
284-
action(basic_format(text, cereal_data, "cereal"))
285-
286-
287-
@asyncio.coroutine
288-
@hook.command
289-
def sushi(text, action):
290-
"""<user> - give sushi to <user>"""
291-
action(basic_format(text, sushi_data, "sushi"))
292-
293-
294-
@asyncio.coroutine
295-
@hook.command
296-
def steak(text, action):
297-
"""<user> - give a steak dinner to <user>"""
298-
action(basic_format(text, steak_data, "a nice steak dinner"))
299-
300-
301-
@asyncio.coroutine
302-
@hook.command
303-
def milkshake(text, action):
304-
"""<user> - give a milkshake to <user>"""
305-
action(basic_format(text, milkshake_data, "a milkshake"))
306-
307-
308-
@asyncio.coroutine
309-
@hook.command
310-
def kebab(text, action):
311-
"""<user> - give a kebab to <user>"""
312-
action(basic_format(text, kebab_data, "a delicious kebab"))
97+
@hook.command(*chain.from_iterable(make_cmd_list(food.commands) for food in BASIC_FOOD))
98+
def basic_food(text, triggered_command, action):
99+
# Find the first food in BASIC_FOOD that matches the triggered command
100+
for food in BASIC_FOOD:
101+
cmd_list = make_cmd_list(food.commands)
102+
if triggered_command in cmd_list or any(cmd.startswith(triggered_command) for cmd in cmd_list):
103+
break
104+
else:
105+
# The triggered command didn't match any loaded foods, WTF!?!?
106+
return "{} matched an unknown food in foods.py. Congrats! You found a bug! " \
107+
"Please report this right away.".format(triggered_command)
108+
action(basic_format(text, basic_food_data[food.name], food.unitname))

0 commit comments

Comments
 (0)