2021-06-21 16:04:10 -05:00
|
|
|
import json
|
|
|
|
import time
|
|
|
|
import os
|
|
|
|
|
|
|
|
class Botany:
|
|
|
|
visitors = "/home/{}/.botany/visitors.json"
|
|
|
|
plant = "/home/{}/.botany/{}_plant_data.json"
|
|
|
|
def __init__(self, user):
|
|
|
|
self.user = user
|
2021-06-23 15:53:53 -05:00
|
|
|
self.euser = user.lower()
|
2021-06-21 16:04:10 -05:00
|
|
|
def getVisitors(self):
|
2021-06-23 15:53:53 -05:00
|
|
|
formatted = self.visitors.format(self.euser)
|
2021-06-21 16:04:10 -05:00
|
|
|
try:
|
|
|
|
if os.stat(formatted).st_size > 0:
|
|
|
|
with open(formatted) as fd:
|
|
|
|
return json.load(fd)
|
|
|
|
else:
|
|
|
|
return []
|
|
|
|
except FileNotFoundError:
|
|
|
|
print("FileNotFoundError in getVisitors()")
|
|
|
|
return False
|
|
|
|
def insertVisitor(self, data, waterer):
|
|
|
|
if data == False: return False
|
2021-06-23 15:53:53 -05:00
|
|
|
formatted = self.visitors.format(self.euser)
|
2021-06-21 16:04:10 -05:00
|
|
|
data.append({"timestamp": int(time.time()), "user": waterer})
|
|
|
|
try:
|
|
|
|
with open(formatted, "w") as fd:
|
|
|
|
json.dump(data, fd, indent=4)
|
|
|
|
return True
|
|
|
|
except PermissionError:
|
|
|
|
print("PermissionError in insertVisitor()")
|
|
|
|
return False
|
|
|
|
except FileNotFoundError:
|
|
|
|
print("FileNotFoundError in insertVisitor()")
|
|
|
|
return False
|
|
|
|
def getInfo(self):
|
2021-06-23 15:53:53 -05:00
|
|
|
formatted = self.plant.format(self.euser, self.euser)
|
2021-06-21 16:04:10 -05:00
|
|
|
try:
|
|
|
|
with open(formatted) as fd:
|
|
|
|
return json.load(fd)
|
|
|
|
except:
|
|
|
|
print("error in getInfo()")
|
|
|
|
return []
|
2021-06-22 22:57:08 -05:00
|
|
|
def score(self):
|
|
|
|
i = self.getInfo()
|
2021-06-23 16:03:28 -05:00
|
|
|
if len(i) > 1: return i['score'] + min(i['last_watered'] - int(time.time()), 24 * 3600) * 0.2 * (i['generation'] - 1) + 1
|
|
|
|
return 0
|
2021-06-21 16:04:10 -05:00
|
|
|
|
|
|
|
class IRCBotany(Botany):
|
|
|
|
def plantDescription(self):
|
|
|
|
string = ""
|
|
|
|
data = self.getInfo()
|
2021-06-23 15:53:53 -05:00
|
|
|
if(data == []): return "{}'s invisible plant".format(self.euser)
|
2021-06-21 16:04:10 -05:00
|
|
|
|
2021-06-23 16:05:37 -05:00
|
|
|
string += self.user
|
2021-06-21 16:04:10 -05:00
|
|
|
string += "'s "
|
|
|
|
if(data['is_dead']): string += "dead "
|
|
|
|
string += data['description']
|
|
|
|
string += ' '
|
|
|
|
string += "generation "
|
|
|
|
string += str(data['generation'])
|
|
|
|
string += ' '
|
|
|
|
string += "plant"
|
|
|
|
|
|
|
|
return string
|
|
|
|
|
|
|
|
def cantWater(self):
|
|
|
|
return "I can't water {}!".format(self.plantDescription())
|
|
|
|
|
|
|
|
def watered(self):
|
|
|
|
return "I watered {}!".format(self.plantDescription())
|
|
|
|
|
|
|
|
def dead(self):
|
|
|
|
return "Your {} is dead!".format(self.plantDescription())
|
|
|
|
|
|
|
|
def water(self, waterer):
|
|
|
|
return self.insertVisitor(self.getVisitors(), waterer)
|