contrib/nitrpg: move statistics to GameEntity
authorAlexandre Terrasa <alexandre@moz-code.org>
Thu, 5 Feb 2015 00:46:59 +0000 (01:46 +0100)
committerAlexandre Terrasa <alexandre@moz-code.org>
Fri, 6 Feb 2015 22:24:04 +0000 (23:24 +0100)
Signed-off-by: Alexandre Terrasa <alexandre@moz-code.org>

contrib/nitrpg/src/statistics.nit

index 1f55592..cf48978 100644 (file)
@@ -24,22 +24,32 @@ import game
 import github::hooks
 import counter
 
+redef class GameEntity
+
+       # Load statistics for this `MEntity` if any.
+       fun load_statistics: nullable GameStats do
+               var key = self.key / "statistics"
+               if not game.store.has_key(key) then return null
+               var json = game.store.load_object(key)
+               return new GameStats.from_json(game, json)
+       end
+
+       # Save statistics under this `MEntity`.
+       fun save_statistics(stats: GameStats) do
+               game.store.store_object(key / stats.key, stats.to_json)
+       end
+end
+
 redef class Game
 
        # Statistics for this game instance.
-       var stats = new GameStats
-
-       redef fun from_json(json) do
-               super
-               if json.has_key("statistics") then
-                       stats.from_json(json["statistics"].as(JsonObject))
-               end
+       var stats: GameStats is lazy do
+               return load_statistics or else new GameStats(game)
        end
 
-       redef fun to_json do
-               var obj = super
-               obj["statistics"] = stats.to_json
-               return obj
+       redef fun save do
+               super
+               save_statistics(stats)
        end
 
        redef fun pretty do
@@ -49,11 +59,6 @@ redef class Game
                res.append stats.pretty
                return res.write_to_string
        end
-
-       redef fun clear do
-               super
-               stats.clear
-       end
 end
 
 # Game statistics structure that can be saved as a `GameEntity`.