visitor queue handled

pull/8/head
Jake Funke 2018-03-07 23:31:00 -08:00
parent 55ce486dee
commit 105a95f92e
2 changed files with 21 additions and 24 deletions

View File

@ -156,7 +156,6 @@ class Plant(object):
# Migrates old data files to new # Migrates old data files to new
if not hasattr(self, 'generation'): if not hasattr(self, 'generation'):
self.generation = 1 self.generation = 1
if not hasattr(self, 'visitors'): if not hasattr(self, 'visitors'):
self.visitors = [] self.visitors = []
if not hasattr(self, 'weekly_visitors'): if not hasattr(self, 'weekly_visitors'):
@ -216,7 +215,7 @@ class Plant(object):
visitor_filepath = os.path.join(botany_dir,'visitors.json') visitor_filepath = os.path.join(botany_dir,'visitors.json')
guest_data = {'latest_timestamp': 0, 'visitors': []} guest_data = {'latest_timestamp': 0, 'visitors': []}
if os.path.isfile(visitor_filepath): if os.path.isfile(visitor_filepath):
with open(visitor_filepath, 'rw') as visitor_file: with open(visitor_filepath, 'r') as visitor_file:
data = json.load(visitor_file) data = json.load(visitor_file)
for element in data: for element in data:
if element['user'] not in self.visitors: if element['user'] not in self.visitors:
@ -228,10 +227,8 @@ class Plant(object):
self.weekly_visitors[element['user']] += 1 self.weekly_visitors[element['user']] += 1
if element['timestamp'] > guest_data['latest_timestamp']: if element['timestamp'] > guest_data['latest_timestamp']:
guest_data['latest_timestamp'] = element['timestamp'] guest_data['latest_timestamp'] = element['timestamp']
# TODO: reenable emptying of visitor file with open(visitor_filepath, 'w') as visitor_file2:
# TODO: create visitor_file2.write('[]')
# visitor_file.truncate()
# json.dump([], visitor_file)
else: else:
with open(visitor_filepath, mode='w') as f: with open(visitor_filepath, mode='w') as f:
json.dump([], f) json.dump([], f)

View File

@ -484,7 +484,7 @@ class CursedMenu(object):
def clear_info_pane(self): def clear_info_pane(self):
# Clears bottom part of screen # Clears bottom part of screen
clear_bar = " " * (self.maxx - 3) clear_bar = " " * (self.maxx - 3)
this_y = 12 this_y = 14
while this_y < self.maxy: while this_y < self.maxy:
self.screen.addstr(this_y, 2, clear_bar, curses.A_NORMAL) self.screen.addstr(this_y, 2, clear_bar, curses.A_NORMAL)
this_y += 1 this_y += 1
@ -525,28 +525,32 @@ class CursedMenu(object):
def build_visitor_output(self, visitors): def build_visitor_output(self, visitors):
visitor_block = "" visitor_block = ""
visitor_line = "" visitor_line = ""
for visitor in visitors[:-1]: if len(visitors) == 1:
if visitor_block.count('\n') > self.maxy-12: visitor_block += str(visitors[0])
visitor_block += "and more"
break
if len(visitor_line + visitor) > self.maxx-4:
visitor_block += '\n'
visitor_line = ""
visitor_block += str(visitor) + ', '
visitor_line += str(visitor) + ', '
else: else:
visitor_block += "& " + str(visitors[-1]) for visitor in visitors[:-1]:
visitor_line += "& " + str(visitors[-1]) if visitor_block.count('\n') > self.maxy-12:
visitor_block += "and more"
break
if len(visitor_line + visitor) > self.maxx-4:
visitor_block += '\n'
visitor_line = ""
visitor_block += str(visitor) + ', '
visitor_line += str(visitor) + ', '
else:
visitor_block += "& " + str(visitors[-1])
visitor_line += "& " + str(visitors[-1])
return visitor_block return visitor_block
def visit_handler(self): def visit_handler(self):
self.clear_info_pane()
self.draw_info_text("whose plant would you like to visit?") self.draw_info_text("whose plant would you like to visit?")
self.screen.addstr(15, 2, '~') self.screen.addstr(15, 2, '~')
if self.plant.visitors: if self.plant.visitors:
self.draw_info_text("you've been visited by: ", 4) self.draw_info_text("you've been visited by: ", 4)
visitor_text = self.build_visitor_output(self.plant.visitors) visitor_text = self.build_visitor_output(self.plant.visitors)
self.draw_info_text(visitor_text, 5) self.draw_info_text(visitor_text, 5)
self.plant.visitors = []
guest_garden = "" guest_garden = ""
user_input = 0 user_input = 0
@ -573,16 +577,12 @@ class CursedMenu(object):
if os.path.isfile(guest_visitor_file): if os.path.isfile(guest_visitor_file):
self.water_on_visit(guest_visitor_file) self.water_on_visit(guest_visitor_file)
self.screen.addstr(16, 2, "...you watered ~{}'s {}...".format(str(guest_garden), guest_plant_description)) self.screen.addstr(16, 2, "...you watered ~{}'s {}...".format(str(guest_garden), guest_plant_description))
# you watered their x plant
else: else:
self.screen.addstr(16, 2, "i can't seem to find directions to {}...".format(guest_garden)) self.screen.addstr(16, 2, "i can't seem to find directions to {}...".format(guest_garden))
self.screen.getch() self.screen.getch()
self.clear_info_pane() self.clear_info_pane()
def water_on_visit(self, guest_visitor_file): def water_on_visit(self, guest_visitor_file):
# check if path exists
# if not exists create and dump json in
# if exists open and append to file
visitor_data = {} visitor_data = {}
guest_data = {'user': getpass.getuser(), 'timestamp': int(time.time())} guest_data = {'user': getpass.getuser(), 'timestamp': int(time.time())}
if os.path.isfile(guest_visitor_file): if os.path.isfile(guest_visitor_file):