- # Load `self` from saved data
- private fun from_json(json: JsonObject) do
- for k, v in json do stats[k] = v.as(Int)
+ # Returns the `GameStats` instance for the current year statistics.
+ var yearly: GameStats = load_stats_for(date.strftime("%Y")) is lazy
+
+ # Returns the `GameStats` instance for the current month statistics.
+ var monthly: GameStats = load_stats_for(date.strftime("%Y-%m")) is lazy
+
+ # Returns the `GameStats` instance for the current day statistics.
+ var daily: GameStats = load_stats_for(date.strftime("%Y-%m-%d")) is lazy
+
+ # Returns the `GameStats` instance for the current week statistics.
+ var weekly: GameStats = load_stats_for(date.strftime("%Y-W%U")) is lazy
+
+ # Load statistics for a `period` key.
+ fun load_stats_for(period: String): GameStats do
+ var req = new JsonObject
+ req["period"] = period
+ req["owner"] = owner.key
+ var obj = game.db.collection("statistics").find(req)
+ if obj isa JsonObject then
+ return new GameStats.from_json(game, period, owner, obj)
+ else
+ return new GameStats(game, period, owner)
+ end