all repos — groupgardenbot @ 0e04eaf6a49e512e4821d45558b1166c9404708c

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

Gardening.py (view raw)

  1import random, time, math, datetime, os
  2from Constants import *
  3
  4water_duration = 3600 # * 24
  5stage_factors = (1, 3, 10, 20, 30)
  6indicator_squares = 6
  7
  8class Plant(object):
  9    # This is your plant!
 10    def __init__(self, owner, generation=1):
 11        # Constructor
 12        self.points = 0 # one point per second
 13        self.life_stages = tuple(st * water_duration for st in stage_factors)
 14        self.stage = 0
 15        self.mutation = 0
 16        self.species = random.randint(0, len(species_list) - 1)
 17        self.color = random.randint(0, len(color_list) - 1)
 18        self.name = plant_names[random.randint(0, len(plant_names) - 1)]
 19        self.rarity = self.rarity_check()
 20        self.ticks = 0
 21        self.age_formatted = "0"
 22        self.generation = generation
 23        self.generation_bonus = 1 + (0.2 * (generation - 1))
 24        self.dead = False
 25        self.owner = owner
 26        self.start_time = int(time.time())
 27        self.last_time = int(time.time())
 28        self.last_update = int(time.time())
 29        # must water plant first day
 30        #self.last_water = int(time.time())-(24*3600)-1
 31        self.last_water = int(time.time())
 32        self.watered_24h = True
 33        self.visitors = []
 34
 35    def update(self):
 36        # find out stage:
 37        self.water_check()
 38        if self.dead_check(): # updates self.time_delta_watered
 39            return
 40
 41        self.stage = find_stage(self)
 42        
 43    def parse_plant(self):
 44        # Converts plant data to human-readable format
 45        output = ""
 46        if self.stage >= 3:
 47            output += rarity_list[self.rarity] + " "
 48        if self.mutation != 0:
 49            output += mutation_list[self.mutation] + " "
 50        if self.stage >= 4:
 51            output += color_list[self.color] + " "
 52        output += stage_list[self.stage] + " "
 53        if self.stage >= 2:
 54            output += species_list[self.species] + " "
 55        return output.strip()
 56
 57    def rarity_check(self):
 58        # Generate plant rarity
 59        CONST_RARITY_MAX = 256.0
 60        rare_seed = random.randint(1,CONST_RARITY_MAX)
 61        common_range =    round((2.0/3)*CONST_RARITY_MAX)
 62        uncommon_range =  round((2.0/3)*(CONST_RARITY_MAX-common_range))
 63        rare_range =      round((2.0/3)*(CONST_RARITY_MAX-common_range-uncommon_range))
 64        legendary_range = round((2.0/3)*(CONST_RARITY_MAX-common_range-uncommon_range-rare_range))
 65
 66        common_max = common_range
 67        uncommon_max = common_max + uncommon_range
 68        rare_max = uncommon_max + rare_range
 69        legendary_max = rare_max + legendary_range
 70        godly_max = CONST_RARITY_MAX
 71
 72        if 0 <= rare_seed <= common_max:
 73            return 0
 74        elif common_max < rare_seed <= uncommon_max:
 75            return 1
 76        elif uncommon_max < rare_seed <= rare_max:
 77            return 2
 78        elif rare_max < rare_seed <= legendary_max:
 79            return 3
 80        elif legendary_max < rare_seed <= godly_max:
 81            return 4
 82
 83    def dead_check(self):
 84        # if it has been >5 days since watering, sorry plant is dead :(
 85        self.time_delta_watered = int(time.time()) - self.last_water
 86        if self.time_delta_watered > (5 * water_duration):
 87            self.dead = True
 88        return self.dead
 89
 90    def water_check(self):
 91        self.time_delta_watered = int(time.time()) - self.last_water
 92        if self.time_delta_watered <= (water_duration):
 93            if not self.watered_24h:
 94                self.watered_24h = True
 95            return True
 96        else:
 97            self.watered_24h = False
 98            return False
 99
100    def mutate_check(self):
101        # Create plant mutation
102        # Increase this # to make mutation rarer (chance 1 out of x each second)
103        CONST_MUTATION_RARITY = 20000
104        mutation_seed = random.randint(1,CONST_MUTATION_RARITY)
105        if mutation_seed == CONST_MUTATION_RARITY:
106            # mutation gained!
107            mutation = random.randint(0,len(self.mutation_list)-1)
108            if self.mutation == 0:
109                self.mutation = mutation
110                return True
111        else:
112            return False
113
114    def growth(self):
115        # Increase plant growth stage
116        if self.stage < (len(stage_list)-1):
117            self.stage += 1
118
119    def water(self):
120        # Increase plant growth stage
121        if not self.dead:
122            self.last_water = int(time.time())
123            self.watered_24h = True
124
125    def start_over(self):
126        # After plant reaches final stage, given option to restart
127        # increment generation only if previous stage is final stage and plant
128        # is alive
129        if not self.dead:
130            next_generation = self.generation + 1
131        else:
132            # Should this reset to 1? Seems unfair.. for now generations will
133            # persist through death.
134            next_generation = self.generation
135        self.kill_plant()
136        
137        self.__init__(self.owner, next_generation)
138
139    def kill_plant(self):
140        self.dead = True
141
142def find_stage(plant: Plant):
143    now = int(time.time())
144    
145    res1 = min(now - plant.last_water, water_duration)
146    res2 = min(plant.last_update - plant.last_water, water_duration)
147    
148    plant.points += max(0, res1 - res2) # max() not necessary but just in case
149
150    plant.last_update = now
151    
152    stages = tuple(ti / plant.generation_bonus for ti in plant.life_stages) # bonus is applied to stage thresholds
153    count = 0
154    closest = None
155    delta = plant.points
156    
157    for n in stages:
158        if (n <= delta and (closest is None or (delta - n) < (delta - closest))):
159            closest = n
160            count += 1
161    return count
162    
163def get_plant_water(plant: Plant):
164    water_delta = time.time() - plant.last_water
165    water_left_pct = max(0, 1 - (water_delta/water_duration)) # 24h
166    water_left = int(math.ceil(water_left_pct * indicator_squares))
167    return f"{water_left * '🟦'}{'⬛' * (indicator_squares - water_left)} {str(int(water_left_pct * 100))}% "
168
169def get_plant_description(plant: Plant):
170    output_text = ""
171    this_species = species_list[plant.species]
172    this_color = color_list[plant.color]
173    this_stage = plant.stage
174    
175    if plant.dead:
176            this_stage = 99
177    try:
178        description_num = random.randint(0,len(stage_descriptions[this_stage]) - 1)
179    except KeyError as e:
180        print(e)
181        description_num = 0
182    # If not fully grown
183    if this_stage <= 4:
184        # Growth hint
185        if this_stage >= 1:
186            last_growth_at = plant.life_stages[this_stage - 1]
187        else:
188            last_growth_at = 0
189        ticks_since_last = plant.ticks - last_growth_at
190        ticks_between_stage = plant.life_stages[this_stage] - last_growth_at
191        if ticks_since_last >= ticks_between_stage * 0.8:
192            output_text += "You notice your plant looks different.\n"
193
194    output_text += get_stage_description(this_stage, description_num, this_species, this_color) + "\n"
195
196    # if seedling
197    if this_stage == 1:
198        species_options = [species_list[plant.species],
199                species_list[(plant.species+3) % len(species_list)],
200                species_list[(plant.species-3) % len(species_list)]]
201        random.shuffle(species_options)
202        plant_hint = "It could be a(n) " + species_options[0] + ", " + species_options[1] + ", or " + species_options[2]
203        output_text += plant_hint + ".\n"
204
205    # if young plant
206    if this_stage == 2:
207        if plant.rarity >= 2:
208            rarity_hint = "You feel like your plant is special."
209            output_text += rarity_hint + ".\n"
210
211    # if mature plant
212    if this_stage == 3:
213        color_options = [color_list[plant.color],
214                color_list[(plant.color+3) % len(color_list)],
215                color_list[(plant.color-3) % len(color_list)]]
216        random.shuffle(color_options)
217        return "You can see the first hints of " + color_options[0] + ", " + color_options[1] + ", or " + color_options[2]
218
219    return output_text
220
221def get_plant_art(plant: Plant):
222    
223    if plant.dead == True:
224        filename = 'rip.txt'
225    elif datetime.date.today().month == 10 and datetime.date.today().day == 31:
226        filename = 'jackolantern.txt'
227    elif plant.stage == 0:
228        filename = 'seed.txt'
229    elif plant.stage == 1:
230        filename = 'seedling.txt'
231    elif plant.stage == 2:
232        filename = plant_art_list[plant.species]+'1.txt'
233    elif plant.stage == 3 or plant.stage == 5:
234        filename = plant_art_list[plant.species]+'2.txt'
235    elif plant.stage == 4:
236        filename = plant_art_list[plant.species]+'3.txt'
237
238    # Prints ASCII art from file at given coordinates
239    this_dir = os.path.join(os.path.dirname(os.path.realpath(__file__)), "art")
240    this_filename = os.path.join(this_dir, filename)
241    this_file = open(this_filename,"r")
242    this_string = this_file.read()
243    this_file.close()
244    return this_string
245
246def get_plant_info(plant: Plant):
247    
248    return f'''
249{get_plant_description(plant)}
250```{get_plant_art(plant)}```
251{plant.name}, the {plant.parse_plant()}
252
253{get_plant_water(plant)}
254
255Points: {plant.points}
256Bonus: x{plant.generation_bonus - 1}
257Owner: {plant.owner}
258'''