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