all repos — groupgardenbot @ dd587ca3c8ea6677ed6671c9aa264c494d3d681e

An extension of the game "botany", originally designed for unix-based systems, to the Telegram Bot API.

major code cleanup
Bi-Rabittoh andronacomarco@gmail.com
Sat, 24 Sep 2022 23:19:18 +0200
commit

dd587ca3c8ea6677ed6671c9aa264c494d3d681e

parent

64d444b4247f96a1cab7164bb35f9f075d20b704

3 files changed, 18 insertions(+), 50 deletions(-)

jump to
M Constants.pyConstants.py

@@ -82,31 +82,6 @@

def get_stage_description(stage: int, number: int, species: str, color: str) -> str: return stage_descriptions[stage][number].format(color=color, species=species) -plant_art_list = [ - 'poppy', - 'cactus', - 'aloe', - 'flytrap', - 'jadeplant', - 'fern', - 'daffodil', - 'sunflower', - 'baobab', - 'lithops', - 'hemp', - 'pansy', - 'iris', - 'agave', - 'ficus', - 'moss', - 'sage', - 'snapdragon', - 'columbine', - 'brugmansia', - 'palm', - 'pachypodium', - ] - stage_list = [ 'seed', 'seedling',
M Gardening.pyGardening.py

@@ -1,4 +1,4 @@

-import random, time, math, datetime, os +import random, os, time, datetime from Constants import * water_duration = 3600 * 24

@@ -20,7 +20,6 @@ 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.generation = generation self.generation_bonus = 1 + (0.2 * (generation - 1)) self.dead = False

@@ -117,7 +116,7 @@

def get_plant_water(plant: Plant): water_delta = int(time.time()) - plant.last_water water_left_pct = max(0, 1 - (water_delta/water_duration)) - water_left = int(math.ceil(water_left_pct * indicator_squares)) + water_left = int(round(water_left_pct * indicator_squares)) return f"{water_left * '🟦'}{'⬛' * (indicator_squares - water_left)} {str(round(water_left_pct * 100))}% " def get_plant_description(plant: Plant):

@@ -140,7 +139,7 @@ if this_stage >= 1:

last_growth_at = plant.life_stages[this_stage - 1] else: last_growth_at = 0 - ticks_since_last = plant.ticks - last_growth_at + ticks_since_last = plant.points - last_growth_at ticks_between_stage = plant.life_stages[this_stage] - last_growth_at if ticks_since_last >= ticks_between_stage * 0.8: output_text += "You notice your plant looks different.\n"

@@ -150,17 +149,14 @@

# if seedling if this_stage == 1: species_options = [species_list[plant.species], - species_list[(plant.species+3) % len(species_list)], - species_list[(plant.species-3) % len(species_list)]] + species_list[(plant.species + 3) % len(species_list)], + species_list[(plant.species - 3) % len(species_list)]] random.shuffle(species_options) - plant_hint = "It could be a(n) " + species_options[0] + ", " + species_options[1] + ", or " + species_options[2] - output_text += plant_hint + ".\n" - + output_text += f"It could be a(n) {species_options[0]}, {species_options[1]} or {species_options[2]}.\n" # if young plant if this_stage == 2: if plant.rarity >= 2: - rarity_hint = "You feel like your plant is special." - output_text += rarity_hint + ".\n" + output_text += "You feel like your plant is special.\n" # if mature plant if this_stage == 3:

@@ -168,7 +164,7 @@ color_options = [color_list[plant.color],

color_list[(plant.color+3) % len(color_list)], color_list[(plant.color-3) % len(color_list)]] random.shuffle(color_options) - return "You can see the first hints of " + color_options[0] + ", " + color_options[1] + ", or " + color_options[2] + output_text += f"You can see the first hints of {color_options[0]}, {color_options[1]}, or {color_options[2]}.\n" return output_text

@@ -177,9 +173,9 @@ if plant.dead == True: return 'rip.txt'

if datetime.date.today().month == 10 and datetime.date.today().day == 31: return 'jackolantern.txt' if plant.stage == 0: return 'seed.txt' if plant.stage == 1: return 'seedling.txt' - if plant.stage == 2: return plant_art_list[plant.species]+'1.txt' - if plant.stage == 3 or plant.stage == 5: return plant_art_list[plant.species]+'2.txt' - if plant.stage == 4: return plant_art_list[plant.species]+'3.txt' + if plant.stage == 2: return species_list[plant.species]+'1.txt' + if plant.stage == 3 or plant.stage == 5: return species_list[plant.species]+'2.txt' + if plant.stage == 4: return species_list[plant.species]+'3.txt' return "template.txt" def get_plant_art(plant: Plant):

@@ -200,6 +196,6 @@ [{plant.name}, the {plant.parse_plant()}](tg://user?id={plant.owner})

{get_plant_water(plant)} -Points: {plant.points} Bonus: x{plant.generation_bonus - 1} +Points: {plant.points} '''
M main.pymain.py

@@ -1,11 +1,10 @@

import os, logging +from dotenv import load_dotenv +from telegram import Update, InlineKeyboardMarkup, InlineKeyboardButton +from telegram.ext import Updater, CallbackContext, CallbackQueryHandler, CommandHandler, PicklePersistence +from Gardening import Plant, get_plant_info logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', level=logging.INFO) -from telegram.error import BadRequest -from telegram import Update, User, InlineKeyboardMarkup, InlineKeyboardButton -from telegram.ext import Updater, CallbackContext, CallbackQueryHandler, CommandHandler, MessageHandler, PicklePersistence -from dotenv import load_dotenv load_dotenv() -from Gardening import Plant, get_plant_info def reply(update: Update, context: CallbackContext, text: str = "", markup: str = ""): return context.bot.send_message(chat_id=update.effective_chat.id, text=text, reply_markup=markup, parse_mode='Markdown')

@@ -113,6 +112,7 @@ query.answer(answer)

if data.startswith("show"): user_id = int(data.split(" ")[1]) + query.answer() if user_id is not None: text, markup = show(context, user_id)

@@ -120,7 +120,7 @@ return edit(update, context, text, markup)

return query.answer("Questo tasto non fa nulla.") -def main(): +if __name__ == "__main__": updater = Updater(token=os.getenv("token"), persistence=PicklePersistence(filename='bot-data.pkl', store_user_data=False,

@@ -139,6 +139,3 @@

updater.start_polling() print(updater.bot.name, "is up and running!") updater.idle() - -if __name__ == "__main__": - main()