nitrpg: use names as keys
authorAlexandre Terrasa <alexandre@moz-code.org>
Thu, 25 Jun 2015 02:37:22 +0000 (22:37 -0400)
committerAlexandre Terrasa <alexandre@moz-code.org>
Sat, 28 Nov 2015 17:59:21 +0000 (12:59 -0500)
Signed-off-by: Alexandre Terrasa <alexandre@moz-code.org>

contrib/nitrpg/src/achievements.nit
contrib/nitrpg/src/events.nit
contrib/nitrpg/src/game.nit
contrib/nitrpg/src/statistics.nit
contrib/nitrpg/src/templates/templates_base.nit

index fba7b81..6bb26d8 100644 (file)
@@ -74,7 +74,6 @@ end
 class Achievement
        super GameEntity
 
-       redef var key is lazy do return "achievements" / id
 
        redef var game
 
index d1ba25a..a233573 100644 (file)
@@ -61,7 +61,6 @@ end
 class GameEvent
        super GameEntity
 
-       redef var key is lazy do return "events" / internal_id
 
        redef var game
 
@@ -76,6 +75,8 @@ class GameEvent
        # GameEvent uniq id used for storage.
        var internal_id: String is noinit
 
+       redef var key = internal_id is lazy
+
        # Date and time of the event.
        var time: ISODate is noinit, writable
 
index a109949..2924e46 100644 (file)
@@ -65,20 +65,20 @@ class Game
 
        redef fun game do return self
 
-       # Returns the repo `full_name`.
-       #
-       # Example: `"nitlang/nit"`
-       redef fun key do return repo.full_name
-
        # We need a `GithubAPI` client to load Github data.
        var api: GithubAPI
 
        # A game takes place in a `github::Repo`.
        var repo: Repo
 
+       # Game name
+       var name: String = repo.full_name is lazy
+
        # Directory where game data are stored.
        var game_dir: String is lazy do return "nitrpg_data" / repo.full_name
 
+       redef var key = name is lazy
+
        # Used for data storage.
        #
        # File are stored in `game_dir`.
@@ -92,6 +92,12 @@ class Game
        # Used to load entities from saved data.
        fun from_json(json: JsonObject) do end
 
+       redef fun to_json do
+               var json = super
+               json["name"] = name
+               return json
+       end
+
        # Create a player from a Github `User`.
        #
        # Or return the existing one from game data.
@@ -180,9 +186,6 @@ end
 class Player
        super GameEntity
 
-       # Key is based on player `name`.
-       redef var key is lazy do return "players" / name
-
        redef var game
 
        # FIXME contructor should be private
@@ -195,6 +198,8 @@ class Player
        # The name is also used to load the user data lazilly from Github API.
        var name: String
 
+       redef var key = name is lazy
+
        # Player amount of nitcoins.
        #
        # Nitcoins is the currency used in nitrpg.
@@ -218,6 +223,7 @@ class Player
 
        redef fun to_json do
                var json = super
+               json["game"] = game.key
                json["name"] = name
                json["nitcoins"] = nitcoins
                return json
index b7d3530..97102d2 100644 (file)
@@ -79,7 +79,6 @@ class GameStatsManager
        # The GameEntity monitored by these statistics.
        var owner: GameEntity
 
-       redef var key = "stats"
 
        # Returns the `GameStats` instance for the overall statistics.
        var overall: GameStats is lazy do
@@ -118,10 +117,10 @@ class GameStatsManager
        fun load_stats_for(period: String): GameStats do
                var key = owner.key / self.key / period
                if not game.store.has_key(key) then
-                       return new GameStats(game, period)
+                       return new GameStats(game, period, owner)
                end
                var json = game.store.load_object(key)
-               return new GameStats.from_json(game, period, json)
+               return new GameStats.from_json(game, period, owner, json)
        end
 
        redef fun [](key) do return overall[key]
@@ -171,7 +170,6 @@ class GameStats
        # The pedriod these stats are about.
        var period: String
 
-       redef fun key do return period
 
        # Load `self` from saved data.
        init from_json(game: Game, period: String, json: JsonObject) do
index 8b85a4d..a4f40d9 100644 (file)
@@ -34,9 +34,6 @@ redef class Game
 
        redef fun url do return "{root_url}/games" / key
 
-       # Displayed name.
-       fun name: String do return repo.full_name
-
        # Return a HTML link to this Game.
        fun link: String do return "<a href=\"{url}\">{name}</a>"
 end