Mercurial > libervia-backend
comparison src/plugins/plugin_misc_tarot.py @ 717:358018c5c398
plugins (games): more factorization and flexibility for launching and joining games:
- "MUC user joined", "MUC user left" and class XMPPHandler are managed directly in RoomGame
- renamed __init__ parameters 'player_init_data' to 'player_init' and 'options' to 'game_init'
- pass the players list in radiocol method 'createGame' and signal 'radiocolStarted' (needed for invitation system and for UI players identification)
- added some parameters to manage who can invite, who can join, who to wait for... managed with check***Auth methods
- joining a game that is already launched may be possible, regarding these parameters and the invitation list
- leave and join a game again is partly managed: new tarot round is launched, we should keep playing the same round instead
author | souliane <souliane@mailoo.org> |
---|---|
date | Thu, 21 Nov 2013 15:49:53 +0100 |
parents | ecc5a5b34ee1 |
children | 074970227bc0 |
comparison
equal
deleted
inserted
replaced
716:30eb49e4e05d | 717:358018c5c398 |
---|---|
18 # along with this program. If not, see <http://www.gnu.org/licenses/>. | 18 # along with this program. If not, see <http://www.gnu.org/licenses/>. |
19 | 19 |
20 from logging import debug, info, warning, error | 20 from logging import debug, info, warning, error |
21 from twisted.words.xish import domish | 21 from twisted.words.xish import domish |
22 from twisted.words.protocols.jabber import jid | 22 from twisted.words.protocols.jabber import jid |
23 import random | 23 from wokkel import data_form |
24 | 24 |
25 from zope.interface import implements | |
26 | |
27 from wokkel import disco, iwokkel, data_form | |
28 from sat.tools.xml_tools import dataForm2XML | 25 from sat.tools.xml_tools import dataForm2XML |
29 | |
30 from sat.tools.frontends.games import TarotCard | 26 from sat.tools.frontends.games import TarotCard |
31 from sat.tools.plugins.games import RoomGame | 27 from sat.tools.plugins.games import RoomGame |
32 from time import time | 28 from time import time |
33 | 29 import random |
34 try: | 30 |
35 from twisted.words.protocols.xmlstream import XMPPHandler | 31 |
36 except ImportError: | |
37 from wokkel.subprotocols import XMPPHandler | |
38 | |
39 MESSAGE = '/message' | |
40 NS_CG = 'http://www.goffi.org/protocol/card_game' | 32 NS_CG = 'http://www.goffi.org/protocol/card_game' |
41 CG_TAG = 'card_game' | 33 CG_TAG = 'card_game' |
42 CG_REQUEST = MESSAGE + '/' + CG_TAG + '[@xmlns="' + NS_CG + '"]' | |
43 | 34 |
44 PLUGIN_INFO = { | 35 PLUGIN_INFO = { |
45 "name": "Tarot cards plugin", | 36 "name": "Tarot cards plugin", |
46 "import_name": "Tarot", | 37 "import_name": "Tarot", |
47 "type": "Misc", | 38 "type": "Misc", |
55 | 46 |
56 class Tarot(RoomGame): | 47 class Tarot(RoomGame): |
57 | 48 |
58 def __init__(self, host): | 49 def __init__(self, host): |
59 info(_("Plugin Tarot initialization")) | 50 info(_("Plugin Tarot initialization")) |
60 RoomGame.__init__(self, host, PLUGIN_INFO, (NS_CG, CG_TAG), player_init_data={'score': 0}, | 51 RoomGame.__init__(self, host, PLUGIN_INFO, (NS_CG, CG_TAG), |
61 options={'hand_size': 18, 'init_player': 0, 'current_player': None, 'contrat': None, 'stage': None}) | 52 game_init={'hand_size': 18, 'init_player': 0, 'current_player': None, 'contrat': None, 'stage': None}, |
53 player_init={'score': 0}) | |
62 self.contrats = [_('Passe'), _('Petite'), _('Garde'), _('Garde Sans'), _('Garde Contre')] | 54 self.contrats = [_('Passe'), _('Petite'), _('Garde'), _('Garde Sans'), _('Garde Contre')] |
63 host.bridge.addMethod("tarotGameLaunch", ".plugin", in_sign='asss', out_sign='', method=self.prepareRoom) # args: players, room_jid, profile | 55 host.bridge.addMethod("tarotGameLaunch", ".plugin", in_sign='asss', out_sign='', method=self.prepareRoom) # args: players, room_jid, profile |
64 host.bridge.addMethod("tarotGameCreate", ".plugin", in_sign='sass', out_sign='', method=self.createGame) # args: room_jid, players, profile | 56 host.bridge.addMethod("tarotGameCreate", ".plugin", in_sign='sass', out_sign='', method=self.createGame) # args: room_jid, players, profile |
65 host.bridge.addMethod("tarotGameReady", ".plugin", in_sign='sss', out_sign='', method=self.playerReady) # args: player, referee, profile | 57 host.bridge.addMethod("tarotGameReady", ".plugin", in_sign='sss', out_sign='', method=self.playerReady) # args: player, referee, profile |
66 host.bridge.addMethod("tarotGameContratChoosed", ".plugin", in_sign='ssss', out_sign='', method=self.contratChoosed) # args: player, referee, contrat, profile | 58 host.bridge.addMethod("tarotGameContratChoosed", ".plugin", in_sign='ssss', out_sign='', method=self.contratChoosed) # args: player, referee, contrat, profile |
67 host.bridge.addMethod("tarotGamePlayCards", ".plugin", in_sign='ssa(ss)s', out_sign='', method=self.play_cards) # args: player, referee, cards, profile | 59 host.bridge.addMethod("tarotGamePlayCards", ".plugin", in_sign='ssa(ss)s', out_sign='', method=self.play_cards) # args: player, referee, cards, profile |
60 host.bridge.addSignal("tarotGamePlayers", ".plugin", signature='ssass') # args: room_jid, referee, players, profile | |
68 host.bridge.addSignal("tarotGameStarted", ".plugin", signature='ssass') # args: room_jid, referee, players, profile | 61 host.bridge.addSignal("tarotGameStarted", ".plugin", signature='ssass') # args: room_jid, referee, players, profile |
69 host.bridge.addSignal("tarotGameNew", ".plugin", signature='sa(ss)s') # args: room_jid, hand, profile | 62 host.bridge.addSignal("tarotGameNew", ".plugin", signature='sa(ss)s') # args: room_jid, hand, profile |
70 host.bridge.addSignal("tarotGameChooseContrat", ".plugin", signature='sss') # args: room_jid, xml_data, profile | 63 host.bridge.addSignal("tarotGameChooseContrat", ".plugin", signature='sss') # args: room_jid, xml_data, profile |
71 host.bridge.addSignal("tarotGameShowCards", ".plugin", signature='ssa(ss)a{ss}s') # args: room_jid, type ["chien", "poignée",...], cards, data[dict], profile | 64 host.bridge.addSignal("tarotGameShowCards", ".plugin", signature='ssa(ss)a{ss}s') # args: room_jid, type ["chien", "poignée",...], cards, data[dict], profile |
72 host.bridge.addSignal("tarotGameCardsPlayed", ".plugin", signature='ssa(ss)s') # args: room_jid, player, type ["chien", "poignée",...], cards, data[dict], profile | 65 host.bridge.addSignal("tarotGameCardsPlayed", ".plugin", signature='ssa(ss)s') # args: room_jid, player, type ["chien", "poignée",...], cards, data[dict], profile |
73 host.bridge.addSignal("tarotGameYourTurn", ".plugin", signature='ss') # args: room_jid, profile | 66 host.bridge.addSignal("tarotGameYourTurn", ".plugin", signature='ss') # args: room_jid, profile |
74 host.bridge.addSignal("tarotGameScore", ".plugin", signature='ssasass') # args: room_jid, xml_data, winners (list of nicks), loosers (list of nicks), profile | 67 host.bridge.addSignal("tarotGameScore", ".plugin", signature='ssasass') # args: room_jid, xml_data, winners (list of nicks), loosers (list of nicks), profile |
75 host.bridge.addSignal("tarotGameInvalidCards", ".plugin", signature='ssa(ss)a(ss)s') # args: room_jid, game phase, played_cards, invalid_cards, profile | 68 host.bridge.addSignal("tarotGameInvalidCards", ".plugin", signature='ssa(ss)a(ss)s') # args: room_jid, game phase, played_cards, invalid_cards, profile |
76 host.trigger.add("MUC user joined", self.userJoinedTrigger) | |
77 self.deck_ordered = [] | 69 self.deck_ordered = [] |
78 for value in ['excuse'] + map(str, range(1, 22)): | 70 for value in ['excuse'] + map(str, range(1, 22)): |
79 self.deck_ordered.append(TarotCard(("atout", value))) | 71 self.deck_ordered.append(TarotCard(("atout", value))) |
80 for suit in ["pique", "coeur", "carreau", "trefle"]: | 72 for suit in ["pique", "coeur", "carreau", "trefle"]: |
81 for value in map(str, range(1, 11)) + ["valet", "cavalier", "dame", "roi"]: | 73 for value in map(str, range(1, 11)) + ["valet", "cavalier", "dame", "roi"]: |
441 pl_idx = game_data['current_player'] = (game_data['init_player'] + 1) % len(players) # the player after the dealer start | 433 pl_idx = game_data['current_player'] = (game_data['init_player'] + 1) % len(players) # the player after the dealer start |
442 player = players[pl_idx] | 434 player = players[pl_idx] |
443 to_jid = jid.JID(room_jid.userhost() + "/" + player) # FIXME: gof: | 435 to_jid = jid.JID(room_jid.userhost() + "/" + player) # FIXME: gof: |
444 self.send(to_jid, self.__ask_contrat(), profile=profile) | 436 self.send(to_jid, self.__ask_contrat(), profile=profile) |
445 | 437 |
446 def card_game_cmd(self, mess_elt, profile): | 438 def room_game_cmd(self, mess_elt, profile): |
447 """ | 439 """ |
448 @param mess_elt: instance of twisted.words.xish.domish.Element | 440 @param mess_elt: instance of twisted.words.xish.domish.Element |
449 """ | 441 """ |
450 from_jid = jid.JID(mess_elt['from']) | 442 from_jid = jid.JID(mess_elt['from']) |
451 room_jid = jid.JID(from_jid.userhost()) | 443 room_jid = jid.JID(from_jid.userhost()) |
452 game_elt = mess_elt.firstChildElement() | 444 game_elt = mess_elt.firstChildElement() |
453 game_data = self.games[room_jid.userhost()] | 445 game_data = self.games[room_jid.userhost()] |
454 players_data = game_data['players_data'] | 446 if 'players_data' in game_data: |
447 players_data = game_data['players_data'] | |
455 | 448 |
456 for elt in game_elt.elements(): | 449 for elt in game_elt.elements(): |
457 | 450 |
458 if elt.name == 'started': # new game created | 451 if elt.name == 'started' or elt.name == 'players': # new game created |
459 players = [] | 452 players = [] |
460 for player in elt.elements(): | 453 for player in elt.elements(): |
461 players.append(unicode(player)) | 454 players.append(unicode(player)) |
462 self.host.bridge.tarotGameStarted(room_jid.userhost(), from_jid.full(), players, profile) | 455 signal = self.host.bridge.tarotGameStarted if elt.name == 'started' else self.host.bridge.tarotGamePlayers |
456 signal(room_jid.userhost(), from_jid.full(), players, profile) | |
463 | 457 |
464 elif elt.name == 'player_ready': # ready to play | 458 elif elt.name == 'player_ready': # ready to play |
465 player = elt['player'] | 459 player = elt['player'] |
466 status = self.games[room_jid.userhost()]['status'] | 460 status = self.games[room_jid.userhost()]['status'] |
467 nb_players = len(self.games[room_jid.userhost()]['players']) | 461 nb_players = len(self.games[room_jid.userhost()]['players']) |
623 self.host.bridge.tarotGameInvalidCards(room_jid.userhost(), elt['phase'], played_cards, invalid_cards, profile) | 617 self.host.bridge.tarotGameInvalidCards(room_jid.userhost(), elt['phase'], played_cards, invalid_cards, profile) |
624 else: | 618 else: |
625 error(_('Unmanaged error type: %s') % elt['type']) | 619 error(_('Unmanaged error type: %s') % elt['type']) |
626 else: | 620 else: |
627 error(_('Unmanaged card game element: %s') % elt.name) | 621 error(_('Unmanaged card game element: %s') % elt.name) |
628 | |
629 def getHandler(self, profile): | |
630 return CardGameHandler(self) | |
631 | |
632 | |
633 class CardGameHandler (XMPPHandler): | |
634 implements(iwokkel.IDisco) | |
635 | |
636 def __init__(self, plugin_parent): | |
637 self.plugin_parent = plugin_parent | |
638 self.host = plugin_parent.host | |
639 | |
640 def connectionInitialized(self): | |
641 self.xmlstream.addObserver(CG_REQUEST, self.plugin_parent.card_game_cmd, profile=self.parent.profile) | |
642 | |
643 def getDiscoInfo(self, requestor, target, nodeIdentifier=''): | |
644 return [disco.DiscoFeature(NS_CG)] | |
645 | |
646 def getDiscoItems(self, requestor, target, nodeIdentifier=''): | |
647 return [] |