consolidate autosave into life thread

pull/53/head
Nate Smith 2023-12-06 21:52:40 -08:00
parent 8d45997537
commit 9162888a7d
2 changed files with 13 additions and 26 deletions

View File

@ -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)

View File

@ -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)