nitrpg: init web instance from mongo
authorAlexandre Terrasa <alexandre@moz-code.org>
Mon, 10 Aug 2015 22:23:31 +0000 (18:23 -0400)
committerAlexandre Terrasa <alexandre@moz-code.org>
Thu, 3 Dec 2015 04:09:01 +0000 (23:09 -0500)
Signed-off-by: Alexandre Terrasa <alexandre@moz-code.org>

contrib/nitrpg/src/game.nit
contrib/nitrpg/src/web.nit

index 9b550c3..6e04b18 100644 (file)
@@ -89,7 +89,8 @@ class Game
        redef var collection_name = "games"
 
        # Init the Game and try to load saved data.
-       init do
+       init from_mongo(api: GithubAPI, repo: Repo) do
+               init(api, repo)
                var req = new JsonObject
                req["name"] = repo.full_name
                var res = db.collection("games").find(req)
index 38ed68e..f624f48 100644 (file)
@@ -57,8 +57,8 @@ class RpgAction
        # Returns the game with `name` or null if no game exists with this name.
        fun load_game(name: String): nullable Game do
                var repo = api.load_repo(name)
-               if api.was_error or repo == null then return null
-               var game = new Game(api, repo)
+               if repo == null then return null
+               var game = new Game.from_mongo(api, repo)
                game.root_url = root_url
                return game
        end
@@ -66,13 +66,16 @@ class RpgAction
        # Returns the list of saved games from NitRPG data.
        fun load_games: Array[Game] do
                var res = new Array[Game]
-               var rpgdir = "nitrpg_data"
-               if not rpgdir.file_exists then return res
-               for user in rpgdir.files do
-                       for repo in "{rpgdir}/{user}".files do
-                               var game = load_game("{user}/{repo}")
-                               if game != null then res.add game
-                       end
+               # TODO should be option
+               var mongo = new MongoClient("mongodb://localhost:27017")
+               var db = mongo.database("nitrpg")
+               for obj in db.collection("games").find_all(new JsonObject) do
+                       var repo = api.load_repo(obj["name"].to_s)
+                       assert repo != null
+                       var game = new Game(api, repo)
+                       game.from_json(obj)
+                       game.root_url = root_url
+                       res.add game
                end
                return res
        end