Add average score stat
parent
93a0301b6e
commit
04193f5a72
67
wilty.py
67
wilty.py
|
@ -12,7 +12,9 @@ class Wilty():
|
||||||
self.plant_file = "_plant_data.json"
|
self.plant_file = "_plant_data.json"
|
||||||
self.visit_file = "visitors.json"
|
self.visit_file = "visitors.json"
|
||||||
self.harvest_file = "harvest_file.json"
|
self.harvest_file = "harvest_file.json"
|
||||||
|
self.water_user = "wiltychan"
|
||||||
self.plants = {}
|
self.plants = {}
|
||||||
|
self.live_plants = {}
|
||||||
self.stats_model = {
|
self.stats_model = {
|
||||||
"total": 0,
|
"total": 0,
|
||||||
"live": 0,
|
"live": 0,
|
||||||
|
@ -27,6 +29,8 @@ class Wilty():
|
||||||
"avg_gen": 0,
|
"avg_gen": 0,
|
||||||
"harv": 0,
|
"harv": 0,
|
||||||
"avg_harv": 0,
|
"avg_harv": 0,
|
||||||
|
"score": 0,
|
||||||
|
"avg_score": 0
|
||||||
}
|
}
|
||||||
self.border = "-" * 47
|
self.border = "-" * 47
|
||||||
|
|
||||||
|
@ -68,7 +72,9 @@ class Wilty():
|
||||||
self.plants[user]["time_since_fmt"] = self.formatTime(time_since)
|
self.plants[user]["time_since_fmt"] = self.formatTime(time_since)
|
||||||
|
|
||||||
def getRawData(self):
|
def getRawData(self):
|
||||||
"""Load plant data into the plants dictionary."""
|
"""Load save file data into the plants dictionary and update plant
|
||||||
|
states.
|
||||||
|
"""
|
||||||
self.getUsers()
|
self.getUsers()
|
||||||
for u in self.plants:
|
for u in self.plants:
|
||||||
sv_dir = "/home/" + u + "/" + self.save_dir + "/"
|
sv_dir = "/home/" + u + "/" + self.save_dir + "/"
|
||||||
|
@ -93,8 +99,10 @@ class Wilty():
|
||||||
self.plants[u]["harvests"] = json.loads(harvest_json)
|
self.plants[u]["harvests"] = json.loads(harvest_json)
|
||||||
else:
|
else:
|
||||||
self.plants[u]["harvests"] = {}
|
self.plants[u]["harvests"] = {}
|
||||||
# Update plant watered state
|
# Update plant state and live plants dictionary
|
||||||
self.checkPlant(u)
|
self.checkPlant(u)
|
||||||
|
if not self.plants[u]["is_dead"]:
|
||||||
|
self.live_plants[u] = self.plants[u]
|
||||||
|
|
||||||
def listLivePlants(self, *args, **kwargs):
|
def listLivePlants(self, *args, **kwargs):
|
||||||
"""Prints a list of living plants open to visitors. Optionally specify
|
"""Prints a list of living plants open to visitors. Optionally specify
|
||||||
|
@ -103,11 +111,12 @@ class Wilty():
|
||||||
interval since the last watered time descending.
|
interval since the last watered time descending.
|
||||||
"""
|
"""
|
||||||
sort_l = []
|
sort_l = []
|
||||||
for p in self.plants:
|
for p in self.live_plants:
|
||||||
if self.plants[p]["allow_visit"] and not self.plants[p]["is_dead"]:
|
if self.live_plants[p]["allow_visit"]:
|
||||||
sort_l.append((p, self.plants[p]["description"].split()[-1], \
|
sort_l.append((p, \
|
||||||
self.plants[p]["time_since_fmt"], \
|
self.live_plants[p]["description"].split()[-1], \
|
||||||
self.plants[p]["time_since"]))
|
self.live_plants[p]["time_since_fmt"], \
|
||||||
|
self.live_plants[p]["time_since"]))
|
||||||
# Sort by column
|
# Sort by column
|
||||||
if kwargs.get("sort", "") == "user":
|
if kwargs.get("sort", "") == "user":
|
||||||
sort_l.sort(key=lambda c: c[0])
|
sort_l.sort(key=lambda c: c[0])
|
||||||
|
@ -126,7 +135,8 @@ class Wilty():
|
||||||
def countPlantStages(self, key, count):
|
def countPlantStages(self, key, count):
|
||||||
"""Count the plants in different stages (seedling, young, mature). Take
|
"""Count the plants in different stages (seedling, young, mature). Take
|
||||||
as arguments a key for plants dictionary lookup and a dictionary to
|
as arguments a key for plants dictionary lookup and a dictionary to
|
||||||
append the results."""
|
append the results.
|
||||||
|
"""
|
||||||
if "stage" in self.plants[key]:
|
if "stage" in self.plants[key]:
|
||||||
if self.plants[key]["stage"] == ("mature" or "flowering" or \
|
if self.plants[key]["stage"] == ("mature" or "flowering" or \
|
||||||
"seed-bearing"):
|
"seed-bearing"):
|
||||||
|
@ -153,17 +163,16 @@ class Wilty():
|
||||||
elif plant_type != ("seed" or "seedling"):
|
elif plant_type != ("seed" or "seedling"):
|
||||||
count["flower"] += 1
|
count["flower"] += 1
|
||||||
|
|
||||||
def countGenRate(self, key, count):
|
def countGen(self, key, count):
|
||||||
"""Count total generation rates."""
|
"""Count total generations."""
|
||||||
if "generation" in self.plants[key]:
|
if "generation" in self.plants[key]:
|
||||||
count["gen"] += self.plants[key]["generation"]
|
count["gen"] += self.plants[key]["generation"]
|
||||||
else:
|
else:
|
||||||
count["gen"] += 1
|
count["gen"] += 1
|
||||||
|
|
||||||
def avgGenRate(self, count):
|
def avgGen(self, count):
|
||||||
"""Calculate the average plant generation."""
|
"""Calculate the average plant generation."""
|
||||||
count["avg_gen"] = round(count["gen"] / count["total"], \
|
count["avg_gen"] = round(count["gen"] / count["total"], 3)
|
||||||
3)
|
|
||||||
|
|
||||||
def countHarvests(self, key, count):
|
def countHarvests(self, key, count):
|
||||||
"""Count the total number of harvests."""
|
"""Count the total number of harvests."""
|
||||||
|
@ -176,27 +185,32 @@ class Wilty():
|
||||||
"""Calculate the average number of harvests per user."""
|
"""Calculate the average number of harvests per user."""
|
||||||
count["avg_harv"] = round(count["harv"] / count["total"], 3)
|
count["avg_harv"] = round(count["harv"] / count["total"], 3)
|
||||||
|
|
||||||
|
def countScores(self, key, count):
|
||||||
|
"""Count total scores."""
|
||||||
|
count["score"] += self.plants[key]["score"]
|
||||||
|
|
||||||
|
def avgScore(self, count):
|
||||||
|
"""Calculate the average user score."""
|
||||||
|
count["avg_score"] = int(count["score"] / count["total"])
|
||||||
|
|
||||||
def genStats(self, subset):
|
def genStats(self, subset):
|
||||||
"""Generate garden stats of all plants or only live plants."""
|
"""Generate garden stats of all plants or only live plants."""
|
||||||
stats = dict(self.stats_model)
|
stats = dict(self.stats_model)
|
||||||
if subset == "live":
|
if subset == "live":
|
||||||
for p in self.plants:
|
plants_set = self.live_plants
|
||||||
if not self.plants[p]["is_dead"]:
|
stats["total"] = len(self.live_plants)
|
||||||
stats["live"] += 1
|
|
||||||
self.countPlantStages(p, stats)
|
|
||||||
self.countPlantGroups(p, stats)
|
|
||||||
self.countGenRate(p, stats)
|
|
||||||
self.countHarvests(p, stats)
|
|
||||||
stats["total"] = stats["live"]
|
|
||||||
else:
|
else:
|
||||||
for p in self.plants:
|
plants_set = self.plants
|
||||||
|
stats["total"] = len(self.plants)
|
||||||
|
for p in plants_set:
|
||||||
self.countPlantStages(p, stats)
|
self.countPlantStages(p, stats)
|
||||||
self.countPlantGroups(p, stats)
|
self.countPlantGroups(p, stats)
|
||||||
self.countGenRate(p, stats)
|
self.countGen(p, stats)
|
||||||
self.countHarvests(p, stats)
|
self.countHarvests(p, stats)
|
||||||
stats["total"] = len(self.plants)
|
self.countScores(p, stats)
|
||||||
self.avgGenRate(stats)
|
self.avgGen(stats)
|
||||||
self.avgHarvests(stats)
|
self.avgHarvests(stats)
|
||||||
|
self.avgScore(stats)
|
||||||
return stats
|
return stats
|
||||||
|
|
||||||
def listStats(self, *args, **kwargs):
|
def listStats(self, *args, **kwargs):
|
||||||
|
@ -213,6 +227,7 @@ class Wilty():
|
||||||
print(
|
print(
|
||||||
"Avg. generation: " + str(stats["avg_gen"]) +
|
"Avg. generation: " + str(stats["avg_gen"]) +
|
||||||
"\nAvg. harvests per user: " + str(stats["avg_harv"]) +
|
"\nAvg. harvests per user: " + str(stats["avg_harv"]) +
|
||||||
|
"\nAvg. score: " + str(stats["avg_score"]) +
|
||||||
"\nSeedlings: " + str(stats["seed"]) +
|
"\nSeedlings: " + str(stats["seed"]) +
|
||||||
"\nYoung plants: " + str(stats["young"]) +
|
"\nYoung plants: " + str(stats["young"]) +
|
||||||
"\nMature plants: " + str(stats["mature"]) +
|
"\nMature plants: " + str(stats["mature"]) +
|
||||||
|
@ -228,4 +243,4 @@ class Wilty():
|
||||||
instance = Wilty()
|
instance = Wilty()
|
||||||
instance.getRawData()
|
instance.getRawData()
|
||||||
instance.listLivePlants()
|
instance.listLivePlants()
|
||||||
instance.listStats()
|
instance.listStats(subset="live")
|
||||||
|
|
Loading…
Reference in New Issue