+
+ redef fun save do
+ super
+ stats.save
+ end
+end
+
+# Store game stats for defined period.
+class GameStatsManager
+ super GameEntity
+ super Counter[String]
+
+ redef var game
+
+ # The GameEntity monitored by these statistics.
+ var owner: GameEntity
+
+ # Current date to extract stats
+ private var date = new Tm.gmtime
+
+ # Returns the `GameStats` instance for the overall statistics.
+ var overall: GameStats = load_stats_for("all") is lazy
+
+ # 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
+ end
+
+ redef fun [](key) do return overall[key]
+
+ redef fun []=(key, value) do
+ overall[key] = value
+ yearly[key] = value
+ monthly[key] = value
+ daily[key] = value
+ weekly[key] = value
+ end
+
+ redef fun inc(e) do
+ overall.inc(e)
+ yearly.inc(e)
+ monthly.inc(e)
+ daily.inc(e)
+ weekly.inc(e)
+ end
+
+ redef fun dec(e) do
+ overall.dec(e)
+ yearly.dec(e)
+ monthly.dec(e)
+ daily.dec(e)
+ weekly.dec(e)
+ end
+
+ redef fun save do
+ overall.save
+ yearly.save
+ monthly.save
+ daily.save
+ weekly.save
+ end
+
+ redef fun pretty do return overall.pretty