online_ide: update to the new API of the loader
[nit.git] / contrib / nitrpg / src / game.nit
index 9c71566..a109949 100644 (file)
@@ -43,11 +43,11 @@ interface GameEntity
        # Date are stored under `self.key`.
        fun save do game.store.store_object(key, to_json)
 
-       # Saves `self` state into `target` key data.
+       # Saves `self` state under `key` data.
        #
-       # Data are stored under `target.key / self.key`.
-       fun save_in(target: GameEntity) do
-               game.store.store_object(target.key / key, to_json)
+       # Data are stored under `key / self.key`.
+       fun save_in(key: String) do
+               game.store.store_object(key / self.key, to_json)
        end
 
        # Json representation of `self`.
@@ -67,7 +67,7 @@ class Game
 
        # Returns the repo `full_name`.
        #
-       # Example: `"privat/nit"`
+       # Example: `"nitlang/nit"`
        redef fun key do return repo.full_name
 
        # We need a `GithubAPI` client to load Github data.
@@ -212,8 +212,7 @@ class Player
        #
        # Used to load players from saved data.
        init from_json(game: Game, json: JsonObject) do
-               self.game = game
-               name = json["name"].to_s
+               init(game, json["name"].to_s)
                nitcoins = json["nitcoins"].as(Int)
        end
 
@@ -235,11 +234,16 @@ end
 
 redef class User
        # The player linked to `self`.
-       fun player(game: Game): Player is lazy do
-               var player = game.load_player(login)
+       fun player(game: Game): Player do
+               var player = player_cache.get_or_null(game)
+               if player != null then return player
+               player = game.load_player(login)
                if player == null then player = game.add_player(self)
+               player_cache[game] = player
                return player
        end
+
+       private var player_cache = new HashMap[Game, Player]
 end
 
 # A GameReactor reacts to event sent by a `Github::HookListener`.
@@ -287,6 +291,19 @@ end
 
 # utils
 
+# Sort games by descending number of players.
+#
+# The first in the list is the game with the more players.
+class GamePlayersComparator
+       super Comparator
+
+       redef type COMPARED: Game
+
+       redef fun compare(a, b) do
+               return b.load_players.length <=> a.load_players.length
+       end
+end
+
 # Sort players by descending number of nitcoins.
 #
 # The first in the list is the player with the more of nitcoins.