botany/botany.py

647 lines
22 KiB
Python
Raw Normal View History

2019-10-30 17:26:49 +00:00
#!/usr/bin/env python3
2017-03-06 21:38:31 +00:00
import time
2017-03-06 22:22:13 +00:00
import pickle
2017-03-06 21:38:31 +00:00
import json
2017-03-14 22:23:28 +00:00
import os
2017-03-06 21:38:31 +00:00
import random
2017-03-07 01:56:11 +00:00
import getpass
2017-03-07 18:07:13 +00:00
import threading
2017-03-08 02:35:04 +00:00
import errno
import uuid
2017-03-23 01:49:38 +00:00
import sqlite3
2017-03-08 02:35:04 +00:00
from menu_screen import *
2017-03-06 21:38:31 +00:00
2017-03-31 19:06:08 +00:00
# TODO:
# - Switch from personal data file to table in DB
2017-03-07 01:56:11 +00:00
class Plant(object):
2017-03-07 21:04:14 +00:00
# This is your plant!
2017-09-21 21:29:11 +00:00
stage_list = [
'seed',
'seedling',
'young',
'mature',
'flowering',
'seed-bearing',
]
color_list = [
'red',
'orange',
'yellow',
'green',
'blue',
'indigo',
'violet',
'white',
'black',
'gold',
'rainbow',
]
rarity_list = [
'common',
'uncommon',
'rare',
'legendary',
'godly',
]
species_list = [
'poppy',
'cactus',
'aloe',
'venus flytrap',
'jade plant',
'fern',
'daffodil',
'sunflower',
'baobab',
'lithops',
'hemp',
'pansy',
'iris',
'agave',
'ficus',
'moss',
'sage',
'snapdragon',
'columbine',
'brugmansia',
'palm',
2018-03-04 02:20:33 +00:00
'pachypodium',
2017-09-21 21:29:11 +00:00
]
mutation_list = [
'',
'humming',
'noxious',
'vorpal',
'glowing',
'electric',
'icy',
'flaming',
'psychic',
'screaming',
'chaotic',
'hissing',
'gelatinous',
'deformed',
'shaggy',
'scaly',
'depressed',
'anxious',
'metallic',
'glossy',
'psychedelic',
'bonsai',
'foamy',
'singing',
'fractal',
'crunchy',
'goth',
'oozing',
'stinky',
'aromatic',
'juicy',
'smug',
'vibrating',
'lithe',
'chalky',
'naive',
'ersatz',
'disco',
'levitating',
'colossal',
'luminous',
'cosmic',
'ethereal',
2018-07-12 17:42:34 +00:00
'cursed',
'buff',
'narcotic',
'gnu/linux',
'abraxan', # rip dear friend
2017-09-21 21:29:11 +00:00
]
2017-03-06 23:20:31 +00:00
2017-05-03 19:51:21 +00:00
def __init__(self, this_filename, generation=1):
2017-03-07 21:04:14 +00:00
# Constructor
self.plant_id = str(uuid.uuid4())
self.life_stages = (3600*24, (3600*24)*3, (3600*24)*10, (3600*24)*20, (3600*24)*30)
2018-03-11 21:30:10 +00:00
# self.life_stages = (2, 4, 6, 8, 10) # debug mode
2017-03-06 21:38:31 +00:00
self.stage = 0
self.mutation = 0
2017-09-21 21:29:11 +00:00
self.species = random.randint(0,len(self.species_list)-1)
self.color = random.randint(0,len(self.color_list)-1)
2017-03-06 21:38:31 +00:00
self.rarity = self.rarity_check()
2017-03-07 00:57:11 +00:00
self.ticks = 0
self.age_formatted = "0"
2017-05-03 19:51:21 +00:00
self.generation = generation
2017-03-07 18:07:13 +00:00
self.dead = False
2017-03-14 22:23:28 +00:00
self.write_lock = False
self.owner = getpass.getuser()
2017-03-08 08:18:45 +00:00
self.file_name = this_filename
self.start_time = int(time.time())
self.last_time = int(time.time())
2017-03-08 23:04:09 +00:00
# must water plant first day
2017-03-09 19:32:40 +00:00
self.watered_timestamp = int(time.time())-(24*3600)-1
self.watered_24h = False
2018-03-08 07:07:32 +00:00
self.visitors = []
2017-03-06 21:38:31 +00:00
2017-05-03 19:51:21 +00:00
def migrate_properties(self):
# Migrates old data files to new
if not hasattr(self, 'generation'):
self.generation = 1
2018-03-08 07:07:32 +00:00
if not hasattr(self, 'visitors'):
self.visitors = []
2017-03-14 22:23:28 +00:00
def parse_plant(self):
2017-03-18 00:33:40 +00:00
# Converts plant data to human-readable format
2017-03-14 22:23:28 +00:00
output = ""
if self.stage >= 3:
2017-09-21 21:29:11 +00:00
output += self.rarity_list[self.rarity] + " "
2017-03-14 22:23:28 +00:00
if self.mutation != 0:
2017-09-21 21:29:11 +00:00
output += self.mutation_list[self.mutation] + " "
2017-03-14 22:23:28 +00:00
if self.stage >= 4:
2017-09-21 21:29:11 +00:00
output += self.color_list[self.color] + " "
output += self.stage_list[self.stage] + " "
2017-03-14 22:23:28 +00:00
if self.stage >= 2:
2017-09-21 21:29:11 +00:00
output += self.species_list[self.species] + " "
2017-03-14 22:23:28 +00:00
return output.strip()
2017-03-09 19:32:40 +00:00
2017-03-06 21:38:31 +00:00
def rarity_check(self):
2017-03-07 21:04:14 +00:00
# Generate plant rarity
2017-03-06 22:22:13 +00:00
CONST_RARITY_MAX = 256.0
rare_seed = random.randint(1,CONST_RARITY_MAX)
common_range = round((2/3)*CONST_RARITY_MAX)
uncommon_range = round((2/3)*(CONST_RARITY_MAX-common_range))
rare_range = round((2/3)*(CONST_RARITY_MAX-common_range-uncommon_range))
legendary_range = round((2/3)*(CONST_RARITY_MAX-common_range-uncommon_range-rare_range))
common_max = common_range
uncommon_max = common_max + uncommon_range
rare_max = uncommon_max + rare_range
legendary_max = rare_max + legendary_range
2017-03-08 08:18:45 +00:00
godly_max = CONST_RARITY_MAX
2017-03-06 22:22:13 +00:00
2017-03-24 01:32:44 +00:00
if 0 <= rare_seed <= common_max:
2017-03-06 21:38:31 +00:00
rarity = 0
2017-03-06 22:22:13 +00:00
elif common_max < rare_seed <= uncommon_max:
2017-03-06 21:38:31 +00:00
rarity = 1
2017-03-06 22:22:13 +00:00
elif uncommon_max < rare_seed <= rare_max:
2017-03-06 21:38:31 +00:00
rarity = 2
2017-03-06 22:22:13 +00:00
elif rare_max < rare_seed <= legendary_max:
2017-03-06 21:38:31 +00:00
rarity = 3
2017-03-08 08:18:45 +00:00
elif legendary_max < rare_seed <= godly_max:
2017-03-06 21:38:31 +00:00
rarity = 4
return rarity
2017-03-08 23:04:09 +00:00
def dead_check(self):
# if it has been >5 days since watering, sorry plant is dead :(
2017-03-24 01:32:44 +00:00
time_delta_watered = int(time.time()) - self.watered_timestamp
2017-03-08 23:04:09 +00:00
if time_delta_watered > (5 * (24 * 3600)):
self.dead = True
return self.dead
2017-03-08 08:18:45 +00:00
2018-03-11 20:46:45 +00:00
def update_visitor_db(self, visitor_names):
game_dir = os.path.dirname(os.path.realpath(__file__))
garden_db_path = os.path.join(game_dir, 'sqlite/garden_db.sqlite')
conn = sqlite3.connect(garden_db_path)
for name in (visitor_names):
c = conn.cursor()
c.execute("SELECT * FROM visitors WHERE garden_name = '{}' AND visitor_name = '{}' ".format(self.owner, name))
data=c.fetchone()
if data is None:
sql = """ INSERT INTO visitors (garden_name,visitor_name,weekly_visits) VALUES('{}', '{}',1)""".format(self.owner, name)
c.execute(sql)
else:
sql = """ UPDATE visitors SET weekly_visits = weekly_visits + 1 WHERE garden_name = '{}' AND visitor_name = '{}'""".format(self.owner, name)
c.execute(sql)
conn.commit()
conn.close()
2018-03-03 05:20:29 +00:00
def guest_check(self):
user_dir = os.path.expanduser("~")
botany_dir = os.path.join(user_dir,'.botany')
visitor_filepath = os.path.join(botany_dir,'visitors.json')
2018-06-14 20:11:07 +00:00
guest_timestamps = []
2018-03-11 20:46:45 +00:00
visitors_this_check = []
2018-03-03 05:20:29 +00:00
if os.path.isfile(visitor_filepath):
2018-03-08 07:31:00 +00:00
with open(visitor_filepath, 'r') as visitor_file:
2018-03-03 05:20:29 +00:00
data = json.load(visitor_file)
2018-03-11 22:44:10 +00:00
if data:
for element in data:
if element['user'] not in self.visitors:
self.visitors.append(element['user'])
if element['user'] not in visitors_this_check:
visitors_this_check.append(element['user'])
2018-03-14 01:03:57 +00:00
# prevent users from manually setting watered_time in the future
if element['timestamp'] <= int(time.time()):
2018-06-14 20:11:07 +00:00
guest_timestamps.append(element['timestamp'])
2018-03-11 22:44:10 +00:00
try:
self.update_visitor_db(visitors_this_check)
except:
pass
with open(visitor_filepath, 'w') as visitor_file:
visitor_file.write('[]')
2018-03-08 07:07:32 +00:00
else:
with open(visitor_filepath, mode='w') as f:
json.dump([], f)
2019-10-30 17:26:49 +00:00
os.chmod(visitor_filepath, 0o666)
2018-06-14 21:07:43 +00:00
if not guest_timestamps:
return self.watered_timestamp
all_timestamps = [self.watered_timestamp] + guest_timestamps
all_timestamps.sort()
# calculate # of days between each guest watering
timestamp_diffs = [(j-i)/86400.0 for i, j in zip(all_timestamps[:-1], all_timestamps[1:])]
# plant's latest timestamp should be set to last timestamp before a
# gap of 5 days
last_valid_element = next((x for x in timestamp_diffs if x > 5), None)
if not last_valid_element:
# all timestamps are within a 5 day range, can just use latest one
return all_timestamps[-1]
last_valid_index = timestamp_diffs.index(last_valid_element)
# slice list to only include up until a >5 day gap
valid_timestamps = all_timestamps[:last_valid_index + 1]
return valid_timestamps[-1]
2018-03-03 05:20:29 +00:00
def water_check(self):
2018-06-14 21:07:43 +00:00
self.watered_timestamp = self.guest_check()
self.time_delta_watered = int(time.time()) - self.watered_timestamp
if self.time_delta_watered <= (24 * 3600):
if not self.watered_24h:
self.watered_24h = True
return True
else:
self.watered_24h = False
return False
2017-03-07 21:04:14 +00:00
def mutate_check(self):
# Create plant mutation
2017-03-18 00:33:40 +00:00
# Increase this # to make mutation rarer (chance 1 out of x each second)
CONST_MUTATION_RARITY = 20000
2017-03-07 21:04:14 +00:00
mutation_seed = random.randint(1,CONST_MUTATION_RARITY)
if mutation_seed == CONST_MUTATION_RARITY:
# mutation gained!
2017-09-21 21:29:11 +00:00
mutation = random.randint(0,len(self.mutation_list)-1)
2017-03-07 21:04:14 +00:00
if self.mutation == 0:
self.mutation = mutation
return True
else:
return False
2017-03-14 22:23:28 +00:00
def growth(self):
# Increase plant growth stage
2017-09-21 21:29:11 +00:00
if self.stage < (len(self.stage_list)-1):
2017-03-14 22:23:28 +00:00
self.stage += 1
def water(self):
# Increase plant growth stage
if not self.dead:
self.watered_timestamp = int(time.time())
self.watered_24h = True
def start_over(self):
2017-03-18 00:33:40 +00:00
# After plant reaches final stage, given option to restart
2017-05-03 19:51:21 +00:00
# increment generation only if previous stage is final stage and plant
# is alive
if not self.dead:
next_generation = self.generation + 1
else:
# Should this reset to 1? Seems unfair.. for now generations will
# persist through death.
next_generation = self.generation
2017-03-14 22:23:28 +00:00
self.write_lock = True
self.kill_plant()
while self.write_lock:
# Wait for garden writer to unlock
# garden db needs to update before allowing the user to reset
2017-03-17 19:00:46 +00:00
pass
2017-03-14 22:23:28 +00:00
if not self.write_lock:
2017-05-03 19:51:21 +00:00
self.__init__(self.file_name, next_generation)
2017-03-14 22:23:28 +00:00
def kill_plant(self):
self.dead = True
def unlock_new_creation(self):
self.write_lock = False
2017-03-06 21:38:31 +00:00
2017-03-07 18:07:13 +00:00
def start_life(self):
2017-03-07 21:04:14 +00:00
# runs life on a thread
2017-03-07 18:07:13 +00:00
thread = threading.Thread(target=self.life, args=())
thread.daemon = True
thread.start()
def life(self):
2017-03-07 00:57:11 +00:00
# I've created life :)
2017-03-09 19:32:40 +00:00
while True:
if not self.dead:
if self.watered_24h:
self.ticks += 1
2017-09-21 21:29:11 +00:00
if self.stage < len(self.stage_list)-1:
2017-03-14 22:23:28 +00:00
if self.ticks >= self.life_stages[self.stage]:
2017-03-09 19:32:40 +00:00
self.growth()
if self.mutate_check():
2017-03-17 19:00:46 +00:00
pass
if self.water_check():
# Do something
2017-03-17 19:00:46 +00:00
pass
2017-03-09 19:32:40 +00:00
if self.dead_check():
# Do something else
2017-03-17 19:00:46 +00:00
pass
# TODO: event check
2017-05-03 23:21:43 +00:00
generation_bonus = 0.2 * (self.generation - 1)
adjusted_sleep_time = 1 / (1 + generation_bonus)
time.sleep(adjusted_sleep_time)
2017-03-07 01:56:11 +00:00
class DataManager(object):
2017-03-07 21:04:14 +00:00
# handles user data, puts a .botany dir in user's home dir (OSX/Linux)
2017-03-23 01:49:38 +00:00
# handles shared data with sqlite db
2018-05-21 18:00:53 +00:00
# TODO: .dat save should only happen on mutation, water, death, exit,
# harvest, otherwise
# data hasn't changed...
# can write json whenever bc this isn't ever read for data within botany
2017-03-23 01:49:38 +00:00
2017-03-07 21:04:14 +00:00
user_dir = os.path.expanduser("~")
botany_dir = os.path.join(user_dir,'.botany')
game_dir = os.path.dirname(os.path.realpath(__file__))
2017-03-07 21:04:14 +00:00
this_user = getpass.getuser()
2017-03-23 01:49:38 +00:00
2017-03-07 21:04:14 +00:00
savefile_name = this_user + '_plant.dat'
savefile_path = os.path.join(botany_dir, savefile_name)
2018-03-03 05:20:29 +00:00
#set this.savefile_path to guest_garden path
2017-03-23 01:49:38 +00:00
garden_db_path = os.path.join(game_dir, 'sqlite/garden_db.sqlite')
2017-03-15 20:56:00 +00:00
garden_json_path = os.path.join(game_dir, 'garden_file.json')
2017-03-21 19:55:11 +00:00
harvest_file_path = os.path.join(botany_dir, 'harvest_file.dat')
harvest_json_path = os.path.join(botany_dir, 'harvest_file.json')
2017-03-07 21:04:14 +00:00
2017-03-07 01:56:11 +00:00
def __init__(self):
self.this_user = getpass.getuser()
2017-03-14 22:23:28 +00:00
# check if instance is already running
2017-03-08 02:35:04 +00:00
# check for .botany dir in home
try:
2017-03-07 21:04:14 +00:00
os.makedirs(self.botany_dir)
2017-03-08 02:35:04 +00:00
except OSError as exception:
if exception.errno != errno.EEXIST:
raise
2017-03-07 01:56:11 +00:00
self.savefile_name = self.this_user + '_plant.dat'
def check_plant(self):
2017-03-07 21:04:14 +00:00
# check for existing save file
if os.path.isfile(self.savefile_path):
2017-03-07 01:56:11 +00:00
return True
else:
return False
def start_threads(self,this_plant):
# creates threads to save files every minute
death_check_thread = threading.Thread(target=self.death_check_update, args=(this_plant,))
death_check_thread.daemon = True
death_check_thread.start()
autosave_thread = threading.Thread(target=self.autosave, args=(this_plant,))
autosave_thread.daemon = True
autosave_thread.start()
def death_check_update(self,this_plant):
# .1 second updates and lock to minimize race condition
while True:
is_dead = this_plant.dead_check()
if is_dead:
self.save_plant(this_plant)
self.data_write_json(this_plant)
2017-03-23 01:49:38 +00:00
self.update_garden_db(this_plant)
2017-03-21 19:55:11 +00:00
self.harvest_plant(this_plant)
2017-03-14 22:23:28 +00:00
this_plant.unlock_new_creation()
time.sleep(.1)
2017-03-07 01:56:11 +00:00
2017-03-09 02:06:35 +00:00
def autosave(self, this_plant):
2018-05-21 18:00:53 +00:00
# running on thread, saves plant every 5s TODO: this is unnecessary
# and breaks shit probably
2017-03-23 01:49:38 +00:00
file_update_count = 0
2017-03-09 02:06:35 +00:00
while True:
2017-03-23 01:49:38 +00:00
file_update_count += 1
2017-03-09 02:06:35 +00:00
self.save_plant(this_plant)
self.data_write_json(this_plant)
2017-03-23 01:49:38 +00:00
self.update_garden_db(this_plant)
if file_update_count == 12:
# only update garden json every 60s
self.update_garden_json()
2017-03-09 02:06:35 +00:00
time.sleep(5)
2017-03-23 01:49:38 +00:00
file_update_count %= 12
2017-03-09 02:06:35 +00:00
2017-03-07 01:56:11 +00:00
def load_plant(self):
2017-03-07 21:04:14 +00:00
# load savefile
with open(self.savefile_path, 'rb') as f:
2017-03-07 01:56:11 +00:00
this_plant = pickle.load(f)
2017-05-03 19:51:21 +00:00
# migrate data structure to create data for empty/nonexistent plant
# properties
this_plant.migrate_properties()
# get status since last login
is_watered = this_plant.water_check()
2018-10-03 18:53:49 +00:00
is_dead = this_plant.dead_check()
2017-03-08 23:04:09 +00:00
if not is_dead:
if is_watered:
time_delta_last = int(time.time()) - this_plant.last_time
ticks_to_add = min(time_delta_last, 24*3600)
this_plant.time_delta_watered = 0
self.last_water_gain = time.time()
else:
ticks_to_add = 0
2018-06-05 00:05:07 +00:00
this_plant.ticks += ticks_to_add * (0.2 * (this_plant.generation - 1) + 1)
2017-03-08 08:45:21 +00:00
return this_plant
2017-03-07 01:56:11 +00:00
def plant_age_convert(self,this_plant):
# human-readable plant age
age_seconds = int(time.time()) - this_plant.start_time
days, age_seconds = divmod(age_seconds, 24 * 60 * 60)
hours, age_seconds = divmod(age_seconds, 60 * 60)
minutes, age_seconds = divmod(age_seconds, 60)
age_formatted = ("%dd:%dh:%dm:%ds" % (days, hours, minutes, age_seconds))
return age_formatted
2017-03-23 01:49:38 +00:00
def init_database(self):
# check if dir exists, create sqlite directory and set OS permissions to 777
sqlite_dir_path = os.path.join(self.game_dir,'sqlite')
if not os.path.exists(sqlite_dir_path):
os.makedirs(sqlite_dir_path)
2019-10-30 17:26:49 +00:00
os.chmod(sqlite_dir_path, 0o777)
2017-03-23 01:49:38 +00:00
conn = sqlite3.connect(self.garden_db_path)
init_table_string = """CREATE TABLE IF NOT EXISTS garden (
plant_id tinytext PRIMARY KEY,
owner text,
description text,
age text,
score integer,
is_dead numeric
)"""
c = conn.cursor()
c.execute(init_table_string)
conn.close()
# init only, creates and sets permissions for garden db and json
if os.stat(self.garden_db_path).st_uid == os.getuid():
2019-10-30 17:26:49 +00:00
os.chmod(self.garden_db_path, 0o666)
2017-03-15 20:56:00 +00:00
open(self.garden_json_path, 'a').close()
2019-10-30 17:26:49 +00:00
os.chmod(self.garden_json_path, 0o666)
2017-03-15 20:56:00 +00:00
2018-03-11 20:46:45 +00:00
def migrate_database(self):
conn = sqlite3.connect(self.garden_db_path)
migrate_table_string = """CREATE TABLE IF NOT EXISTS visitors (
id integer PRIMARY KEY,
garden_name text,
visitor_name text,
weekly_visits integer
)"""
c = conn.cursor()
c.execute(migrate_table_string)
conn.close()
return True
2017-03-23 01:49:38 +00:00
def update_garden_db(self, this_plant):
# insert or update this plant id's entry in DB
2017-09-21 21:29:11 +00:00
# TODO: make sure other instances of user are deleted
# Could create a clean db function
2017-03-23 01:49:38 +00:00
self.init_database()
2018-03-11 20:46:45 +00:00
self.migrate_database()
2017-03-23 01:49:38 +00:00
age_formatted = self.plant_age_convert(this_plant)
conn = sqlite3.connect(self.garden_db_path)
c = conn.cursor()
# try to insert or replace
update_query = """INSERT OR REPLACE INTO garden (
plant_id, owner, description, age, score, is_dead
) VALUES (
'{pid}', '{pown}', '{pdes}', '{page}', {psco}, {pdead}
)
""".format(pid = this_plant.plant_id,
pown = this_plant.owner,
pdes = this_plant.parse_plant(),
page = age_formatted,
psco = str(this_plant.ticks),
pdead = int(this_plant.dead))
c.execute(update_query)
conn.commit()
conn.close()
def retrieve_garden_from_db(self):
# Builds a dict of dicts from garden sqlite db
garden_dict = {}
conn = sqlite3.connect(self.garden_db_path)
# Need to allow write permissions by others
conn.row_factory = sqlite3.Row
c = conn.cursor()
c.execute('SELECT * FROM garden ORDER BY owner')
tuple_list = c.fetchall()
conn.close()
# Building dict from table rows
for item in tuple_list:
garden_dict[item[0]] = {
"owner":item[1],
"description":item[2],
"age":item[3],
"score":item[4],
"dead":item[5],
}
return garden_dict
def update_garden_json(self):
this_garden = self.retrieve_garden_from_db()
2017-03-15 20:56:00 +00:00
with open(self.garden_json_path, 'w') as outfile:
json.dump(this_garden, outfile)
2017-03-23 01:49:38 +00:00
pass
def save_plant(self, this_plant):
# create savefile
this_plant.last_time = int(time.time())
temp_path = self.savefile_path + ".temp"
with open(temp_path, 'wb') as f:
pickle.dump(this_plant, f, protocol=2)
os.rename(temp_path, self.savefile_path)
2017-03-09 02:18:01 +00:00
2017-03-07 01:56:11 +00:00
def data_write_json(self, this_plant):
# create personal json file for user to use outside of the game (website?)
2017-03-07 21:04:14 +00:00
json_file = os.path.join(self.botany_dir,self.this_user + '_plant_data.json')
2017-03-09 02:18:01 +00:00
# also updates age
age_formatted = self.plant_age_convert(this_plant)
2017-03-09 02:06:35 +00:00
plant_info = {
"owner":this_plant.owner,
"description":this_plant.parse_plant(),
2017-03-09 02:18:01 +00:00
"age":age_formatted,
2017-03-09 02:06:35 +00:00
"score":this_plant.ticks,
"is_dead":this_plant.dead,
2017-03-09 02:18:01 +00:00
"last_watered":this_plant.watered_timestamp,
2017-03-09 02:06:35 +00:00
"file_name":this_plant.file_name,
2017-09-21 21:29:11 +00:00
"stage": this_plant.stage_list[this_plant.stage],
2017-05-03 23:21:43 +00:00
"generation": this_plant.generation,
2017-03-09 02:06:35 +00:00
}
if this_plant.stage >= 3:
2017-09-21 21:29:11 +00:00
plant_info["rarity"] = this_plant.rarity_list[this_plant.rarity]
if this_plant.mutation != 0:
2017-09-21 21:29:11 +00:00
plant_info["mutation"] = this_plant.mutation_list[this_plant.mutation]
if this_plant.stage >= 4:
2017-09-21 21:29:11 +00:00
plant_info["color"] = this_plant.color_list[this_plant.color]
if this_plant.stage >= 2:
2017-09-21 21:29:11 +00:00
plant_info["species"] = this_plant.species_list[this_plant.species]
2017-05-03 19:51:21 +00:00
2017-03-07 21:04:14 +00:00
with open(json_file, 'w') as outfile:
2017-03-09 02:06:35 +00:00
json.dump(plant_info, outfile)
2017-03-07 00:57:11 +00:00
2017-03-21 19:55:11 +00:00
def harvest_plant(self, this_plant):
2018-03-11 21:30:10 +00:00
# TODO: plant history feature - could just use a sqlite query to retrieve all of user's dead plants
2017-03-23 01:49:38 +00:00
2017-03-21 19:55:11 +00:00
# harvest is a dict of dicts
# harvest contains one entry for each plant id
age_formatted = self.plant_age_convert(this_plant)
this_plant_id = this_plant.plant_id
plant_info = {
"description":this_plant.parse_plant(),
"age":age_formatted,
"score":this_plant.ticks,
}
if os.path.isfile(self.harvest_file_path):
# harvest file exists: load data
with open(self.harvest_file_path, 'rb') as f:
this_harvest = pickle.load(f)
new_file_check = False
else:
this_harvest = {}
new_file_check = True
2017-03-21 19:55:11 +00:00
this_harvest[this_plant_id] = plant_info
# dump harvest file
temp_path = self.harvest_file_path + ".temp"
with open(temp_path, 'wb') as f:
2017-03-21 19:55:11 +00:00
pickle.dump(this_harvest, f, protocol=2)
os.rename(temp_path, self.harvest_file_path)
2017-03-21 19:55:11 +00:00
# dump json file
with open(self.harvest_json_path, 'w') as outfile:
json.dump(this_harvest, outfile)
return new_file_check
2017-03-06 21:38:31 +00:00
if __name__ == '__main__':
2017-03-07 01:56:11 +00:00
my_data = DataManager()
2017-03-07 21:04:14 +00:00
# if plant save file exists
2017-03-07 01:56:11 +00:00
if my_data.check_plant():
my_plant = my_data.load_plant()
# otherwise create new plant
else:
2017-03-07 21:04:14 +00:00
my_plant = Plant(my_data.savefile_path)
my_data.data_write_json(my_plant)
2017-05-03 19:51:21 +00:00
# my_plant is either a fresh plant or an existing plant at this point
2017-03-07 18:07:13 +00:00
my_plant.start_life()
my_data.start_threads(my_plant)
try:
botany_menu = CursedMenu(my_plant,my_data)
my_data.save_plant(my_plant)
my_data.data_write_json(my_plant)
my_data.update_garden_db(my_plant)
finally:
cleanup()