all repos — groupgardenbot @ 1ec1debfd56b3a392a334872571b618d50472845

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

major code refactor
Bi-Rabittoh andronacomarco@gmail.com
Sun, 25 Sep 2022 00:59:05 +0200
commit

1ec1debfd56b3a392a334872571b618d50472845

parent

dd587ca3c8ea6677ed6671c9aa264c494d3d681e

3 files changed, 143 insertions(+), 126 deletions(-)

jump to
M Constants.pyConstants.py

@@ -22,53 +22,53 @@ "Your seedling grows slowly and quietly.",

"You meditate on the paths your plant's life could take.", ], 2:[ - "The {plant} makes you feel relaxed.", - "You sing a song to your {plant}.", - "You quietly sit with your {plant} for a few minutes.", - "Your {plant} looks pretty good.", - "You play loud techno to your {plant}.", - "You play piano to your {plant}.", - "You play rap music to your {plant}.", - "You whistle a tune to your {plant}.", - "You read a poem to your {plant}.", - "You tell a secret to your {plant}.", - "You play your favorite record for your {plant}.", + "The {species} makes you feel relaxed.", + "You sing a song to your {species}.", + "You quietly sit with your {species} for a few minutes.", + "Your {species} looks pretty good.", + "You play loud techno to your {species}.", + "You play piano to your {species}.", + "You play rap music to your {species}.", + "You whistle a tune to your {species}.", + "You read a poem to your {species}.", + "You tell a secret to your {species}.", + "You play your favorite record for your {species}.", ], 3:[ - "Your {plant} is growing nicely!", - "You're proud of the dedication it took to grow your {plant}.", - "You take a deep breath with your {plant}.", - "You think of all the words that rhyme with {plant}.", - "The {plant} looks full of life.", - "The {plant} inspires you.", - "Your {plant} makes you forget about your problems.", - "Your {plant} gives you a reason to keep going.", - "Looking at your {plant} helps you focus on what matters.", - "You think about how nice this {plant} looks here.", - "The buds of your {plant} might bloom soon.", + "Your {species} is growing nicely!", + "You're proud of the dedication it took to grow your {species}.", + "You take a deep breath with your {species}.", + "You think of all the words that rhyme with {species}.", + "The {species} looks full of life.", + "The {species} inspires you.", + "Your {species} makes you forget about your problems.", + "Your {species} gives you a reason to keep going.", + "Looking at your {species} helps you focus on what matters.", + "You think about how nice this {species} looks here.", + "The buds of your {species} might bloom soon.", ], 4:[ - "The {color} flowers look nice on your {plant}!", + "The {color} flowers look nice on your {species}!", "The {color} flowers have bloomed and fill you with positivity.", "The {color} flowers remind you of your childhood.", "The {color} flowers remind you of spring mornings.", "The {color} flowers remind you of a forgotten memory.", "The {color} flowers remind you of your happy place.", "The aroma of the {color} flowers energize you.", - "The {color} {plant} has grown beautiful flowers." + "The {color} {species} has grown beautiful flowers." "The {color} petals remind you of that favorite shirt you lost.", "The {color} flowers remind you of your crush.", "You smell the {color} flowers and are filled with peace.", ], 5:[ - "You fondly remember the time you spent caring for your {plant}.", - "Seed pods have grown on your {plant}.", - "You feel like your {plant} appreciates your care.", - "The {plant} fills you with love.", - "You're ready for whatever comes after your {plant}.", + "You fondly remember the time you spent caring for your {species}.", + "Seed pods have grown on your {species}.", + "You feel like your {species} appreciates your care.", + "The {species} fills you with love.", + "You're ready for whatever comes after your {species}.", "You're excited to start growing your next plant.", - "You reflect on when your {plant} was just a seedling.", - "You grow nostalgic about the early days with your {plant}.", + "You reflect on when your {species} was just a seedling.", + "You grow nostalgic about the early days with your {species}.", ], 99:[ "You wish you had taken better care of your plant.",
M Gardening.pyGardening.py

@@ -2,9 +2,9 @@ import random, os, time, datetime

from Constants import * water_duration = 3600 * 24 -death_duration = 6 * water_duration +death_duration = 5 * water_duration stage_factors = (1, 3, 10, 20, 30) -indicator_squares = 6 +indicator_squares = 10 mutation_rarity = 20000 # Increase this # to make mutation rarer (chance 1 out of x each second) max_plant_rarity = 256.0

@@ -23,10 +23,14 @@ self.rarity = self.rarity_check()

self.generation = generation self.generation_bonus = 1 + (0.2 * (generation - 1)) self.dead = False - self.owner = owner + self.owner = owner.id + self.owner_name = owner.full_name + self.age_days = 0 self.start_time = int(time.time()) self.last_update = self.start_time self.last_water = self.start_time - water_duration + self.last_water_user = owner.id + self.last_water_name = "" def update(self): now = int(time.time())

@@ -34,7 +38,8 @@ water_delta = now - self.last_water

if water_delta > death_duration: self.dead = True return - + self.age_days = round((now - self.start_time) / water_duration) + increase = min(water_delta, water_duration) - min(self.last_update - self.last_water, water_duration) if increase != 0:

@@ -105,97 +110,93 @@ return True

else: return False - def water(self): + def water(self, who): if not self.dead: self.last_water = int(time.time()) + self.last_water_user = who.id + self.last_water_name = who.full_name - def start_over(self): + def start_over(self, owner): next_generation = self.generation if self.dead else self.generation + 1 - self.__init__(self.owner, next_generation) + self.__init__(owner, next_generation) -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(round(water_left_pct * indicator_squares)) - return f"{water_left * '🟦'}{'⬛' * (indicator_squares - water_left)} {str(round(water_left_pct * 100))}% " + def get_water(self): + water_delta = int(time.time()) - self.last_water + water_left_pct = max(0, 1 - (water_delta/water_duration)) + 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): - output_text = "" - this_species = species_list[plant.species] - this_color = color_list[plant.color] - this_stage = plant.stage - - if plant.dead: - this_stage = 99 - try: - description_num = random.randint(0,len(stage_descriptions[this_stage]) - 1) - except KeyError as e: - print(e) - description_num = 0 - # If not fully grown - if this_stage <= 4: - # Growth hint - if this_stage >= 1: - last_growth_at = plant.life_stages[this_stage - 1] - else: - last_growth_at = 0 - 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" + def get_water_ascii(self): + water_delta = int(time.time()) - self.last_water + water_left_pct = max(0, 1 - (water_delta/water_duration)) + water_left = int(round(water_left_pct * indicator_squares)) + return f"|{water_left * '█'}{' ' * (indicator_squares - water_left)}| {str(round(water_left_pct * 100))}%" - output_text += get_stage_description(this_stage, description_num, this_species, this_color) + "\n" + def get_filename(self): + if self.dead == True: return 'rip.txt' + if datetime.date.today().month == 10 and datetime.date.today().day == 31: return 'jackolantern.txt' + if self.stage == 0: return 'seed.txt' + if self.stage == 1: return 'seedling.txt' + if self.stage == 2: return species_list[self.species]+'1.txt' + if self.stage == 3 or self.stage == 5: return species_list[self.species]+'2.txt' + if self.stage == 4: return species_list[self.species]+'3.txt' + return "template.txt" - # 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)]] - random.shuffle(species_options) - 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: - output_text += "You feel like your plant is special.\n" + def get_art(self): + filename = self.get_filename() + # Prints ASCII art from file at given coordinates + this_dir = os.path.join(os.path.dirname(os.path.realpath(__file__)), "art") + this_filename = os.path.join(this_dir, filename) + this_file = open(this_filename,"r") + this_string = this_file.read() + this_file.close() + return this_string - # if mature plant - if this_stage == 3: - 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) - output_text += f"You can see the first hints of {color_options[0]}, {color_options[1]}, or {color_options[2]}.\n" + def get_description(self): + output_text = "" + this_species = species_list[self.species] + this_color = color_list[self.color] + this_stage = self.stage - return output_text - -def get_art_filename(plant: Plant): - 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 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" + if self.dead: + this_stage = 99 + try: + description_num = random.randint(0,len(stage_descriptions[this_stage]) - 1) + except KeyError as e: + print(e) + description_num = 0 + # If not fully grown + if this_stage <= 4: + # Growth hint + if this_stage >= 1: + last_growth_at = self.life_stages[this_stage - 1] + else: + last_growth_at = 0 + ticks_since_last = self.points - last_growth_at + ticks_between_stage = self.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" -def get_plant_art(plant: Plant): - filename = get_art_filename(plant) - # Prints ASCII art from file at given coordinates - this_dir = os.path.join(os.path.dirname(os.path.realpath(__file__)), "art") - this_filename = os.path.join(this_dir, filename) - this_file = open(this_filename,"r") - this_string = this_file.read() - this_file.close() - return this_string + output_text += get_stage_description(this_stage, description_num, this_species, this_color) + "\n" -def get_plant_info(plant: Plant): - return f''' -{get_plant_description(plant)} -```{get_plant_art(plant)}``` -[{plant.name}, the {plant.parse_plant()}](tg://user?id={plant.owner}) + # if seedling + if this_stage == 1: + species_options = [species_list[self.species], + species_list[(self.species + 3) % len(species_list)], + species_list[(self.species - 3) % len(species_list)]] + random.shuffle(species_options) + 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 self.rarity >= 2: + output_text += "You feel like your plant is special.\n" -{get_plant_water(plant)} + # if mature plant + if this_stage == 3: + color_options = [color_list[self.color], + color_list[(self.color + 3) % len(color_list)], + color_list[(self.color - 3) % len(color_list)]] + random.shuffle(color_options) + output_text += f"You can see the first hints of {color_options[0]}, {color_options[1]}, or {color_options[2]}.\n" -Bonus: x{plant.generation_bonus - 1} -Points: {plant.points} -''' + return output_text
M main.pymain.py

@@ -1,8 +1,8 @@

-import os, logging +import os, logging, time from dotenv import load_dotenv -from telegram import Update, InlineKeyboardMarkup, InlineKeyboardButton +from telegram import Update, User, InlineKeyboardMarkup, InlineKeyboardButton from telegram.ext import Updater, CallbackContext, CallbackQueryHandler, CommandHandler, PicklePersistence -from Gardening import Plant, get_plant_info +from Gardening import Plant logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', level=logging.INFO) load_dotenv()

@@ -12,6 +12,22 @@

def edit(update: Update, context: CallbackContext, text: str = "", markup: str = ""): context.bot.editMessageText(chat_id=update.effective_chat.id, message_id=update.effective_message.message_id, text=text, reply_markup=markup, parse_mode='Markdown') +def get_plant_info(plant: Plant): + return f''' +```{plant.get_art()} +owner : {plant.owner_name} +name : {plant.name} +stage : {plant.parse_plant()} +age : {plant.age_days} days +score : {plant.points} +bonus : x{plant.generation_bonus - 1} +water : {plant.get_water_ascii()}``` + +{plant.get_description()} + +{f'Last watered by {plant.last_water_name}.' if plant.last_water_user != plant.owner else ""} +''' + def get_plant(context: CallbackContext, user_id: int): try: plant = context.bot_data[user_id]["plant"]

@@ -35,12 +51,12 @@ plant = get_plant(context, user_id)

new = False if plant is None: - plant = Plant(user_id) - context.bot_data[update.effective_user.id] = { "plant" : plant } + plant = Plant(update.effective_user) + context.bot_data[user_id] = { "plant" : plant } new = True if plant.dead or plant.stage == 5: - plant.start_over() + plant.start_over(update.effective_user) new = True if new:

@@ -49,7 +65,7 @@ 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!") -def water(context: CallbackContext, user_id: int): +def water(context: CallbackContext, user_id: int, who: User): plant = get_plant(context, user_id) if plant is None:

@@ -57,8 +73,8 @@ return "Non hai niente da innaffiare! Usa /start per piantare un seme."

if plant.dead: return "La pianta è morta..." - - plant.water() + + plant.water(who) return "Pianta innaffiata." def show(context: CallbackContext, user_id: int):

@@ -87,7 +103,7 @@ 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) + answer = water(context, update.effective_user.id, update.effective_user) return reply(update, context, answer) def show_handler(update: Update, context: CallbackContext):

@@ -107,7 +123,7 @@ user_id = None

if data.startswith("water"): user_id = int(data.split(" ")[1]) - answer = water(context, user_id) + answer = water(context, user_id, update.effective_user) query.answer(answer) if data.startswith("show"):