Add random watering, assign commands

trunk
mio 2019-01-07 05:37:32 +00:00
parent 04193f5a72
commit 67c73305d3
2 changed files with 86 additions and 9 deletions

11
wilty 100755
View File

@ -0,0 +1,11 @@
#!/usr/bin/env python3
import sys
from wilty import Wilty
def main(args):
app = Wilty()
app.mapCommands(args)
if __name__ == "__main__":
main(sys.argv[1:])

View File

@ -1,18 +1,20 @@
#!/usr/bin/env python3
import json
import os
import pwd
import random
import time
class Wilty():
def __init__(self):
self.name = "wilty"
self.version = self.name + " 0.1"
self.save_dir = ".botany"
self.plant_file = "_plant_data.json"
self.visit_file = "visitors.json"
self.harvest_file = "harvest_file.json"
self.water_user = "wiltychan"
self.water_user = self.name
self.plants = {}
self.live_plants = {}
self.stats_model = {
@ -34,6 +36,46 @@ class Wilty():
}
self.border = "-" * 47
def showVersion(self):
"""Show version info."""
print(self.version)
def showHelp(self):
"""Show usage info."""
print("Usage: " + self.name + \
" list [user|plant], stats [live], water")
def mapCommands(self, args):
"""Map a list of command keywords to functions."""
if len(args) == 1:
if args[0] == "list":
self.getRawData()
self.listLivePlants()
elif args[0] == "stats":
self.getRawData()
self.listStats()
elif args[0] == "water":
self.getRawData()
self.waterRandomPlant()
elif args[0] == "version":
self.showVersion()
else:
self.showHelp()
elif len(args) == 2:
if args[0] == "list" and args[1] == "user":
self.getRawData()
self.listLivePlants(sort=user)
elif args[0] == "list" and args[1] == "plant":
self.getRawData()
self.listLivePlants(sort=user)
elif args[0] == "stats" and args[1] == "live":
self.getRawData()
self.listStats(subset="live")
else:
self.showHelp()
else:
self.showHelp()
def formatTime(self, ts):
"""Format an integer timestamp (seconds) to a timestamp string (days,
hours and minutes), e.g. 12d 12h 12m.
@ -104,6 +146,35 @@ class Wilty():
if not self.plants[u]["is_dead"]:
self.live_plants[u] = self.plants[u]
def waterRandomPlant(self):
"""Randomly select a live plant to water."""
# Select a user's plant that is open to visitors
user = list(self.live_plants)[random.randint(0, \
len(self.live_plants) - 1)]
while not self.live_plants[user]["allow_visit"]:
user = list(self.live_plants)[random.randint(0, \
len(self.live_plants) - 1)]
# Contenate string to ensure fixed order of keys in the json,
# to be visually consistent with the in-game visitor watering quirk.
# (Notably, there is an extra space after the comma of the timestamp
# value, and after the comma of the preceding item's closing brace.)
visit_entry = " {\n \"timestamp\": " + str(int(time.time())) + \
", " + "\n \"user\": " + "\"" + self.water_user + "\"" + \
"\n }"
sv_dir = "/home/" + user + "/" + self.save_dir + "/"
with open(sv_dir + self.visit_file, "r") as visit_rfh:
visit_json = visit_rfh.read()
# 1st visitor
if "[]" in visit_json:
visit_data = visit_json.replace("[]", "[\n" + visit_entry + "\n]")
# nth visitor
else:
visit_data = visit_json.replace("\n]", \
", \n" + visit_entry + "\n]")
with open(sv_dir + self.visit_file, "w") as visit_wfh:
visit_wfh.write(visit_data)
print("You watered " + user + "\'s plant. Thanks!")
def listLivePlants(self, *args, **kwargs):
"""Prints a list of living plants open to visitors. Optionally specify
a sort flag to sort the list alphabetically by username or plant type,
@ -194,7 +265,7 @@ class Wilty():
count["avg_score"] = int(count["score"] / count["total"])
def genStats(self, subset):
"""Generate garden stats of all plants or only live plants."""
"""Generate garden stats of all or only live plants."""
stats = dict(self.stats_model)
if subset == "live":
plants_set = self.live_plants
@ -222,6 +293,7 @@ class Wilty():
stats = self.genStats("live")
else:
stats = self.genStats("all")
print(self.border)
print("Garden Stats (" + kwargs.get("subset", "all") + " plants)")
print(self.border)
print(
@ -238,9 +310,3 @@ class Wilty():
"\nTotal plants: " + str(stats["total"])
)
print(self.border)
instance = Wilty()
instance.getRawData()
instance.listLivePlants()
instance.listStats(subset="live")