Add eventBus, add node display master
authorBen Doumenc <bdoumenc@parkeon.com>
Sun, 9 Aug 2015 18:04:03 +0000 (20:04 +0200)
committerBen Doumenc <bdoumenc@parkeon.com>
Sun, 9 Aug 2015 18:04:03 +0000 (20:04 +0200)
r15k/cli/api.py
r15k/cli/client.py
r15k/cli/eventBus.py [new file with mode: 0644]

index c1da5fe..108a891 100644 (file)
@@ -4,6 +4,7 @@ import logging
 import json
 import requests
 
+
 ROOT = "http://%(host)s:%(port)s/api" % {"host": "localhost", "port": "5000"}
 
 logger = logging.getLogger('r15k.client.api')
@@ -20,9 +21,8 @@ def game_get(game):
     return r.json()
 
 def game_getNodeList(gameData):
-    nodes = []
+    nodes = {}
     for node in gameData["world"]["nodes"]:
-        nodes.append(node["name"])
-    nodes.sort()
+        nodes[node["name"]] = node
     return nodes
 
index 06ec2c6..c5ab46c 100644 (file)
@@ -16,6 +16,9 @@ logger.info("")
 logger.info("Starting with rootPath='%s'" % rootPath)
 
 import r15k.cli.api as api
+from eventBus import bus
+
+eventBus = bus("client.game")
 
 class AppContext:
     def __init__(self):
@@ -69,20 +72,29 @@ class Lobby(npyscreen.FormBaseNewWithMenus):
         self.loadBtn.hidden = True
         self.popup_menu(self.m1)
 
+
+class NodesView(npyscreen.MultiLineAction):
+    def actionHighlighted(self, act_on_this, key_press):
+        logger.info("Node highlighted:" + act_on_this)
+        eventBus.broadcast("NodeSelected", name=act_on_this)
+    
+
 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.nodes = {}
+        self.nodeList = self.add(NodesView, max_height=20, name='Nodes', values = self.nodes.keys())
+        self.nodeInfo = self.add(npyscreen.TitlePager, name="Node info", values=[])
 
         self.m1 = self.add_menu(name="Game Menu", shortcut="^X")
         self.m1.addItem(text='Quit', onSelect=self._onExit)
+        eventBus.on("NodeSelected", self._onNodeSelected)
 
     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
+        self.nodes = api.game_getNodeList(self.game)
+        nodeNames = self.nodes.keys()
+        nodeNames.sort()
+        self.nodeList.values = nodeNames
 
     def beforeEditing(self):
         self._refreshGame()
@@ -92,6 +104,11 @@ class GameView(npyscreen.FormBaseNewWithMenus):
             self.parentApp.setNextForm('MAIN')
             self.parentApp.switchFormNow()
 
+    def _onNodeSelected(self, event, name):
+        node = self.nodes[name]
+        logger.info("Node selected:" + str(node))
+        self.nodeInfo.values = ["Name: ", node["name"], "Corp:", node["corp"], "CPU:", node["cpu"]]
+        self.nodeInfo.display()
 
 if __name__ == '__main__':
     TA = R15KClient()
diff --git a/r15k/cli/eventBus.py b/r15k/cli/eventBus.py
new file mode 100644 (file)
index 0000000..96df954
--- /dev/null
@@ -0,0 +1,38 @@
+# -*- coding: utf-8 -*-
+
+import logging
+logger = logging.getLogger('r15k.eventBus')
+
+class EventBus:
+    def __init__(self, name):
+        self.name = name
+        self._listeners = {}
+
+    def on(self, event, listener):
+        logger.debug("Register %s for %s", listener, event)
+        self._listeners.setdefault(event, []).append(listener)
+
+    def remove(self, event, listener=None):
+        if listener is None:
+            self._listeners[event] = []
+        else:
+            listeners = self._listeners.get(event, [])
+            if listener in listeners:
+                listeners.remove(listener)
+
+    def broadcast(self, event, **kwargs):
+        logger.debug("Broadcast %s", event)
+        listeners = self._listeners.get(event, [])[:]
+        for l in listeners:
+            logger.debug("Call %s", l)
+            l(event, **kwargs)
+
+    
+
+__BUSES = {}
+
+def bus(name):
+    if not __BUSES.has_key(name):
+        __BUSES[name] = EventBus(name)
+    return __BUSES[name]
+