diff --git a/botany.py b/botany.py index 932b13d..41afc2c 100755 --- a/botany.py +++ b/botany.py @@ -15,9 +15,6 @@ from menu_screen import * # TODO: # - Switch from personal data file to table in DB -# - Table in DB would allow others to modify (personal files might be -# permission locked) -# - this is a good idea. class Plant(object): # This is your plant! @@ -147,10 +144,6 @@ class Plant(object): self.watered_24h = False # visitors are coming self.visitors = [] - # TODO: manually checking for pending visitors - # TODO: clear visitors when checking - self.weekly_visitors = {} - # TODO: clear weekly visitors on sunday 00:00:00 def migrate_properties(self): # Migrates old data files to new @@ -158,8 +151,6 @@ class Plant(object): self.generation = 1 if not hasattr(self, 'visitors'): self.visitors = [] - if not hasattr(self, 'weekly_visitors'): - self.weekly_visitors = {} def parse_plant(self): # Converts plant data to human-readable format @@ -209,30 +200,49 @@ class Plant(object): self.dead = True return self.dead + 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() + 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') - guest_data = {'latest_timestamp': 0, 'visitors': []} + latest_timestamp = 0 + visitors_this_check = [] if os.path.isfile(visitor_filepath): with open(visitor_filepath, 'r') as visitor_file: data = json.load(visitor_file) for element in data: if element['user'] not in self.visitors: self.visitors.append(element['user']) - # counter for weekly visitors - if element['user'] not in self.weekly_visitors: - self.weekly_visitors[element['user']] = 1 - else: - self.weekly_visitors[element['user']] += 1 - if element['timestamp'] > guest_data['latest_timestamp']: - guest_data['latest_timestamp'] = element['timestamp'] - with open(visitor_filepath, 'w') as visitor_file2: - visitor_file2.write('[]') + if element['user'] not in visitors_this_check: + visitors_this_check.append(element['user']) + if element['timestamp'] > latest_timestamp: + latest_timestamp = element['timestamp'] + with open(visitor_filepath, 'w') as visitor_file: + visitor_file.write('[]') + try: + self.update_visitor_db(visitors_this_check) + except: + pass else: with open(visitor_filepath, mode='w') as f: json.dump([], f) - return guest_data['latest_timestamp'] + return latest_timestamp def water_check(self): latest_visitor_timestamp = self.guest_check() @@ -456,11 +466,25 @@ class DataManager(object): open(self.garden_json_path, 'a').close() os.chmod(self.garden_json_path, 0666) + 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 + def update_garden_db(self, this_plant): # insert or update this plant id's entry in DB # TODO: make sure other instances of user are deleted # Could create a clean db function self.init_database() + self.migrate_database() age_formatted = self.plant_age_convert(this_plant) conn = sqlite3.connect(self.garden_db_path) c = conn.cursor() diff --git a/menu_screen.py b/menu_screen.py index 25b7ef5..1a7a336 100644 --- a/menu_screen.py +++ b/menu_screen.py @@ -7,6 +7,7 @@ import time import random import getpass import json +import sqlite3 class CursedMenu(object): #TODO: name your plant @@ -86,6 +87,7 @@ class CursedMenu(object): def draw(self): # Draw the menu and lines + self.maxy, self.maxx = self.screen.getmaxyx() self.screen.refresh() try: self.draw_default() @@ -130,32 +132,7 @@ class CursedMenu(object): def draw_plant_ascii(self, this_plant): ypos = 1 xpos = int((self.maxx-37)/2 + 25) - # TODO: pull this from botany class - plant_art_list = [ - 'poppy', - 'cactus', - 'aloe', - 'flytrap', - 'jadeplant', - 'fern', - 'daffodil', - 'sunflower', - 'baobab', - 'lithops', - 'hemp', - 'pansy', - 'iris', - 'agave', - 'ficus', - 'moss', - 'sage', - 'snapdragon', - 'columbine', - 'brugmansia', - 'palm', - 'pachypodium', - ] - + plant_art_list = this_plant.species_list if this_plant.dead == True: self.ascii_render('rip.txt', ypos, xpos) elif this_plant.stage == 0: @@ -175,18 +152,18 @@ class CursedMenu(object): def draw_default(self): # draws default menu clear_bar = " " * (int(self.maxx*2/3)) - self.screen.addstr(2, 2, self.title, curses.A_STANDOUT) # Title for this menu - self.screen.addstr(4, 2, self.subtitle, curses.A_BOLD) #Subtitle for this menu + self.screen.addstr(1, 2, self.title, curses.A_STANDOUT) # Title for this menu + self.screen.addstr(3, 2, self.subtitle, curses.A_BOLD) #Subtitle for this menu # clear menu on screen for index in range(len(self.options)+1): - self.screen.addstr(5+index, 4, clear_bar, curses.A_NORMAL) + self.screen.addstr(4+index, 4, clear_bar, curses.A_NORMAL) # display all the menu items, showing the 'pos' item highlighted for index in range(len(self.options)): textstyle = self.normal if index == self.selected: textstyle = self.highlighted - self.screen.addstr(5+index ,4, clear_bar, curses.A_NORMAL) - self.screen.addstr(5+index ,4, "%d - %s" % (index+1, self.options[index]), textstyle) + self.screen.addstr(4+index ,4, clear_bar, curses.A_NORMAL) + self.screen.addstr(4+index ,4, "%d - %s" % (index+1, self.options[index]), textstyle) self.screen.addstr(12, 2, clear_bar, curses.A_NORMAL) self.screen.addstr(13, 2, clear_bar, curses.A_NORMAL) @@ -215,7 +192,6 @@ class CursedMenu(object): water_string = "(" + (")" * water_left) + ("." * (10 - water_left)) + ") " + str(int(water_left_pct * 100)) + "% " return water_string - def update_plant_live(self): # updates plant data on menu screen, live! while not self.exit: @@ -522,50 +498,85 @@ class CursedMenu(object): pass self.clear_info_pane() - def build_visitor_output(self, visitors): + def build_weekly_visitor_output(self, visitors): visitor_block = "" visitor_line = "" - if len(visitors) == 1: - visitor_block += str(visitors[0]) - else: - for visitor in visitors[:-1]: - if visitor_block.count('\n') > self.maxy-12: - visitor_block += "and more" - break - if len(visitor_line + visitor) > self.maxx-4: + for visitor in visitors: + this_visitor_string = str(visitor) + "({}) ".format(visitors[str(visitor)]) + if len(visitor_line + this_visitor_string) > self.maxx-3: + visitor_block += '\n' + visitor_line = "" + visitor_block += this_visitor_string + visitor_line += this_visitor_string + return visitor_block + + def build_latest_visitor_output(self, visitors): + visitor_line = "" + for visitor in visitors: + if len(visitor_line + visitor) > self.maxx-10: + visitor_line += "and more" + break + visitor_line += visitor + ' ' + return [visitor_line] + + def get_weekly_visitors(self): + 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) + c = conn.cursor() + c.execute("SELECT * FROM visitors WHERE garden_name = '{}' ORDER BY weekly_visits".format(self.plant.owner)) + # if c.rowcount: + visitor_data = c.fetchall() + conn.close() + visitor_block = "" + visitor_line = "" + if visitor_data: + for visitor in visitor_data: + visitor_name = visitor[2] + weekly_visits = visitor[3] + this_visitor_string = "{}({}) ".format(visitor_name, weekly_visits) + if len(visitor_line + this_visitor_string) > self.maxx-3: visitor_block += '\n' visitor_line = "" - visitor_block += str(visitor) + ', ' - visitor_line += str(visitor) + ', ' - else: - visitor_block += "& " + str(visitors[-1]) - visitor_line += "& " + str(visitors[-1]) + visitor_block += this_visitor_string + visitor_line += this_visitor_string + else: + visitor_block = 'nobody :(' return visitor_block + def get_user_string(self, xpos=3, ypos=15): + user_string = "" + user_input = 0 + while user_input != 10: + user_input = self.screen.getch() + if user_input == 127: + if len(user_string) > 0: + user_string = user_string[:-1] + self.screen.addstr(15, 3, " " * (self.maxx-2) ) + if user_input in range(256): + if chr(user_input).isalnum(): + user_string += chr(user_input) + self.screen.addstr(ypos, xpos, str(user_string)) + self.screen.refresh() + return user_string + def visit_handler(self): self.clear_info_pane() self.draw_info_text("whose plant would you like to visit?") self.screen.addstr(15, 2, '~') if self.plant.visitors: - self.draw_info_text("you've been visited by: ", 4) - visitor_text = self.build_visitor_output(self.plant.visitors) - self.draw_info_text(visitor_text, 5) + latest_visitor_string = self.build_latest_visitor_output(self.plant.visitors) + self.draw_info_text("since last time, you were visited by: ", 3) + self.draw_info_text(latest_visitor_string, 4) self.plant.visitors = [] - - guest_garden = "" - user_input = 0 - while user_input != 10: - user_input = self.screen.getch() - if user_input == 127: - if len(guest_garden) > 0: - guest_garden = guest_garden[:-1] - self.screen.addstr(15, 3, " " * (self.maxx-2) ) - if user_input in range(256): - if chr(user_input).isalnum(): - guest_garden += chr(user_input) - self.screen.addstr(15, 3, str(guest_garden)) - self.screen.refresh() - # test if user exists and has botany directory and file + weekly_visitor_text = self.get_weekly_visitors() + self.draw_info_text("this week you've been visited by: ", 6) + self.draw_info_text(weekly_visitor_text, 7) + # user input section (can't get getstr to work) + guest_garden = self.get_user_string() + if not guest_garden: + self.clear_info_pane() + return None home_folder = os.path.dirname(os.path.expanduser("~")) guest_json = home_folder + "/{}/.botany/{}_plant_data.json".format(guest_garden, guest_garden) guest_plant_description = ""