From 003ef278bdac3c9516ec05421702e6247dcc1a9c Mon Sep 17 00:00:00 2001 From: Alexandre Terrasa Date: Thu, 5 Feb 2015 01:46:59 +0100 Subject: [PATCH] contrib/nitrpg: move statistics to GameEntity Signed-off-by: Alexandre Terrasa --- contrib/nitrpg/src/statistics.nit | 37 +++++++++++++++++++++---------------- 1 file changed, 21 insertions(+), 16 deletions(-) diff --git a/contrib/nitrpg/src/statistics.nit b/contrib/nitrpg/src/statistics.nit index 1f55592..cf48978 100644 --- a/contrib/nitrpg/src/statistics.nit +++ b/contrib/nitrpg/src/statistics.nit @@ -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`. -- 1.7.9.5