diff src/plugins/plugin_misc_tarot.py @ 714:ecc5a5b34ee1

plugins (games): add a method to send messages more easily
author souliane <souliane@mailoo.org>
date Mon, 18 Nov 2013 14:25:40 +0100
parents f610864eb7a5
children 358018c5c398
line wrap: on
line diff
--- a/src/plugins/plugin_misc_tarot.py	Sun Nov 17 17:02:22 2013 +0100
+++ b/src/plugins/plugin_misc_tarot.py	Mon Nov 18 14:25:40 2013 +0100
@@ -380,9 +380,7 @@
         next_player_idx = game_data['current_player'] = (game_data['init_player'] + 1) % len(game_data['players'])  # the player after the dealer start
         game_data['first_player'] = next_player = game_data['players'][next_player_idx]
         to_jid = jid.JID(room_jid.userhost() + "/" + next_player)  # FIXME: gof:
-        mess = self.createGameElt(to_jid)
-        yourturn_elt = mess.firstChildElement().addElement('your_turn')
-        self.host.profiles[profile].xmlstream.send(mess)
+        self.send(to_jid, 'your_turn', profile=profile)
 
     def contratChoosed(self, player, referee, contrat, profile_key='@NONE@'):
         """Must be call by player when the contrat is selected
@@ -396,10 +394,7 @@
             error(_("profile %s is unknown") % profile_key)
             return
         debug(_('contrat [%(contrat)s] choosed by %(profile)s') % {'contrat': contrat, 'profile': profile})
-        mess = self.createGameElt(jid.JID(referee))
-        contrat_elt = mess.firstChildElement().addElement(('', 'contrat_choosed'), content=contrat)
-        contrat_elt['player'] = player
-        self.host.profiles[profile].xmlstream.send(mess)
+        self.send(jid.JID(referee), ('', 'contrat_choosed'), {'player': player}, content=contrat, profile=profile)
 
     def play_cards(self, player, referee, cards, profile_key='@NONE@'):
         """Must be call by player when the contrat is selected
@@ -413,10 +408,8 @@
             error(_("profile %s is unknown") % profile_key)
             return
         debug(_('Cards played by %(profile)s: [%(cards)s]') % {'profile': profile, 'cards': cards})
-        mess = self.createGameElt(jid.JID(referee))
-        playcard_elt = mess.firstChildElement().addChild(self.__card_list_to_xml(TarotCard.from_tuples(cards), 'cards_played'))
-        playcard_elt['player'] = player
-        self.host.profiles[profile].xmlstream.send(mess)
+        elem = self.__card_list_to_xml(TarotCard.from_tuples(cards), 'cards_played')
+        self.send(jid.JID(referee), elem, {'player': player}, profile=profile)
 
     def newRound(self, room_jid, profile):
         game_data = self.games[room_jid.userhost()]
@@ -448,9 +441,7 @@
         pl_idx = game_data['current_player'] = (game_data['init_player'] + 1) % len(players)  # the player after the dealer start
         player = players[pl_idx]
         to_jid = jid.JID(room_jid.userhost() + "/" + player)  # FIXME: gof:
-        mess = self.createGameElt(to_jid)
-        mess.firstChildElement().addChild(self.__ask_contrat())
-        self.host.profiles[profile].xmlstream.send(mess)
+        self.send(to_jid, self.__ask_contrat(), profile=profile)
 
     def card_game_cmd(self, mess_elt, profile):
         """
@@ -497,9 +488,7 @@
                     #not everybody has choosed his contrat, it's next one turn
                     player = self.__next_player(game_data)
                     to_jid = jid.JID(room_jid.userhost() + "/" + player)  # FIXME: gof:
-                    mess = self.createGameElt(to_jid)
-                    mess.firstChildElement().addChild(self.__ask_contrat())
-                    self.host.profiles[profile].xmlstream.send(mess)
+                    self.send(to_jid, self.__ask_contrat(), profile=profile)
                 else:
                     best_contrat = [None, "Passe"]
                     for player in game_data['players']:
@@ -512,9 +501,7 @@
                     if best_contrat[1] == "Passe":
                         debug(_("Everybody is passing, round ended"))
                         to_jid = jid.JID(room_jid.userhost())
-                        mess = self.createGameElt(to_jid)
-                        mess.firstChildElement().addChild(self.__give_scores(*self.__draw_game(game_data)))
-                        self.host.profiles[profile].xmlstream.send(mess)
+                        self.send(to_jid, self.__give_scores(*self.__draw_game(game_data)), profile=profile)
                         game_data['init_player'] = (game_data['init_player'] + 1) % len(game_data['players'])  # we change the dealer
                         for player in game_data['players']:
                             game_data['status'][player] = "init"
@@ -528,10 +515,8 @@
                     else:
                         #Time to show the chien to everybody
                         to_jid = jid.JID(room_jid.userhost())  # FIXME: gof:
-                        mess = self.createGameElt(to_jid)
-                        chien_elt = mess.firstChildElement().addChild(self.__card_list_to_xml(game_data['chien'], 'chien'))
-                        chien_elt['attaquant'] = best_contrat[0]
-                        self.host.profiles[profile].xmlstream.send(mess)
+                        elem = self.__card_list_to_xml(game_data['chien'], 'chien')
+                        self.send(to_jid, elem, {'attaquant': best_contrat[0]}, profile=profile)
                         #the attacker (attaquant) get the chien
                         game_data['hand'][best_contrat[0]].extend(game_data['chien'])
                         del game_data['chien'][:]
@@ -556,9 +541,8 @@
                     #we now check validity of card
                     invalid_cards = self.__invalid_cards(game_data, list_cards)
                     if invalid_cards:
-                        mess = self.createGameElt(jid.JID(room_jid.userhost() + '/' + elt['player']))
-                        mess.firstChildElement().addChild(self.__invalid_cards_elt(list_cards, invalid_cards, game_data['stage']))
-                        self.host.profiles[profile].xmlstream.send(mess)
+                        elem = self.__invalid_cards_elt(list_cards, invalid_cards, game_data['stage'])
+                        self.send(jid.JID(room_jid.userhost() + '/' + elt['player']), elem, profile=profile)
                         return
 
                     #FIXME: gof: manage Garde Sans & Garde Contre cases
@@ -578,9 +562,8 @@
                         #we first check validity of card
                         invalid_cards = self.__invalid_cards(game_data, cards)
                         if invalid_cards:
-                            mess = self.createGameElt(jid.JID(room_jid.userhost() + '/' + current_player))
-                            mess.firstChildElement().addChild(self.__invalid_cards_elt(cards, invalid_cards, game_data['stage']))
-                            self.host.profiles[profile].xmlstream.send(mess)
+                            elem = self.__invalid_cards_elt(cards, invalid_cards, game_data['stage'])
+                            self.send(jid.JID(room_jid.userhost() + '/' + current_player), elem, profile=profile)
                             return
                         #the card played is ok, we forward it to everybody
                         #first we remove it from the hand and put in on the table
@@ -588,9 +571,7 @@
                         players_data[current_player]['played'] = cards[0]
 
                         #then we forward the message
-                        mess = self.createGameElt(room_jid)
-                        playcard_elt = mess.firstChildElement().addChild(elt)
-                        self.host.profiles[profile].xmlstream.send(mess)
+                        self.send(room_jid, elt, profile=profile)
 
                         #Did everybody played ?
                         played = [players_data[player]['played'] for player in game_data['players']]
@@ -606,10 +587,8 @@
                                 players_data[player]['played'] = None
                             if len(game_data['hand'][current_player]) == 0:
                                 #no card lef: the game is finished
-                                to_jid = room_jid
-                                mess = self.createGameElt(to_jid)
-                                chien_elt = mess.firstChildElement().addChild(self.__give_scores(*self.__calculate_scores(game_data)))
-                                self.host.profiles[profile].xmlstream.send(mess)
+                                elem = self.__give_scores(*self.__calculate_scores(game_data))
+                                self.send(room_jid, elem, profile=profile)
                                 game_data['init_player'] = (game_data['init_player'] + 1) % len(game_data['players'])  # we change the dealer
                                 for player in game_data['players']:
                                     game_data['status'][player] = "init"
@@ -621,9 +600,7 @@
 
                         #finally, we tell to the next player to play
                         to_jid = jid.JID(room_jid.userhost() + "/" + next_player)
-                        mess = self.createGameElt(to_jid)
-                        yourturn_elt = mess.firstChildElement().addElement('your_turn')
-                        self.host.profiles[profile].xmlstream.send(mess)
+                        self.send(to_jid, 'your_turn', profile=profile)
 
             elif elt.name == 'your_turn':
                 self.host.bridge.tarotGameYourTurn(room_jid.userhost(), profile)