+
+ # 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 repo == null then return null
+ var game = new Game.from_mongo(api, repo)
+ game.root_url = root_url
+ return game
+ end
+
+ # Returns the list of saved games from NitRPG data.
+ fun load_games: Array[Game] do
+ var res = new Array[Game]
+ # TODO should be option
+ var mongo = new MongoClient("mongodb://mongo: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
+end
+
+# Repo overview page.
+class RpgHome
+ super RpgAction
+
+ # Response page stub.
+ var page: NitRpgPage is noinit
+
+ redef fun answer(request, url) do
+ var readme = load_readme
+ var games = load_games
+ var response = new HttpResponse(200)
+ page = new NitRpgPage(root_url)
+ page.side_panels.add new GamesShortListPanel(root_url, games)
+ page.flow_panels.add new MDPanel(readme)
+ response.body = page
+ return response
+ end
+
+ # Load the string content of the nitrpg readme file.
+ private fun load_readme: String do
+ var readme = "README.md"
+ if not readme.file_exists then
+ return "Unable to locate README file."
+ end
+ var file = new FileReader.open(readme)
+ var text = file.read_all
+ file.close
+ return text
+ end
+end
+
+# Display the list of active game.
+class ListGames
+ super RpgAction
+
+ # Response page stub.
+ var page: NitRpgPage is noinit
+
+ redef fun answer(request, url) do
+ var games = load_games
+ var response = new HttpResponse(200)
+ page = new NitRpgPage(root_url)
+ page.breadcrumbs = new Breadcrumbs
+ page.breadcrumbs.add_link(root_url / "games", "games")
+ page.flow_panels.add new GamesListPanel(root_url, games)
+ response.body = page
+ return response
+ end