consolidate autosave into life thread
parent
8d45997537
commit
9162888a7d
24
botany.py
24
botany.py
|
@ -20,8 +20,6 @@ from plant import Plant
|
||||||
|
|
||||||
# there are threads.
|
# there are threads.
|
||||||
# - life thread. sleeps a variable amount of time based on generation bonus. increases tick count (ticks == score).
|
# - 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.
|
# - 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.
|
# meanwhile, the main thread handles input and redraws curses as needed.
|
||||||
|
|
||||||
|
@ -88,27 +86,6 @@ class DataManager(object):
|
||||||
else:
|
else:
|
||||||
return False
|
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):
|
def load_plant(self):
|
||||||
# load savefile
|
# load savefile
|
||||||
with open(self.savefile_path, 'rb') as f:
|
with open(self.savefile_path, 'rb') as f:
|
||||||
|
@ -319,7 +296,6 @@ if __name__ == '__main__':
|
||||||
my_data.data_write_json(my_plant)
|
my_data.data_write_json(my_plant)
|
||||||
# my_plant is either a fresh plant or an existing plant at this point
|
# my_plant is either a fresh plant or an existing plant at this point
|
||||||
my_plant.start_life(my_data)
|
my_plant.start_life(my_data)
|
||||||
my_data.start_threads(my_plant)
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
botany_menu = ms.CursedMenu(my_plant,my_data)
|
botany_menu = ms.CursedMenu(my_plant,my_data)
|
||||||
|
|
15
plant.py
15
plant.py
|
@ -331,9 +331,11 @@ class Plant:
|
||||||
|
|
||||||
def life(self, dm):
|
def life(self, dm):
|
||||||
# I've created life :)
|
# I've created life :)
|
||||||
|
counter = 0
|
||||||
generation_bonus = round(0.2 * (self.generation - 1), 1)
|
generation_bonus = round(0.2 * (self.generation - 1), 1)
|
||||||
score_inc = 1 * (1 + generation_bonus)
|
score_inc = 1 * (1 + generation_bonus)
|
||||||
while True:
|
while True:
|
||||||
|
counter += 1
|
||||||
if not self.dead:
|
if not self.dead:
|
||||||
if self.watered_24h:
|
if self.watered_24h:
|
||||||
self.ticks += score_inc
|
self.ticks += score_inc
|
||||||
|
@ -342,14 +344,23 @@ class Plant:
|
||||||
self.growth()
|
self.growth()
|
||||||
if self.mutate_check():
|
if self.mutate_check():
|
||||||
pass
|
pass
|
||||||
|
|
||||||
if self.water_check():
|
if self.water_check():
|
||||||
# Do something
|
# Do something
|
||||||
pass
|
pass
|
||||||
|
|
||||||
if self.dead_check():
|
if self.dead_check():
|
||||||
|
dm.harvest_plant(self)
|
||||||
|
self.unlock_new_creation()
|
||||||
|
|
||||||
|
if counter % 3 == 0:
|
||||||
dm.save_plant(self)
|
dm.save_plant(self)
|
||||||
dm.data_write_json(self)
|
dm.data_write_json(self)
|
||||||
dm.update_garden_db(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
|
# TODO: event check
|
||||||
time.sleep(2)
|
time.sleep(2)
|
||||||
|
|
Loading…
Reference in New Issue