From 9162888a7daea867ff57ec74c62cb0ba59f564bf Mon Sep 17 00:00:00 2001 From: Nate Smith Date: Wed, 6 Dec 2023 21:52:40 -0800 Subject: [PATCH] consolidate autosave into life thread --- botany.py | 24 ------------------------ plant.py | 15 +++++++++++++-- 2 files changed, 13 insertions(+), 26 deletions(-) diff --git a/botany.py b/botany.py index 8525f9e..b5ef833 100755 --- a/botany.py +++ b/botany.py @@ -20,8 +20,6 @@ from plant import Plant # there are threads. # - life thread. sleeps a variable amount of time based on generation bonus. increases tick count (ticks == score). -# - death check; sleeps .1 per loop. checks if plant is dead and harvests it if so. -# - autosave: saves plant file every 5 seconds. every 60 seconds, updates the garden db # - screen: sleeps 1s per loop. draws interface (including plant). for seeing score/plant change without user input. # meanwhile, the main thread handles input and redraws curses as needed. @@ -88,27 +86,6 @@ class DataManager(object): else: return False - def start_threads(self,this_plant): - # creates threads to save files every minute - autosave_thread = threading.Thread(target=self.autosave, args=(this_plant,)) - autosave_thread.daemon = True - autosave_thread.start() - - def autosave(self, this_plant): - # running on thread, saves plant every 5s TODO: this is unnecessary - # and breaks shit probably - file_update_count = 0 - while True: - file_update_count += 1 - self.save_plant(this_plant) - self.data_write_json(this_plant) - self.update_garden_db(this_plant) - if file_update_count == 12: - # only update garden json every 60s - self.update_garden_json() - time.sleep(5) - file_update_count %= 12 - def load_plant(self): # load savefile with open(self.savefile_path, 'rb') as f: @@ -319,7 +296,6 @@ if __name__ == '__main__': my_data.data_write_json(my_plant) # my_plant is either a fresh plant or an existing plant at this point my_plant.start_life(my_data) - my_data.start_threads(my_plant) try: botany_menu = ms.CursedMenu(my_plant,my_data) diff --git a/plant.py b/plant.py index 2d95ee8..e533963 100644 --- a/plant.py +++ b/plant.py @@ -331,9 +331,11 @@ class Plant: def life(self, dm): # I've created life :) + counter = 0 generation_bonus = round(0.2 * (self.generation - 1), 1) score_inc = 1 * (1 + generation_bonus) while True: + counter += 1 if not self.dead: if self.watered_24h: self.ticks += score_inc @@ -342,14 +344,23 @@ class Plant: self.growth() if self.mutate_check(): pass + if self.water_check(): # Do something pass + if self.dead_check(): + dm.harvest_plant(self) + self.unlock_new_creation() + + if counter % 3 == 0: dm.save_plant(self) dm.data_write_json(self) dm.update_garden_db(self) - dm.harvest_plant(self) - self.unlock_new_creation() + + if counter % 30 == 0: + self.update_garden_json() + counter = 0 + # TODO: event check time.sleep(2)