changeset 275:a763b2ac5d41

bridge + browser_side: bridge signals for games and their callbacks: - added radiocolPlayers and tarotGamePlayers to get the list of players - added roomLeft to remove the muc from the host's room list - display symbols to identify the players in a muc - factorization of ChatPanel.startGame
author souliane <souliane@mailoo.org>
date Thu, 21 Nov 2013 16:13:14 +0100
parents 886b47896f3c
children aebb96bfa8d1
files browser_side/card_game.py browser_side/panels.py browser_side/radiocol.py libervia.py libervia.tac
diffstat 5 files changed, 44 insertions(+), 27 deletions(-) [+]
line wrap: on
line diff
--- a/browser_side/card_game.py	Thu Nov 21 16:05:14 2013 +0100
+++ b/browser_side/card_game.py	Thu Nov 21 16:13:14 2013 +0100
@@ -121,7 +121,7 @@
 
 class CardPanel(DockPanel, ClickHandler):
 
-    def __init__(self, parent, referee, players, player_nick):
+    def __init__(self, parent, referee, player_nick, players):
         DockPanel.__init__(self)
         ClickHandler.__init__(self)
         self._parent = parent
--- a/browser_side/panels.py	Thu Nov 21 16:05:14 2013 +0100
+++ b/browser_side/panels.py	Thu Nov 21 16:13:14 2013 +0100
@@ -52,6 +52,7 @@
 from plugin_xep_0085 import ChatStateMachine
 from pyjamas import Window
 from __pyjamas__ import doc
+from sat.tools.frontends.games import SYMBOLS
 
 
 class UniBoxPanel(HorizontalPanel):
@@ -837,22 +838,23 @@
         self.content.add(ChatText(timestamp, nick, mymess, msg, extra.get('xhtml')))
         self.content_scroll.scrollToBottom()
 
-    def startGame(self, game_type, referee, players):
+    def startGame(self, game_type, referee, players, waiting=False):
         """Configure the chat window to start a game"""
-        if game_type == "Tarot":
-            if hasattr(self, "tarot_panel"):
-                return
-            self.tarot_panel = CardPanel(self, referee, players, self.nick)
-            self.vpanel.insert(self.tarot_panel, 0)
-            self.vpanel.setCellHeight(self.tarot_panel, self.tarot_panel.getHeight())
-        elif game_type == "RadioCol":
-            # XXX: We can have double panel if we join quickly enough to have the group chat start signal
-            #     on invitation + the one triggered on room join
-            if hasattr(self, "radiocol_panel"):
-                return
-            self.radiocol_panel = RadioColPanel(self, referee, self.nick)
-            self.vpanel.insert(self.radiocol_panel, 0)
-            self.vpanel.setCellHeight(self.radiocol_panel, self.radiocol_panel.getHeight())
+        classes = {"Tarot": CardPanel, "RadioCol": RadioColPanel}
+        if game_type not in classes.keys():
+            return  # unknown game
+        attr = game_type.lower()
+        self.occupants_list.addSpecials(players, SYMBOLS[attr])
+        if waiting or not self.nick in players:
+            return  # waiting for player or not playing
+        attr = "%s_panel" % attr
+        if hasattr(self, attr):
+            return
+        print ("%s Game Started \o/" % game_type)
+        panel = classes[game_type](self, referee, self.nick, players)
+        setattr(self, attr, panel)
+        self.vpanel.insert(panel, 0)
+        self.vpanel.setCellHeight(panel, panel.getHeight())
 
     def getGame(self, game_type):
         """Return class managing the game type"""
--- a/browser_side/radiocol.py	Thu Nov 21 16:05:14 2013 +0100
+++ b/browser_side/radiocol.py	Thu Nov 21 16:13:14 2013 +0100
@@ -174,7 +174,7 @@
 
 class RadioColPanel(HorizontalPanel, ClickHandler):
 
-    def __init__(self, parent, referee, player_nick):
+    def __init__(self, parent, referee, player_nick, players=None):
         HorizontalPanel.__init__(self)
         ClickHandler.__init__(self)
         self._parent = parent
--- a/libervia.py	Thu Nov 21 16:05:14 2013 +0100
+++ b/libervia.py	Thu Nov 21 16:13:14 2013 +0100
@@ -167,8 +167,8 @@
         self.discuss_panel = self.panel.discuss_panel
         self.tab_panel = self.panel.tab_panel
         self.tab_panel.addTabListener(self)
-        self.room_list = set() #set of rooms
         self.libervia_widgets = set()  # keep track of all actives LiberviaWidgets
+        self.room_list = []  # list of rooms
         self.mblog_cache = []  # used to keep our own blog entries in memory, to show them in new mblog panel
         self.avatars_cache = {}  # keep track of jid's avatar hash (key=jid, value=file)
         self.current_action_ids = set()
@@ -357,6 +357,8 @@
             self._presenceUpdateCb(*args)
         elif name == 'roomJoined':
             self._roomJoinedCb(*args)
+        elif name == 'roomLeft':
+            self._roomLeftCb(*args)
         elif name == 'roomUserJoined':
             self._roomUserJoinedCb(*args)
         elif name == 'roomUserLeft':
@@ -365,6 +367,8 @@
             self._askConfirmation(*args)
         elif name == 'newAlert':
             self._newAlert(*args)
+        elif name == 'tarotGamePlayers':
+            self._tarotGameStartedCb(*args, waiting=True)
         elif name == 'tarotGameStarted':
             self._tarotGameStartedCb(*args)
         elif name == 'tarotGameNew' or \
@@ -375,6 +379,8 @@
              name == 'tarotGameYourTurn' or \
              name == 'tarotGameScore':
             self._tarotGameGenericCb(name, args[0], args[1:])
+        elif name == 'radiocolPlayers':
+            self._radioColStartedCb(*args, waiting=True)
         elif name == 'radiocolStarted':
             self._radioColStartedCb(*args)
         elif name == 'radiocolPreload':
@@ -577,7 +583,8 @@
 
     def _roomJoinedCb(self, room_jid, room_nicks, user_nick):
         _target = JID(room_jid)
-        self.room_list.add(_target)
+        if _target not in self.room_list:
+            self.room_list.append(_target)
         chat_panel = panels.ChatPanel(self, _target, type_='group')
         chat_panel.setUserNick(user_nick)
         if _target.node.startswith('sat_tarot_'): #XXX: it's not really beautiful, but it works :)
@@ -589,6 +596,15 @@
         chat_panel.setPresents(room_nicks)
         chat_panel.historyPrint()
 
+    def _roomLeftCb(self, room_jid, room_nicks, user_nick):
+        # FIXME: room_list contains JID instances so why MUST we do
+        # 'remove(room_jid)' and not 'remove(JID(room_jid))' ????!!
+        # This looks like a pyjamas bug --> check/report
+        try:
+            self.room_list.remove(room_jid)
+        except KeyError:
+            pass
+
     def _roomUserJoinedCb(self, room_jid_s, user_nick, user_data):
         for lib_wid in self.libervia_widgets:
             if isinstance(lib_wid, panels.ChatPanel) and lib_wid.type == 'group' and lib_wid.target.bare == room_jid_s:
@@ -599,21 +615,20 @@
             if isinstance(lib_wid, panels.ChatPanel) and lib_wid.type == 'group' and lib_wid.target.bare == room_jid_s:
                 lib_wid.userLeft(user_nick, user_data)
 
-    def _tarotGameStartedCb(self, room_jid_s, referee, players):
-        print ("Tarot Game Started \o/")
+    def _tarotGameStartedCb(self, room_jid_s, referee, players, waiting=False):
         for lib_wid in self.libervia_widgets:
             if isinstance(lib_wid, panels.ChatPanel) and lib_wid.type == 'group' and lib_wid.target.bare == room_jid_s:
-                lib_wid.startGame("Tarot", referee, players)
+                lib_wid.startGame("Tarot", referee, players, waiting)
 
     def _tarotGameGenericCb(self, event_name, room_jid_s, args):
         for lib_wid in self.libervia_widgets:
             if isinstance(lib_wid, panels.ChatPanel) and lib_wid.type == 'group' and lib_wid.target.bare == room_jid_s:
                 getattr(lib_wid.getGame("Tarot"), event_name)(*args)
 
-    def _radioColStartedCb(self, room_jid_s, referee):
+    def _radioColStartedCb(self, room_jid_s, referee, players, waiting=False):
         for lib_wid in self.libervia_widgets:
             if isinstance(lib_wid, panels.ChatPanel) and lib_wid.type == 'group' and lib_wid.target.bare == room_jid_s:
-                lib_wid.startGame("RadioCol", referee)
+                lib_wid.startGame("RadioCol", referee, players, waiting)
 
     def _radioColGenericCb(self, event_name, room_jid_s, args):
         for lib_wid in self.libervia_widgets:
--- a/libervia.tac	Thu Nov 21 16:05:14 2013 +0100
+++ b/libervia.tac	Thu Nov 21 16:13:14 2013 +0100
@@ -888,9 +888,9 @@
             self.bridge.register(signal_name, self.signal_handler.getGenericCb(signal_name))
         #plugins
         for signal_name in ['personalEvent', 'roomJoined', 'roomUserJoined', 'roomUserLeft', 'tarotGameStarted', 'tarotGameNew', 'tarotGameChooseContrat',
-                            'tarotGameShowCards', 'tarotGameInvalidCards', 'tarotGameCardsPlayed', 'tarotGameYourTurn', 'tarotGameScore',
-                            'radiocolStarted', 'radiocolPreload', 'radiocolPlay', 'radiocolNoUpload', 'radiocolUploadOk', 'radiocolSongRejected',
-                            'chatStateReceived']:
+                            'tarotGameShowCards', 'tarotGameInvalidCards', 'tarotGameCardsPlayed', 'tarotGameYourTurn', 'tarotGameScore', 'tarotGamePlayers',
+                            'radiocolStarted', 'radiocolPreload', 'radiocolPlay', 'radiocolNoUpload', 'radiocolUploadOk', 'radiocolSongRejected', 'radiocolPlayers',
+                            'roomLeft', 'chatStateReceived']:
             self.bridge.register(signal_name, self.signal_handler.getGenericCb(signal_name), "plugin")
         self.media_dir = self.bridge.getConfig('','media_dir')
         self.local_dir = self.bridge.getConfig('','local_dir')