Update data management + bug fixes

pull/1/head
Jake Funke 2017-03-07 21:04:14 +00:00
parent 852c672198
commit a87bc5bae4
1 changed files with 73 additions and 27 deletions

100
botany.py
View File

@ -15,7 +15,6 @@ import threading
# seed -> seedling -> sprout -> young plant -> mature plant -> flower ->
# pollination -> fruit -> seeds
# mutations over time to yield different things? idk
# neighboring plants can cross pollinate for different plants
# health based on checkups and watering
@ -27,8 +26,24 @@ import threading
# interaction
# - watering?
# - look at plant, how do you feel?
# - fertilize?
# - look at plant, how do you feel? (also gets rid of pests)
#
# if >5 days with no water, plant dies
# events
# - heatwave
# - rain
# - bugs
#
# neighborhood system
# - create plant id (sort of like userid)
# - list sorted by plantid that wraps so everybody has 2 neighbors :)
# - can water neighbors plant once (have to choose which)
# - pollination - seed is combination of your plant and neighbor plant
# - create rarer species by diff gens
# - if neighbor plant dies, node will be removed from list
#
# garden system
# - can plant your plant in the garden to start a new plant
# build time system
# build persistence across sessions
@ -55,6 +70,7 @@ import threading
# curses.endwin()
class Plant(object):
# This is your plant!
stage_dict = {
0: 'seed',
1: 'seedling',
@ -119,7 +135,8 @@ class Plant(object):
19: 'glossy',
}
def __init__(self):
def __init__(self, this_filename):
# Constructor
self.stage = 0
self.mutation = 0
self.species = random.randint(0,len(self.species_dict)-1)
@ -127,8 +144,10 @@ class Plant(object):
self.rarity = self.rarity_check()
self.ticks = 0
self.dead = False
self.filename = this_filename
def rarity_check(self):
# Generate plant rarity
CONST_RARITY_MAX = 256.0
rare_seed = random.randint(1,CONST_RARITY_MAX)
common_range = round((2/3)*CONST_RARITY_MAX)
@ -159,19 +178,33 @@ class Plant(object):
return rarity
def growth(self):
# Increase plant growth stage
if self.stage < (len(self.stage_dict)-1):
self.stage += 1
# do stage growth stuff
CONST_MUTATION_RARITY = 9 # Increase this # to make mutation rarer (chance 1 out of x)
mutation_seed = random.randint(0,CONST_MUTATION_RARITY)
if mutation_seed == CONST_MUTATION_RARITY:
mutation = random.randint(0,len(self.mutation_dict)-1)
if self.mutation == 0:
self.mutation = mutation
else:
# do stage 5 stuff (after fruiting)
1==1
def mutate_check(self):
# Create plant mutation
# TODO: when out of debug this needs to be set to high number (1000
# even maybe)
CONST_MUTATION_RARITY = 10 # Increase this # to make mutation rarer (chance 1 out of x)
mutation_seed = random.randint(1,CONST_MUTATION_RARITY)
if mutation_seed == CONST_MUTATION_RARITY:
# mutation gained!
mutation = random.randint(0,len(self.mutation_dict)-1)
if self.mutation == 0:
self.mutation = mutation
print "mutation!"
return True
else:
return False
def parse_plant(self):
# reads plant info (maybe want to reorg this into a different class
# with the reader dicts...)
output = ""
output += self.rarity_dict[self.rarity] + " "
if self.mutation != 0:
@ -184,16 +217,16 @@ class Plant(object):
print output
def start_life(self):
# runs forever
# runs life on a thread
thread = threading.Thread(target=self.life, args=())
thread.daemon = True
thread.start()
def life(self):
# I've created life :)
# TODO: change out of debug
# life_stages = (5, 15, 30, 45, 60)
life_stages = (1, 2, 3, 4, 5)
# TODO: stopped here 3/6 evening
# leave this untouched bc it works for now
while (self.stage < 5) or (self.dead == False):
time.sleep(1)
@ -203,8 +236,8 @@ class Plant(object):
if self.ticks == life_stages[self.stage]:
self.growth()
self.parse_plant()
# raw_input("...")
if self.mutate_check():
self.parse_plant()
# what kills the plant?
@ -215,47 +248,60 @@ class Plant(object):
# my_plant.parse_plant()
class DataManager(object):
# handles user data, puts a .botany dir in user's home dir (OSX/Linux)
# TODO: windows... lol
user_dir = os.path.expanduser("~")
botany_dir = os.path.join(user_dir,'.botany')
this_user = getpass.getuser()
savefile_name = this_user + '_plant.dat'
savefile_path = os.path.join(botany_dir,savefile_name)
def __init__(self):
self.this_user = getpass.getuser()
if not os.path.exists(self.botany_dir):
os.makedirs(self.botany_dir)
self.savefile_name = self.this_user + '_plant.dat'
def check_plant(self):
if os.path.isfile(self.savefile_name):
print "found savefile!"
# check for existing save file
if os.path.isfile(self.savefile_path):
return True
else:
print "no savefile found"
return False
def save_plant(self, this_plant):
with open(self.savefile_name, 'wb') as f:
# create savefile
with open(self.savefile_path, 'wb') as f:
pickle.dump(this_plant, f, protocol=2)
def load_plant(self):
with open(self.savefile_name, 'rb') as f:
# load savefile
with open(self.savefile_path, 'rb') as f:
this_plant = pickle.load(f)
return this_plant
def data_write_json(self, this_plant):
json_filename = self.this_user + '_plant_data.json'
with open(json_filename, 'w') as outfile:
# create json file for user to use outside of the game (website?)
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)
if __name__ == '__main__':
my_data = DataManager()
# if plant save file does not exist
# if plant save file exists
if my_data.check_plant():
print "Welcome back, " + getpass.getuser()
my_plant = my_data.load_plant()
my_plant.parse_plant()
# otherwise create new plant
else:
my_plant = Plant()
# print my_plant.stage, my_plant.species, my_plant.color, my_plant.rarity, my_plant.mutation
print "Welcome, " + getpass.getuser()
#TODO: onboarding, select seed, select whatever else
my_plant = Plant(my_data.savefile_path)
my_plant.start_life()
print "Thread's running!"
raw_input('...')
print "Your plant is living :)"
raw_input('Press return to save and exit...\n')
my_data.save_plant(my_plant)
my_data.data_write_json(my_plant)
print "end"