From 5d968de9914d724db70d7f4dc847de0fb26a6547 Mon Sep 17 00:00:00 2001 From: Alexandre Terrasa Date: Wed, 24 Jun 2015 22:37:22 -0400 Subject: [PATCH] nitrpg: use names as keys Signed-off-by: Alexandre Terrasa --- contrib/nitrpg/src/achievements.nit | 1 - contrib/nitrpg/src/events.nit | 3 ++- contrib/nitrpg/src/game.nit | 22 ++++++++++++++-------- contrib/nitrpg/src/statistics.nit | 6 ++---- contrib/nitrpg/src/templates/templates_base.nit | 3 --- 5 files changed, 18 insertions(+), 17 deletions(-) diff --git a/contrib/nitrpg/src/achievements.nit b/contrib/nitrpg/src/achievements.nit index fba7b81..6bb26d8 100644 --- a/contrib/nitrpg/src/achievements.nit +++ b/contrib/nitrpg/src/achievements.nit @@ -74,7 +74,6 @@ end class Achievement super GameEntity - redef var key is lazy do return "achievements" / id redef var game diff --git a/contrib/nitrpg/src/events.nit b/contrib/nitrpg/src/events.nit index d1ba25a..a233573 100644 --- a/contrib/nitrpg/src/events.nit +++ b/contrib/nitrpg/src/events.nit @@ -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 diff --git a/contrib/nitrpg/src/game.nit b/contrib/nitrpg/src/game.nit index a109949..2924e46 100644 --- a/contrib/nitrpg/src/game.nit +++ b/contrib/nitrpg/src/game.nit @@ -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 diff --git a/contrib/nitrpg/src/statistics.nit b/contrib/nitrpg/src/statistics.nit index b7d3530..97102d2 100644 --- a/contrib/nitrpg/src/statistics.nit +++ b/contrib/nitrpg/src/statistics.nit @@ -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 diff --git a/contrib/nitrpg/src/templates/templates_base.nit b/contrib/nitrpg/src/templates/templates_base.nit index 8b85a4d..a4f40d9 100644 --- a/contrib/nitrpg/src/templates/templates_base.nit +++ b/contrib/nitrpg/src/templates/templates_base.nit @@ -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 "{name}" end -- 1.7.9.5