add CLI client
authorBen Doumenc <bdoumenc@parkeon.com>
Sun, 9 Aug 2015 15:14:50 +0000 (17:14 +0200)
committerBen Doumenc <bdoumenc@parkeon.com>
Sun, 9 Aug 2015 15:14:50 +0000 (17:14 +0200)
r15k/cli/__init__.py [new file with mode: 0644]
r15k/cli/api.py [new file with mode: 0644]
r15k/cli/client.py [new file with mode: 0644]

diff --git a/r15k/cli/__init__.py b/r15k/cli/__init__.py
new file mode 100644 (file)
index 0000000..e69de29
diff --git a/r15k/cli/api.py b/r15k/cli/api.py
new file mode 100644 (file)
index 0000000..c1da5fe
--- /dev/null
@@ -0,0 +1,28 @@
+# -*- coding: utf-8 -*-
+
+import logging
+import json
+import requests
+
+ROOT = "http://%(host)s:%(port)s/api" % {"host": "localhost", "port": "5000"}
+
+logger = logging.getLogger('r15k.client.api')
+
+def games_get():
+    logger.info("GET game list")
+    r = requests.get(ROOT + "/games")
+    return r.json()["games"]
+
+def game_get(game):
+    logger.info("GET game " + game)
+    r = requests.get(ROOT + "/games/" + game)
+    logger.debug("### " + str(r.json()))
+    return r.json()
+
+def game_getNodeList(gameData):
+    nodes = []
+    for node in gameData["world"]["nodes"]:
+        nodes.append(node["name"])
+    nodes.sort()
+    return nodes
+
diff --git a/r15k/cli/client.py b/r15k/cli/client.py
new file mode 100644 (file)
index 0000000..06ec2c6
--- /dev/null
@@ -0,0 +1,100 @@
+# -*- coding: utf-8 -*-
+
+import os
+import sys
+import logging
+import npyscreen
+
+rootPath = os.path.abspath(os.path.join(os.path.dirname(os.path.realpath(__file__)), ".."))
+sys.path.append(os.path.join(rootPath, ".."))
+
+# configuration
+logging.basicConfig(filename="/tmp/r15k.log", level=logging.DEBUG, format='%(asctime)s %(levelname)-8s %(name)-25s %(message)s')
+logger = logging.getLogger('r15k.client')
+logger.info("")
+logger.info("")
+logger.info("Starting with rootPath='%s'" % rootPath)
+
+import r15k.cli.api as api
+
+class AppContext:
+    def __init__(self):
+        self.currentGame = None
+
+class R15KClient(npyscreen.NPSAppManaged):
+    def onStart(self):
+        self.context = AppContext()
+        self.registerForm("MAIN", Lobby())
+        self.registerForm("GAME", GameView())
+
+class Lobby(npyscreen.FormBaseNewWithMenus):
+    def create(self):
+        self.games = {}
+        self.gameKeys = []
+
+        self.m1 = self.add_menu(name="Main Menu", shortcut="^M")
+        self.m1.addItem(text='Lobby', onSelect=self._onLobby)
+        self.m1.addItem(text='Exit', onSelect=self._onExit)
+
+        self.gameList = self.add(npyscreen.TitleSelectOne, max_height=3, name='Games', values = self.gameKeys, hidden=True)
+        self.refreshBtn = self.add(npyscreen.ButtonPress, name="Refresh", when_pressed_function=self._onRefreshList, hidden=True)
+        self.loadBtn = self.add(npyscreen.ButtonPress, name="Load", when_pressed_function=self._onLoad, rely=self.refreshBtn.rely, relx=self.refreshBtn.relx+10, hidden=True)
+
+    def _onLobby(self):
+        self._onRefreshList()
+        self.gameList.hidden = False
+        self.refreshBtn.hidden = False
+        self.loadBtn.hidden = False
+
+    def _onExit(self):
+        self.parentApp.setNextForm(None)
+        self.parentApp.switchFormNow()
+
+    def _onRefreshList(self):
+        self.games = api.games_get();
+        self.gameKeys = self.games.keys()
+        self.gameList.values = self.gameKeys
+
+    def _onLoad(self):
+        game = self.gameKeys[self.gameList.value[0]]
+        logger.info("Loading game " + game)
+        self.parentApp.context.currentGame = game
+        self.parentApp.setNextForm("GAME")
+        self.parentApp.switchFormNow()
+
+    def beforeEditing(self):
+        npyscreen.blank_terminal()
+        self.gameList.hidden = True
+        self.refreshBtn.hidden = True
+        self.loadBtn.hidden = True
+        self.popup_menu(self.m1)
+
+class GameView(npyscreen.FormBaseNewWithMenus):
+    def create(self):
+        self.nodeNames = []
+        self.nodeList = self.add(npyscreen.TitleSelectOne, max_height=20, name='Nodes', values = self.nodeNames)
+        self.refreshBtn = self.add(npyscreen.ButtonPress, name="Refresh", when_pressed_function=self._refreshGame)
+
+        self.m1 = self.add_menu(name="Game Menu", shortcut="^X")
+        self.m1.addItem(text='Quit', onSelect=self._onExit)
+
+    def _refreshGame(self):
+        self.game = api.game_get(self.parentApp.context.currentGame)
+        self.nodeNames = api.game_getNodeList(self.game)
+        logger.info("### " + str(self.nodeNames))
+        self.nodeList.values = self.nodeNames
+
+    def beforeEditing(self):
+        self._refreshGame()
+
+    def _onExit(self):
+        if npyscreen.notify_yes_no("Quit game?", title="Quit"):
+            self.parentApp.setNextForm('MAIN')
+            self.parentApp.switchFormNow()
+
+
+if __name__ == '__main__':
+    TA = R15KClient()
+    TA.run()
+
+