comparison src/plugins/plugin_misc_tarot.py @ 563:da6eaa2af092

plugin Tarot: fixed plugin
author Goffi <goffi@goffi.org>
date Fri, 28 Dec 2012 02:27:34 +0100
parents a31abb97310d
children ca13633d3b6b
comparison
equal deleted inserted replaced
562:0bb2e0d1c878 563:da6eaa2af092
19 along with this program. If not, see <http://www.gnu.org/licenses/>. 19 along with this program. If not, see <http://www.gnu.org/licenses/>.
20 """ 20 """
21 21
22 from logging import debug, info, warning, error 22 from logging import debug, info, warning, error
23 from twisted.words.xish import domish 23 from twisted.words.xish import domish
24 from twisted.internet import protocol, defer, threads, reactor 24 from twisted.words.protocols.jabber import jid
25 from twisted.words.protocols.jabber import client, jid, xmlstream
26 from twisted.words.protocols.jabber import error as jab_error
27 from twisted.words.protocols.jabber.xmlstream import IQ
28 import random 25 import random
29 26
30 from zope.interface import implements 27 from zope.interface import implements
31 28
32 from wokkel import disco, iwokkel, data_form 29 from wokkel import disco, iwokkel, data_form
108 """Convert a domish element with cards to a list of tuples""" 105 """Convert a domish element with cards to a list of tuples"""
109 cards_list = [] 106 cards_list = []
110 for card in cards_list_elt.elements(): 107 for card in cards_list_elt.elements():
111 cards_list.append((card['suit'], card['value'])) 108 cards_list.append((card['suit'], card['value']))
112 return cards_list 109 return cards_list
110
113 111
114 def __create_started_elt(self, players): 112 def __create_started_elt(self, players):
115 """Create a game_started domish element""" 113 """Create a game_started domish element"""
116 started_elt = domish.Element((None,'started')) 114 started_elt = domish.Element((None,'started'))
117 idx = 0 115 idx = 0
423 players = room.roster.keys() 421 players = room.roster.keys()
424 del self.waiting_inv[_room_jid] 422 del self.waiting_inv[_room_jid]
425 self.createGame(_room_jid.userhost(), players, profile_key=profile) 423 self.createGame(_room_jid.userhost(), players, profile_key=profile)
426 return True 424 return True
427 425
428 def launchGame(self, players, profile_key='@DEFAULT@'): 426 def launchGame(self, players, profile_key='@NONE@'):
429 """Launch a game: helper method to create a room, invite players, and create the tarot game 427 """Launch a game: helper method to create a room, invite players, and create the tarot game
430 @param players: list for players jid""" 428 @param players: list for players jid"""
431 debug(_('Launching tarot game')) 429 debug(_('Launching tarot game'))
432 profile = self.host.memory.getProfileName(profile_key) 430 profile = self.host.memory.getProfileName(profile_key)
433 if not profile: 431 if not profile:
456 if not muc_service: 454 if not muc_service:
457 error(_("Can't find a MUC service")) 455 error(_("Can't find a MUC service"))
458 return 456 return
459 457
460 _jid, xmlstream = self.host.getJidNStream(profile) 458 _jid, xmlstream = self.host.getJidNStream(profile)
461 d = self.host.plugins["XEP-0045"].join(muc_service.userhost(), room_name, _jid.user, profile).addCallback(tarotRoomJoined) 459 d = self.host.plugins["XEP-0045"].join(jid.JID("%s@%s" % (room_name, muc_service.userhost())), _jid.user, {}, profile).addCallback(tarotRoomJoined)
462 460
463 client = self.host.getClient(profile) 461 client = self.host.getClient(profile)
464 if not client: 462 if not client:
465 error(_('No client for this profile key: %s') % profile_key) 463 error(_('No client for this profile key: %s') % profile_key)
466 return 464 return
467 client.client_initialized.addCallback(after_init) 465 client.client_initialized.addCallback(after_init)
468 466
469 467
470 468
471 469
472 def createGame(self, room_jid_param, players, profile_key='@DEFAULT@'): 470 def createGame(self, room_jid_param, players, profile_key='@NONE@'):
473 """Create a new game 471 """Create a new game
474 @param room_jid_param: jid of the room 472 @param room_jid_param: jid of the room
475 @param players: list of players nick (nick must exist in the room) 473 @param players: list of players nick (nick must exist in the room)
476 @param profile_key: %(doc_profile_key)s""" 474 @param profile_key: %(doc_profile_key)s"""
477 debug (_("Creating Tarot game")) 475 debug (_("Creating Tarot game"))
497 for player in players: 495 for player in players:
498 mess = self.createGameElt(jid.JID(room_jid.userhost()+'/'+player)) 496 mess = self.createGameElt(jid.JID(room_jid.userhost()+'/'+player))
499 mess.firstChildElement().addChild(self.__create_started_elt(players)) 497 mess.firstChildElement().addChild(self.__create_started_elt(players))
500 self.host.profiles[profile].xmlstream.send(mess) 498 self.host.profiles[profile].xmlstream.send(mess)
501 499
502 def newPlayerReady(self, player, referee, profile_key='@DEFAULT@'): 500 def newPlayerReady(self, player, referee, profile_key='@NONE@'):
503 """Must be called when player is ready to start a new game""" 501 """Must be called when player is ready to start a new game"""
504 profile = self.host.memory.getProfileName(profile_key) 502 profile = self.host.memory.getProfileName(profile_key)
505 if not profile: 503 if not profile:
506 error (_("profile %s is unknown") % profile_key) 504 error (_("profile %s is unknown") % profile_key)
507 return 505 return
509 mess = self.createGameElt(jid.JID(referee)) 507 mess = self.createGameElt(jid.JID(referee))
510 ready_elt = mess.firstChildElement().addElement('player_ready') 508 ready_elt = mess.firstChildElement().addElement('player_ready')
511 ready_elt['player'] = player 509 ready_elt['player'] = player
512 self.host.profiles[profile].xmlstream.send(mess) 510 self.host.profiles[profile].xmlstream.send(mess)
513 511
514 def contratChoosed(self, player, referee, contrat, profile_key='@DEFAULT@'): 512 def contratChoosed(self, player, referee, contrat, profile_key='@NONE@'):
515 """Must be call by player when the contrat is selected 513 """Must be call by player when the contrat is selected
516 @param player: player's name 514 @param player: player's name
517 @param referee: arbiter jid 515 @param referee: arbiter jid
518 @contrat: contrat choosed (must be the exact same string than in the give list options) 516 @contrat: contrat choosed (must be the exact same string than in the give list options)
519 @profile_key: profile 517 @profile_key: profile
526 mess = self.createGameElt(jid.JID(referee)) 524 mess = self.createGameElt(jid.JID(referee))
527 contrat_elt = mess.firstChildElement().addElement(('','contrat_choosed'), content=contrat) 525 contrat_elt = mess.firstChildElement().addElement(('','contrat_choosed'), content=contrat)
528 contrat_elt['player'] = player 526 contrat_elt['player'] = player
529 self.host.profiles[profile].xmlstream.send(mess) 527 self.host.profiles[profile].xmlstream.send(mess)
530 528
531 def play_cards(self, player, referee, cards, profile_key='@DEFAULT@'): 529 def play_cards(self, player, referee, cards, profile_key='@NONE@'):
532 """Must be call by player when the contrat is selected 530 """Must be call by player when the contrat is selected
533 @param player: player's name 531 @param player: player's name
534 @param referee: arbiter jid 532 @param referee: arbiter jid
535 @cards: cards played (list of tuples) 533 @cards: cards played (list of tuples)
536 @profile_key: profile 534 @profile_key: profile
760 758
761 elif elt.name == 'score': 759 elif elt.name == 'score':
762 form_elt = elt.elements(name='x',uri='jabber:x:data').next() 760 form_elt = elt.elements(name='x',uri='jabber:x:data').next()
763 winners = [] 761 winners = []
764 loosers = [] 762 loosers = []
765 for winner in elt.elements(name='winner', uri=''): 763 for winner in elt.elements(name='winner', uri=NS_CG):
766 winners.append(unicode(winner)) 764 winners.append(unicode(winner))
767 for looser in elt.elements(name='looser', uri=''): 765 for looser in elt.elements(name='looser', uri=NS_CG):
768 loosers.append(unicode(looser)) 766 loosers.append(unicode(looser))
769 form = data_form.Form.fromElement(form_elt) 767 form = data_form.Form.fromElement(form_elt)
770 xml_data = dataForm2xml(form) 768 xml_data = dataForm2xml(form)
771 self.host.bridge.tarotGameScore(room_jid.userhost(), xml_data, winners, loosers, profile) 769 self.host.bridge.tarotGameScore(room_jid.userhost(), xml_data, winners, loosers, profile)
772 elif elt.name == 'error': 770 elif elt.name == 'error':
773 if elt['type'] == 'invalid_cards': 771 if elt['type'] == 'invalid_cards':
774 played_cards = self.__xml_to_list(elt.elements(name='played',uri='').next()) 772 played_cards = self.__xml_to_list(elt.elements(name='played',uri=NS_CG).next())
775 invalid_cards = self.__xml_to_list(elt.elements(name='invalid',uri='').next()) 773 invalid_cards = self.__xml_to_list(elt.elements(name='invalid',uri=NS_CG).next())
776 self.host.bridge.tarotGameInvalidCards(room_jid.userhost(), elt['phase'], played_cards, invalid_cards, profile) 774 self.host.bridge.tarotGameInvalidCards(room_jid.userhost(), elt['phase'], played_cards, invalid_cards, profile)
777 else: 775 else:
778 error (_('Unmanaged error type: %s') % elt['type']) 776 error (_('Unmanaged error type: %s') % elt['type'])
779 else: 777 else:
780 error (_('Unmanaged card game element: %s') % elt.name) 778 error (_('Unmanaged card game element: %s') % elt.name)