Mercurial > libervia-backend
annotate src/plugins/plugin_misc_tarot.py @ 1502:566908d483f6
core (utils): added a method to generate XEP-0082 style dates
author | Goffi <goffi@goffi.org> |
---|---|
date | Thu, 27 Aug 2015 17:59:11 +0200 |
parents | 3265a2639182 |
children | d17772b0fe22 |
rev | line source |
---|---|
88 | 1 #!/usr/bin/python |
2 # -*- coding: utf-8 -*- | |
3 | |
609
84a6e83157c2
fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents:
594
diff
changeset
|
4 # SAT plugin for managing French Tarot game |
1396 | 5 # Copyright (C) 2009, 2010, 2011, 2012, 2013, 2014, 2015 Jérôme Poisson (goffi@goffi.org) |
88 | 6 |
609
84a6e83157c2
fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents:
594
diff
changeset
|
7 # This program is free software: you can redistribute it and/or modify |
84a6e83157c2
fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents:
594
diff
changeset
|
8 # it under the terms of the GNU Affero General Public License as published by |
84a6e83157c2
fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents:
594
diff
changeset
|
9 # the Free Software Foundation, either version 3 of the License, or |
84a6e83157c2
fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents:
594
diff
changeset
|
10 # (at your option) any later version. |
88 | 11 |
609
84a6e83157c2
fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents:
594
diff
changeset
|
12 # This program is distributed in the hope that it will be useful, |
84a6e83157c2
fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents:
594
diff
changeset
|
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of |
84a6e83157c2
fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents:
594
diff
changeset
|
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
84a6e83157c2
fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents:
594
diff
changeset
|
15 # GNU Affero General Public License for more details. |
88 | 16 |
609
84a6e83157c2
fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents:
594
diff
changeset
|
17 # You should have received a copy of the GNU Affero General Public License |
84a6e83157c2
fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents:
594
diff
changeset
|
18 # along with this program. If not, see <http://www.gnu.org/licenses/>. |
88 | 19 |
771 | 20 from sat.core.i18n import _ |
916
1a759096ccbd
core: use of Const for profile_key + replaced '@DEFAULT@' default profile_key by '@NONE@'
Goffi <goffi@goffi.org>
parents:
878
diff
changeset
|
21 from sat.core.constants import Const as C |
993
301b342c697a
core: use of the new core.log module:
Goffi <goffi@goffi.org>
parents:
916
diff
changeset
|
22 from sat.core.log import getLogger |
301b342c697a
core: use of the new core.log module:
Goffi <goffi@goffi.org>
parents:
916
diff
changeset
|
23 log = getLogger(__name__) |
88 | 24 from twisted.words.xish import domish |
563 | 25 from twisted.words.protocols.jabber import jid |
878
36c6495d86b0
plugin card_game: update to use the new XMLUI mechanism:
souliane <souliane@mailoo.org>
parents:
828
diff
changeset
|
26 from twisted.internet import defer |
717
358018c5c398
plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents:
714
diff
changeset
|
27 from wokkel import data_form |
88 | 28 |
878
36c6495d86b0
plugin card_game: update to use the new XMLUI mechanism:
souliane <souliane@mailoo.org>
parents:
828
diff
changeset
|
29 from sat.memory import memory |
36c6495d86b0
plugin card_game: update to use the new XMLUI mechanism:
souliane <souliane@mailoo.org>
parents:
828
diff
changeset
|
30 from sat.tools import xml_tools |
719
56aa0e98c92e
frontends tools: moved src/tools/frontends to frontends/src/tools
souliane <souliane@mailoo.org>
parents:
718
diff
changeset
|
31 from sat_frontends.tools.games import TarotCard |
717
358018c5c398
plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents:
714
diff
changeset
|
32 import random |
320
5fc5e6a7e5c3
plugin Tarot: added a launch method to automatically create a new room, invite players and create the game
Goffi <goffi@goffi.org>
parents:
291
diff
changeset
|
33 |
88 | 34 |
90 | 35 NS_CG = 'http://www.goffi.org/protocol/card_game' |
36 CG_TAG = 'card_game' | |
88 | 37 |
38 PLUGIN_INFO = { | |
594
e629371a28d3
Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
39 "name": "Tarot cards plugin", |
e629371a28d3
Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
40 "import_name": "Tarot", |
e629371a28d3
Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
41 "type": "Misc", |
e629371a28d3
Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
42 "protocols": [], |
718
074970227bc0
plugin tools: turn src/plugin/games.py into a plugin and move it to src/plugins/plugin_misc_room_game.py
souliane <souliane@mailoo.org>
parents:
717
diff
changeset
|
43 "dependencies": ["XEP-0045", "XEP-0249", "ROOM-GAME"], |
594
e629371a28d3
Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
44 "main": "Tarot", |
e629371a28d3
Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
45 "handler": "yes", |
e629371a28d3
Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
46 "description": _("""Implementation of Tarot card game""") |
88 | 47 } |
48 | |
94 | 49 |
718
074970227bc0
plugin tools: turn src/plugin/games.py into a plugin and move it to src/plugins/plugin_misc_room_game.py
souliane <souliane@mailoo.org>
parents:
717
diff
changeset
|
50 class Tarot(object): |
074970227bc0
plugin tools: turn src/plugin/games.py into a plugin and move it to src/plugins/plugin_misc_room_game.py
souliane <souliane@mailoo.org>
parents:
717
diff
changeset
|
51 |
074970227bc0
plugin tools: turn src/plugin/games.py into a plugin and move it to src/plugins/plugin_misc_room_game.py
souliane <souliane@mailoo.org>
parents:
717
diff
changeset
|
52 def inheritFromRoomGame(self, host): |
074970227bc0
plugin tools: turn src/plugin/games.py into a plugin and move it to src/plugins/plugin_misc_room_game.py
souliane <souliane@mailoo.org>
parents:
717
diff
changeset
|
53 global RoomGame |
074970227bc0
plugin tools: turn src/plugin/games.py into a plugin and move it to src/plugins/plugin_misc_room_game.py
souliane <souliane@mailoo.org>
parents:
717
diff
changeset
|
54 RoomGame = host.plugins["ROOM-GAME"].__class__ |
074970227bc0
plugin tools: turn src/plugin/games.py into a plugin and move it to src/plugins/plugin_misc_room_game.py
souliane <souliane@mailoo.org>
parents:
717
diff
changeset
|
55 self.__class__ = type(self.__class__.__name__, (self.__class__, RoomGame, object), {}) |
88 | 56 |
57 def __init__(self, host): | |
993
301b342c697a
core: use of the new core.log module:
Goffi <goffi@goffi.org>
parents:
916
diff
changeset
|
58 log.info(_("Plugin Tarot initialization")) |
878
36c6495d86b0
plugin card_game: update to use the new XMLUI mechanism:
souliane <souliane@mailoo.org>
parents:
828
diff
changeset
|
59 self._sessions = memory.Sessions() |
718
074970227bc0
plugin tools: turn src/plugin/games.py into a plugin and move it to src/plugins/plugin_misc_room_game.py
souliane <souliane@mailoo.org>
parents:
717
diff
changeset
|
60 self.inheritFromRoomGame(host) |
074970227bc0
plugin tools: turn src/plugin/games.py into a plugin and move it to src/plugins/plugin_misc_room_game.py
souliane <souliane@mailoo.org>
parents:
717
diff
changeset
|
61 RoomGame._init_(self, host, PLUGIN_INFO, (NS_CG, CG_TAG), |
1359
83127a4c89ce
plugins room_game, quiz, radiocol, tarot: use JID instead of unicode in many methods + class attributes
souliane <souliane@mailoo.org>
parents:
1257
diff
changeset
|
62 game_init={'hand_size': 18, 'init_player': 0, 'current_player': None, 'contrat': None, 'stage': None}, |
83127a4c89ce
plugins room_game, quiz, radiocol, tarot: use JID instead of unicode in many methods + class attributes
souliane <souliane@mailoo.org>
parents:
1257
diff
changeset
|
63 player_init={'score': 0}) |
91 | 64 self.contrats = [_('Passe'), _('Petite'), _('Garde'), _('Garde Sans'), _('Garde Contre')] |
1359
83127a4c89ce
plugins room_game, quiz, radiocol, tarot: use JID instead of unicode in many methods + class attributes
souliane <souliane@mailoo.org>
parents:
1257
diff
changeset
|
65 host.bridge.addMethod("tarotGameLaunch", ".plugin", in_sign='asss', out_sign='', method=self._prepareRoom, async=True) # args: players, room_jid, profile |
83127a4c89ce
plugins room_game, quiz, radiocol, tarot: use JID instead of unicode in many methods + class attributes
souliane <souliane@mailoo.org>
parents:
1257
diff
changeset
|
66 host.bridge.addMethod("tarotGameCreate", ".plugin", in_sign='sass', out_sign='', method=self._createGame) # args: room_jid, players, profile |
83127a4c89ce
plugins room_game, quiz, radiocol, tarot: use JID instead of unicode in many methods + class attributes
souliane <souliane@mailoo.org>
parents:
1257
diff
changeset
|
67 host.bridge.addMethod("tarotGameReady", ".plugin", in_sign='sss', out_sign='', method=self._playerReady) # args: player, referee, profile |
594
e629371a28d3
Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
68 host.bridge.addMethod("tarotGamePlayCards", ".plugin", in_sign='ssa(ss)s', out_sign='', method=self.play_cards) # args: player, referee, cards, profile |
717
358018c5c398
plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents:
714
diff
changeset
|
69 host.bridge.addSignal("tarotGamePlayers", ".plugin", signature='ssass') # args: room_jid, referee, players, profile |
594
e629371a28d3
Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
70 host.bridge.addSignal("tarotGameStarted", ".plugin", signature='ssass') # args: room_jid, referee, players, profile |
e629371a28d3
Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
71 host.bridge.addSignal("tarotGameNew", ".plugin", signature='sa(ss)s') # args: room_jid, hand, profile |
e629371a28d3
Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
72 host.bridge.addSignal("tarotGameChooseContrat", ".plugin", signature='sss') # args: room_jid, xml_data, profile |
e629371a28d3
Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
73 host.bridge.addSignal("tarotGameShowCards", ".plugin", signature='ssa(ss)a{ss}s') # args: room_jid, type ["chien", "poignée",...], cards, data[dict], profile |
e629371a28d3
Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
74 host.bridge.addSignal("tarotGameCardsPlayed", ".plugin", signature='ssa(ss)s') # args: room_jid, player, type ["chien", "poignée",...], cards, data[dict], profile |
e629371a28d3
Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
75 host.bridge.addSignal("tarotGameYourTurn", ".plugin", signature='ss') # args: room_jid, profile |
e629371a28d3
Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
76 host.bridge.addSignal("tarotGameScore", ".plugin", signature='ssasass') # args: room_jid, xml_data, winners (list of nicks), loosers (list of nicks), profile |
e629371a28d3
Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
77 host.bridge.addSignal("tarotGameInvalidCards", ".plugin", signature='ssa(ss)a(ss)s') # args: room_jid, game phase, played_cards, invalid_cards, profile |
88 | 78 self.deck_ordered = [] |
594
e629371a28d3
Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
79 for value in ['excuse'] + map(str, range(1, 22)): |
e629371a28d3
Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
80 self.deck_ordered.append(TarotCard(("atout", value))) |
92 | 81 for suit in ["pique", "coeur", "carreau", "trefle"]: |
594
e629371a28d3
Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
82 for value in map(str, range(1, 11)) + ["valet", "cavalier", "dame", "roi"]: |
141
8c80d4dec7a8
mover Card class to tools/games and renamed it in TarotCard
Goffi <goffi@goffi.org>
parents:
134
diff
changeset
|
83 self.deck_ordered.append(TarotCard((suit, value))) |
878
36c6495d86b0
plugin card_game: update to use the new XMLUI mechanism:
souliane <souliane@mailoo.org>
parents:
828
diff
changeset
|
84 self.__choose_contrat_id = host.registerCallback(self._contratChoosed, with_data=True) |
36c6495d86b0
plugin card_game: update to use the new XMLUI mechanism:
souliane <souliane@mailoo.org>
parents:
828
diff
changeset
|
85 self.__score_id = host.registerCallback(self._scoreShowed, with_data=True) |
36c6495d86b0
plugin card_game: update to use the new XMLUI mechanism:
souliane <souliane@mailoo.org>
parents:
828
diff
changeset
|
86 |
98
dd556233a1b1
Tarot Plugin: Garde Sans and Garde Contre are now managed
Goffi <goffi@goffi.org>
parents:
96
diff
changeset
|
87 def __card_list_to_xml(self, cards_list, elt_name): |
dd556233a1b1
Tarot Plugin: Garde Sans and Garde Contre are now managed
Goffi <goffi@goffi.org>
parents:
96
diff
changeset
|
88 """Convert a card list to domish element""" |
594
e629371a28d3
Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
89 cards_list_elt = domish.Element((None, elt_name)) |
98
dd556233a1b1
Tarot Plugin: Garde Sans and Garde Contre are now managed
Goffi <goffi@goffi.org>
parents:
96
diff
changeset
|
90 for card in cards_list: |
594
e629371a28d3
Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
91 card_elt = domish.Element((None, 'card')) |
98
dd556233a1b1
Tarot Plugin: Garde Sans and Garde Contre are now managed
Goffi <goffi@goffi.org>
parents:
96
diff
changeset
|
92 card_elt['suit'] = card.suit |
dd556233a1b1
Tarot Plugin: Garde Sans and Garde Contre are now managed
Goffi <goffi@goffi.org>
parents:
96
diff
changeset
|
93 card_elt['value'] = card.value |
587
952322b1d490
Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
572
diff
changeset
|
94 cards_list_elt.addChild(card_elt) |
92 | 95 return cards_list_elt |
90 | 96 |
92 | 97 def __xml_to_list(self, cards_list_elt): |
98 """Convert a domish element with cards to a list of tuples""" | |
99 cards_list = [] | |
100 for card in cards_list_elt.elements(): | |
101 cards_list.append((card['suit'], card['value'])) | |
102 return cards_list | |
587
952322b1d490
Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
572
diff
changeset
|
103 |
91 | 104 def __ask_contrat(self): |
105 """Create a element for asking contrat""" | |
594
e629371a28d3
Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
106 contrat_elt = domish.Element((None, 'contrat')) |
91 | 107 form = data_form.Form('form', title=_('contrat selection')) |
108 field = data_form.Field('list-single', 'contrat', options=map(data_form.Option, self.contrats), required=True) | |
109 form.addField(field) | |
110 contrat_elt.addChild(form.toElement()) | |
111 return contrat_elt | |
112 | |
95 | 113 def __give_scores(self, scores, winners, loosers): |
114 """Create an element to give scores | |
115 @param scores: unicode (can contain line feed) | |
116 @param winners: list of unicode nicks of winners | |
117 @param loosers: list of unicode nicks of loosers""" | |
118 | |
594
e629371a28d3
Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
119 score_elt = domish.Element((None, 'score')) |
95 | 120 form = data_form.Form('form', title=_('scores')) |
121 for line in scores.split('\n'): | |
594
e629371a28d3
Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
122 field = data_form.Field('fixed', value=line) |
95 | 123 form.addField(field) |
124 score_elt.addChild(form.toElement()) | |
125 for winner in winners: | |
594
e629371a28d3
Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
126 winner_elt = domish.Element((None, 'winner')) |
95 | 127 winner_elt.addContent(winner) |
128 score_elt.addChild(winner_elt) | |
129 for looser in loosers: | |
594
e629371a28d3
Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
130 looser_elt = domish.Element((None, 'looser')) |
95 | 131 looser_elt.addContent(looser) |
132 score_elt.addChild(looser_elt) | |
133 return score_elt | |
134 | |
99
63c9067a1499
Tarot game: invalid cards management
Goffi <goffi@goffi.org>
parents:
98
diff
changeset
|
135 def __invalid_cards_elt(self, played_cards, invalid_cards, game_phase): |
63c9067a1499
Tarot game: invalid cards management
Goffi <goffi@goffi.org>
parents:
98
diff
changeset
|
136 """Create a element for invalid_cards error |
63c9067a1499
Tarot game: invalid cards management
Goffi <goffi@goffi.org>
parents:
98
diff
changeset
|
137 @param list_cards: list of Card |
63c9067a1499
Tarot game: invalid cards management
Goffi <goffi@goffi.org>
parents:
98
diff
changeset
|
138 @param game_phase: phase of the game ['ecart', 'play']""" |
594
e629371a28d3
Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
139 error_elt = domish.Element((None, 'error')) |
99
63c9067a1499
Tarot game: invalid cards management
Goffi <goffi@goffi.org>
parents:
98
diff
changeset
|
140 played_elt = self.__card_list_to_xml(played_cards, 'played') |
63c9067a1499
Tarot game: invalid cards management
Goffi <goffi@goffi.org>
parents:
98
diff
changeset
|
141 invalid_elt = self.__card_list_to_xml(invalid_cards, 'invalid') |
63c9067a1499
Tarot game: invalid cards management
Goffi <goffi@goffi.org>
parents:
98
diff
changeset
|
142 error_elt['type'] = 'invalid_cards' |
63c9067a1499
Tarot game: invalid cards management
Goffi <goffi@goffi.org>
parents:
98
diff
changeset
|
143 error_elt['phase'] = game_phase |
63c9067a1499
Tarot game: invalid cards management
Goffi <goffi@goffi.org>
parents:
98
diff
changeset
|
144 error_elt.addChild(played_elt) |
63c9067a1499
Tarot game: invalid cards management
Goffi <goffi@goffi.org>
parents:
98
diff
changeset
|
145 error_elt.addChild(invalid_elt) |
63c9067a1499
Tarot game: invalid cards management
Goffi <goffi@goffi.org>
parents:
98
diff
changeset
|
146 return error_elt |
587
952322b1d490
Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
572
diff
changeset
|
147 |
594
e629371a28d3
Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
148 def __next_player(self, game_data, next_pl=None): |
94 | 149 """Increment player number & return player name |
150 @param next_pl: if given, then next_player is forced to this one | |
151 """ | |
152 if next_pl: | |
153 game_data['current_player'] = game_data['players'].index(next_pl) | |
154 return next_pl | |
155 else: | |
156 pl_idx = game_data['current_player'] = (game_data['current_player'] + 1) % len(game_data['players']) | |
157 return game_data['players'][pl_idx] | |
158 | |
159 def __winner(self, game_data): | |
160 """give the nick of the player who win this trick""" | |
161 players_data = game_data['players_data'] | |
162 first = game_data['first_player'] | |
587
952322b1d490
Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
572
diff
changeset
|
163 first_idx = game_data['players'].index(first) |
94 | 164 suit_asked = None |
165 strongest = None | |
166 winner = None | |
167 for idx in [(first_idx + i) % 4 for i in range(4)]: | |
168 player = game_data['players'][idx] | |
169 card = players_data[player]['played'] | |
170 if card.value == "excuse": | |
171 continue | |
594
e629371a28d3
Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
172 if suit_asked is None: |
94 | 173 suit_asked = card.suit |
174 if (card.suit == suit_asked or card.suit == "atout") and card > strongest: | |
175 strongest = card | |
176 winner = player | |
95 | 177 assert winner |
94 | 178 return winner |
179 | |
180 def __excuse_hack(self, game_data, played, winner): | |
95 | 181 """give a low card to other team and keep excuse if trick is lost |
182 @param game_data: data of the game | |
183 @param played: cards currently on the table | |
184 @param winner: nick of the trick winner""" | |
1359
83127a4c89ce
plugins room_game, quiz, radiocol, tarot: use JID instead of unicode in many methods + class attributes
souliane <souliane@mailoo.org>
parents:
1257
diff
changeset
|
185 # TODO: manage the case where excuse is played on the last trick (and lost) |
94 | 186 players_data = game_data['players_data'] |
594
e629371a28d3
Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
187 excuse = TarotCard(("atout", "excuse")) |
95 | 188 |
1359
83127a4c89ce
plugins room_game, quiz, radiocol, tarot: use JID instead of unicode in many methods + class attributes
souliane <souliane@mailoo.org>
parents:
1257
diff
changeset
|
189 # we first check if the Excuse was already played |
83127a4c89ce
plugins room_game, quiz, radiocol, tarot: use JID instead of unicode in many methods + class attributes
souliane <souliane@mailoo.org>
parents:
1257
diff
changeset
|
190 # and if somebody is waiting for a card |
94 | 191 for player in game_data['players']: |
192 if players_data[player]['wait_for_low']: | |
1359
83127a4c89ce
plugins room_game, quiz, radiocol, tarot: use JID instead of unicode in many methods + class attributes
souliane <souliane@mailoo.org>
parents:
1257
diff
changeset
|
193 # the excuse owner has to give a card to somebody |
94 | 194 if winner == player: |
1359
83127a4c89ce
plugins room_game, quiz, radiocol, tarot: use JID instead of unicode in many methods + class attributes
souliane <souliane@mailoo.org>
parents:
1257
diff
changeset
|
195 # the excuse owner win the trick, we check if we have something to give |
94 | 196 for card in played: |
197 if card.points == 0.5: | |
198 pl_waiting = players_data[player]['wait_for_low'] | |
199 played.remove(card) | |
200 players_data[pl_waiting]['levees'].append(card) | |
1409
3265a2639182
massive (preventive) addition of 'u' (unicode) before the strings passed to logging functions
souliane <souliane@mailoo.org>
parents:
1396
diff
changeset
|
201 log.debug(_(u'Player %(excuse_owner)s give %(card_waited)s to %(player_waiting)s for Excuse compensation') % {"excuse_owner": player, "card_waited": card, "player_waiting": pl_waiting}) |
95 | 202 return |
94 | 203 return |
204 | |
1359
83127a4c89ce
plugins room_game, quiz, radiocol, tarot: use JID instead of unicode in many methods + class attributes
souliane <souliane@mailoo.org>
parents:
1257
diff
changeset
|
205 if excuse not in played: |
83127a4c89ce
plugins room_game, quiz, radiocol, tarot: use JID instead of unicode in many methods + class attributes
souliane <souliane@mailoo.org>
parents:
1257
diff
changeset
|
206 # the Excuse is not on the table, nothing to do |
94 | 207 return |
587
952322b1d490
Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
572
diff
changeset
|
208 |
594
e629371a28d3
Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
209 excuse_player = None # Who has played the Excuse ? |
94 | 210 for player in game_data['players']: |
211 if players_data[player]['played'] == excuse: | |
212 excuse_player = player | |
213 break | |
587
952322b1d490
Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
572
diff
changeset
|
214 |
94 | 215 if excuse_player == winner: |
594
e629371a28d3
Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
216 return # the excuse player win the trick, nothing to do |
587
952322b1d490
Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
572
diff
changeset
|
217 |
1359
83127a4c89ce
plugins room_game, quiz, radiocol, tarot: use JID instead of unicode in many methods + class attributes
souliane <souliane@mailoo.org>
parents:
1257
diff
changeset
|
218 # first we remove the excuse from played cards |
94 | 219 played.remove(excuse) |
1359
83127a4c89ce
plugins room_game, quiz, radiocol, tarot: use JID instead of unicode in many methods + class attributes
souliane <souliane@mailoo.org>
parents:
1257
diff
changeset
|
220 # then we give it back to the original owner |
94 | 221 owner_levees = players_data[excuse_player]['levees'] |
222 owner_levees.append(excuse) | |
1359
83127a4c89ce
plugins room_game, quiz, radiocol, tarot: use JID instead of unicode in many methods + class attributes
souliane <souliane@mailoo.org>
parents:
1257
diff
changeset
|
223 # finally we give a low card to the trick winner |
94 | 224 low_card = None |
1359
83127a4c89ce
plugins room_game, quiz, radiocol, tarot: use JID instead of unicode in many methods + class attributes
souliane <souliane@mailoo.org>
parents:
1257
diff
changeset
|
225 # We look backward in cards won by the Excuse owner to |
83127a4c89ce
plugins room_game, quiz, radiocol, tarot: use JID instead of unicode in many methods + class attributes
souliane <souliane@mailoo.org>
parents:
1257
diff
changeset
|
226 # find a low value card |
594
e629371a28d3
Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
227 for card_idx in range(len(owner_levees) - 1, -1, -1): |
94 | 228 if owner_levees[card_idx].points == 0.5: |
229 low_card = owner_levees[card_idx] | |
230 del owner_levees[card_idx] | |
231 players_data[winner]['levees'].append(low_card) | |
1409
3265a2639182
massive (preventive) addition of 'u' (unicode) before the strings passed to logging functions
souliane <souliane@mailoo.org>
parents:
1396
diff
changeset
|
232 log.debug(_(u'Player %(excuse_owner)s give %(card_waited)s to %(player_waiting)s for Excuse compensation') % {"excuse_owner": excuse_player, "card_waited": low_card, "player_waiting": winner}) |
94 | 233 break |
594
e629371a28d3
Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
234 if not low_card: # The player has no low card yet |
1359
83127a4c89ce
plugins room_game, quiz, radiocol, tarot: use JID instead of unicode in many methods + class attributes
souliane <souliane@mailoo.org>
parents:
1257
diff
changeset
|
235 # TODO: manage case when player never win a trick with low card |
94 | 236 players_data[excuse_player]['wait_for_low'] = winner |
1409
3265a2639182
massive (preventive) addition of 'u' (unicode) before the strings passed to logging functions
souliane <souliane@mailoo.org>
parents:
1396
diff
changeset
|
237 log.debug(_(u"%(excuse_owner)s keep the Excuse but has not card to give, %(winner)s is waiting for one") % {'excuse_owner': excuse_player, 'winner': winner}) |
94 | 238 |
328 | 239 def __draw_game(self, game_data): |
240 """The game is draw, no score change | |
241 @param game_data: data of the game | |
242 @return: tuple with (string victory message, list of winners, list of loosers)""" | |
243 players_data = game_data['players_data'] | |
244 scores_str = _('Draw game') | |
594
e629371a28d3
Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
245 scores_str += '\n' |
328 | 246 for player in game_data['players']: |
1409
3265a2639182
massive (preventive) addition of 'u' (unicode) before the strings passed to logging functions
souliane <souliane@mailoo.org>
parents:
1396
diff
changeset
|
247 scores_str += _(u"\n--\n%(player)s:\nscore for this game ==> %(score_game)i\ntotal score ==> %(total_score)i") % {'player': player, 'score_game': 0, 'total_score': players_data[player]['score']} |
993
301b342c697a
core: use of the new core.log module:
Goffi <goffi@goffi.org>
parents:
916
diff
changeset
|
248 log.debug(scores_str) |
587
952322b1d490
Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
572
diff
changeset
|
249 |
328 | 250 return (scores_str, [], []) |
251 | |
94 | 252 def __calculate_scores(self, game_data): |
95 | 253 """The game is finished, time to know who won :) |
254 @param game_data: data of the game | |
255 @return: tuple with (string victory message, list of winners, list of loosers)""" | |
94 | 256 players_data = game_data['players_data'] |
257 levees = players_data[game_data['attaquant']]['levees'] | |
258 score = 0 | |
259 nb_bouts = 0 | |
95 | 260 bouts = [] |
94 | 261 for card in levees: |
262 if card.bout: | |
594
e629371a28d3
Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
263 nb_bouts += 1 |
95 | 264 bouts.append(card.value) |
94 | 265 score += card.points |
587
952322b1d490
Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
572
diff
changeset
|
266 |
1359
83127a4c89ce
plugins room_game, quiz, radiocol, tarot: use JID instead of unicode in many methods + class attributes
souliane <souliane@mailoo.org>
parents:
1257
diff
changeset
|
267 # We do a basic check on score calculation |
95 | 268 check_score = 0 |
269 defenseurs = game_data['players'][:] | |
270 defenseurs.remove(game_data['attaquant']) | |
271 for defenseur in defenseurs: | |
272 for card in players_data[defenseur]['levees']: | |
594
e629371a28d3
Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
273 check_score += card.points |
98
dd556233a1b1
Tarot Plugin: Garde Sans and Garde Contre are now managed
Goffi <goffi@goffi.org>
parents:
96
diff
changeset
|
274 if game_data['contrat'] == "Garde Contre": |
dd556233a1b1
Tarot Plugin: Garde Sans and Garde Contre are now managed
Goffi <goffi@goffi.org>
parents:
96
diff
changeset
|
275 for card in game_data['chien']: |
594
e629371a28d3
Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
276 check_score += card.points |
95 | 277 assert (score + check_score == 91) |
587
952322b1d490
Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
572
diff
changeset
|
278 |
94 | 279 point_limit = None |
280 if nb_bouts == 3: | |
281 point_limit = 36 | |
282 elif nb_bouts == 2: | |
283 point_limit = 41 | |
284 elif nb_bouts == 1: | |
285 point_limit = 51 | |
286 else: | |
287 point_limit = 56 | |
95 | 288 if game_data['contrat'] == 'Petite': |
289 contrat_mult = 1 | |
290 elif game_data['contrat'] == 'Garde': | |
291 contrat_mult = 2 | |
292 elif game_data['contrat'] == 'Garde Sans': | |
293 contrat_mult = 4 | |
294 elif game_data['contrat'] == 'Garde Contre': | |
295 contrat_mult = 6 | |
296 else: | |
993
301b342c697a
core: use of the new core.log module:
Goffi <goffi@goffi.org>
parents:
916
diff
changeset
|
297 log.error(_('INTERNAL ERROR: contrat not managed (mispelled ?)')) |
161
c37826d80f2a
plugin tarot: fixed bad score calculation
Goffi <goffi@goffi.org>
parents:
147
diff
changeset
|
298 assert(False) |
95 | 299 |
94 | 300 victory = (score >= point_limit) |
161
c37826d80f2a
plugin tarot: fixed bad score calculation
Goffi <goffi@goffi.org>
parents:
147
diff
changeset
|
301 margin = abs(score - point_limit) |
c37826d80f2a
plugin tarot: fixed bad score calculation
Goffi <goffi@goffi.org>
parents:
147
diff
changeset
|
302 points_defenseur = (margin + 25) * contrat_mult * (-1 if victory else 1) |
95 | 303 winners = [] |
304 loosers = [] | |
305 player_score = {} | |
306 for player in game_data['players']: | |
1359
83127a4c89ce
plugins room_game, quiz, radiocol, tarot: use JID instead of unicode in many methods + class attributes
souliane <souliane@mailoo.org>
parents:
1257
diff
changeset
|
307 # TODO: adjust this for 3 and 5 players variants |
83127a4c89ce
plugins room_game, quiz, radiocol, tarot: use JID instead of unicode in many methods + class attributes
souliane <souliane@mailoo.org>
parents:
1257
diff
changeset
|
308 # TODO: manage bonuses (petit au bout, poignée, chelem) |
95 | 309 player_score[player] = points_defenseur if player != game_data['attaquant'] else points_defenseur * -3 |
594
e629371a28d3
Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
310 players_data[player]['score'] += player_score[player] # we add score of this game to the global score |
95 | 311 if player_score[player] > 0: |
312 winners.append(player) | |
313 else: | |
314 loosers.append(player) | |
94 | 315 |
1409
3265a2639182
massive (preventive) addition of 'u' (unicode) before the strings passed to logging functions
souliane <souliane@mailoo.org>
parents:
1396
diff
changeset
|
316 scores_str = _(u'The attacker (%(attaquant)s) makes %(points)i and needs to make %(point_limit)i (%(nb_bouts)s oulder%(plural)s%(separator)s%(bouts)s): (s)he %(victory)s') % {'attaquant': game_data['attaquant'], 'points': score, 'point_limit': point_limit, 'nb_bouts': nb_bouts, 'plural': 's' if nb_bouts > 1 else '', 'separator': ': ' if nb_bouts != 0 else '', 'bouts': ','.join(map(str, bouts)), 'victory': 'wins' if victory else 'looses'} |
594
e629371a28d3
Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
317 scores_str += '\n' |
95 | 318 for player in game_data['players']: |
1409
3265a2639182
massive (preventive) addition of 'u' (unicode) before the strings passed to logging functions
souliane <souliane@mailoo.org>
parents:
1396
diff
changeset
|
319 scores_str += _(u"\n--\n%(player)s:\nscore for this game ==> %(score_game)i\ntotal score ==> %(total_score)i") % {'player': player, 'score_game': player_score[player], 'total_score': players_data[player]['score']} |
993
301b342c697a
core: use of the new core.log module:
Goffi <goffi@goffi.org>
parents:
916
diff
changeset
|
320 log.debug(scores_str) |
587
952322b1d490
Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
572
diff
changeset
|
321 |
95 | 322 return (scores_str, winners, loosers) |
94 | 323 |
99
63c9067a1499
Tarot game: invalid cards management
Goffi <goffi@goffi.org>
parents:
98
diff
changeset
|
324 def __invalid_cards(self, game_data, cards): |
63c9067a1499
Tarot game: invalid cards management
Goffi <goffi@goffi.org>
parents:
98
diff
changeset
|
325 """Checks that the player has the right to play what he wants to |
63c9067a1499
Tarot game: invalid cards management
Goffi <goffi@goffi.org>
parents:
98
diff
changeset
|
326 @param game_data: Game data |
63c9067a1499
Tarot game: invalid cards management
Goffi <goffi@goffi.org>
parents:
98
diff
changeset
|
327 @param cards: cards the player want to play |
63c9067a1499
Tarot game: invalid cards management
Goffi <goffi@goffi.org>
parents:
98
diff
changeset
|
328 @return forbidden_cards cards or empty list if cards are ok""" |
63c9067a1499
Tarot game: invalid cards management
Goffi <goffi@goffi.org>
parents:
98
diff
changeset
|
329 forbidden_cards = [] |
63c9067a1499
Tarot game: invalid cards management
Goffi <goffi@goffi.org>
parents:
98
diff
changeset
|
330 if game_data['stage'] == 'ecart': |
63c9067a1499
Tarot game: invalid cards management
Goffi <goffi@goffi.org>
parents:
98
diff
changeset
|
331 for card in cards: |
594
e629371a28d3
Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
332 if card.bout or card.value == "roi": |
99
63c9067a1499
Tarot game: invalid cards management
Goffi <goffi@goffi.org>
parents:
98
diff
changeset
|
333 forbidden_cards.append(card) |
1359
83127a4c89ce
plugins room_game, quiz, radiocol, tarot: use JID instead of unicode in many methods + class attributes
souliane <souliane@mailoo.org>
parents:
1257
diff
changeset
|
334 # TODO: manage case where atouts (trumps) are in the dog |
99
63c9067a1499
Tarot game: invalid cards management
Goffi <goffi@goffi.org>
parents:
98
diff
changeset
|
335 elif game_data['stage'] == 'play': |
63c9067a1499
Tarot game: invalid cards management
Goffi <goffi@goffi.org>
parents:
98
diff
changeset
|
336 biggest_atout = None |
63c9067a1499
Tarot game: invalid cards management
Goffi <goffi@goffi.org>
parents:
98
diff
changeset
|
337 suit_asked = None |
63c9067a1499
Tarot game: invalid cards management
Goffi <goffi@goffi.org>
parents:
98
diff
changeset
|
338 players = game_data['players'] |
63c9067a1499
Tarot game: invalid cards management
Goffi <goffi@goffi.org>
parents:
98
diff
changeset
|
339 players_data = game_data['players_data'] |
63c9067a1499
Tarot game: invalid cards management
Goffi <goffi@goffi.org>
parents:
98
diff
changeset
|
340 idx = players.index(game_data['first_player']) |
63c9067a1499
Tarot game: invalid cards management
Goffi <goffi@goffi.org>
parents:
98
diff
changeset
|
341 current_idx = game_data['current_player'] |
63c9067a1499
Tarot game: invalid cards management
Goffi <goffi@goffi.org>
parents:
98
diff
changeset
|
342 current_player = players[current_idx] |
63c9067a1499
Tarot game: invalid cards management
Goffi <goffi@goffi.org>
parents:
98
diff
changeset
|
343 if idx == current_idx: |
1359
83127a4c89ce
plugins room_game, quiz, radiocol, tarot: use JID instead of unicode in many methods + class attributes
souliane <souliane@mailoo.org>
parents:
1257
diff
changeset
|
344 # the player is the first to play, he can play what he wants |
99
63c9067a1499
Tarot game: invalid cards management
Goffi <goffi@goffi.org>
parents:
98
diff
changeset
|
345 return forbidden_cards |
63c9067a1499
Tarot game: invalid cards management
Goffi <goffi@goffi.org>
parents:
98
diff
changeset
|
346 while (idx != current_idx): |
63c9067a1499
Tarot game: invalid cards management
Goffi <goffi@goffi.org>
parents:
98
diff
changeset
|
347 player = players[idx] |
63c9067a1499
Tarot game: invalid cards management
Goffi <goffi@goffi.org>
parents:
98
diff
changeset
|
348 played_card = players_data[player]['played'] |
63c9067a1499
Tarot game: invalid cards management
Goffi <goffi@goffi.org>
parents:
98
diff
changeset
|
349 if not suit_asked and played_card.value != "excuse": |
63c9067a1499
Tarot game: invalid cards management
Goffi <goffi@goffi.org>
parents:
98
diff
changeset
|
350 suit_asked = played_card.suit |
63c9067a1499
Tarot game: invalid cards management
Goffi <goffi@goffi.org>
parents:
98
diff
changeset
|
351 if played_card.suit == "atout" and played_card > biggest_atout: |
63c9067a1499
Tarot game: invalid cards management
Goffi <goffi@goffi.org>
parents:
98
diff
changeset
|
352 biggest_atout = played_card |
63c9067a1499
Tarot game: invalid cards management
Goffi <goffi@goffi.org>
parents:
98
diff
changeset
|
353 idx = (idx + 1) % len(players) |
594
e629371a28d3
Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
354 has_suit = False # True if there is one card of the asked suit in the hand of the player |
99
63c9067a1499
Tarot game: invalid cards management
Goffi <goffi@goffi.org>
parents:
98
diff
changeset
|
355 has_atout = False |
63c9067a1499
Tarot game: invalid cards management
Goffi <goffi@goffi.org>
parents:
98
diff
changeset
|
356 biggest_hand_atout = None |
63c9067a1499
Tarot game: invalid cards management
Goffi <goffi@goffi.org>
parents:
98
diff
changeset
|
357 |
63c9067a1499
Tarot game: invalid cards management
Goffi <goffi@goffi.org>
parents:
98
diff
changeset
|
358 for hand_card in game_data['hand'][current_player]: |
63c9067a1499
Tarot game: invalid cards management
Goffi <goffi@goffi.org>
parents:
98
diff
changeset
|
359 if hand_card.suit == suit_asked: |
63c9067a1499
Tarot game: invalid cards management
Goffi <goffi@goffi.org>
parents:
98
diff
changeset
|
360 has_suit = True |
63c9067a1499
Tarot game: invalid cards management
Goffi <goffi@goffi.org>
parents:
98
diff
changeset
|
361 if hand_card.suit == "atout": |
63c9067a1499
Tarot game: invalid cards management
Goffi <goffi@goffi.org>
parents:
98
diff
changeset
|
362 has_atout = True |
63c9067a1499
Tarot game: invalid cards management
Goffi <goffi@goffi.org>
parents:
98
diff
changeset
|
363 if hand_card.suit == "atout" and hand_card > biggest_hand_atout: |
63c9067a1499
Tarot game: invalid cards management
Goffi <goffi@goffi.org>
parents:
98
diff
changeset
|
364 biggest_hand_atout = hand_card |
63c9067a1499
Tarot game: invalid cards management
Goffi <goffi@goffi.org>
parents:
98
diff
changeset
|
365 |
63c9067a1499
Tarot game: invalid cards management
Goffi <goffi@goffi.org>
parents:
98
diff
changeset
|
366 assert len(cards) == 1 |
63c9067a1499
Tarot game: invalid cards management
Goffi <goffi@goffi.org>
parents:
98
diff
changeset
|
367 card = cards[0] |
63c9067a1499
Tarot game: invalid cards management
Goffi <goffi@goffi.org>
parents:
98
diff
changeset
|
368 if card.suit != suit_asked and has_suit and card.value != "excuse": |
63c9067a1499
Tarot game: invalid cards management
Goffi <goffi@goffi.org>
parents:
98
diff
changeset
|
369 forbidden_cards.append(card) |
63c9067a1499
Tarot game: invalid cards management
Goffi <goffi@goffi.org>
parents:
98
diff
changeset
|
370 return forbidden_cards |
63c9067a1499
Tarot game: invalid cards management
Goffi <goffi@goffi.org>
parents:
98
diff
changeset
|
371 if card.suit != suit_asked and card.suit != "atout" and has_atout: |
63c9067a1499
Tarot game: invalid cards management
Goffi <goffi@goffi.org>
parents:
98
diff
changeset
|
372 forbidden_cards.append(card) |
63c9067a1499
Tarot game: invalid cards management
Goffi <goffi@goffi.org>
parents:
98
diff
changeset
|
373 return forbidden_cards |
63c9067a1499
Tarot game: invalid cards management
Goffi <goffi@goffi.org>
parents:
98
diff
changeset
|
374 if card.suit == "atout" and card < biggest_atout and biggest_hand_atout > biggest_atout and card.value != "excuse": |
63c9067a1499
Tarot game: invalid cards management
Goffi <goffi@goffi.org>
parents:
98
diff
changeset
|
375 forbidden_cards.append(card) |
63c9067a1499
Tarot game: invalid cards management
Goffi <goffi@goffi.org>
parents:
98
diff
changeset
|
376 else: |
993
301b342c697a
core: use of the new core.log module:
Goffi <goffi@goffi.org>
parents:
916
diff
changeset
|
377 log.error(_('Internal error: unmanaged game stage')) |
99
63c9067a1499
Tarot game: invalid cards management
Goffi <goffi@goffi.org>
parents:
98
diff
changeset
|
378 return forbidden_cards |
63c9067a1499
Tarot game: invalid cards management
Goffi <goffi@goffi.org>
parents:
98
diff
changeset
|
379 |
98
dd556233a1b1
Tarot Plugin: Garde Sans and Garde Contre are now managed
Goffi <goffi@goffi.org>
parents:
96
diff
changeset
|
380 def __start_play(self, room_jid, game_data, profile): |
dd556233a1b1
Tarot Plugin: Garde Sans and Garde Contre are now managed
Goffi <goffi@goffi.org>
parents:
96
diff
changeset
|
381 """Start the game (tell to the first player after dealer to play""" |
dd556233a1b1
Tarot Plugin: Garde Sans and Garde Contre are now managed
Goffi <goffi@goffi.org>
parents:
96
diff
changeset
|
382 game_data['stage'] = "play" |
594
e629371a28d3
Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
383 next_player_idx = game_data['current_player'] = (game_data['init_player'] + 1) % len(game_data['players']) # the player after the dealer start |
98
dd556233a1b1
Tarot Plugin: Garde Sans and Garde Contre are now managed
Goffi <goffi@goffi.org>
parents:
96
diff
changeset
|
384 game_data['first_player'] = next_player = game_data['players'][next_player_idx] |
594
e629371a28d3
Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
385 to_jid = jid.JID(room_jid.userhost() + "/" + next_player) # FIXME: gof: |
714
ecc5a5b34ee1
plugins (games): add a method to send messages more easily
souliane <souliane@mailoo.org>
parents:
712
diff
changeset
|
386 self.send(to_jid, 'your_turn', profile=profile) |
98
dd556233a1b1
Tarot Plugin: Garde Sans and Garde Contre are now managed
Goffi <goffi@goffi.org>
parents:
96
diff
changeset
|
387 |
878
36c6495d86b0
plugin card_game: update to use the new XMLUI mechanism:
souliane <souliane@mailoo.org>
parents:
828
diff
changeset
|
388 def _contratChoosed(self, raw_data, profile): |
36c6495d86b0
plugin card_game: update to use the new XMLUI mechanism:
souliane <souliane@mailoo.org>
parents:
828
diff
changeset
|
389 """Will be called when the contrat is selected |
36c6495d86b0
plugin card_game: update to use the new XMLUI mechanism:
souliane <souliane@mailoo.org>
parents:
828
diff
changeset
|
390 @param raw_data: contains the choosed session id and the chosen contrat |
36c6495d86b0
plugin card_game: update to use the new XMLUI mechanism:
souliane <souliane@mailoo.org>
parents:
828
diff
changeset
|
391 @param profile_key: profile |
91 | 392 """ |
878
36c6495d86b0
plugin card_game: update to use the new XMLUI mechanism:
souliane <souliane@mailoo.org>
parents:
828
diff
changeset
|
393 try: |
36c6495d86b0
plugin card_game: update to use the new XMLUI mechanism:
souliane <souliane@mailoo.org>
parents:
828
diff
changeset
|
394 session_data = self._sessions.profileGet(raw_data["session_id"], profile) |
36c6495d86b0
plugin card_game: update to use the new XMLUI mechanism:
souliane <souliane@mailoo.org>
parents:
828
diff
changeset
|
395 except KeyError: |
993
301b342c697a
core: use of the new core.log module:
Goffi <goffi@goffi.org>
parents:
916
diff
changeset
|
396 log.warning(_("session id doesn't exist, session has probably expired")) |
878
36c6495d86b0
plugin card_game: update to use the new XMLUI mechanism:
souliane <souliane@mailoo.org>
parents:
828
diff
changeset
|
397 # TODO: send error dialog |
36c6495d86b0
plugin card_game: update to use the new XMLUI mechanism:
souliane <souliane@mailoo.org>
parents:
828
diff
changeset
|
398 return defer.succeed({}) |
36c6495d86b0
plugin card_game: update to use the new XMLUI mechanism:
souliane <souliane@mailoo.org>
parents:
828
diff
changeset
|
399 |
1359
83127a4c89ce
plugins room_game, quiz, radiocol, tarot: use JID instead of unicode in many methods + class attributes
souliane <souliane@mailoo.org>
parents:
1257
diff
changeset
|
400 room_jid = session_data['room_jid'] |
83127a4c89ce
plugins room_game, quiz, radiocol, tarot: use JID instead of unicode in many methods + class attributes
souliane <souliane@mailoo.org>
parents:
1257
diff
changeset
|
401 referee_jid = self.games[room_jid]['referee'] |
83127a4c89ce
plugins room_game, quiz, radiocol, tarot: use JID instead of unicode in many methods + class attributes
souliane <souliane@mailoo.org>
parents:
1257
diff
changeset
|
402 player = self.host.plugins["XEP-0045"].getRoomNick(room_jid, profile) |
878
36c6495d86b0
plugin card_game: update to use the new XMLUI mechanism:
souliane <souliane@mailoo.org>
parents:
828
diff
changeset
|
403 data = xml_tools.XMLUIResult2DataFormResult(raw_data) |
36c6495d86b0
plugin card_game: update to use the new XMLUI mechanism:
souliane <souliane@mailoo.org>
parents:
828
diff
changeset
|
404 contrat = data['contrat'] |
1409
3265a2639182
massive (preventive) addition of 'u' (unicode) before the strings passed to logging functions
souliane <souliane@mailoo.org>
parents:
1396
diff
changeset
|
405 log.debug(_(u'contrat [%(contrat)s] choosed by %(profile)s') % {'contrat': contrat, 'profile': profile}) |
1359
83127a4c89ce
plugins room_game, quiz, radiocol, tarot: use JID instead of unicode in many methods + class attributes
souliane <souliane@mailoo.org>
parents:
1257
diff
changeset
|
406 d = self.send(referee_jid, ('', 'contrat_choosed'), {'player': player}, content=contrat, profile=profile) |
878
36c6495d86b0
plugin card_game: update to use the new XMLUI mechanism:
souliane <souliane@mailoo.org>
parents:
828
diff
changeset
|
407 d.addCallback(lambda ignore: {}) |
36c6495d86b0
plugin card_game: update to use the new XMLUI mechanism:
souliane <souliane@mailoo.org>
parents:
828
diff
changeset
|
408 del self._sessions[raw_data["session_id"]] |
36c6495d86b0
plugin card_game: update to use the new XMLUI mechanism:
souliane <souliane@mailoo.org>
parents:
828
diff
changeset
|
409 return d |
36c6495d86b0
plugin card_game: update to use the new XMLUI mechanism:
souliane <souliane@mailoo.org>
parents:
828
diff
changeset
|
410 |
36c6495d86b0
plugin card_game: update to use the new XMLUI mechanism:
souliane <souliane@mailoo.org>
parents:
828
diff
changeset
|
411 def _scoreShowed(self, raw_data, profile): |
36c6495d86b0
plugin card_game: update to use the new XMLUI mechanism:
souliane <souliane@mailoo.org>
parents:
828
diff
changeset
|
412 """Will be called when the player closes the score dialog |
36c6495d86b0
plugin card_game: update to use the new XMLUI mechanism:
souliane <souliane@mailoo.org>
parents:
828
diff
changeset
|
413 @param raw_data: nothing to retrieve from here but the session id |
36c6495d86b0
plugin card_game: update to use the new XMLUI mechanism:
souliane <souliane@mailoo.org>
parents:
828
diff
changeset
|
414 @param profile_key: profile |
36c6495d86b0
plugin card_game: update to use the new XMLUI mechanism:
souliane <souliane@mailoo.org>
parents:
828
diff
changeset
|
415 """ |
36c6495d86b0
plugin card_game: update to use the new XMLUI mechanism:
souliane <souliane@mailoo.org>
parents:
828
diff
changeset
|
416 try: |
36c6495d86b0
plugin card_game: update to use the new XMLUI mechanism:
souliane <souliane@mailoo.org>
parents:
828
diff
changeset
|
417 session_data = self._sessions.profileGet(raw_data["session_id"], profile) |
36c6495d86b0
plugin card_game: update to use the new XMLUI mechanism:
souliane <souliane@mailoo.org>
parents:
828
diff
changeset
|
418 except KeyError: |
993
301b342c697a
core: use of the new core.log module:
Goffi <goffi@goffi.org>
parents:
916
diff
changeset
|
419 log.warning(_("session id doesn't exist, session has probably expired")) |
878
36c6495d86b0
plugin card_game: update to use the new XMLUI mechanism:
souliane <souliane@mailoo.org>
parents:
828
diff
changeset
|
420 # TODO: send error dialog |
36c6495d86b0
plugin card_game: update to use the new XMLUI mechanism:
souliane <souliane@mailoo.org>
parents:
828
diff
changeset
|
421 return defer.succeed({}) |
36c6495d86b0
plugin card_game: update to use the new XMLUI mechanism:
souliane <souliane@mailoo.org>
parents:
828
diff
changeset
|
422 |
36c6495d86b0
plugin card_game: update to use the new XMLUI mechanism:
souliane <souliane@mailoo.org>
parents:
828
diff
changeset
|
423 room_jid_s = session_data['room_jid'].userhost() |
36c6495d86b0
plugin card_game: update to use the new XMLUI mechanism:
souliane <souliane@mailoo.org>
parents:
828
diff
changeset
|
424 # XXX: empty hand means to the frontend "reset the display"... |
36c6495d86b0
plugin card_game: update to use the new XMLUI mechanism:
souliane <souliane@mailoo.org>
parents:
828
diff
changeset
|
425 self.host.bridge.tarotGameNew(room_jid_s, [], profile) |
36c6495d86b0
plugin card_game: update to use the new XMLUI mechanism:
souliane <souliane@mailoo.org>
parents:
828
diff
changeset
|
426 del self._sessions[raw_data["session_id"]] |
36c6495d86b0
plugin card_game: update to use the new XMLUI mechanism:
souliane <souliane@mailoo.org>
parents:
828
diff
changeset
|
427 return defer.succeed({}) |
88 | 428 |
916
1a759096ccbd
core: use of Const for profile_key + replaced '@DEFAULT@' default profile_key by '@NONE@'
Goffi <goffi@goffi.org>
parents:
878
diff
changeset
|
429 def play_cards(self, player, referee, cards, profile_key=C.PROF_KEY_NONE): |
92 | 430 """Must be call by player when the contrat is selected |
431 @param player: player's name | |
432 @param referee: arbiter jid | |
433 @cards: cards played (list of tuples) | |
434 @profile_key: profile | |
435 """ | |
436 profile = self.host.memory.getProfileName(profile_key) | |
437 if not profile: | |
1409
3265a2639182
massive (preventive) addition of 'u' (unicode) before the strings passed to logging functions
souliane <souliane@mailoo.org>
parents:
1396
diff
changeset
|
438 log.error(_(u"profile %s is unknown") % profile_key) |
92 | 439 return |
1409
3265a2639182
massive (preventive) addition of 'u' (unicode) before the strings passed to logging functions
souliane <souliane@mailoo.org>
parents:
1396
diff
changeset
|
440 log.debug(_(u'Cards played by %(profile)s: [%(cards)s]') % {'profile': profile, 'cards': cards}) |
714
ecc5a5b34ee1
plugins (games): add a method to send messages more easily
souliane <souliane@mailoo.org>
parents:
712
diff
changeset
|
441 elem = self.__card_list_to_xml(TarotCard.from_tuples(cards), 'cards_played') |
ecc5a5b34ee1
plugins (games): add a method to send messages more easily
souliane <souliane@mailoo.org>
parents:
712
diff
changeset
|
442 self.send(jid.JID(referee), elem, {'player': player}, profile=profile) |
88 | 443 |
683
75e4f5e2cc65
plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
682
diff
changeset
|
444 def newRound(self, room_jid, profile): |
1359
83127a4c89ce
plugins room_game, quiz, radiocol, tarot: use JID instead of unicode in many methods + class attributes
souliane <souliane@mailoo.org>
parents:
1257
diff
changeset
|
445 game_data = self.games[room_jid] |
91 | 446 players = game_data['players'] |
594
e629371a28d3
Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
447 game_data['first_player'] = None # first player for the current trick |
95 | 448 game_data['contrat'] = None |
683
75e4f5e2cc65
plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
682
diff
changeset
|
449 common_data = {'contrat': None, |
75e4f5e2cc65
plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
682
diff
changeset
|
450 'levees': [], # cards won |
75e4f5e2cc65
plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
682
diff
changeset
|
451 'played': None, # card on the table |
75e4f5e2cc65
plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
682
diff
changeset
|
452 'wait_for_low': None # Used when a player wait for a low card because of excuse |
75e4f5e2cc65
plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
682
diff
changeset
|
453 } |
75e4f5e2cc65
plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
682
diff
changeset
|
454 |
91 | 455 hand = game_data['hand'] = {} |
456 hand_size = game_data['hand_size'] | |
457 chien = game_data['chien'] = [] | |
683
75e4f5e2cc65
plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
682
diff
changeset
|
458 deck = self.deck_ordered[:] |
75e4f5e2cc65
plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
682
diff
changeset
|
459 random.shuffle(deck) |
328 | 460 for i in range(4): |
88 | 461 hand[players[i]] = deck[0:hand_size] |
462 del deck[0:hand_size] | |
92 | 463 chien.extend(deck) |
88 | 464 del(deck[:]) |
683
75e4f5e2cc65
plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
682
diff
changeset
|
465 msg_elts = {} |
88 | 466 for player in players: |
683
75e4f5e2cc65
plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
682
diff
changeset
|
467 msg_elts[player] = self.__card_list_to_xml(hand[player], 'hand') |
75e4f5e2cc65
plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
682
diff
changeset
|
468 |
75e4f5e2cc65
plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
682
diff
changeset
|
469 RoomGame.newRound(self, room_jid, (common_data, msg_elts), profile) |
91 | 470 |
594
e629371a28d3
Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
471 pl_idx = game_data['current_player'] = (game_data['init_player'] + 1) % len(players) # the player after the dealer start |
91 | 472 player = players[pl_idx] |
594
e629371a28d3
Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
473 to_jid = jid.JID(room_jid.userhost() + "/" + player) # FIXME: gof: |
714
ecc5a5b34ee1
plugins (games): add a method to send messages more easily
souliane <souliane@mailoo.org>
parents:
712
diff
changeset
|
474 self.send(to_jid, self.__ask_contrat(), profile=profile) |
587
952322b1d490
Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
572
diff
changeset
|
475 |
717
358018c5c398
plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents:
714
diff
changeset
|
476 def room_game_cmd(self, mess_elt, profile): |
683
75e4f5e2cc65
plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
682
diff
changeset
|
477 """ |
75e4f5e2cc65
plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
682
diff
changeset
|
478 @param mess_elt: instance of twisted.words.xish.domish.Element |
75e4f5e2cc65
plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
682
diff
changeset
|
479 """ |
587
952322b1d490
Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
572
diff
changeset
|
480 from_jid = jid.JID(mess_elt['from']) |
93 | 481 room_jid = jid.JID(from_jid.userhost()) |
1359
83127a4c89ce
plugins room_game, quiz, radiocol, tarot: use JID instead of unicode in many methods + class attributes
souliane <souliane@mailoo.org>
parents:
1257
diff
changeset
|
482 nick = self.host.plugins["XEP-0045"].getRoomNick(room_jid, profile) |
826
71f8e996f765
plugins radiocol_tarot: do not process received messages for MUC users that are actually not playing the game + fix for testing purpose
souliane <souliane@mailoo.org>
parents:
825
diff
changeset
|
483 |
90 | 484 game_elt = mess_elt.firstChildElement() |
1359
83127a4c89ce
plugins room_game, quiz, radiocol, tarot: use JID instead of unicode in many methods + class attributes
souliane <souliane@mailoo.org>
parents:
1257
diff
changeset
|
485 game_data = self.games[room_jid] |
83127a4c89ce
plugins room_game, quiz, radiocol, tarot: use JID instead of unicode in many methods + class attributes
souliane <souliane@mailoo.org>
parents:
1257
diff
changeset
|
486 is_player = self.isPlayer(room_jid, nick) |
717
358018c5c398
plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents:
714
diff
changeset
|
487 if 'players_data' in game_data: |
358018c5c398
plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents:
714
diff
changeset
|
488 players_data = game_data['players_data'] |
587
952322b1d490
Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
572
diff
changeset
|
489 |
92 | 490 for elt in game_elt.elements(): |
826
71f8e996f765
plugins radiocol_tarot: do not process received messages for MUC users that are actually not playing the game + fix for testing purpose
souliane <souliane@mailoo.org>
parents:
825
diff
changeset
|
491 if not is_player and (elt.name not in ('started', 'players')): |
71f8e996f765
plugins radiocol_tarot: do not process received messages for MUC users that are actually not playing the game + fix for testing purpose
souliane <souliane@mailoo.org>
parents:
825
diff
changeset
|
492 continue # user is in the room but not playing |
587
952322b1d490
Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
572
diff
changeset
|
493 |
826
71f8e996f765
plugins radiocol_tarot: do not process received messages for MUC users that are actually not playing the game + fix for testing purpose
souliane <souliane@mailoo.org>
parents:
825
diff
changeset
|
494 if elt.name in ('started', 'players'): # new game created and/or players list updated |
90 | 495 players = [] |
496 for player in elt.elements(): | |
497 players.append(unicode(player)) | |
717
358018c5c398
plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents:
714
diff
changeset
|
498 signal = self.host.bridge.tarotGameStarted if elt.name == 'started' else self.host.bridge.tarotGamePlayers |
358018c5c398
plugins (games): more factorization and flexibility for launching and joining games:
souliane <souliane@mailoo.org>
parents:
714
diff
changeset
|
499 signal(room_jid.userhost(), from_jid.full(), players, profile) |
587
952322b1d490
Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
572
diff
changeset
|
500 |
594
e629371a28d3
Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
501 elif elt.name == 'player_ready': # ready to play |
92 | 502 player = elt['player'] |
1359
83127a4c89ce
plugins room_game, quiz, radiocol, tarot: use JID instead of unicode in many methods + class attributes
souliane <souliane@mailoo.org>
parents:
1257
diff
changeset
|
503 status = self.games[room_jid]['status'] |
83127a4c89ce
plugins room_game, quiz, radiocol, tarot: use JID instead of unicode in many methods + class attributes
souliane <souliane@mailoo.org>
parents:
1257
diff
changeset
|
504 nb_players = len(self.games[room_jid]['players']) |
90 | 505 status[player] = 'ready' |
1409
3265a2639182
massive (preventive) addition of 'u' (unicode) before the strings passed to logging functions
souliane <souliane@mailoo.org>
parents:
1396
diff
changeset
|
506 log.debug(_(u'Player %(player)s is ready to start [status: %(status)s]') % {'player': player, 'status': status}) |
594
e629371a28d3
Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
507 if status.values().count('ready') == nb_players: # everybody is ready, we can start the game |
683
75e4f5e2cc65
plugins radiocol, card_game, quiz: code factorization
souliane <souliane@mailoo.org>
parents:
682
diff
changeset
|
508 self.newRound(room_jid, profile) |
587
952322b1d490
Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
572
diff
changeset
|
509 |
594
e629371a28d3
Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
510 elif elt.name == 'hand': # a new hand has been received |
92 | 511 self.host.bridge.tarotGameNew(room_jid.userhost(), self.__xml_to_list(elt), profile) |
91 | 512 |
594
e629371a28d3
Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
513 elif elt.name == 'contrat': # it's time to choose contrat |
91 | 514 form = data_form.Form.fromElement(elt.firstChildElement()) |
878
36c6495d86b0
plugin card_game: update to use the new XMLUI mechanism:
souliane <souliane@mailoo.org>
parents:
828
diff
changeset
|
515 session_id, session_data = self._sessions.newSession(profile=profile) |
36c6495d86b0
plugin card_game: update to use the new XMLUI mechanism:
souliane <souliane@mailoo.org>
parents:
828
diff
changeset
|
516 session_data["room_jid"] = room_jid |
36c6495d86b0
plugin card_game: update to use the new XMLUI mechanism:
souliane <souliane@mailoo.org>
parents:
828
diff
changeset
|
517 xml_data = xml_tools.dataForm2XMLUI(form, self.__choose_contrat_id, session_id).toXml() |
92 | 518 self.host.bridge.tarotGameChooseContrat(room_jid.userhost(), xml_data, profile) |
587
952322b1d490
Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
572
diff
changeset
|
519 |
92 | 520 elif elt.name == 'contrat_choosed': |
1359
83127a4c89ce
plugins room_game, quiz, radiocol, tarot: use JID instead of unicode in many methods + class attributes
souliane <souliane@mailoo.org>
parents:
1257
diff
changeset
|
521 # TODO: check we receive the contrat from the right person |
83127a4c89ce
plugins room_game, quiz, radiocol, tarot: use JID instead of unicode in many methods + class attributes
souliane <souliane@mailoo.org>
parents:
1257
diff
changeset
|
522 # TODO: use proper XEP-0004 way for answering form |
92 | 523 player = elt['player'] |
524 players_data[player]['contrat'] = unicode(elt) | |
91 | 525 contrats = [players_data[player]['contrat'] for player in game_data['players']] |
526 if contrats.count(None): | |
1359
83127a4c89ce
plugins room_game, quiz, radiocol, tarot: use JID instead of unicode in many methods + class attributes
souliane <souliane@mailoo.org>
parents:
1257
diff
changeset
|
527 # not everybody has choosed his contrat, it's next one turn |
91 | 528 player = self.__next_player(game_data) |
594
e629371a28d3
Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
529 to_jid = jid.JID(room_jid.userhost() + "/" + player) # FIXME: gof: |
714
ecc5a5b34ee1
plugins (games): add a method to send messages more easily
souliane <souliane@mailoo.org>
parents:
712
diff
changeset
|
530 self.send(to_jid, self.__ask_contrat(), profile=profile) |
91 | 531 else: |
532 best_contrat = [None, "Passe"] | |
533 for player in game_data['players']: | |
534 contrat = players_data[player]['contrat'] | |
535 idx_best = self.contrats.index(best_contrat[1]) | |
536 idx_pl = self.contrats.index(contrat) | |
537 if idx_pl > idx_best: | |
538 best_contrat[0] = player | |
539 best_contrat[1] = contrat | |
328 | 540 if best_contrat[1] == "Passe": |
993
301b342c697a
core: use of the new core.log module:
Goffi <goffi@goffi.org>
parents:
916
diff
changeset
|
541 log.debug(_("Everybody is passing, round ended")) |
328 | 542 to_jid = jid.JID(room_jid.userhost()) |
714
ecc5a5b34ee1
plugins (games): add a method to send messages more easily
souliane <souliane@mailoo.org>
parents:
712
diff
changeset
|
543 self.send(to_jid, self.__give_scores(*self.__draw_game(game_data)), profile=profile) |
594
e629371a28d3
Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
544 game_data['init_player'] = (game_data['init_player'] + 1) % len(game_data['players']) # we change the dealer |
328 | 545 for player in game_data['players']: |
546 game_data['status'][player] = "init" | |
547 return | |
1409
3265a2639182
massive (preventive) addition of 'u' (unicode) before the strings passed to logging functions
souliane <souliane@mailoo.org>
parents:
1396
diff
changeset
|
548 log.debug(_(u"%(player)s win the bid with %(contrat)s") % {'player': best_contrat[0], 'contrat': best_contrat[1]}) |
95 | 549 game_data['contrat'] = best_contrat[1] |
587
952322b1d490
Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
572
diff
changeset
|
550 |
98
dd556233a1b1
Tarot Plugin: Garde Sans and Garde Contre are now managed
Goffi <goffi@goffi.org>
parents:
96
diff
changeset
|
551 if game_data['contrat'] == "Garde Sans" or game_data['contrat'] == "Garde Contre": |
dd556233a1b1
Tarot Plugin: Garde Sans and Garde Contre are now managed
Goffi <goffi@goffi.org>
parents:
96
diff
changeset
|
552 self.__start_play(room_jid, game_data, profile) |
dd556233a1b1
Tarot Plugin: Garde Sans and Garde Contre are now managed
Goffi <goffi@goffi.org>
parents:
96
diff
changeset
|
553 game_data['attaquant'] = best_contrat[0] |
dd556233a1b1
Tarot Plugin: Garde Sans and Garde Contre are now managed
Goffi <goffi@goffi.org>
parents:
96
diff
changeset
|
554 else: |
1359
83127a4c89ce
plugins room_game, quiz, radiocol, tarot: use JID instead of unicode in many methods + class attributes
souliane <souliane@mailoo.org>
parents:
1257
diff
changeset
|
555 # Time to show the chien to everybody |
594
e629371a28d3
Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
556 to_jid = jid.JID(room_jid.userhost()) # FIXME: gof: |
714
ecc5a5b34ee1
plugins (games): add a method to send messages more easily
souliane <souliane@mailoo.org>
parents:
712
diff
changeset
|
557 elem = self.__card_list_to_xml(game_data['chien'], 'chien') |
ecc5a5b34ee1
plugins (games): add a method to send messages more easily
souliane <souliane@mailoo.org>
parents:
712
diff
changeset
|
558 self.send(to_jid, elem, {'attaquant': best_contrat[0]}, profile=profile) |
1359
83127a4c89ce
plugins room_game, quiz, radiocol, tarot: use JID instead of unicode in many methods + class attributes
souliane <souliane@mailoo.org>
parents:
1257
diff
changeset
|
559 # the attacker (attaquant) get the chien |
98
dd556233a1b1
Tarot Plugin: Garde Sans and Garde Contre are now managed
Goffi <goffi@goffi.org>
parents:
96
diff
changeset
|
560 game_data['hand'][best_contrat[0]].extend(game_data['chien']) |
dd556233a1b1
Tarot Plugin: Garde Sans and Garde Contre are now managed
Goffi <goffi@goffi.org>
parents:
96
diff
changeset
|
561 del game_data['chien'][:] |
587
952322b1d490
Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
572
diff
changeset
|
562 |
98
dd556233a1b1
Tarot Plugin: Garde Sans and Garde Contre are now managed
Goffi <goffi@goffi.org>
parents:
96
diff
changeset
|
563 if game_data['contrat'] == "Garde Sans": |
1359
83127a4c89ce
plugins room_game, quiz, radiocol, tarot: use JID instead of unicode in many methods + class attributes
souliane <souliane@mailoo.org>
parents:
1257
diff
changeset
|
564 # The chien go into attaquant's (attacker) levees |
98
dd556233a1b1
Tarot Plugin: Garde Sans and Garde Contre are now managed
Goffi <goffi@goffi.org>
parents:
96
diff
changeset
|
565 players_data[best_contrat[0]]['levees'].extend(game_data['chien']) |
dd556233a1b1
Tarot Plugin: Garde Sans and Garde Contre are now managed
Goffi <goffi@goffi.org>
parents:
96
diff
changeset
|
566 del game_data['chien'][:] |
91 | 567 |
594
e629371a28d3
Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
568 elif elt.name == 'chien': # we have received the chien |
993
301b342c697a
core: use of the new core.log module:
Goffi <goffi@goffi.org>
parents:
916
diff
changeset
|
569 log.debug(_("tarot: chien received")) |
594
e629371a28d3
Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
570 data = {"attaquant": elt['attaquant']} |
92 | 571 game_data['stage'] = "ecart" |
572 game_data['attaquant'] = elt['attaquant'] | |
573 self.host.bridge.tarotGameShowCards(room_jid.userhost(), "chien", self.__xml_to_list(elt), data, profile) | |
574 | |
575 elif elt.name == 'cards_played': | |
576 if game_data['stage'] == "ecart": | |
1359
83127a4c89ce
plugins room_game, quiz, radiocol, tarot: use JID instead of unicode in many methods + class attributes
souliane <souliane@mailoo.org>
parents:
1257
diff
changeset
|
577 # TODO: show atouts (trumps) if player put some in écart |
594
e629371a28d3
Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
578 assert (game_data['attaquant'] == elt['player']) # TODO: throw an xml error here |
141
8c80d4dec7a8
mover Card class to tools/games and renamed it in TarotCard
Goffi <goffi@goffi.org>
parents:
134
diff
changeset
|
579 list_cards = TarotCard.from_tuples(self.__xml_to_list(elt)) |
1359
83127a4c89ce
plugins room_game, quiz, radiocol, tarot: use JID instead of unicode in many methods + class attributes
souliane <souliane@mailoo.org>
parents:
1257
diff
changeset
|
580 # we now check validity of card |
99
63c9067a1499
Tarot game: invalid cards management
Goffi <goffi@goffi.org>
parents:
98
diff
changeset
|
581 invalid_cards = self.__invalid_cards(game_data, list_cards) |
63c9067a1499
Tarot game: invalid cards management
Goffi <goffi@goffi.org>
parents:
98
diff
changeset
|
582 if invalid_cards: |
714
ecc5a5b34ee1
plugins (games): add a method to send messages more easily
souliane <souliane@mailoo.org>
parents:
712
diff
changeset
|
583 elem = self.__invalid_cards_elt(list_cards, invalid_cards, game_data['stage']) |
ecc5a5b34ee1
plugins (games): add a method to send messages more easily
souliane <souliane@mailoo.org>
parents:
712
diff
changeset
|
584 self.send(jid.JID(room_jid.userhost() + '/' + elt['player']), elem, profile=profile) |
99
63c9067a1499
Tarot game: invalid cards management
Goffi <goffi@goffi.org>
parents:
98
diff
changeset
|
585 return |
63c9067a1499
Tarot game: invalid cards management
Goffi <goffi@goffi.org>
parents:
98
diff
changeset
|
586 |
1359
83127a4c89ce
plugins room_game, quiz, radiocol, tarot: use JID instead of unicode in many methods + class attributes
souliane <souliane@mailoo.org>
parents:
1257
diff
changeset
|
587 # FIXME: gof: manage Garde Sans & Garde Contre cases |
594
e629371a28d3
Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
588 players_data[elt['player']]['levees'].extend(list_cards) # we add the chien to attaquant's levées |
95 | 589 for card in list_cards: |
590 game_data['hand'][elt['player']].remove(card) | |
587
952322b1d490
Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
572
diff
changeset
|
591 |
98
dd556233a1b1
Tarot Plugin: Garde Sans and Garde Contre are now managed
Goffi <goffi@goffi.org>
parents:
96
diff
changeset
|
592 self.__start_play(room_jid, game_data, profile) |
587
952322b1d490
Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
572
diff
changeset
|
593 |
93 | 594 elif game_data['stage'] == "play": |
595 current_player = game_data['players'][game_data['current_player']] | |
141
8c80d4dec7a8
mover Card class to tools/games and renamed it in TarotCard
Goffi <goffi@goffi.org>
parents:
134
diff
changeset
|
596 cards = TarotCard.from_tuples(self.__xml_to_list(elt)) |
587
952322b1d490
Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
572
diff
changeset
|
597 |
94 | 598 if mess_elt['type'] == 'groupchat': |
599 self.host.bridge.tarotGameCardsPlayed(room_jid.userhost(), elt['player'], self.__xml_to_list(elt), profile) | |
600 else: | |
1359
83127a4c89ce
plugins room_game, quiz, radiocol, tarot: use JID instead of unicode in many methods + class attributes
souliane <souliane@mailoo.org>
parents:
1257
diff
changeset
|
601 # we first check validity of card |
99
63c9067a1499
Tarot game: invalid cards management
Goffi <goffi@goffi.org>
parents:
98
diff
changeset
|
602 invalid_cards = self.__invalid_cards(game_data, cards) |
63c9067a1499
Tarot game: invalid cards management
Goffi <goffi@goffi.org>
parents:
98
diff
changeset
|
603 if invalid_cards: |
714
ecc5a5b34ee1
plugins (games): add a method to send messages more easily
souliane <souliane@mailoo.org>
parents:
712
diff
changeset
|
604 elem = self.__invalid_cards_elt(cards, invalid_cards, game_data['stage']) |
ecc5a5b34ee1
plugins (games): add a method to send messages more easily
souliane <souliane@mailoo.org>
parents:
712
diff
changeset
|
605 self.send(jid.JID(room_jid.userhost() + '/' + current_player), elem, profile=profile) |
99
63c9067a1499
Tarot game: invalid cards management
Goffi <goffi@goffi.org>
parents:
98
diff
changeset
|
606 return |
1359
83127a4c89ce
plugins room_game, quiz, radiocol, tarot: use JID instead of unicode in many methods + class attributes
souliane <souliane@mailoo.org>
parents:
1257
diff
changeset
|
607 # the card played is ok, we forward it to everybody |
83127a4c89ce
plugins room_game, quiz, radiocol, tarot: use JID instead of unicode in many methods + class attributes
souliane <souliane@mailoo.org>
parents:
1257
diff
changeset
|
608 # first we remove it from the hand and put in on the table |
93 | 609 game_data['hand'][current_player].remove(cards[0]) |
98
dd556233a1b1
Tarot Plugin: Garde Sans and Garde Contre are now managed
Goffi <goffi@goffi.org>
parents:
96
diff
changeset
|
610 players_data[current_player]['played'] = cards[0] |
93 | 611 |
1359
83127a4c89ce
plugins room_game, quiz, radiocol, tarot: use JID instead of unicode in many methods + class attributes
souliane <souliane@mailoo.org>
parents:
1257
diff
changeset
|
612 # then we forward the message |
714
ecc5a5b34ee1
plugins (games): add a method to send messages more easily
souliane <souliane@mailoo.org>
parents:
712
diff
changeset
|
613 self.send(room_jid, elt, profile=profile) |
587
952322b1d490
Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
572
diff
changeset
|
614 |
1359
83127a4c89ce
plugins room_game, quiz, radiocol, tarot: use JID instead of unicode in many methods + class attributes
souliane <souliane@mailoo.org>
parents:
1257
diff
changeset
|
615 # Did everybody played ? |
94 | 616 played = [players_data[player]['played'] for player in game_data['players']] |
95 | 617 if all(played): |
1359
83127a4c89ce
plugins room_game, quiz, radiocol, tarot: use JID instead of unicode in many methods + class attributes
souliane <souliane@mailoo.org>
parents:
1257
diff
changeset
|
618 # everybody has played |
94 | 619 winner = self.__winner(game_data) |
1409
3265a2639182
massive (preventive) addition of 'u' (unicode) before the strings passed to logging functions
souliane <souliane@mailoo.org>
parents:
1396
diff
changeset
|
620 log.debug(_(u'The winner of this trick is %s') % winner) |
1359
83127a4c89ce
plugins room_game, quiz, radiocol, tarot: use JID instead of unicode in many methods + class attributes
souliane <souliane@mailoo.org>
parents:
1257
diff
changeset
|
621 # the winner win the trick |
94 | 622 self.__excuse_hack(game_data, played, winner) |
623 players_data[elt['player']]['levees'].extend(played) | |
1359
83127a4c89ce
plugins room_game, quiz, radiocol, tarot: use JID instead of unicode in many methods + class attributes
souliane <souliane@mailoo.org>
parents:
1257
diff
changeset
|
624 # nothing left on the table |
94 | 625 for player in game_data['players']: |
626 players_data[player]['played'] = None | |
627 if len(game_data['hand'][current_player]) == 0: | |
1359
83127a4c89ce
plugins room_game, quiz, radiocol, tarot: use JID instead of unicode in many methods + class attributes
souliane <souliane@mailoo.org>
parents:
1257
diff
changeset
|
628 # no card left: the game is finished |
714
ecc5a5b34ee1
plugins (games): add a method to send messages more easily
souliane <souliane@mailoo.org>
parents:
712
diff
changeset
|
629 elem = self.__give_scores(*self.__calculate_scores(game_data)) |
ecc5a5b34ee1
plugins (games): add a method to send messages more easily
souliane <souliane@mailoo.org>
parents:
712
diff
changeset
|
630 self.send(room_jid, elem, profile=profile) |
594
e629371a28d3
Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
631 game_data['init_player'] = (game_data['init_player'] + 1) % len(game_data['players']) # we change the dealer |
328 | 632 for player in game_data['players']: |
633 game_data['status'][player] = "init" | |
94 | 634 return |
1359
83127a4c89ce
plugins room_game, quiz, radiocol, tarot: use JID instead of unicode in many methods + class attributes
souliane <souliane@mailoo.org>
parents:
1257
diff
changeset
|
635 # next player is the winner |
94 | 636 next_player = game_data['first_player'] = self.__next_player(game_data, winner) |
637 else: | |
638 next_player = self.__next_player(game_data) | |
639 | |
1359
83127a4c89ce
plugins room_game, quiz, radiocol, tarot: use JID instead of unicode in many methods + class attributes
souliane <souliane@mailoo.org>
parents:
1257
diff
changeset
|
640 # finally, we tell to the next player to play |
594
e629371a28d3
Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
641 to_jid = jid.JID(room_jid.userhost() + "/" + next_player) |
714
ecc5a5b34ee1
plugins (games): add a method to send messages more easily
souliane <souliane@mailoo.org>
parents:
712
diff
changeset
|
642 self.send(to_jid, 'your_turn', profile=profile) |
93 | 643 |
92 | 644 elif elt.name == 'your_turn': |
645 self.host.bridge.tarotGameYourTurn(room_jid.userhost(), profile) | |
91 | 646 |
95 | 647 elif elt.name == 'score': |
594
e629371a28d3
Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
648 form_elt = elt.elements(name='x', uri='jabber:x:data').next() |
95 | 649 winners = [] |
650 loosers = [] | |
563 | 651 for winner in elt.elements(name='winner', uri=NS_CG): |
95 | 652 winners.append(unicode(winner)) |
563 | 653 for looser in elt.elements(name='looser', uri=NS_CG): |
95 | 654 loosers.append(unicode(looser)) |
655 form = data_form.Form.fromElement(form_elt) | |
878
36c6495d86b0
plugin card_game: update to use the new XMLUI mechanism:
souliane <souliane@mailoo.org>
parents:
828
diff
changeset
|
656 session_id, session_data = self._sessions.newSession(profile=profile) |
36c6495d86b0
plugin card_game: update to use the new XMLUI mechanism:
souliane <souliane@mailoo.org>
parents:
828
diff
changeset
|
657 session_data["room_jid"] = room_jid |
36c6495d86b0
plugin card_game: update to use the new XMLUI mechanism:
souliane <souliane@mailoo.org>
parents:
828
diff
changeset
|
658 xml_data = xml_tools.dataForm2XMLUI(form, self.__score_id, session_id).toXml() |
95 | 659 self.host.bridge.tarotGameScore(room_jid.userhost(), xml_data, winners, loosers, profile) |
99
63c9067a1499
Tarot game: invalid cards management
Goffi <goffi@goffi.org>
parents:
98
diff
changeset
|
660 elif elt.name == 'error': |
63c9067a1499
Tarot game: invalid cards management
Goffi <goffi@goffi.org>
parents:
98
diff
changeset
|
661 if elt['type'] == 'invalid_cards': |
594
e629371a28d3
Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
662 played_cards = self.__xml_to_list(elt.elements(name='played', uri=NS_CG).next()) |
e629371a28d3
Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
663 invalid_cards = self.__xml_to_list(elt.elements(name='invalid', uri=NS_CG).next()) |
99
63c9067a1499
Tarot game: invalid cards management
Goffi <goffi@goffi.org>
parents:
98
diff
changeset
|
664 self.host.bridge.tarotGameInvalidCards(room_jid.userhost(), elt['phase'], played_cards, invalid_cards, profile) |
63c9067a1499
Tarot game: invalid cards management
Goffi <goffi@goffi.org>
parents:
98
diff
changeset
|
665 else: |
1409
3265a2639182
massive (preventive) addition of 'u' (unicode) before the strings passed to logging functions
souliane <souliane@mailoo.org>
parents:
1396
diff
changeset
|
666 log.error(_(u'Unmanaged error type: %s') % elt['type']) |
99
63c9067a1499
Tarot game: invalid cards management
Goffi <goffi@goffi.org>
parents:
98
diff
changeset
|
667 else: |
1409
3265a2639182
massive (preventive) addition of 'u' (unicode) before the strings passed to logging functions
souliane <souliane@mailoo.org>
parents:
1396
diff
changeset
|
668 log.error(_(u'Unmanaged card game element: %s') % elt.name) |
825
e3f4d80f987d
plugins room_games, radiocol: better synchronization after a user joins a running game
souliane <souliane@mailoo.org>
parents:
811
diff
changeset
|
669 |
1359
83127a4c89ce
plugins room_game, quiz, radiocol, tarot: use JID instead of unicode in many methods + class attributes
souliane <souliane@mailoo.org>
parents:
1257
diff
changeset
|
670 def getSyncDataForPlayer(self, room_jid, nick): |
828
8f335c03eebb
plugins room_games, radiocol, libervia: small changes like refactorization to ease the maintenance
souliane <souliane@mailoo.org>
parents:
826
diff
changeset
|
671 return [] |