added plant names
Marco Andronaco andronacomarco@gmail.com
Sat, 24 Sep 2022 15:01:28 +0200
3 files changed,
116 insertions(+),
24 deletions(-)
M
Constants.py
→
Constants.py
@@ -213,3 +213,66 @@ 'narcotic',
'gnu/linux', 'abraxan', # rip dear friend ] + +plant_names = [ + 'Adele', + 'Alessandro', + 'Alessia', + 'Alice', + 'Andrea', + 'Anna', + 'Antonio', + 'Arianna', + 'Aurora', + 'Azzurra', + 'Beatrice', + 'Bianca', + 'Camilla', + 'Chiara', + 'Christian', + 'Davide', + 'Diego', + 'Edoardo', + 'Elena', + 'Elia', + 'Emma', + 'Enea', + 'Federico', + 'Filippo', + 'Francesca', + 'Francesco', + 'Gabriel', + 'Gabriele', + 'Gaia', + 'Ginevra', + 'Gioele', + 'Giorgia', + 'Giovanni', + 'Giulia', + 'Giulio', + 'Giuseppe', + 'Greta', + 'Isabel', + 'Leonardo', + 'Lorenzo', + 'Luca', + 'Ludovica', + 'Marco', + 'Martina', + 'Matilde', + 'Matteo', + 'Mattia', + 'Mia', + 'Michele', + 'Nicole', + 'Nicolò', + 'Noemi', + 'Pietro', + 'Rebecca', + 'Riccardo', + 'Samuele', + 'Sara', + 'Sofia', + 'Tommaso', + 'Vittoria', +]
M
Gardening.py
→
Gardening.py
@@ -3,6 +3,7 @@ from Constants import *
water_duration = 3600 # * 24 stage_factors = (1, 3, 10, 20, 30) +indicator_squares = 6 class Plant(object): # This is your plant!@@ -12,8 +13,9 @@ self.points = 0 # one point per second
self.life_stages = tuple(st * water_duration for st in stage_factors) self.stage = 0 self.mutation = 0 - self.species = random.randint(0,len(species_list)-1) - self.color = random.randint(0,len(color_list)-1) + self.species = random.randint(0, len(species_list) - 1) + self.color = random.randint(0, len(color_list) - 1) + self.name = plant_names[random.randint(0, len(plant_names) - 1)] self.rarity = self.rarity_check() self.ticks = 0 self.age_formatted = "0"@@ -31,7 +33,6 @@ self.watered_24h = True
self.visitors = [] def update(self): - # find out stage: self.water_check() if self.dead_check(): # updates self.time_delta_watered@@ -145,8 +146,7 @@ res1 = min(now - plant.last_water, water_duration)
res2 = min(plant.last_update - plant.last_water, water_duration) plant.points += max(0, res1 - res2) # max() not necessary but just in case - - #print("generation bonus: ", plant.generation_bonus, ". increase: ", res1 - res2, "max: ", water_duration) + plant.last_update = now stages = tuple(ti / plant.generation_bonus for ti in plant.life_stages) # bonus is applied to stage thresholds@@ -158,15 +158,13 @@ for n in stages:
if (n <= delta and (closest is None or (delta - n) < (delta - closest))): closest = n count += 1 - - #print("plant is in stage", count, "because it passed", closest, "seconds of life") return count def get_plant_water(plant: Plant): water_delta = time.time() - plant.last_water water_left_pct = max(0, 1 - (water_delta/water_duration)) # 24h - water_left = int(math.ceil(water_left_pct * 10)) - return f"{water_left * '🟦'}{'⬛' * (10 - water_left)} {str(int(water_left_pct * 100))}% " + water_left = int(math.ceil(water_left_pct * indicator_squares)) + return f"{water_left * '🟦'}{'⬛' * (indicator_squares - water_left)} {str(int(water_left_pct * 100))}% " def get_plant_description(plant: Plant): output_text = ""@@ -250,7 +248,7 @@
return f''' {get_plant_description(plant)} ```{get_plant_art(plant)}``` -{plant.parse_plant()} +{plant.name}, the {plant.parse_plant()} {get_plant_water(plant)}
M
main.py
→
main.py
@@ -19,13 +19,22 @@
plant.update() return plant +def get_plant_markup(user_id: int): + return InlineKeyboardMarkup([ + [ + InlineKeyboardButton(text="Innaffia 🚰", callback_data=f"water {user_id}"), + InlineKeyboardButton(text="Mostra 🌱", callback_data=f"show {user_id}"), + ] + ]) + def start_handler(update: Update, context: CallbackContext): - - plant = get_plant(context, update.effective_user.id) + user_id = update.effective_user.id + plant = get_plant(context, user_id) new = False if plant is None: - context.bot_data[update.effective_user.id] = { "plant" : Plant(update.effective_user.id) } + plant = Plant(user_id) + context.bot_data[update.effective_user.id] = { "plant" : Plant(user_id) } new = True if plant.dead or plant.stage == 5:@@ -33,6 +42,7 @@ plant.start_over()
new = True if new: + show_handler(update, context) return reply(update, context, "Hai piantato un nuovo seme! Adesso usa /water per innaffiarlo.") return reply(update, context, "La tua pianta non è ancora pronta per andarsene!")@@ -41,7 +51,7 @@ def water(context: CallbackContext, user_id: int):
plant = get_plant(context, user_id) if plant is None: - return "Non hai nessuna pianta da innaffiare! Usa /start per piantarne una." + return "Non hai niente da innaffiare! Usa /start per piantare un seme." if plant.dead: return "La pianta è morta..."@@ -53,9 +63,25 @@ def show(context: CallbackContext, user_id: int):
plant = get_plant(context, user_id) if plant is None: - return "Non hai nessuna pianta da mostrare! Usa /start per piantarne una." + return "Non hai nessuna pianta da mostrare! Usa /start per piantarne una.", "" + + markup = get_plant_markup(user_id) + return get_plant_info(plant), markup - return get_plant_info(plant) +def rename(context: CallbackContext, user_id: int): + plant = get_plant(context, user_id) + + if plant is None: + return "Non hai ancora piantato niente! Usa /start per piantare un seme." + try: + new_name = " ".join(context.args).strip() + if new_name == "": + raise IndexError + except IndexError: + return "Utilizzo: /rename <nuovo nome>" + + plant.name = new_name + return f"Fatto! Adesso la tua pianta si chiama {new_name}!" def water_handler(update: Update, context: CallbackContext): answer = water(context, update.effective_user.id)@@ -63,10 +89,13 @@ return reply(update, context, answer)
def show_handler(update: Update, context: CallbackContext): user_id = update.effective_user.id - answer = show(context, user_id) + answer, markup = show(context, user_id) + reply(update, context, answer, markup) - markup = InlineKeyboardMarkup([[InlineKeyboardButton(text="Innaffia 🚰", callback_data=f"water {user_id}")]]) - reply(update, context, answer, markup) +def rename_handler(update: Update, context: CallbackContext): + user_id = update.effective_user.id + answer = rename(context, user_id) + reply(update, context, answer) def keyboard_handler(update: Update, context: CallbackContext): query = update.callback_query@@ -79,10 +108,14 @@ if user_id != update.effective_user.id:
reply(update, context, f"{update.effective_user.full_name} ha innaffiato la pianta di qualcuno!") return query.answer(answer) + if data.startswith("show"): + user_id = int(data.split(" ")[1]) + answer, markup = show(context, user_id) + reply(update, context, answer, markup) + return query.answer("Questo tasto non fa nulla.") def main(): - updater = Updater(token=os.getenv("token"), persistence=PicklePersistence(filename='bot-data.pkl', store_user_data=False,@@ -90,19 +123,17 @@ store_bot_data=True,
store_callback_data=False, store_chat_data=False )) - dispatcher = updater.dispatcher - # commands dispatcher.add_handler(CommandHandler('start', start_handler)) dispatcher.add_handler(CommandHandler('water', water_handler)) dispatcher.add_handler(CommandHandler('show', show_handler)) - + dispatcher.add_handler(CommandHandler('rename', rename_handler)) dispatcher.add_handler(CallbackQueryHandler(callback=keyboard_handler)) updater.start_polling() - print(os.getenv("bot_name")) + print(updater.bot.name, "is up and running!") updater.idle() if __name__ == "__main__":