From: Alexandre Terrasa Date: Mon, 10 Aug 2015 22:23:31 +0000 (-0400) Subject: nitrpg: init web instance from mongo X-Git-Tag: v0.8~53^2~1 X-Git-Url: http://nitlanguage.org nitrpg: init web instance from mongo Signed-off-by: Alexandre Terrasa --- diff --git a/contrib/nitrpg/src/game.nit b/contrib/nitrpg/src/game.nit index 9b550c3..6e04b18 100644 --- a/contrib/nitrpg/src/game.nit +++ b/contrib/nitrpg/src/game.nit @@ -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) diff --git a/contrib/nitrpg/src/web.nit b/contrib/nitrpg/src/web.nit index 38ed68e..f624f48 100644 --- a/contrib/nitrpg/src/web.nit +++ b/contrib/nitrpg/src/web.nit @@ -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