Synchronize plant and menu updatesw

This commit is contained in:
Jake Funke 2017-03-08 21:22:50 +00:00
parent 5fc813acb7
commit 02476fcaf7
2 changed files with 47 additions and 19 deletions

View File

@ -147,7 +147,7 @@ class Plant(object):
self.file_name = this_filename
self.start_time = int(time.time())
self.last_time = int(time.time())
self.watered_date = datetime.datetime.fromtimestamp(0)
self.watered_timestamp = int(0)
self.watered_times = 0
def rarity_check(self):
@ -192,7 +192,7 @@ class Plant(object):
def water(self):
# Increase plant growth stage
self.watered_date = datetime.datetime.now().date()
self.watered_timestamp = int(time.time())
self.watered_times += 1
def mutate_check(self):
@ -236,6 +236,8 @@ class Plant(object):
# I've created life :)
# TODO: change out of debug
life_stages = (5, 15, 30, 45, 60)
day = 3600*24
# life_stages = (1*day, 2*day, 3*day, 4*day, 5*day)
# life_stages = (1, 2, 3, 4, 5)
# leave this untouched bc it works for now
while (self.stage < 5) or (self.dead == False):
@ -305,10 +307,10 @@ class DataManager(object):
# TODO: this needs to check the current ticks w/ life stage
# compare timestamp of signout to timestamp now
time_delta_last = current_timestamp - this_plant.last_time
time_delta_watered = int((current_date - this_plant.watered_date).days)
time_delta_watered = int(time.time()) - this_plant.watered_timestamp
# if it has been >5 days since watering, sorry plant is dead :(
if time_delta_watered > 5:
if time_delta_watered > 5 * (24 * 3600):
this_plant.dead = True
this_plant.ticks += time_delta_last
@ -316,7 +318,6 @@ class DataManager(object):
def data_write_json(self, this_plant):
# create json file for user to use outside of the game (website?)
this_plant.watered_date = this_plant.watered_date.strftime("%Y-%m-%d")
json_file = os.path.join(self.botany_dir,self.this_user + '_plant_data.json')
with open(json_file, 'w') as outfile:
json.dump(this_plant.__dict__, outfile)
@ -336,7 +337,7 @@ if __name__ == '__main__':
my_plant.start_life()
#print "Your plant is living :)"
botany_menu = CursedMenu(my_plant)
botany_menu.show([1,"water",3], title=' botany ', subtitle='Options')
botany_menu.show([1,"water","look","instructions"], title=' botany ', subtitle='Options')
#raw_input('Press return to save and exit...\n')
my_data.save_plant(my_plant)
my_data.data_write_json(my_plant)

View File

@ -2,6 +2,7 @@ import curses, os, traceback, threading, time, datetime
class CursedMenu(object):
#TODO: create a side panel with log of events..?
# TODO: display that updates on either keypress OR tick
'''A class which abstracts the horrors of building a curses-based menu system'''
def __init__(self, this_plant):
'''Initialization'''
@ -12,8 +13,11 @@ class CursedMenu(object):
curses.curs_set(0)
self.screen.keypad(1)
self.plant = this_plant
self.plant_string = self.plant.parse_plant()
self.plant_ticks = str(self.plant.ticks)
self.exit = False
self.instructiontoggle = False
self.maxy, self.maxx = self.screen.getmaxyx()
# Highlighted and Normal line definitions
curses.init_pair(1, curses.COLOR_BLACK, curses.COLOR_WHITE)
self.highlighted = curses.color_pair(1)
@ -69,6 +73,13 @@ class CursedMenu(object):
textstyle = self.highlighted
self.screen.addstr(5+index,4, "%d - %s" % (index+1, self.options[index]), textstyle)
self.screen.addstr(10,2, self.plant_string, curses.A_NORMAL)
self.screen.addstr(11,2, self.plant_ticks, curses.A_NORMAL)
if int(time.time()) <= self.plant.watered_timestamp + 5*24*3600:
self.screen.addstr(6,13, " - plant watered today :)", curses.A_NORMAL)
else:
self.screen.addstr(6,13, " ", curses.A_NORMAL)
try:
self.screen.refresh()
except Exception as exception:
@ -77,18 +88,12 @@ class CursedMenu(object):
traceback.print_exc()
def update_plant_live(self):
# TODO: fix thread synchronization issue.. text garbling
# Updates plant data on menu screen, live!
# Will eventually use this to display ascii art...
while self.exit is not True:
plant_string = self.plant.parse_plant()
plant_ticks = str(self.plant.ticks)
self.screen.addstr(10,2, plant_string, curses.A_NORMAL)
self.screen.addstr(11,2, plant_ticks, curses.A_NORMAL)
if self.plant.watered_date == datetime.datetime.now().date():
self.screen.addstr(6,13, " - plant watered today :)", curses.A_NORMAL)
self.screen.refresh()
self.plant_string = self.plant.parse_plant()
self.plant_ticks = str(self.plant.ticks)
self.draw()
time.sleep(1)
def get_user_input(self):
@ -118,11 +123,33 @@ class CursedMenu(object):
def handle_request(self, request):
'''This is where you do things with the request'''
if request is None: return
if request == 1:
self.screen.addstr(8,15,str(self.plant.ticks), curses.A_STANDOUT) # Title for this menu
self.screen.refresh()
if request == "water":
self.plant.water()
if request == "instructions":
if not self.instructiontoggle:
instructions_txt = """welcome to botany. you've been given a seed
that will grow into a beautiful plant. check
in and water your plant every day to keep it
alive. it depends on you to live! More info
is available in the readme :)
cheers,
curio"""
self.instructiontoggle = not self.instructiontoggle
else:
instructions_txt = """
"""
# TODO:
self.instructiontoggle = not self.instructiontoggle
# self.screen.clear()
for y, line in enumerate(instructions_txt.splitlines(), 2):
self.screen.addstr(y,self.maxx-50, line)
#self.screen.addstr(8,15,str(self.plant.ticks), curses.A_STANDOUT) # Title for this menu
self.screen.refresh()
def __exit__(self):