Mercurial > libervia-backend
comparison src/plugins/plugin_misc_tarot.py @ 878:36c6495d86b0
plugin card_game: update to use the new XMLUI mechanism:
TODO: the new round is not starting after the scores have been displayed
author | souliane <souliane@mailoo.org> |
---|---|
date | Wed, 26 Feb 2014 02:17:43 +0100 |
parents | 8f335c03eebb |
children | 1a759096ccbd |
comparison
equal
deleted
inserted
replaced
877:386ab96af575 | 878:36c6495d86b0 |
---|---|
19 | 19 |
20 from sat.core.i18n import _ | 20 from sat.core.i18n import _ |
21 from logging import debug, info, warning, error | 21 from logging import debug, info, warning, error |
22 from twisted.words.xish import domish | 22 from twisted.words.xish import domish |
23 from twisted.words.protocols.jabber import jid | 23 from twisted.words.protocols.jabber import jid |
24 from twisted.internet import defer | |
24 from wokkel import data_form | 25 from wokkel import data_form |
25 | 26 |
26 from sat.tools.xml_tools import dataForm2XMLUI | 27 from sat.memory import memory |
28 from sat.tools import xml_tools | |
27 from sat_frontends.tools.games import TarotCard | 29 from sat_frontends.tools.games import TarotCard |
28 from time import time | 30 from time import time |
29 import random | 31 import random |
30 | 32 |
31 | 33 |
51 RoomGame = host.plugins["ROOM-GAME"].__class__ | 53 RoomGame = host.plugins["ROOM-GAME"].__class__ |
52 self.__class__ = type(self.__class__.__name__, (self.__class__, RoomGame, object), {}) | 54 self.__class__ = type(self.__class__.__name__, (self.__class__, RoomGame, object), {}) |
53 | 55 |
54 def __init__(self, host): | 56 def __init__(self, host): |
55 info(_("Plugin Tarot initialization")) | 57 info(_("Plugin Tarot initialization")) |
58 self._sessions = memory.Sessions() | |
56 self.inheritFromRoomGame(host) | 59 self.inheritFromRoomGame(host) |
57 RoomGame._init_(self, host, PLUGIN_INFO, (NS_CG, CG_TAG), | 60 RoomGame._init_(self, host, PLUGIN_INFO, (NS_CG, CG_TAG), |
58 game_init={'hand_size': 18, 'init_player': 0, 'current_player': None, 'contrat': None, 'stage': None}, | 61 game_init={'hand_size': 18, 'init_player': 0, 'current_player': None, 'contrat': None, 'stage': None}, |
59 player_init={'score': 0}) | 62 player_init={'score': 0}) |
60 self.contrats = [_('Passe'), _('Petite'), _('Garde'), _('Garde Sans'), _('Garde Contre')] | 63 self.contrats = [_('Passe'), _('Petite'), _('Garde'), _('Garde Sans'), _('Garde Contre')] |
61 host.bridge.addMethod("tarotGameLaunch", ".plugin", in_sign='asss', out_sign='', method=self.prepareRoom) # args: players, room_jid, profile | 64 host.bridge.addMethod("tarotGameLaunch", ".plugin", in_sign='asss', out_sign='', method=self.prepareRoom) # args: players, room_jid, profile |
62 host.bridge.addMethod("tarotGameCreate", ".plugin", in_sign='sass', out_sign='', method=self.createGame) # args: room_jid, players, profile | 65 host.bridge.addMethod("tarotGameCreate", ".plugin", in_sign='sass', out_sign='', method=self.createGame) # args: room_jid, players, profile |
63 host.bridge.addMethod("tarotGameReady", ".plugin", in_sign='sss', out_sign='', method=self.playerReady) # args: player, referee, profile | 66 host.bridge.addMethod("tarotGameReady", ".plugin", in_sign='sss', out_sign='', method=self.playerReady) # args: player, referee, profile |
64 host.bridge.addMethod("tarotGameContratChoosed", ".plugin", in_sign='ssss', out_sign='', method=self.contratChoosed) # args: player, referee, contrat, profile | |
65 host.bridge.addMethod("tarotGamePlayCards", ".plugin", in_sign='ssa(ss)s', out_sign='', method=self.play_cards) # args: player, referee, cards, profile | 67 host.bridge.addMethod("tarotGamePlayCards", ".plugin", in_sign='ssa(ss)s', out_sign='', method=self.play_cards) # args: player, referee, cards, profile |
66 host.bridge.addSignal("tarotGamePlayers", ".plugin", signature='ssass') # args: room_jid, referee, players, profile | 68 host.bridge.addSignal("tarotGamePlayers", ".plugin", signature='ssass') # args: room_jid, referee, players, profile |
67 host.bridge.addSignal("tarotGameStarted", ".plugin", signature='ssass') # args: room_jid, referee, players, profile | 69 host.bridge.addSignal("tarotGameStarted", ".plugin", signature='ssass') # args: room_jid, referee, players, profile |
68 host.bridge.addSignal("tarotGameNew", ".plugin", signature='sa(ss)s') # args: room_jid, hand, profile | 70 host.bridge.addSignal("tarotGameNew", ".plugin", signature='sa(ss)s') # args: room_jid, hand, profile |
69 host.bridge.addSignal("tarotGameChooseContrat", ".plugin", signature='sss') # args: room_jid, xml_data, profile | 71 host.bridge.addSignal("tarotGameChooseContrat", ".plugin", signature='sss') # args: room_jid, xml_data, profile |
76 for value in ['excuse'] + map(str, range(1, 22)): | 78 for value in ['excuse'] + map(str, range(1, 22)): |
77 self.deck_ordered.append(TarotCard(("atout", value))) | 79 self.deck_ordered.append(TarotCard(("atout", value))) |
78 for suit in ["pique", "coeur", "carreau", "trefle"]: | 80 for suit in ["pique", "coeur", "carreau", "trefle"]: |
79 for value in map(str, range(1, 11)) + ["valet", "cavalier", "dame", "roi"]: | 81 for value in map(str, range(1, 11)) + ["valet", "cavalier", "dame", "roi"]: |
80 self.deck_ordered.append(TarotCard((suit, value))) | 82 self.deck_ordered.append(TarotCard((suit, value))) |
83 self.__choose_contrat_id = host.registerCallback(self._contratChoosed, with_data=True) | |
84 self.__score_id = host.registerCallback(self._scoreShowed, with_data=True) | |
85 | |
81 | 86 |
82 def __card_list_to_xml(self, cards_list, elt_name): | 87 def __card_list_to_xml(self, cards_list, elt_name): |
83 """Convert a card list to domish element""" | 88 """Convert a card list to domish element""" |
84 cards_list_elt = domish.Element((None, elt_name)) | 89 cards_list_elt = domish.Element((None, elt_name)) |
85 for card in cards_list: | 90 for card in cards_list: |
378 next_player_idx = game_data['current_player'] = (game_data['init_player'] + 1) % len(game_data['players']) # the player after the dealer start | 383 next_player_idx = game_data['current_player'] = (game_data['init_player'] + 1) % len(game_data['players']) # the player after the dealer start |
379 game_data['first_player'] = next_player = game_data['players'][next_player_idx] | 384 game_data['first_player'] = next_player = game_data['players'][next_player_idx] |
380 to_jid = jid.JID(room_jid.userhost() + "/" + next_player) # FIXME: gof: | 385 to_jid = jid.JID(room_jid.userhost() + "/" + next_player) # FIXME: gof: |
381 self.send(to_jid, 'your_turn', profile=profile) | 386 self.send(to_jid, 'your_turn', profile=profile) |
382 | 387 |
383 def contratChoosed(self, player, referee, contrat, profile_key='@NONE@'): | 388 def _contratChoosed(self, raw_data, profile): |
384 """Must be call by player when the contrat is selected | 389 """Will be called when the contrat is selected |
385 @param player: player's name | 390 @param raw_data: contains the choosed session id and the chosen contrat |
386 @param referee: arbiter jid | 391 @param profile_key: profile |
387 @contrat: contrat choosed (must be the exact same string than in the give list options) | |
388 @profile_key: profile | |
389 """ | 392 """ |
390 profile = self.host.memory.getProfileName(profile_key) | 393 try: |
391 if not profile: | 394 session_data = self._sessions.profileGet(raw_data["session_id"], profile) |
392 error(_("profile %s is unknown") % profile_key) | 395 except KeyError: |
393 return | 396 warning(_("session id doesn't exist, session has probably expired")) |
397 # TODO: send error dialog | |
398 return defer.succeed({}) | |
399 | |
400 room_jid_s = session_data['room_jid'].userhost() | |
401 referee = self.games[room_jid_s]['referee'] | |
402 player = self.host.plugins["XEP-0045"].getRoomNick(room_jid_s, profile) | |
403 data = xml_tools.XMLUIResult2DataFormResult(raw_data) | |
404 contrat = data['contrat'] | |
394 debug(_('contrat [%(contrat)s] choosed by %(profile)s') % {'contrat': contrat, 'profile': profile}) | 405 debug(_('contrat [%(contrat)s] choosed by %(profile)s') % {'contrat': contrat, 'profile': profile}) |
395 self.send(jid.JID(referee), ('', 'contrat_choosed'), {'player': player}, content=contrat, profile=profile) | 406 d = self.send(jid.JID(referee), ('', 'contrat_choosed'), {'player': player}, content=contrat, profile=profile) |
407 d.addCallback(lambda ignore: {}) | |
408 del self._sessions[raw_data["session_id"]] | |
409 return d | |
410 | |
411 def _scoreShowed(self, raw_data, profile): | |
412 """Will be called when the player closes the score dialog | |
413 @param raw_data: nothing to retrieve from here but the session id | |
414 @param profile_key: profile | |
415 """ | |
416 try: | |
417 session_data = self._sessions.profileGet(raw_data["session_id"], profile) | |
418 except KeyError: | |
419 warning(_("session id doesn't exist, session has probably expired")) | |
420 # TODO: send error dialog | |
421 return defer.succeed({}) | |
422 | |
423 room_jid_s = session_data['room_jid'].userhost() | |
424 # XXX: empty hand means to the frontend "reset the display"... | |
425 self.host.bridge.tarotGameNew(room_jid_s, [], profile) | |
426 del self._sessions[raw_data["session_id"]] | |
427 return defer.succeed({}) | |
396 | 428 |
397 def play_cards(self, player, referee, cards, profile_key='@NONE@'): | 429 def play_cards(self, player, referee, cards, profile_key='@NONE@'): |
398 """Must be call by player when the contrat is selected | 430 """Must be call by player when the contrat is selected |
399 @param player: player's name | 431 @param player: player's name |
400 @param referee: arbiter jid | 432 @param referee: arbiter jid |
478 elif elt.name == 'hand': # a new hand has been received | 510 elif elt.name == 'hand': # a new hand has been received |
479 self.host.bridge.tarotGameNew(room_jid.userhost(), self.__xml_to_list(elt), profile) | 511 self.host.bridge.tarotGameNew(room_jid.userhost(), self.__xml_to_list(elt), profile) |
480 | 512 |
481 elif elt.name == 'contrat': # it's time to choose contrat | 513 elif elt.name == 'contrat': # it's time to choose contrat |
482 form = data_form.Form.fromElement(elt.firstChildElement()) | 514 form = data_form.Form.fromElement(elt.firstChildElement()) |
483 xml_data = dataForm2XMLUI(form, "").toXml() | 515 session_id, session_data = self._sessions.newSession(profile=profile) |
516 session_data["room_jid"] = room_jid | |
517 xml_data = xml_tools.dataForm2XMLUI(form, self.__choose_contrat_id, session_id).toXml() | |
484 self.host.bridge.tarotGameChooseContrat(room_jid.userhost(), xml_data, profile) | 518 self.host.bridge.tarotGameChooseContrat(room_jid.userhost(), xml_data, profile) |
485 | 519 |
486 elif elt.name == 'contrat_choosed': | 520 elif elt.name == 'contrat_choosed': |
487 #TODO: check we receive the contrat from the right person | 521 #TODO: check we receive the contrat from the right person |
488 #TODO: use proper XEP-0004 way for answering form | 522 #TODO: use proper XEP-0004 way for answering form |
617 for winner in elt.elements(name='winner', uri=NS_CG): | 651 for winner in elt.elements(name='winner', uri=NS_CG): |
618 winners.append(unicode(winner)) | 652 winners.append(unicode(winner)) |
619 for looser in elt.elements(name='looser', uri=NS_CG): | 653 for looser in elt.elements(name='looser', uri=NS_CG): |
620 loosers.append(unicode(looser)) | 654 loosers.append(unicode(looser)) |
621 form = data_form.Form.fromElement(form_elt) | 655 form = data_form.Form.fromElement(form_elt) |
622 xml_data = dataForm2XMLUI(form, "").toXml() | 656 session_id, session_data = self._sessions.newSession(profile=profile) |
657 session_data["room_jid"] = room_jid | |
658 xml_data = xml_tools.dataForm2XMLUI(form, self.__score_id, session_id).toXml() | |
623 self.host.bridge.tarotGameScore(room_jid.userhost(), xml_data, winners, loosers, profile) | 659 self.host.bridge.tarotGameScore(room_jid.userhost(), xml_data, winners, loosers, profile) |
624 elif elt.name == 'error': | 660 elif elt.name == 'error': |
625 if elt['type'] == 'invalid_cards': | 661 if elt['type'] == 'invalid_cards': |
626 played_cards = self.__xml_to_list(elt.elements(name='played', uri=NS_CG).next()) | 662 played_cards = self.__xml_to_list(elt.elements(name='played', uri=NS_CG).next()) |
627 invalid_cards = self.__xml_to_list(elt.elements(name='invalid', uri=NS_CG).next()) | 663 invalid_cards = self.__xml_to_list(elt.elements(name='invalid', uri=NS_CG).next()) |