Add plant generations

pull/5/head
Jake Funke 2017-05-03 19:51:21 +00:00
parent 2786a46397
commit e0432f149f
2 changed files with 41 additions and 10 deletions

View File

@ -119,7 +119,7 @@ class Plant(object):
39: 'colossal', 39: 'colossal',
} }
def __init__(self, this_filename): def __init__(self, this_filename, generation=1):
# Constructor # Constructor
self.plant_id = str(uuid.uuid4()) self.plant_id = str(uuid.uuid4())
self.life_stages = (3600*24, (3600*24)*3, (3600*24)*10, (3600*24)*20, (3600*24)*30) self.life_stages = (3600*24, (3600*24)*3, (3600*24)*10, (3600*24)*20, (3600*24)*30)
@ -130,6 +130,7 @@ class Plant(object):
self.rarity = self.rarity_check() self.rarity = self.rarity_check()
self.ticks = 0 self.ticks = 0
self.age_formatted = "0" self.age_formatted = "0"
self.generation = generation
self.dead = False self.dead = False
self.write_lock = False self.write_lock = False
self.owner = getpass.getuser() self.owner = getpass.getuser()
@ -140,6 +141,11 @@ class Plant(object):
self.watered_timestamp = int(time.time())-(24*3600)-1 self.watered_timestamp = int(time.time())-(24*3600)-1
self.watered_24h = False self.watered_24h = False
def migrate_properties(self):
# Migrates old data files to new
if not hasattr(self, 'generation'):
self.generation = 1
def parse_plant(self): def parse_plant(self):
# Converts plant data to human-readable format # Converts plant data to human-readable format
output = "" output = ""
@ -213,10 +219,6 @@ class Plant(object):
else: else:
return False return False
def new_seed(self,this_filename):
# Creates life after death
self.__init__(this_filename)
def growth(self): def growth(self):
# Increase plant growth stage # Increase plant growth stage
if self.stage < (len(self.stage_dict)-1): if self.stage < (len(self.stage_dict)-1):
@ -230,6 +232,13 @@ class Plant(object):
def start_over(self): def start_over(self):
# After plant reaches final stage, given option to restart # After plant reaches final stage, given option to restart
# increment generation only if previous stage is final stage and plant
# is alive
if not self.dead:
next_generation = self.generation + 1
else:
next_generation = 1
self.write_lock = True self.write_lock = True
self.kill_plant() self.kill_plant()
while self.write_lock: while self.write_lock:
@ -237,7 +246,7 @@ class Plant(object):
# garden db needs to update before allowing the user to reset # garden db needs to update before allowing the user to reset
pass pass
if not self.write_lock: if not self.write_lock:
self.new_seed(self.file_name) self.__init__(self.file_name, next_generation)
def kill_plant(self): def kill_plant(self):
self.dead = True self.dead = True
@ -346,6 +355,10 @@ class DataManager(object):
with open(self.savefile_path, 'rb') as f: with open(self.savefile_path, 'rb') as f:
this_plant = pickle.load(f) this_plant = pickle.load(f)
# migrate data structure to create data for empty/nonexistent plant
# properties
this_plant.migrate_properties()
# get status since last login # get status since last login
is_dead = this_plant.dead_check() is_dead = this_plant.dead_check()
is_watered = this_plant.water_check() is_watered = this_plant.water_check()
@ -474,7 +487,7 @@ class DataManager(object):
plant_info["color"] = this_plant.color_dict[this_plant.color] plant_info["color"] = this_plant.color_dict[this_plant.color]
if this_plant.stage >= 2: if this_plant.stage >= 2:
plant_info["species"] = this_plant.species_dict[this_plant.species] plant_info["species"] = this_plant.species_dict[this_plant.species]
with open(json_file, 'w') as outfile: with open(json_file, 'w') as outfile:
json.dump(plant_info, outfile) json.dump(plant_info, outfile)
@ -520,9 +533,9 @@ if __name__ == '__main__':
else: else:
my_plant = Plant(my_data.savefile_path) my_plant = Plant(my_data.savefile_path)
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.start_life() my_plant.start_life()
my_data.start_threads(my_plant) my_data.start_threads(my_plant)
# TODO: curses wrapper
botany_menu = CursedMenu(my_plant,my_data) botany_menu = CursedMenu(my_plant,my_data)
my_data.save_plant(my_plant) my_data.save_plant(my_plant)
my_data.data_write_json(my_plant) my_data.data_write_json(my_plant)

View File

@ -26,7 +26,7 @@ class CursedMenu(object):
self.infotoggle = 0 self.infotoggle = 0
self.maxy, self.maxx = self.screen.getmaxyx() self.maxy, self.maxx = self.screen.getmaxyx()
# Highlighted and Normal line definitions # Highlighted and Normal line definitions
curses.init_pair(1, curses.COLOR_BLACK, curses.COLOR_WHITE) self.define_colors()
self.highlighted = curses.color_pair(1) self.highlighted = curses.color_pair(1)
self.normal = curses.A_NORMAL self.normal = curses.A_NORMAL
# Threaded screen update for live changes # Threaded screen update for live changes
@ -36,6 +36,17 @@ class CursedMenu(object):
self.screen.clear() self.screen.clear()
self.show(["water","look","garden","instructions"], title=' botany ', subtitle='options') self.show(["water","look","garden","instructions"], title=' botany ', subtitle='options')
def define_colors(self):
# set curses color pairs manually
curses.init_pair(1, curses.COLOR_BLACK, curses.COLOR_WHITE)
curses.init_pair(2, curses.COLOR_WHITE, curses.COLOR_BLACK)
curses.init_pair(3, curses.COLOR_GREEN, curses.COLOR_BLACK)
curses.init_pair(4, curses.COLOR_BLUE, curses.COLOR_BLACK)
curses.init_pair(5, curses.COLOR_MAGENTA, curses.COLOR_BLACK)
curses.init_pair(6, curses.COLOR_YELLOW, curses.COLOR_BLACK)
curses.init_pair(7, curses.COLOR_RED, curses.COLOR_BLACK)
curses.init_pair(8, curses.COLOR_CYAN, curses.COLOR_BLACK)
def show(self, options, title, subtitle): def show(self, options, title, subtitle):
# Draws a menu with parameters # Draws a menu with parameters
self.set_options(options) self.set_options(options)
@ -465,6 +476,7 @@ class CursedMenu(object):
self.screen.refresh() self.screen.refresh()
def draw_info_text(self, info_text): def draw_info_text(self, info_text):
# print lines of text to info pane at bottom of screen
if type(info_text) is str: if type(info_text) is str:
info_text = info_text.splitlines() info_text = info_text.splitlines()
@ -475,7 +487,13 @@ class CursedMenu(object):
def harvest_confirmation(self): def harvest_confirmation(self):
self.clear_info_pane() self.clear_info_pane()
# get plant description before printing # get plant description before printing
harvest_text = "If you harvest your plant you'll start over from a seed.\nContinue? (Y/n)" max_stage = len(self.plant.stage_dict) - 1
harvest_text = ""
if not self.plant.dead:
if self.plant.stage == max_stage:
harvest_text += "Congratulations! You raised your plant to its final stage of growth.\n"
harvest_text += "Generation: {}\n".format(str(self.plant.generation))
harvest_text += "If you harvest your plant you'll start over from a seed.\nContinue? (Y/n)"
self.draw_info_text(harvest_text) self.draw_info_text(harvest_text)
try: try:
user_in = self.screen.getch() # Gets user input user_in = self.screen.getch() # Gets user input