2017-03-24 00:08:27 +00:00
|
|
|
import curses
|
2017-03-24 23:27:25 +00:00
|
|
|
import math
|
2017-03-24 00:08:27 +00:00
|
|
|
import os
|
|
|
|
import traceback
|
|
|
|
import threading
|
|
|
|
import time
|
|
|
|
import random
|
2018-03-08 07:07:32 +00:00
|
|
|
import getpass
|
|
|
|
import json
|
2018-03-11 20:46:45 +00:00
|
|
|
import sqlite3
|
2018-06-06 15:25:28 +00:00
|
|
|
import string
|
|
|
|
import re
|
2018-06-10 10:05:17 +00:00
|
|
|
import completer
|
2018-10-03 18:53:49 +00:00
|
|
|
import datetime
|
2017-03-08 02:35:04 +00:00
|
|
|
|
|
|
|
class CursedMenu(object):
|
2017-03-14 22:23:28 +00:00
|
|
|
#TODO: name your plant
|
2017-03-08 02:35:04 +00:00
|
|
|
'''A class which abstracts the horrors of building a curses-based menu system'''
|
2017-03-23 01:49:38 +00:00
|
|
|
def __init__(self, this_plant, this_data):
|
2017-03-08 02:35:04 +00:00
|
|
|
'''Initialization'''
|
2017-03-08 21:30:28 +00:00
|
|
|
self.initialized = False
|
2017-03-08 02:35:04 +00:00
|
|
|
self.screen = curses.initscr()
|
|
|
|
curses.noecho()
|
2017-03-24 22:07:17 +00:00
|
|
|
curses.raw()
|
2018-05-23 19:03:20 +00:00
|
|
|
if curses.has_colors():
|
|
|
|
curses.start_color()
|
2017-05-28 19:44:08 +00:00
|
|
|
try:
|
|
|
|
curses.curs_set(0)
|
|
|
|
except curses.error:
|
|
|
|
# Not all terminals support this functionality.
|
|
|
|
# When the error is ignored the screen will look a little uglier, but that's not terrible
|
|
|
|
# So in order to keep botany as accesible as possible to everyone, it should be safe to ignore the error.
|
|
|
|
pass
|
2017-03-08 02:35:04 +00:00
|
|
|
self.screen.keypad(1)
|
|
|
|
self.plant = this_plant
|
2018-06-05 12:38:11 +00:00
|
|
|
self.visited_plant = None
|
2017-03-23 01:49:38 +00:00
|
|
|
self.user_data = this_data
|
2017-03-08 21:22:50 +00:00
|
|
|
self.plant_string = self.plant.parse_plant()
|
2018-06-07 09:09:29 +00:00
|
|
|
self.plant_ticks = str(int(self.plant.ticks))
|
2017-03-08 02:35:04 +00:00
|
|
|
self.exit = False
|
2017-03-15 01:31:18 +00:00
|
|
|
self.infotoggle = 0
|
2017-03-08 21:22:50 +00:00
|
|
|
self.maxy, self.maxx = self.screen.getmaxyx()
|
2017-03-08 02:35:04 +00:00
|
|
|
# Highlighted and Normal line definitions
|
2018-05-23 19:03:20 +00:00
|
|
|
if curses.has_colors():
|
|
|
|
self.define_colors()
|
|
|
|
self.highlighted = curses.color_pair(1)
|
|
|
|
else:
|
|
|
|
self.highlighted = curses.A_REVERSE
|
2017-03-08 02:35:04 +00:00
|
|
|
self.normal = curses.A_NORMAL
|
2017-03-14 22:23:28 +00:00
|
|
|
# Threaded screen update for live changes
|
2017-03-08 08:18:45 +00:00
|
|
|
screen_thread = threading.Thread(target=self.update_plant_live, args=())
|
|
|
|
screen_thread.daemon = True
|
|
|
|
screen_thread.start()
|
2018-06-07 09:09:29 +00:00
|
|
|
# Recusive lock to prevent both threads from drawing at the same time
|
|
|
|
self.screen_lock = threading.RLock()
|
2017-03-08 08:18:45 +00:00
|
|
|
self.screen.clear()
|
2018-03-03 05:20:29 +00:00
|
|
|
self.show(["water","look","garden","visit", "instructions"], title=' botany ', subtitle='options')
|
2017-03-08 02:35:04 +00:00
|
|
|
|
2017-05-03 19:51:21 +00:00
|
|
|
def define_colors(self):
|
2019-08-16 17:25:45 +00:00
|
|
|
# TODO: implement colors
|
2017-05-03 19:51:21 +00:00
|
|
|
# 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)
|
|
|
|
|
2017-03-13 05:21:17 +00:00
|
|
|
def show(self, options, title, subtitle):
|
|
|
|
# Draws a menu with parameters
|
2017-03-08 02:35:04 +00:00
|
|
|
self.set_options(options)
|
2017-03-09 19:32:40 +00:00
|
|
|
self.update_options()
|
2017-03-08 02:35:04 +00:00
|
|
|
self.title = title
|
|
|
|
self.subtitle = subtitle
|
|
|
|
self.selected = 0
|
2017-03-08 21:30:28 +00:00
|
|
|
self.initialized = True
|
2017-03-08 02:35:04 +00:00
|
|
|
self.draw_menu()
|
|
|
|
|
2017-03-09 19:32:40 +00:00
|
|
|
def update_options(self):
|
2017-03-13 05:21:17 +00:00
|
|
|
# Makes sure you can get a new plant if it dies
|
2019-02-28 00:41:15 +00:00
|
|
|
if self.plant.dead or self.plant.stage == 5:
|
2017-03-21 19:55:11 +00:00
|
|
|
if "harvest" not in self.options:
|
|
|
|
self.options.insert(-1,"harvest")
|
2017-03-09 19:32:40 +00:00
|
|
|
else:
|
2019-02-28 00:41:15 +00:00
|
|
|
if "harvest" in self.options:
|
|
|
|
self.options.remove("harvest")
|
2017-03-09 19:32:40 +00:00
|
|
|
|
2017-03-08 02:35:04 +00:00
|
|
|
def set_options(self, options):
|
2017-03-13 05:21:17 +00:00
|
|
|
# Validates that the last option is "exit"
|
2017-03-09 19:32:40 +00:00
|
|
|
if options[-1] is not 'exit':
|
|
|
|
options.append('exit')
|
2017-03-08 02:35:04 +00:00
|
|
|
self.options = options
|
|
|
|
|
2017-03-14 22:23:28 +00:00
|
|
|
def draw(self):
|
|
|
|
# Draw the menu and lines
|
2018-03-11 20:46:45 +00:00
|
|
|
self.maxy, self.maxx = self.screen.getmaxyx()
|
2018-06-07 09:09:29 +00:00
|
|
|
self.screen_lock.acquire()
|
2017-03-14 22:23:28 +00:00
|
|
|
self.screen.refresh()
|
|
|
|
try:
|
|
|
|
self.draw_default()
|
|
|
|
self.screen.refresh()
|
|
|
|
except Exception as exception:
|
|
|
|
# Makes sure data is saved in event of a crash due to window resizing
|
2017-03-15 01:31:18 +00:00
|
|
|
self.screen.clear()
|
2017-03-24 00:08:27 +00:00
|
|
|
self.screen.addstr(0, 0, "Enlarge terminal!", curses.A_NORMAL)
|
2017-03-15 01:31:18 +00:00
|
|
|
self.screen.refresh()
|
2017-03-14 22:23:28 +00:00
|
|
|
self.__exit__()
|
|
|
|
traceback.print_exc()
|
2018-06-07 09:09:29 +00:00
|
|
|
self.screen_lock.release()
|
2017-03-14 22:23:28 +00:00
|
|
|
|
2017-03-08 02:35:04 +00:00
|
|
|
def draw_menu(self):
|
2017-03-13 05:21:17 +00:00
|
|
|
# Actually draws the menu and handles branching
|
2017-03-08 02:35:04 +00:00
|
|
|
request = ""
|
|
|
|
try:
|
2017-03-09 19:32:40 +00:00
|
|
|
while request is not "exit":
|
2017-03-08 02:35:04 +00:00
|
|
|
self.draw()
|
|
|
|
request = self.get_user_input()
|
|
|
|
self.handle_request(request)
|
|
|
|
self.__exit__()
|
|
|
|
|
|
|
|
# Also calls __exit__, but adds traceback after
|
|
|
|
except Exception as exception:
|
2017-03-15 01:31:18 +00:00
|
|
|
self.screen.clear()
|
2017-03-24 00:08:27 +00:00
|
|
|
self.screen.addstr(0, 0, "Enlarge terminal!", curses.A_NORMAL)
|
2017-03-15 01:31:18 +00:00
|
|
|
self.screen.refresh()
|
2017-03-08 02:35:04 +00:00
|
|
|
self.__exit__()
|
2017-03-15 01:31:18 +00:00
|
|
|
#traceback.print_exc()
|
2018-07-03 12:18:09 +00:00
|
|
|
except IOError as exception:
|
|
|
|
self.screen.clear()
|
|
|
|
self.screen.refresh()
|
|
|
|
self.__exit__()
|
2017-03-08 02:35:04 +00:00
|
|
|
|
2017-03-15 06:51:52 +00:00
|
|
|
def ascii_render(self, filename, ypos, xpos):
|
2017-03-15 20:56:00 +00:00
|
|
|
# Prints ASCII art from file at given coordinates
|
2017-03-15 06:51:52 +00:00
|
|
|
this_dir = os.path.join(os.path.dirname(os.path.realpath(__file__)),"art")
|
|
|
|
this_filename = os.path.join(this_dir,filename)
|
|
|
|
this_file = open(this_filename,"r")
|
|
|
|
this_string = this_file.readlines()
|
|
|
|
this_file.close()
|
2018-06-07 09:09:29 +00:00
|
|
|
self.screen_lock.acquire()
|
2017-03-15 06:51:52 +00:00
|
|
|
for y, line in enumerate(this_string, 2):
|
2017-03-24 00:08:27 +00:00
|
|
|
self.screen.addstr(ypos+y, xpos, line, curses.A_NORMAL)
|
2017-03-15 06:51:52 +00:00
|
|
|
# self.screen.refresh()
|
2018-06-07 09:09:29 +00:00
|
|
|
self.screen_lock.release()
|
2017-03-15 06:51:52 +00:00
|
|
|
|
2017-03-17 22:03:32 +00:00
|
|
|
def draw_plant_ascii(self, this_plant):
|
2018-03-11 21:23:00 +00:00
|
|
|
ypos = 0
|
2017-03-17 22:03:32 +00:00
|
|
|
xpos = int((self.maxx-37)/2 + 25)
|
2018-03-11 23:36:55 +00:00
|
|
|
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',
|
|
|
|
]
|
2017-05-22 19:06:57 +00:00
|
|
|
if this_plant.dead == True:
|
|
|
|
self.ascii_render('rip.txt', ypos, xpos)
|
2018-10-03 18:53:49 +00:00
|
|
|
elif datetime.date.today().month == 10 and datetime.date.today().day == 31:
|
|
|
|
self.ascii_render('jackolantern.txt', ypos, xpos)
|
2017-05-22 19:06:57 +00:00
|
|
|
elif this_plant.stage == 0:
|
2017-03-17 22:03:32 +00:00
|
|
|
self.ascii_render('seed.txt', ypos, xpos)
|
|
|
|
elif this_plant.stage == 1:
|
|
|
|
self.ascii_render('seedling.txt', ypos, xpos)
|
2017-03-22 00:42:39 +00:00
|
|
|
elif this_plant.stage == 2:
|
2017-09-21 21:29:11 +00:00
|
|
|
this_filename = plant_art_list[this_plant.species]+'1.txt'
|
2017-03-17 22:03:32 +00:00
|
|
|
self.ascii_render(this_filename, ypos, xpos)
|
2017-04-26 21:46:42 +00:00
|
|
|
elif this_plant.stage == 3 or this_plant.stage == 5:
|
2017-09-21 21:29:11 +00:00
|
|
|
this_filename = plant_art_list[this_plant.species]+'2.txt'
|
2017-03-22 00:42:39 +00:00
|
|
|
self.ascii_render(this_filename, ypos, xpos)
|
2017-04-26 21:46:42 +00:00
|
|
|
elif this_plant.stage == 4:
|
2017-09-21 21:29:11 +00:00
|
|
|
this_filename = plant_art_list[this_plant.species]+'3.txt'
|
2017-04-19 20:32:18 +00:00
|
|
|
self.ascii_render(this_filename, ypos, xpos)
|
2017-03-17 22:03:32 +00:00
|
|
|
|
2017-03-13 05:21:17 +00:00
|
|
|
def draw_default(self):
|
2017-03-15 01:31:18 +00:00
|
|
|
# draws default menu
|
2017-03-10 01:02:19 +00:00
|
|
|
clear_bar = " " * (int(self.maxx*2/3))
|
2018-06-07 09:09:29 +00:00
|
|
|
self.screen_lock.acquire()
|
2018-03-11 20:46:45 +00:00
|
|
|
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
|
2017-03-15 01:31:18 +00:00
|
|
|
# clear menu on screen
|
2017-03-14 22:23:28 +00:00
|
|
|
for index in range(len(self.options)+1):
|
2018-03-11 20:46:45 +00:00
|
|
|
self.screen.addstr(4+index, 4, clear_bar, curses.A_NORMAL)
|
2017-03-15 01:31:18 +00:00
|
|
|
# display all the menu items, showing the 'pos' item highlighted
|
2017-03-08 02:35:04 +00:00
|
|
|
for index in range(len(self.options)):
|
|
|
|
textstyle = self.normal
|
|
|
|
if index == self.selected:
|
|
|
|
textstyle = self.highlighted
|
2018-03-11 20:46:45 +00:00
|
|
|
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)
|
2017-03-08 02:35:04 +00:00
|
|
|
|
2017-03-24 00:08:27 +00:00
|
|
|
self.screen.addstr(12, 2, clear_bar, curses.A_NORMAL)
|
2018-03-03 05:20:29 +00:00
|
|
|
self.screen.addstr(13, 2, clear_bar, curses.A_NORMAL)
|
|
|
|
self.screen.addstr(12, 2, "plant: ", curses.A_DIM)
|
|
|
|
self.screen.addstr(12, 9, self.plant_string, curses.A_NORMAL)
|
|
|
|
self.screen.addstr(13, 2, "score: ", curses.A_DIM)
|
|
|
|
self.screen.addstr(13, 9, self.plant_ticks, curses.A_NORMAL)
|
2017-03-08 21:22:50 +00:00
|
|
|
|
2017-04-26 21:46:42 +00:00
|
|
|
# display fancy water gauge
|
2017-03-08 23:04:09 +00:00
|
|
|
if not self.plant.dead:
|
2017-03-24 23:27:25 +00:00
|
|
|
water_gauge_str = self.water_gauge()
|
2018-04-24 17:47:19 +00:00
|
|
|
self.screen.addstr(4,14, water_gauge_str, curses.A_NORMAL)
|
2017-03-08 21:22:50 +00:00
|
|
|
else:
|
2018-04-24 17:47:19 +00:00
|
|
|
self.screen.addstr(4,13, clear_bar, curses.A_NORMAL)
|
|
|
|
self.screen.addstr(4,14, "( RIP )", curses.A_NORMAL)
|
2017-03-15 20:56:00 +00:00
|
|
|
|
2017-03-24 23:27:25 +00:00
|
|
|
# draw cute ascii from files
|
2018-06-05 12:38:11 +00:00
|
|
|
if self.visited_plant:
|
|
|
|
# Needed to prevent drawing over a visited plant
|
|
|
|
self.draw_plant_ascii(self.visited_plant)
|
|
|
|
else:
|
|
|
|
self.draw_plant_ascii(self.plant)
|
2018-06-07 09:09:29 +00:00
|
|
|
self.screen_lock.release()
|
2017-03-13 05:21:17 +00:00
|
|
|
|
2017-03-24 23:27:25 +00:00
|
|
|
def water_gauge(self):
|
|
|
|
# build nice looking water gauge
|
|
|
|
water_left_pct = 1 - ((time.time() - self.plant.watered_timestamp)/86400)
|
|
|
|
# don't allow negative value
|
|
|
|
water_left_pct = max(0, water_left_pct)
|
|
|
|
water_left = int(math.ceil(water_left_pct * 10))
|
2017-03-27 18:28:00 +00:00
|
|
|
water_string = "(" + (")" * water_left) + ("." * (10 - water_left)) + ") " + str(int(water_left_pct * 100)) + "% "
|
2017-03-24 23:27:25 +00:00
|
|
|
return water_string
|
|
|
|
|
2017-03-14 22:23:28 +00:00
|
|
|
def update_plant_live(self):
|
2017-03-15 01:31:18 +00:00
|
|
|
# updates plant data on menu screen, live!
|
2017-03-14 22:23:28 +00:00
|
|
|
while not self.exit:
|
|
|
|
self.plant_string = self.plant.parse_plant()
|
2018-06-05 00:19:47 +00:00
|
|
|
self.plant_ticks = str(int(self.plant.ticks))
|
2017-03-14 22:23:28 +00:00
|
|
|
if self.initialized:
|
|
|
|
self.update_options()
|
|
|
|
self.draw()
|
|
|
|
time.sleep(1)
|
|
|
|
|
|
|
|
def get_user_input(self):
|
2017-03-23 01:49:38 +00:00
|
|
|
# gets the user's input
|
2017-03-15 01:31:18 +00:00
|
|
|
try:
|
|
|
|
user_in = self.screen.getch() # Gets user input
|
|
|
|
except Exception as e:
|
|
|
|
self.__exit__()
|
2018-07-03 12:18:09 +00:00
|
|
|
if user_in == -1: # Input comes from pipe/file and is closed
|
|
|
|
raise IOError
|
2017-04-26 21:46:42 +00:00
|
|
|
## DEBUG KEYS - enable these lines to see curses key codes
|
2018-11-14 23:24:34 +00:00
|
|
|
# self.screen.addstr(2, 2, str(user_in), curses.A_NORMAL)
|
2017-03-16 20:01:08 +00:00
|
|
|
# self.screen.refresh()
|
|
|
|
|
|
|
|
# Resize sends curses.KEY_RESIZE, update display
|
|
|
|
if user_in == curses.KEY_RESIZE:
|
|
|
|
self.maxy,self.maxx = self.screen.getmaxyx()
|
|
|
|
self.screen.clear()
|
|
|
|
self.screen.refresh()
|
|
|
|
|
2018-12-13 22:54:57 +00:00
|
|
|
# enter, exit, and Q Keys are special cases
|
2017-03-14 22:23:28 +00:00
|
|
|
if user_in == 10:
|
|
|
|
return self.options[self.selected]
|
|
|
|
if user_in == 27:
|
|
|
|
return self.options[-1]
|
2018-12-13 22:54:57 +00:00
|
|
|
if user_in == 113:
|
|
|
|
self.selected = len(self.options) - 1
|
|
|
|
return
|
2017-03-14 22:23:28 +00:00
|
|
|
|
2017-03-15 01:31:18 +00:00
|
|
|
# this is a number; check to see if we can set it
|
2019-08-16 17:21:40 +00:00
|
|
|
if user_in >= ord('1') and user_in <= ord(str(min(7,len(self.options)))):
|
2017-03-14 22:23:28 +00:00
|
|
|
self.selected = user_in - ord('0') - 1 # convert keypress back to a number, then subtract 1 to get index
|
|
|
|
return
|
|
|
|
|
2017-03-15 01:31:18 +00:00
|
|
|
# increment or Decrement
|
2017-03-24 00:08:27 +00:00
|
|
|
down_keys = [curses.KEY_DOWN, 14, ord('j')]
|
|
|
|
up_keys = [curses.KEY_UP, 16, ord('k')]
|
|
|
|
|
2017-03-16 20:01:08 +00:00
|
|
|
if user_in in down_keys: # down arrow
|
2017-03-14 22:23:28 +00:00
|
|
|
self.selected += 1
|
2017-03-16 20:01:08 +00:00
|
|
|
if user_in in up_keys: # up arrow
|
2017-03-14 22:23:28 +00:00
|
|
|
self.selected -=1
|
2017-03-24 00:08:27 +00:00
|
|
|
|
2017-03-24 23:27:25 +00:00
|
|
|
# modulo to wrap menu cursor
|
2017-03-14 22:23:28 +00:00
|
|
|
self.selected = self.selected % len(self.options)
|
|
|
|
return
|
|
|
|
|
2017-03-13 05:21:17 +00:00
|
|
|
def format_garden_data(self,this_garden):
|
2017-03-21 19:55:11 +00:00
|
|
|
# Returns list of lists (pages) of garden entries
|
2018-06-06 15:25:28 +00:00
|
|
|
plant_table = []
|
2017-03-13 05:21:17 +00:00
|
|
|
for plant_id in this_garden:
|
|
|
|
if this_garden[plant_id]:
|
|
|
|
if not this_garden[plant_id]["dead"]:
|
|
|
|
this_plant = this_garden[plant_id]
|
2018-11-25 04:34:04 +00:00
|
|
|
plant_table.append((this_plant["owner"],
|
2018-06-06 15:25:28 +00:00
|
|
|
this_plant["age"],
|
|
|
|
int(this_plant["score"]),
|
|
|
|
this_plant["description"]))
|
|
|
|
return plant_table
|
|
|
|
|
|
|
|
def format_garden_entry(self, entry):
|
2018-11-25 04:47:28 +00:00
|
|
|
return "{:14.14} - {:>16} - {:>8}p - {}".format(*entry)
|
2018-06-06 15:25:28 +00:00
|
|
|
|
|
|
|
def sort_garden_table(self, table, column, ascending):
|
|
|
|
""" Sort table in place by a specified column """
|
2018-06-06 20:34:30 +00:00
|
|
|
def key(entry):
|
|
|
|
entry = entry[column]
|
|
|
|
# In when sorting ages, convert to seconds
|
|
|
|
if column == 1:
|
|
|
|
coeffs = [24*60*60, 60*60, 60, 1]
|
|
|
|
nums = [int(n[:-1]) for n in entry.split(":")]
|
|
|
|
if len(nums) == len(coeffs):
|
|
|
|
entry = sum(nums[i] * coeffs[i] for i in range(len(nums)))
|
|
|
|
return entry
|
|
|
|
|
|
|
|
return table.sort(key=key, reverse=not ascending)
|
2018-06-06 15:25:28 +00:00
|
|
|
|
|
|
|
def filter_garden_table(self, table, pattern):
|
|
|
|
""" Filter table using a pattern, and return the new table """
|
|
|
|
def filterfunc(entry):
|
|
|
|
if len(pattern) == 0:
|
|
|
|
return True
|
|
|
|
entry_txt = self.format_garden_entry(entry)
|
|
|
|
try:
|
|
|
|
result = bool(re.search(pattern, entry_txt))
|
|
|
|
except Exception as e:
|
|
|
|
# In case of invalid regex, don't match anything
|
|
|
|
result = False
|
|
|
|
return result
|
|
|
|
return list(filter(filterfunc, table))
|
2017-03-13 05:21:17 +00:00
|
|
|
|
|
|
|
def draw_garden(self):
|
2017-03-24 00:08:27 +00:00
|
|
|
# draws community garden
|
2017-03-23 01:49:38 +00:00
|
|
|
# load data from sqlite db
|
|
|
|
this_garden = self.user_data.retrieve_garden_from_db()
|
2017-03-13 05:21:17 +00:00
|
|
|
# format data
|
2017-03-24 00:08:27 +00:00
|
|
|
self.clear_info_pane()
|
2018-06-06 15:25:28 +00:00
|
|
|
|
|
|
|
if self.infotoggle == 2:
|
2017-03-21 19:55:11 +00:00
|
|
|
# the screen IS currently showing the garden (1 page), make the
|
|
|
|
# text a bunch of blanks to clear it out
|
2017-03-15 01:31:18 +00:00
|
|
|
self.infotoggle = 0
|
2018-06-06 15:25:28 +00:00
|
|
|
return
|
|
|
|
|
|
|
|
# if infotoggle isn't 2, the screen currently displays other stuff
|
|
|
|
plant_table_orig = self.format_garden_data(this_garden)
|
|
|
|
self.infotoggle = 2
|
2017-03-13 05:21:17 +00:00
|
|
|
|
2017-03-21 19:55:11 +00:00
|
|
|
# print garden information OR clear it
|
2018-06-06 15:25:28 +00:00
|
|
|
index = 0
|
|
|
|
sort_column, sort_ascending = 0, True
|
|
|
|
sort_keys = ["n", "a", "s", "d"] # Name, Age, Score, Description
|
|
|
|
plant_table = plant_table_orig
|
|
|
|
self.sort_garden_table(plant_table, sort_column, sort_ascending)
|
|
|
|
while True:
|
|
|
|
entries_per_page = self.maxy - 16
|
|
|
|
index_max = min(len(plant_table), index + entries_per_page)
|
|
|
|
plants = plant_table[index:index_max]
|
|
|
|
page = [self.format_garden_entry(entry) for entry in plants]
|
2018-06-07 09:09:29 +00:00
|
|
|
self.screen_lock.acquire()
|
2017-03-24 00:08:27 +00:00
|
|
|
self.draw_info_text(page)
|
2018-06-06 15:25:28 +00:00
|
|
|
# Multiple pages, paginate and require keypress
|
2018-06-13 00:06:57 +00:00
|
|
|
page_text = "(%d-%d/%d) | sp/next | bksp/prev | s <col #>/sort | f/filter | q/quit" % (index, index_max, len(plant_table))
|
2018-06-06 15:25:28 +00:00
|
|
|
self.screen.addstr(self.maxy-2, 2, page_text)
|
|
|
|
self.screen.refresh()
|
2018-06-07 09:09:29 +00:00
|
|
|
self.screen_lock.release()
|
2018-06-06 15:25:28 +00:00
|
|
|
c = self.screen.getch()
|
2018-07-03 12:18:09 +00:00
|
|
|
if c == -1: # Input comes from pipe/file and is closed
|
|
|
|
raise IOError
|
2018-06-06 15:25:28 +00:00
|
|
|
self.infotoggle = 0
|
|
|
|
|
|
|
|
# Quit
|
|
|
|
if c == ord("q") or c == ord("x") or c == 27:
|
|
|
|
break
|
|
|
|
# Next page
|
|
|
|
elif c in [curses.KEY_ENTER, curses.KEY_NPAGE, ord(" "), ord("\n")]:
|
|
|
|
index += entries_per_page
|
|
|
|
if index >= len(plant_table):
|
|
|
|
break
|
|
|
|
# Previous page
|
|
|
|
elif c == curses.KEY_BACKSPACE or c == curses.KEY_PPAGE:
|
|
|
|
index = max(index - entries_per_page, 0)
|
|
|
|
# Next line
|
|
|
|
elif c == ord("j") or c == curses.KEY_DOWN:
|
|
|
|
index = max(min(index + 1, len(plant_table) - 1), 0)
|
|
|
|
# Previous line
|
|
|
|
elif c == ord("k") or c == curses.KEY_UP:
|
|
|
|
index = max(index - 1, 0)
|
|
|
|
# Sort entries
|
|
|
|
elif c == ord("s"):
|
|
|
|
c = self.screen.getch()
|
2018-07-03 12:18:09 +00:00
|
|
|
if c == -1: # Input comes from pipe/file and is closed
|
|
|
|
raise IOError
|
2018-06-06 15:25:28 +00:00
|
|
|
column = -1
|
|
|
|
if c < 255 and chr(c) in sort_keys:
|
|
|
|
column = sort_keys.index(chr(c))
|
|
|
|
elif ord("1") <= c <= ord("4"):
|
|
|
|
column = c - ord("1")
|
|
|
|
if column != -1:
|
|
|
|
if sort_column == column:
|
|
|
|
sort_ascending = not sort_ascending
|
|
|
|
else:
|
|
|
|
sort_column = column
|
|
|
|
sort_ascending = True
|
|
|
|
self.sort_garden_table(plant_table, sort_column, sort_ascending)
|
|
|
|
# Filter entries
|
|
|
|
elif c == ord("/") or c == ord("f"):
|
|
|
|
self.screen.addstr(self.maxy-2, 2, "Filter: " + " " * (len(page_text)-8))
|
|
|
|
pattern = self.get_user_string(10, self.maxy-2, lambda x: x in string.printable)
|
|
|
|
plant_table = self.filter_garden_table(plant_table_orig, pattern)
|
|
|
|
self.sort_garden_table(plant_table, sort_column, sort_ascending)
|
|
|
|
index = 0
|
|
|
|
|
|
|
|
# Clear page before drawing next
|
|
|
|
self.clear_info_pane()
|
|
|
|
self.clear_info_pane()
|
2017-03-13 05:21:17 +00:00
|
|
|
|
2017-03-14 22:23:28 +00:00
|
|
|
def get_plant_description(self, this_plant):
|
|
|
|
output_text = ""
|
2017-09-21 21:29:11 +00:00
|
|
|
this_species = this_plant.species_list[this_plant.species]
|
|
|
|
this_color = this_plant.color_list[this_plant.color]
|
2017-03-14 22:23:28 +00:00
|
|
|
this_stage = this_plant.stage
|
2017-03-08 02:35:04 +00:00
|
|
|
|
2017-03-14 22:23:28 +00:00
|
|
|
stage_descriptions = {
|
|
|
|
0:[
|
|
|
|
"You're excited about your new seed.",
|
|
|
|
"You wonder what kind of plant your seed will grow into.",
|
|
|
|
"You're ready for a new start with this plant.",
|
|
|
|
"You're tired of waiting for your seed to grow.",
|
|
|
|
"You wish your seed could tell you what it needs.",
|
2017-03-15 01:31:18 +00:00
|
|
|
"You can feel the spirit inside your seed.",
|
2017-03-17 19:00:46 +00:00
|
|
|
"These pretzels are making you thirsty.",
|
2017-03-21 19:55:11 +00:00
|
|
|
"Way to plant, Ann!",
|
|
|
|
"'To see things in the seed, that is genius' - Lao Tzu",
|
2017-03-14 22:23:28 +00:00
|
|
|
],
|
|
|
|
1:[
|
|
|
|
"The seedling fills you with hope.",
|
2017-03-15 01:31:18 +00:00
|
|
|
"The seedling shakes in the wind.",
|
2017-03-14 22:23:28 +00:00
|
|
|
"You can make out a tiny leaf - or is that a thorn?",
|
|
|
|
"You can feel the seedling looking back at you.",
|
2017-03-24 23:27:25 +00:00
|
|
|
"You blow a kiss to your seedling.",
|
2017-03-14 22:23:28 +00:00
|
|
|
"You think about all the seedlings who came before it.",
|
|
|
|
"You and your seedling make a great team.",
|
2017-03-15 01:31:18 +00:00
|
|
|
"Your seedling grows slowly and quietly.",
|
2017-03-15 06:51:52 +00:00
|
|
|
"You meditate on the paths your plant's life could take.",
|
2017-03-14 22:23:28 +00:00
|
|
|
],
|
|
|
|
2:[
|
|
|
|
"The " + this_species + " makes you feel relaxed.",
|
|
|
|
"You sing a song to your " + this_species + ".",
|
|
|
|
"You quietly sit with your " + this_species + " for a few minutes.",
|
|
|
|
"Your " + this_species + " looks pretty good.",
|
|
|
|
"You play loud techno to your " + this_species + ".",
|
2017-03-15 01:31:18 +00:00
|
|
|
"You play piano to your " + this_species + ".",
|
|
|
|
"You play rap music to your " + this_species + ".",
|
|
|
|
"You whistle a tune to your " + this_species + ".",
|
2017-03-22 00:42:39 +00:00
|
|
|
"You read a poem to your " + this_species + ".",
|
|
|
|
"You tell a secret to your " + this_species + ".",
|
|
|
|
"You play your favorite record for your " + this_species + ".",
|
2017-03-14 22:23:28 +00:00
|
|
|
],
|
|
|
|
3:[
|
|
|
|
"Your " + this_species + " is growing nicely!",
|
|
|
|
"You're proud of the dedication it took to grow your " + this_species + ".",
|
2017-03-22 00:42:39 +00:00
|
|
|
"You take a deep breath with your " + this_species + ".",
|
|
|
|
"You think of all the words that rhyme with " + this_species + ".",
|
|
|
|
"The " + this_species + " looks full of life.",
|
|
|
|
"The " + this_species + " inspires you.",
|
|
|
|
"Your " + this_species + " makes you forget about your problems.",
|
|
|
|
"Your " + this_species + " gives you a reason to keep going.",
|
|
|
|
"Looking at your " + this_species + " helps you focus on what matters.",
|
|
|
|
"You think about how nice this " + this_species + " looks here.",
|
2017-03-21 19:55:11 +00:00
|
|
|
"The buds of your " + this_species + " might bloom soon.",
|
2017-03-14 22:23:28 +00:00
|
|
|
],
|
|
|
|
4:[
|
|
|
|
"The " + this_color + " flowers look nice on your " + this_species +"!",
|
2017-03-15 01:31:18 +00:00
|
|
|
"The " + this_color + " flowers have bloomed and fill you with positivity.",
|
2017-03-22 00:42:39 +00:00
|
|
|
"The " + this_color + " flowers remind you of your childhood.",
|
|
|
|
"The " + this_color + " flowers remind you of spring mornings.",
|
|
|
|
"The " + this_color + " flowers remind you of a forgotten memory.",
|
|
|
|
"The " + this_color + " flowers remind you of your happy place.",
|
|
|
|
"The aroma of the " + this_color + " flowers energize you.",
|
2017-03-14 22:23:28 +00:00
|
|
|
"The " + this_species + " has grown beautiful " + this_color + " flowers.",
|
2017-03-15 01:31:18 +00:00
|
|
|
"The " + this_color + " petals remind you of that favorite shirt you lost.",
|
|
|
|
"The " + this_color + " flowers remind you of your crush.",
|
2017-03-22 00:42:39 +00:00
|
|
|
"You smell the " + this_color + " flowers and are filled with peace.",
|
2017-03-14 22:23:28 +00:00
|
|
|
],
|
|
|
|
5:[
|
2017-03-22 00:42:39 +00:00
|
|
|
"You fondly remember the time you spent caring for your " + this_species + ".",
|
2017-03-14 22:23:28 +00:00
|
|
|
"Seed pods have grown on your " + this_species + ".",
|
2017-03-22 00:42:39 +00:00
|
|
|
"You feel like your " + this_species + " appreciates your care.",
|
2017-03-14 22:23:28 +00:00
|
|
|
"The " + this_species + " fills you with love.",
|
2017-03-22 00:42:39 +00:00
|
|
|
"You're ready for whatever comes after your " + this_species + ".",
|
|
|
|
"You're excited to start growing your next plant.",
|
|
|
|
"You reflect on when your " + this_species + " was just a seedling.",
|
2017-03-15 01:31:18 +00:00
|
|
|
"You grow nostalgic about the early days with your " + this_species + ".",
|
2017-03-14 22:23:28 +00:00
|
|
|
],
|
|
|
|
99:[
|
|
|
|
"You wish you had taken better care of your plant.",
|
|
|
|
"If only you had watered your plant more often..",
|
|
|
|
"Your plant is dead, there's always next time.",
|
|
|
|
"You cry over the withered leaves of your plant.",
|
|
|
|
"Your plant died. Maybe you need a fresh start.",
|
|
|
|
],
|
|
|
|
}
|
|
|
|
# self.life_stages is tuple containing length of each stage
|
|
|
|
# (seed, seedling, young, mature, flowering)
|
|
|
|
if this_plant.dead:
|
|
|
|
this_stage = 99
|
2017-03-08 02:35:04 +00:00
|
|
|
|
2017-03-14 22:23:28 +00:00
|
|
|
this_stage_descriptions = stage_descriptions[this_stage]
|
|
|
|
description_num = random.randint(0,len(this_stage_descriptions) - 1)
|
2017-03-23 01:49:38 +00:00
|
|
|
# If not fully grown
|
2017-03-14 22:23:28 +00:00
|
|
|
if this_stage <= 4:
|
|
|
|
# Growth hint
|
|
|
|
if this_stage >= 1:
|
|
|
|
last_growth_at = this_plant.life_stages[this_stage - 1]
|
|
|
|
else:
|
|
|
|
last_growth_at = 0
|
|
|
|
ticks_since_last = this_plant.ticks - last_growth_at
|
|
|
|
ticks_between_stage = this_plant.life_stages[this_stage] - last_growth_at
|
|
|
|
if ticks_since_last >= ticks_between_stage * 0.8:
|
|
|
|
output_text += "You notice your plant looks different.\n"
|
2017-03-08 02:35:04 +00:00
|
|
|
|
2017-03-14 22:23:28 +00:00
|
|
|
output_text += this_stage_descriptions[description_num] + "\n"
|
2017-03-08 02:35:04 +00:00
|
|
|
|
2017-03-23 01:49:38 +00:00
|
|
|
# if seedling
|
2017-03-14 22:23:28 +00:00
|
|
|
if this_stage == 1:
|
2017-09-21 21:29:11 +00:00
|
|
|
species_options = [this_plant.species_list[this_plant.species],
|
|
|
|
this_plant.species_list[(this_plant.species+3) % len(this_plant.species_list)],
|
|
|
|
this_plant.species_list[(this_plant.species-3) % len(this_plant.species_list)]]
|
2017-03-14 22:23:28 +00:00
|
|
|
random.shuffle(species_options)
|
|
|
|
plant_hint = "It could be a(n) " + species_options[0] + ", " + species_options[1] + ", or " + species_options[2]
|
|
|
|
output_text += plant_hint + ".\n"
|
|
|
|
|
2017-03-23 01:49:38 +00:00
|
|
|
# if young plant
|
2017-03-14 22:23:28 +00:00
|
|
|
if this_stage == 2:
|
|
|
|
if this_plant.rarity >= 2:
|
|
|
|
rarity_hint = "You feel like your plant is special."
|
|
|
|
output_text += rarity_hint + ".\n"
|
|
|
|
|
2017-03-23 01:49:38 +00:00
|
|
|
# if mature plant
|
2017-03-14 22:23:28 +00:00
|
|
|
if this_stage == 3:
|
2017-09-21 21:29:11 +00:00
|
|
|
color_options = [this_plant.color_list[this_plant.color],
|
|
|
|
this_plant.color_list[(this_plant.color+3) % len(this_plant.color_list)],
|
|
|
|
this_plant.color_list[(this_plant.color-3) % len(this_plant.color_list)]]
|
2017-03-14 22:23:28 +00:00
|
|
|
random.shuffle(color_options)
|
|
|
|
plant_hint = "You can see the first hints of " + color_options[0] + ", " + color_options[1] + ", or " + color_options[2]
|
|
|
|
output_text += plant_hint + ".\n"
|
|
|
|
|
|
|
|
return output_text
|
|
|
|
|
|
|
|
def draw_plant_description(self, this_plant):
|
2017-03-23 01:49:38 +00:00
|
|
|
# If menu is currently showing something other than the description
|
2017-03-24 00:08:27 +00:00
|
|
|
self.clear_info_pane()
|
2017-03-15 01:31:18 +00:00
|
|
|
if self.infotoggle != 1:
|
2017-03-23 01:49:38 +00:00
|
|
|
# get plant description before printing
|
2017-03-14 22:23:28 +00:00
|
|
|
output_string = self.get_plant_description(this_plant)
|
2017-05-03 23:43:58 +00:00
|
|
|
growth_multiplier = 1 + (0.2 * (this_plant.generation-1))
|
2019-08-26 16:34:08 +00:00
|
|
|
output_string += "Generation: {}\nGrowth rate: {}x".format(self.plant.generation, growth_multiplier)
|
2017-03-24 00:08:27 +00:00
|
|
|
self.draw_info_text(output_string)
|
2017-03-15 01:31:18 +00:00
|
|
|
self.infotoggle = 1
|
2017-03-14 22:23:28 +00:00
|
|
|
else:
|
2017-03-24 00:08:27 +00:00
|
|
|
# otherwise just set toggle
|
2017-03-15 01:31:18 +00:00
|
|
|
self.infotoggle = 0
|
2017-03-14 22:23:28 +00:00
|
|
|
|
2017-03-13 05:21:17 +00:00
|
|
|
def draw_instructions(self):
|
2017-03-24 00:08:27 +00:00
|
|
|
# Draw instructions on screen
|
|
|
|
self.clear_info_pane()
|
|
|
|
if self.infotoggle != 4:
|
2017-03-27 18:28:00 +00:00
|
|
|
instructions_txt = ("welcome to botany. you've been given a seed\n"
|
|
|
|
"that will grow into a beautiful plant. check\n"
|
|
|
|
"in and water your plant every 24h to keep it\n"
|
|
|
|
"growing. 5 days without water = death. your\n"
|
2018-03-14 21:42:10 +00:00
|
|
|
"plant depends on you & your friends to live!\n"
|
|
|
|
"more info is available in the readme :)\n"
|
2017-03-27 18:28:00 +00:00
|
|
|
" cheers,\n"
|
|
|
|
" curio\n"
|
|
|
|
)
|
2017-03-24 00:08:27 +00:00
|
|
|
self.draw_info_text(instructions_txt)
|
|
|
|
self.infotoggle = 4
|
2017-03-13 05:21:17 +00:00
|
|
|
else:
|
2017-03-24 00:08:27 +00:00
|
|
|
self.infotoggle = 0
|
|
|
|
|
|
|
|
def clear_info_pane(self):
|
|
|
|
# Clears bottom part of screen
|
2018-06-07 09:09:29 +00:00
|
|
|
self.screen_lock.acquire()
|
2018-03-08 07:07:32 +00:00
|
|
|
clear_bar = " " * (self.maxx - 3)
|
2018-03-08 07:31:00 +00:00
|
|
|
this_y = 14
|
2018-03-08 07:07:32 +00:00
|
|
|
while this_y < self.maxy:
|
2018-06-07 09:09:29 +00:00
|
|
|
self.screen.addstr(this_y, 2, clear_bar, curses.A_NORMAL)
|
|
|
|
this_y += 1
|
2017-03-24 00:08:27 +00:00
|
|
|
self.screen.refresh()
|
2018-06-07 09:09:29 +00:00
|
|
|
self.screen_lock.release()
|
2017-03-24 00:08:27 +00:00
|
|
|
|
2018-03-08 07:07:32 +00:00
|
|
|
def draw_info_text(self, info_text, y_offset = 0):
|
2017-05-03 19:51:21 +00:00
|
|
|
# print lines of text to info pane at bottom of screen
|
2018-06-07 09:09:29 +00:00
|
|
|
self.screen_lock.acquire()
|
2017-03-24 00:08:27 +00:00
|
|
|
if type(info_text) is str:
|
|
|
|
info_text = info_text.splitlines()
|
|
|
|
for y, line in enumerate(info_text, 2):
|
2018-03-08 07:07:32 +00:00
|
|
|
this_y = y+12 + y_offset
|
2018-06-06 09:01:29 +00:00
|
|
|
if len(line) > self.maxx - 3:
|
|
|
|
line = line[:self.maxx-3]
|
2018-03-08 07:07:32 +00:00
|
|
|
if this_y < self.maxy:
|
|
|
|
self.screen.addstr(this_y, 2, line, curses.A_NORMAL)
|
2017-03-13 05:21:17 +00:00
|
|
|
self.screen.refresh()
|
2018-06-07 09:09:29 +00:00
|
|
|
self.screen_lock.release()
|
2017-03-13 17:42:13 +00:00
|
|
|
|
2017-03-24 00:08:27 +00:00
|
|
|
def harvest_confirmation(self):
|
|
|
|
self.clear_info_pane()
|
|
|
|
# get plant description before printing
|
2017-09-21 21:29:11 +00:00
|
|
|
max_stage = len(self.plant.stage_list) - 1
|
2017-05-03 19:51:21 +00:00
|
|
|
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"
|
2017-05-03 23:21:43 +00:00
|
|
|
harvest_text += "Your next plant will grow at a speed of: {}x\n".format(1 + (0.2 * self.plant.generation))
|
2017-05-03 19:51:21 +00:00
|
|
|
harvest_text += "If you harvest your plant you'll start over from a seed.\nContinue? (Y/n)"
|
2017-03-24 00:08:27 +00:00
|
|
|
self.draw_info_text(harvest_text)
|
|
|
|
try:
|
|
|
|
user_in = self.screen.getch() # Gets user input
|
|
|
|
except Exception as e:
|
|
|
|
self.__exit__()
|
2018-07-03 12:18:09 +00:00
|
|
|
if user_in == -1: # Input comes from pipe/file and is closed
|
|
|
|
raise IOError
|
2017-03-24 00:08:27 +00:00
|
|
|
|
2017-05-09 19:31:46 +00:00
|
|
|
if user_in in [ord('Y'), ord('y')]:
|
2017-03-24 00:08:27 +00:00
|
|
|
self.plant.start_over()
|
|
|
|
else:
|
|
|
|
pass
|
|
|
|
self.clear_info_pane()
|
2017-03-23 01:49:38 +00:00
|
|
|
|
2018-03-11 20:46:45 +00:00
|
|
|
def build_weekly_visitor_output(self, visitors):
|
2018-03-08 07:07:32 +00:00
|
|
|
visitor_block = ""
|
|
|
|
visitor_line = ""
|
2018-03-11 20:46:45 +00:00
|
|
|
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))
|
|
|
|
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:
|
2018-03-08 07:31:00 +00:00
|
|
|
visitor_block += '\n'
|
|
|
|
visitor_line = ""
|
2018-03-11 20:46:45 +00:00
|
|
|
visitor_block += this_visitor_string
|
|
|
|
visitor_line += this_visitor_string
|
|
|
|
else:
|
|
|
|
visitor_block = 'nobody :('
|
2018-03-08 07:07:32 +00:00
|
|
|
return visitor_block
|
2018-03-08 07:31:00 +00:00
|
|
|
|
2018-06-10 10:05:17 +00:00
|
|
|
def get_user_string(self, xpos=3, ypos=15, filterfunc=str.isalnum, completer=None):
|
2018-06-06 15:25:28 +00:00
|
|
|
# filter allowed characters using filterfunc, alphanumeric by default
|
2018-03-11 20:46:45 +00:00
|
|
|
user_string = ""
|
2018-03-03 05:20:29 +00:00
|
|
|
user_input = 0
|
2018-06-10 10:05:17 +00:00
|
|
|
if completer:
|
|
|
|
completer = completer(self)
|
2018-03-03 05:20:29 +00:00
|
|
|
while user_input != 10:
|
|
|
|
user_input = self.screen.getch()
|
2018-07-03 12:18:09 +00:00
|
|
|
if user_input == -1: # Input comes from pipe/file and is closed
|
|
|
|
raise IOError
|
2018-06-07 09:09:29 +00:00
|
|
|
self.screen_lock.acquire()
|
2018-03-11 22:10:07 +00:00
|
|
|
# osx and unix backspace chars...
|
|
|
|
if user_input == 127 or user_input == 263:
|
2018-03-11 20:46:45 +00:00
|
|
|
if len(user_string) > 0:
|
|
|
|
user_string = user_string[:-1]
|
2018-06-10 10:05:17 +00:00
|
|
|
if completer:
|
|
|
|
completer.update_input(user_string)
|
2018-06-06 15:25:28 +00:00
|
|
|
self.screen.addstr(ypos, xpos, " " * (self.maxx-xpos-1))
|
2018-06-10 10:05:17 +00:00
|
|
|
elif user_input in [ord('\t'), curses.KEY_BTAB] and completer:
|
|
|
|
direction = 1 if user_input == ord('\t') else -1
|
|
|
|
user_string = completer.complete(direction)
|
|
|
|
self.screen.addstr(ypos, xpos, " " * (self.maxx-xpos-1))
|
|
|
|
elif user_input < 256 and user_input != 10:
|
2018-11-14 23:24:34 +00:00
|
|
|
if filterfunc(chr(user_input)) or chr(user_input) == '_':
|
2018-03-11 20:46:45 +00:00
|
|
|
user_string += chr(user_input)
|
2018-06-10 10:05:17 +00:00
|
|
|
if completer:
|
|
|
|
completer.update_input(user_string)
|
2018-03-11 20:46:45 +00:00
|
|
|
self.screen.addstr(ypos, xpos, str(user_string))
|
2018-03-03 05:20:29 +00:00
|
|
|
self.screen.refresh()
|
2018-06-07 09:09:29 +00:00
|
|
|
self.screen_lock.release()
|
2018-03-11 20:46:45 +00:00
|
|
|
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:
|
|
|
|
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 = []
|
|
|
|
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)
|
2018-06-10 10:05:17 +00:00
|
|
|
guest_garden = self.get_user_string(completer = completer.LoginCompleter)
|
2018-03-11 20:46:45 +00:00
|
|
|
if not guest_garden:
|
|
|
|
self.clear_info_pane()
|
|
|
|
return None
|
2018-03-12 21:35:42 +00:00
|
|
|
if guest_garden.lower() == getpass.getuser().lower():
|
|
|
|
self.screen.addstr(16, 2, "you're already here!")
|
|
|
|
self.screen.getch()
|
|
|
|
self.clear_info_pane()
|
|
|
|
return None
|
2018-03-08 07:07:32 +00:00
|
|
|
home_folder = os.path.dirname(os.path.expanduser("~"))
|
|
|
|
guest_json = home_folder + "/{}/.botany/{}_plant_data.json".format(guest_garden, guest_garden)
|
|
|
|
guest_plant_description = ""
|
|
|
|
if os.path.isfile(guest_json):
|
|
|
|
with open(guest_json) as f:
|
|
|
|
visitor_data = json.load(f)
|
|
|
|
guest_plant_description = visitor_data['description']
|
2018-06-05 12:38:11 +00:00
|
|
|
self.visited_plant = self.get_visited_plant(visitor_data)
|
2018-03-08 07:07:32 +00:00
|
|
|
guest_visitor_file = home_folder + "/{}/.botany/visitors.json".format(guest_garden, guest_garden)
|
|
|
|
if os.path.isfile(guest_visitor_file):
|
2018-06-19 23:58:51 +00:00
|
|
|
water_success = self.water_on_visit(guest_visitor_file)
|
|
|
|
if water_success:
|
|
|
|
self.screen.addstr(16, 2, "...you watered ~{}'s {}...".format(str(guest_garden), guest_plant_description))
|
|
|
|
if self.visited_plant:
|
|
|
|
self.draw_plant_ascii(self.visited_plant)
|
|
|
|
else:
|
|
|
|
self.screen.addstr(16, 2, "{}'s garden is locked, but you can see in...".format(guest_garden))
|
2018-03-03 05:20:29 +00:00
|
|
|
else:
|
|
|
|
self.screen.addstr(16, 2, "i can't seem to find directions to {}...".format(guest_garden))
|
|
|
|
self.screen.getch()
|
|
|
|
self.clear_info_pane()
|
2018-06-05 12:38:11 +00:00
|
|
|
self.draw_plant_ascii(self.plant)
|
|
|
|
self.visited_plant = None
|
2018-03-03 05:20:29 +00:00
|
|
|
|
2018-03-08 07:07:32 +00:00
|
|
|
def water_on_visit(self, guest_visitor_file):
|
|
|
|
visitor_data = {}
|
2018-08-27 21:01:46 +00:00
|
|
|
# using -1 here so that old running instances can be watered
|
|
|
|
guest_data = {'user': getpass.getuser(), 'timestamp': int(time.time()) - 1}
|
2018-03-08 07:07:32 +00:00
|
|
|
if os.path.isfile(guest_visitor_file):
|
2018-06-19 23:58:51 +00:00
|
|
|
if not os.access(guest_visitor_file, os.W_OK):
|
|
|
|
return False
|
2018-03-08 07:07:32 +00:00
|
|
|
with open(guest_visitor_file) as f:
|
|
|
|
visitor_data = json.load(f)
|
|
|
|
visitor_data.append(guest_data)
|
|
|
|
with open(guest_visitor_file, mode='w') as f:
|
|
|
|
f.write(json.dumps(visitor_data, indent=2))
|
2018-06-19 23:58:51 +00:00
|
|
|
return True
|
2018-03-03 05:20:29 +00:00
|
|
|
|
2018-06-05 12:38:11 +00:00
|
|
|
def get_visited_plant(self, visitor_data):
|
|
|
|
""" Returns a drawable pseudo plant object from json data """
|
|
|
|
class VisitedPlant: pass
|
|
|
|
plant = VisitedPlant()
|
|
|
|
plant.stage = 0
|
|
|
|
plant.species = 0
|
|
|
|
|
|
|
|
if "is_dead" not in visitor_data:
|
|
|
|
return None
|
|
|
|
plant.dead = visitor_data["is_dead"]
|
|
|
|
if plant.dead:
|
|
|
|
return plant
|
|
|
|
|
|
|
|
if "stage" in visitor_data:
|
|
|
|
stage = visitor_data["stage"]
|
|
|
|
if stage in self.plant.stage_list:
|
|
|
|
plant.stage = self.plant.stage_list.index(stage)
|
|
|
|
|
|
|
|
if "species" in visitor_data:
|
|
|
|
species = visitor_data["species"]
|
|
|
|
if species in self.plant.species_list:
|
|
|
|
plant.species = self.plant.species_list.index(species)
|
|
|
|
else:
|
|
|
|
return None
|
|
|
|
elif plant.stage > 1:
|
|
|
|
return None
|
|
|
|
return plant
|
|
|
|
|
2017-03-13 05:21:17 +00:00
|
|
|
def handle_request(self, request):
|
2017-03-24 00:08:27 +00:00
|
|
|
# Menu options call functions here
|
2017-03-13 05:21:17 +00:00
|
|
|
if request == None: return
|
2017-03-21 19:55:11 +00:00
|
|
|
if request == "harvest":
|
2017-03-24 00:08:27 +00:00
|
|
|
self.harvest_confirmation()
|
2017-03-13 05:21:17 +00:00
|
|
|
if request == "water":
|
|
|
|
self.plant.water()
|
2017-03-14 22:23:28 +00:00
|
|
|
if request == "look":
|
2017-03-15 01:31:18 +00:00
|
|
|
try:
|
|
|
|
self.draw_plant_description(self.plant)
|
|
|
|
except Exception as exception:
|
|
|
|
self.screen.refresh()
|
|
|
|
# traceback.print_exc()
|
2017-03-13 05:21:17 +00:00
|
|
|
if request == "instructions":
|
2017-03-13 17:42:13 +00:00
|
|
|
try:
|
|
|
|
self.draw_instructions()
|
|
|
|
except Exception as exception:
|
2017-03-15 01:31:18 +00:00
|
|
|
self.screen.refresh()
|
|
|
|
# traceback.print_exc()
|
2018-03-03 05:20:29 +00:00
|
|
|
if request == "visit":
|
|
|
|
try:
|
|
|
|
self.visit_handler()
|
|
|
|
except Exception as exception:
|
2018-03-11 21:30:10 +00:00
|
|
|
self.screen.refresh()
|
|
|
|
# traceback.print_exc()
|
2017-03-13 05:21:17 +00:00
|
|
|
if request == "garden":
|
2017-03-13 17:42:13 +00:00
|
|
|
try:
|
|
|
|
self.draw_garden()
|
|
|
|
except Exception as exception:
|
2017-03-15 01:31:18 +00:00
|
|
|
self.screen.refresh()
|
|
|
|
# traceback.print_exc()
|
2017-03-13 17:42:13 +00:00
|
|
|
|
2017-03-08 02:35:04 +00:00
|
|
|
def __exit__(self):
|
|
|
|
self.exit = True
|
2018-05-23 19:45:51 +00:00
|
|
|
cleanup()
|
|
|
|
|
|
|
|
def cleanup():
|
|
|
|
try:
|
2017-03-08 02:35:04 +00:00
|
|
|
curses.curs_set(2)
|
2018-05-23 19:45:51 +00:00
|
|
|
except curses.error:
|
|
|
|
# cursor not supported; just ignore
|
|
|
|
pass
|
|
|
|
curses.endwin()
|
|
|
|
os.system('clear')
|
2017-03-08 02:35:04 +00:00
|
|
|
|