Mercurial > libervia-backend
annotate src/plugins/plugin_misc_tarot.py @ 594:e629371a28d3
Fix pep8 support in src/plugins.
author | Emmanuel Gil Peyrot <linkmauve@linkmauve.fr> |
---|---|
date | Fri, 18 Jan 2013 17:55:35 +0100 |
parents | beaf6bec2fcd |
children | 84a6e83157c2 |
rev | line source |
---|---|
88 | 1 #!/usr/bin/python |
2 # -*- coding: utf-8 -*- | |
3 | |
4 """ | |
361 | 5 SAT plugin for managing French Tarot game |
572 | 6 Copyright (C) 2009, 2010, 2011, 2012, 2013 Jérôme Poisson (goffi@goffi.org) |
88 | 7 |
8 This program is free software: you can redistribute it and/or modify | |
480
2a072735e459
Licence modification: the full project is now under AGPL v3+ instead of GPL v3+
Goffi <goffi@goffi.org>
parents:
459
diff
changeset
|
9 it under the terms of the GNU Affero General Public License as published by |
88 | 10 the Free Software Foundation, either version 3 of the License, or |
11 (at your option) any later version. | |
12 | |
13 This program is distributed in the hope that it will be useful, | |
14 but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
480
2a072735e459
Licence modification: the full project is now under AGPL v3+ instead of GPL v3+
Goffi <goffi@goffi.org>
parents:
459
diff
changeset
|
16 GNU Affero General Public License for more details. |
88 | 17 |
480
2a072735e459
Licence modification: the full project is now under AGPL v3+ instead of GPL v3+
Goffi <goffi@goffi.org>
parents:
459
diff
changeset
|
18 You should have received a copy of the GNU Affero General Public License |
88 | 19 along with this program. If not, see <http://www.gnu.org/licenses/>. |
20 """ | |
21 | |
22 from logging import debug, info, warning, error | |
23 from twisted.words.xish import domish | |
563 | 24 from twisted.words.protocols.jabber import jid |
88 | 25 import random |
26 | |
27 from zope.interface import implements | |
28 | |
91 | 29 from wokkel import disco, iwokkel, data_form |
223 | 30 from sat.tools.xml_tools import dataForm2xml |
31 from sat.tools.games import TarotCard | |
88 | 32 |
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 from time import time |
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
|
34 |
88 | 35 try: |
36 from twisted.words.protocols.xmlstream import XMPPHandler | |
37 except ImportError: | |
38 from wokkel.subprotocols import XMPPHandler | |
39 | |
90 | 40 MESSAGE = '/message' |
41 NS_CG = 'http://www.goffi.org/protocol/card_game' | |
42 CG_TAG = 'card_game' | |
43 CG_REQUEST = MESSAGE + '/' + CG_TAG + '[@xmlns="' + NS_CG + '"]' | |
88 | 44 |
45 PLUGIN_INFO = { | |
594
e629371a28d3
Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
46 "name": "Tarot cards plugin", |
e629371a28d3
Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
47 "import_name": "Tarot", |
e629371a28d3
Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
48 "type": "Misc", |
e629371a28d3
Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
49 "protocols": [], |
e629371a28d3
Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
50 "dependencies": ["XEP-0045", "XEP-0249"], |
e629371a28d3
Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
51 "main": "Tarot", |
e629371a28d3
Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
52 "handler": "yes", |
e629371a28d3
Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
53 "description": _("""Implementation of Tarot card game""") |
88 | 54 } |
55 | |
94 | 56 |
588
beaf6bec2fcd
Remove every old-style class.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
587
diff
changeset
|
57 class Tarot(object): |
88 | 58 |
59 def __init__(self, host): | |
60 info(_("Plugin Tarot initialization")) | |
61 self.host = host | |
594
e629371a28d3
Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
62 self.games = {} |
e629371a28d3
Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
63 self.waiting_inv = {} # Invitation waiting for people to join to launch a game |
91 | 64 self.contrats = [_('Passe'), _('Petite'), _('Garde'), _('Garde Sans'), _('Garde Contre')] |
594
e629371a28d3
Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
65 host.bridge.addMethod("tarotGameLaunch", ".plugin", in_sign='ass', out_sign='', method=self.launchGame) # args: room_jid, players, profile |
e629371a28d3
Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
66 host.bridge.addMethod("tarotGameCreate", ".plugin", in_sign='sass', out_sign='', method=self.createGame) # args: room_jid, players, profile |
e629371a28d3
Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
67 host.bridge.addMethod("tarotGameReady", ".plugin", in_sign='sss', out_sign='', method=self.newPlayerReady) # args: player, referee, profile |
e629371a28d3
Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
68 host.bridge.addMethod("tarotGameContratChoosed", ".plugin", in_sign='ssss', out_sign='', method=self.contratChoosed) # args: player, referee, contrat, profile |
e629371a28d3
Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
69 host.bridge.addMethod("tarotGamePlayCards", ".plugin", in_sign='ssa(ss)s', out_sign='', method=self.play_cards) # args: player, referee, cards, profile |
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 |
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
|
78 host.trigger.add("MUC user joined", self.userJoinedTrigger) |
88 | 79 self.deck_ordered = [] |
594
e629371a28d3
Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
80 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
|
81 self.deck_ordered.append(TarotCard(("atout", value))) |
92 | 82 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
|
83 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
|
84 self.deck_ordered.append(TarotCard((suit, value))) |
88 | 85 |
92 | 86 def createGameElt(self, to_jid, type="normal"): |
87 type = "normal" if to_jid.resource else "groupchat" | |
594
e629371a28d3
Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
88 elt = domish.Element((None, 'message')) |
90 | 89 elt["to"] = to_jid.full() |
92 | 90 elt["type"] = type |
90 | 91 elt.addElement((NS_CG, CG_TAG)) |
92 return elt | |
93 | |
98
dd556233a1b1
Tarot Plugin: Garde Sans and Garde Contre are now managed
Goffi <goffi@goffi.org>
parents:
96
diff
changeset
|
94 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
|
95 """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
|
96 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
|
97 for card in cards_list: |
594
e629371a28d3
Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
98 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
|
99 card_elt['suit'] = card.suit |
dd556233a1b1
Tarot Plugin: Garde Sans and Garde Contre are now managed
Goffi <goffi@goffi.org>
parents:
96
diff
changeset
|
100 card_elt['value'] = card.value |
587
952322b1d490
Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
572
diff
changeset
|
101 cards_list_elt.addChild(card_elt) |
92 | 102 return cards_list_elt |
90 | 103 |
92 | 104 def __xml_to_list(self, cards_list_elt): |
105 """Convert a domish element with cards to a list of tuples""" | |
106 cards_list = [] | |
107 for card in cards_list_elt.elements(): | |
108 cards_list.append((card['suit'], card['value'])) | |
109 return cards_list | |
587
952322b1d490
Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
572
diff
changeset
|
110 |
90 | 111 def __create_started_elt(self, players): |
112 """Create a game_started domish element""" | |
594
e629371a28d3
Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
113 started_elt = domish.Element((None, 'started')) |
90 | 114 idx = 0 |
115 for player in players: | |
594
e629371a28d3
Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
116 player_elt = domish.Element((None, 'player')) |
90 | 117 player_elt.addContent(player) |
118 player_elt['index'] = str(idx) | |
594
e629371a28d3
Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
119 idx += 1 |
90 | 120 started_elt.addChild(player_elt) |
121 return started_elt | |
122 | |
91 | 123 def __ask_contrat(self): |
124 """Create a element for asking contrat""" | |
594
e629371a28d3
Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
125 contrat_elt = domish.Element((None, 'contrat')) |
91 | 126 form = data_form.Form('form', title=_('contrat selection')) |
127 field = data_form.Field('list-single', 'contrat', options=map(data_form.Option, self.contrats), required=True) | |
128 form.addField(field) | |
129 contrat_elt.addChild(form.toElement()) | |
130 return contrat_elt | |
131 | |
95 | 132 def __give_scores(self, scores, winners, loosers): |
133 """Create an element to give scores | |
134 @param scores: unicode (can contain line feed) | |
135 @param winners: list of unicode nicks of winners | |
136 @param loosers: list of unicode nicks of loosers""" | |
137 | |
594
e629371a28d3
Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
138 score_elt = domish.Element((None, 'score')) |
95 | 139 form = data_form.Form('form', title=_('scores')) |
140 for line in scores.split('\n'): | |
594
e629371a28d3
Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
141 field = data_form.Field('fixed', value=line) |
95 | 142 form.addField(field) |
143 score_elt.addChild(form.toElement()) | |
144 for winner in winners: | |
594
e629371a28d3
Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
145 winner_elt = domish.Element((None, 'winner')) |
95 | 146 winner_elt.addContent(winner) |
147 score_elt.addChild(winner_elt) | |
148 for looser in loosers: | |
594
e629371a28d3
Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
149 looser_elt = domish.Element((None, 'looser')) |
95 | 150 looser_elt.addContent(looser) |
151 score_elt.addChild(looser_elt) | |
152 return score_elt | |
153 | |
99
63c9067a1499
Tarot game: invalid cards management
Goffi <goffi@goffi.org>
parents:
98
diff
changeset
|
154 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
|
155 """Create a element for invalid_cards error |
63c9067a1499
Tarot game: invalid cards management
Goffi <goffi@goffi.org>
parents:
98
diff
changeset
|
156 @param list_cards: list of Card |
63c9067a1499
Tarot game: invalid cards management
Goffi <goffi@goffi.org>
parents:
98
diff
changeset
|
157 @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
|
158 error_elt = domish.Element((None, 'error')) |
99
63c9067a1499
Tarot game: invalid cards management
Goffi <goffi@goffi.org>
parents:
98
diff
changeset
|
159 played_elt = self.__card_list_to_xml(played_cards, 'played') |
63c9067a1499
Tarot game: invalid cards management
Goffi <goffi@goffi.org>
parents:
98
diff
changeset
|
160 invalid_elt = self.__card_list_to_xml(invalid_cards, 'invalid') |
63c9067a1499
Tarot game: invalid cards management
Goffi <goffi@goffi.org>
parents:
98
diff
changeset
|
161 error_elt['type'] = 'invalid_cards' |
63c9067a1499
Tarot game: invalid cards management
Goffi <goffi@goffi.org>
parents:
98
diff
changeset
|
162 error_elt['phase'] = game_phase |
63c9067a1499
Tarot game: invalid cards management
Goffi <goffi@goffi.org>
parents:
98
diff
changeset
|
163 error_elt.addChild(played_elt) |
63c9067a1499
Tarot game: invalid cards management
Goffi <goffi@goffi.org>
parents:
98
diff
changeset
|
164 error_elt.addChild(invalid_elt) |
63c9067a1499
Tarot game: invalid cards management
Goffi <goffi@goffi.org>
parents:
98
diff
changeset
|
165 return error_elt |
587
952322b1d490
Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
572
diff
changeset
|
166 |
594
e629371a28d3
Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
167 def __next_player(self, game_data, next_pl=None): |
94 | 168 """Increment player number & return player name |
169 @param next_pl: if given, then next_player is forced to this one | |
170 """ | |
171 if next_pl: | |
172 game_data['current_player'] = game_data['players'].index(next_pl) | |
173 return next_pl | |
174 else: | |
175 pl_idx = game_data['current_player'] = (game_data['current_player'] + 1) % len(game_data['players']) | |
176 return game_data['players'][pl_idx] | |
177 | |
178 def __winner(self, game_data): | |
179 """give the nick of the player who win this trick""" | |
180 players_data = game_data['players_data'] | |
181 first = game_data['first_player'] | |
587
952322b1d490
Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
572
diff
changeset
|
182 first_idx = game_data['players'].index(first) |
94 | 183 suit_asked = None |
184 strongest = None | |
185 winner = None | |
186 for idx in [(first_idx + i) % 4 for i in range(4)]: | |
187 player = game_data['players'][idx] | |
188 card = players_data[player]['played'] | |
189 if card.value == "excuse": | |
190 continue | |
594
e629371a28d3
Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
191 if suit_asked is None: |
94 | 192 suit_asked = card.suit |
193 if (card.suit == suit_asked or card.suit == "atout") and card > strongest: | |
194 strongest = card | |
195 winner = player | |
95 | 196 assert winner |
94 | 197 return winner |
198 | |
199 def __excuse_hack(self, game_data, played, winner): | |
95 | 200 """give a low card to other team and keep excuse if trick is lost |
201 @param game_data: data of the game | |
202 @param played: cards currently on the table | |
203 @param winner: nick of the trick winner""" | |
94 | 204 #TODO: manage the case where excuse is played on the last trick (and lost) |
205 players_data = game_data['players_data'] | |
594
e629371a28d3
Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
206 excuse = TarotCard(("atout", "excuse")) |
95 | 207 |
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
|
208 #we first check if the Excuse was already played |
95 | 209 #and if somebody is waiting for a card |
94 | 210 for player in game_data['players']: |
211 if players_data[player]['wait_for_low']: | |
212 #the excuse owner has to give a card to somebody | |
213 if winner == player: | |
214 #the excuse owner win the trick, we check if we have something to give | |
215 for card in played: | |
216 if card.points == 0.5: | |
217 pl_waiting = players_data[player]['wait_for_low'] | |
218 played.remove(card) | |
219 players_data[pl_waiting]['levees'].append(card) | |
594
e629371a28d3
Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
220 debug(_('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 | 221 return |
94 | 222 return |
223 | |
224 if not excuse in played: | |
95 | 225 #the Excuse is not on the table, nothing to do |
94 | 226 return |
587
952322b1d490
Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
572
diff
changeset
|
227 |
594
e629371a28d3
Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
228 excuse_player = None # Who has played the Excuse ? |
94 | 229 for player in game_data['players']: |
230 if players_data[player]['played'] == excuse: | |
231 excuse_player = player | |
232 break | |
587
952322b1d490
Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
572
diff
changeset
|
233 |
94 | 234 if excuse_player == winner: |
594
e629371a28d3
Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
235 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
|
236 |
94 | 237 #first we remove the excuse from played cards |
238 played.remove(excuse) | |
587
952322b1d490
Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
572
diff
changeset
|
239 #then we give it back to the original owner |
94 | 240 owner_levees = players_data[excuse_player]['levees'] |
241 owner_levees.append(excuse) | |
242 #finally we give a low card to the trick winner | |
243 low_card = None | |
95 | 244 #We look backward in cards won by the Excuse owner to |
245 #find a low value card | |
594
e629371a28d3
Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
246 for card_idx in range(len(owner_levees) - 1, -1, -1): |
94 | 247 if owner_levees[card_idx].points == 0.5: |
248 low_card = owner_levees[card_idx] | |
249 del owner_levees[card_idx] | |
250 players_data[winner]['levees'].append(low_card) | |
594
e629371a28d3
Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
251 debug(_('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 | 252 break |
594
e629371a28d3
Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
253 if not low_card: # The player has no low card yet |
94 | 254 #TODO: manage case when player never win a trick with low card |
255 players_data[excuse_player]['wait_for_low'] = winner | |
594
e629371a28d3
Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
256 debug(_("%(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 | 257 |
328 | 258 def __draw_game(self, game_data): |
259 """The game is draw, no score change | |
260 @param game_data: data of the game | |
261 @return: tuple with (string victory message, list of winners, list of loosers)""" | |
262 players_data = game_data['players_data'] | |
263 scores_str = _('Draw game') | |
594
e629371a28d3
Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
264 scores_str += '\n' |
328 | 265 for player in game_data['players']: |
594
e629371a28d3
Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
266 scores_str += _("\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']} |
328 | 267 debug(scores_str) |
587
952322b1d490
Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
572
diff
changeset
|
268 |
328 | 269 return (scores_str, [], []) |
270 | |
94 | 271 def __calculate_scores(self, game_data): |
95 | 272 """The game is finished, time to know who won :) |
273 @param game_data: data of the game | |
274 @return: tuple with (string victory message, list of winners, list of loosers)""" | |
94 | 275 players_data = game_data['players_data'] |
276 levees = players_data[game_data['attaquant']]['levees'] | |
277 score = 0 | |
278 nb_bouts = 0 | |
95 | 279 bouts = [] |
94 | 280 for card in levees: |
281 if card.bout: | |
594
e629371a28d3
Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
282 nb_bouts += 1 |
95 | 283 bouts.append(card.value) |
94 | 284 score += card.points |
587
952322b1d490
Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
572
diff
changeset
|
285 |
161
c37826d80f2a
plugin tarot: fixed bad score calculation
Goffi <goffi@goffi.org>
parents:
147
diff
changeset
|
286 #We we do a basic check on score calculation |
95 | 287 check_score = 0 |
288 defenseurs = game_data['players'][:] | |
289 defenseurs.remove(game_data['attaquant']) | |
290 for defenseur in defenseurs: | |
291 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
|
292 check_score += card.points |
98
dd556233a1b1
Tarot Plugin: Garde Sans and Garde Contre are now managed
Goffi <goffi@goffi.org>
parents:
96
diff
changeset
|
293 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
|
294 for card in game_data['chien']: |
594
e629371a28d3
Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
295 check_score += card.points |
95 | 296 assert (score + check_score == 91) |
587
952322b1d490
Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
572
diff
changeset
|
297 |
94 | 298 point_limit = None |
299 if nb_bouts == 3: | |
300 point_limit = 36 | |
301 elif nb_bouts == 2: | |
302 point_limit = 41 | |
303 elif nb_bouts == 1: | |
304 point_limit = 51 | |
305 else: | |
306 point_limit = 56 | |
95 | 307 if game_data['contrat'] == 'Petite': |
308 contrat_mult = 1 | |
309 elif game_data['contrat'] == 'Garde': | |
310 contrat_mult = 2 | |
311 elif game_data['contrat'] == 'Garde Sans': | |
312 contrat_mult = 4 | |
313 elif game_data['contrat'] == 'Garde Contre': | |
314 contrat_mult = 6 | |
315 else: | |
161
c37826d80f2a
plugin tarot: fixed bad score calculation
Goffi <goffi@goffi.org>
parents:
147
diff
changeset
|
316 error(_('INTERNAL ERROR: contrat not managed (mispelled ?)')) |
c37826d80f2a
plugin tarot: fixed bad score calculation
Goffi <goffi@goffi.org>
parents:
147
diff
changeset
|
317 assert(False) |
95 | 318 |
94 | 319 victory = (score >= point_limit) |
161
c37826d80f2a
plugin tarot: fixed bad score calculation
Goffi <goffi@goffi.org>
parents:
147
diff
changeset
|
320 margin = abs(score - point_limit) |
c37826d80f2a
plugin tarot: fixed bad score calculation
Goffi <goffi@goffi.org>
parents:
147
diff
changeset
|
321 points_defenseur = (margin + 25) * contrat_mult * (-1 if victory else 1) |
95 | 322 winners = [] |
323 loosers = [] | |
324 player_score = {} | |
325 for player in game_data['players']: | |
326 #TODO: adjust this for 3 and 5 players variants | |
327 #TODO: manage bonuses (petit au bout, poignée, chelem) | |
328 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
|
329 players_data[player]['score'] += player_score[player] # we add score of this game to the global score |
95 | 330 if player_score[player] > 0: |
331 winners.append(player) | |
332 else: | |
333 loosers.append(player) | |
94 | 334 |
594
e629371a28d3
Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
335 scores_str = _('The attacker (%(attaquant)s) makes %(points)i and needs to make %(point_limit)i (%(nb_bouts)s oulder%(plural)s%(separator)s%(bouts)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': 'win' if victory else 'loose'} |
e629371a28d3
Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
336 scores_str += '\n' |
95 | 337 for player in game_data['players']: |
594
e629371a28d3
Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
338 scores_str += _("\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']} |
95 | 339 debug(scores_str) |
587
952322b1d490
Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
572
diff
changeset
|
340 |
95 | 341 return (scores_str, winners, loosers) |
94 | 342 |
99
63c9067a1499
Tarot game: invalid cards management
Goffi <goffi@goffi.org>
parents:
98
diff
changeset
|
343 def __invalid_cards(self, game_data, cards): |
63c9067a1499
Tarot game: invalid cards management
Goffi <goffi@goffi.org>
parents:
98
diff
changeset
|
344 """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
|
345 @param game_data: Game data |
63c9067a1499
Tarot game: invalid cards management
Goffi <goffi@goffi.org>
parents:
98
diff
changeset
|
346 @param cards: cards the player want to play |
63c9067a1499
Tarot game: invalid cards management
Goffi <goffi@goffi.org>
parents:
98
diff
changeset
|
347 @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
|
348 forbidden_cards = [] |
63c9067a1499
Tarot game: invalid cards management
Goffi <goffi@goffi.org>
parents:
98
diff
changeset
|
349 if game_data['stage'] == 'ecart': |
63c9067a1499
Tarot game: invalid cards management
Goffi <goffi@goffi.org>
parents:
98
diff
changeset
|
350 for card in cards: |
594
e629371a28d3
Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
351 if card.bout or card.value == "roi": |
99
63c9067a1499
Tarot game: invalid cards management
Goffi <goffi@goffi.org>
parents:
98
diff
changeset
|
352 forbidden_cards.append(card) |
63c9067a1499
Tarot game: invalid cards management
Goffi <goffi@goffi.org>
parents:
98
diff
changeset
|
353 #TODO: manage case where atouts (trumps) are in the dog |
63c9067a1499
Tarot game: invalid cards management
Goffi <goffi@goffi.org>
parents:
98
diff
changeset
|
354 elif game_data['stage'] == 'play': |
63c9067a1499
Tarot game: invalid cards management
Goffi <goffi@goffi.org>
parents:
98
diff
changeset
|
355 biggest_atout = None |
63c9067a1499
Tarot game: invalid cards management
Goffi <goffi@goffi.org>
parents:
98
diff
changeset
|
356 suit_asked = None |
63c9067a1499
Tarot game: invalid cards management
Goffi <goffi@goffi.org>
parents:
98
diff
changeset
|
357 players = game_data['players'] |
63c9067a1499
Tarot game: invalid cards management
Goffi <goffi@goffi.org>
parents:
98
diff
changeset
|
358 players_data = game_data['players_data'] |
63c9067a1499
Tarot game: invalid cards management
Goffi <goffi@goffi.org>
parents:
98
diff
changeset
|
359 idx = players.index(game_data['first_player']) |
63c9067a1499
Tarot game: invalid cards management
Goffi <goffi@goffi.org>
parents:
98
diff
changeset
|
360 current_idx = game_data['current_player'] |
63c9067a1499
Tarot game: invalid cards management
Goffi <goffi@goffi.org>
parents:
98
diff
changeset
|
361 current_player = players[current_idx] |
63c9067a1499
Tarot game: invalid cards management
Goffi <goffi@goffi.org>
parents:
98
diff
changeset
|
362 if idx == current_idx: |
63c9067a1499
Tarot game: invalid cards management
Goffi <goffi@goffi.org>
parents:
98
diff
changeset
|
363 #the player is the first to play, he can play what he wants |
63c9067a1499
Tarot game: invalid cards management
Goffi <goffi@goffi.org>
parents:
98
diff
changeset
|
364 return forbidden_cards |
63c9067a1499
Tarot game: invalid cards management
Goffi <goffi@goffi.org>
parents:
98
diff
changeset
|
365 while (idx != current_idx): |
63c9067a1499
Tarot game: invalid cards management
Goffi <goffi@goffi.org>
parents:
98
diff
changeset
|
366 player = players[idx] |
63c9067a1499
Tarot game: invalid cards management
Goffi <goffi@goffi.org>
parents:
98
diff
changeset
|
367 played_card = players_data[player]['played'] |
63c9067a1499
Tarot game: invalid cards management
Goffi <goffi@goffi.org>
parents:
98
diff
changeset
|
368 if not suit_asked and played_card.value != "excuse": |
63c9067a1499
Tarot game: invalid cards management
Goffi <goffi@goffi.org>
parents:
98
diff
changeset
|
369 suit_asked = played_card.suit |
63c9067a1499
Tarot game: invalid cards management
Goffi <goffi@goffi.org>
parents:
98
diff
changeset
|
370 if played_card.suit == "atout" and played_card > biggest_atout: |
63c9067a1499
Tarot game: invalid cards management
Goffi <goffi@goffi.org>
parents:
98
diff
changeset
|
371 biggest_atout = played_card |
63c9067a1499
Tarot game: invalid cards management
Goffi <goffi@goffi.org>
parents:
98
diff
changeset
|
372 idx = (idx + 1) % len(players) |
594
e629371a28d3
Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
373 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
|
374 has_atout = False |
63c9067a1499
Tarot game: invalid cards management
Goffi <goffi@goffi.org>
parents:
98
diff
changeset
|
375 biggest_hand_atout = None |
63c9067a1499
Tarot game: invalid cards management
Goffi <goffi@goffi.org>
parents:
98
diff
changeset
|
376 |
63c9067a1499
Tarot game: invalid cards management
Goffi <goffi@goffi.org>
parents:
98
diff
changeset
|
377 for hand_card in game_data['hand'][current_player]: |
63c9067a1499
Tarot game: invalid cards management
Goffi <goffi@goffi.org>
parents:
98
diff
changeset
|
378 if hand_card.suit == suit_asked: |
63c9067a1499
Tarot game: invalid cards management
Goffi <goffi@goffi.org>
parents:
98
diff
changeset
|
379 has_suit = True |
63c9067a1499
Tarot game: invalid cards management
Goffi <goffi@goffi.org>
parents:
98
diff
changeset
|
380 if hand_card.suit == "atout": |
63c9067a1499
Tarot game: invalid cards management
Goffi <goffi@goffi.org>
parents:
98
diff
changeset
|
381 has_atout = True |
63c9067a1499
Tarot game: invalid cards management
Goffi <goffi@goffi.org>
parents:
98
diff
changeset
|
382 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
|
383 biggest_hand_atout = hand_card |
63c9067a1499
Tarot game: invalid cards management
Goffi <goffi@goffi.org>
parents:
98
diff
changeset
|
384 |
63c9067a1499
Tarot game: invalid cards management
Goffi <goffi@goffi.org>
parents:
98
diff
changeset
|
385 assert len(cards) == 1 |
63c9067a1499
Tarot game: invalid cards management
Goffi <goffi@goffi.org>
parents:
98
diff
changeset
|
386 card = cards[0] |
63c9067a1499
Tarot game: invalid cards management
Goffi <goffi@goffi.org>
parents:
98
diff
changeset
|
387 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
|
388 forbidden_cards.append(card) |
63c9067a1499
Tarot game: invalid cards management
Goffi <goffi@goffi.org>
parents:
98
diff
changeset
|
389 return forbidden_cards |
63c9067a1499
Tarot game: invalid cards management
Goffi <goffi@goffi.org>
parents:
98
diff
changeset
|
390 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
|
391 forbidden_cards.append(card) |
63c9067a1499
Tarot game: invalid cards management
Goffi <goffi@goffi.org>
parents:
98
diff
changeset
|
392 return forbidden_cards |
63c9067a1499
Tarot game: invalid cards management
Goffi <goffi@goffi.org>
parents:
98
diff
changeset
|
393 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
|
394 forbidden_cards.append(card) |
63c9067a1499
Tarot game: invalid cards management
Goffi <goffi@goffi.org>
parents:
98
diff
changeset
|
395 else: |
63c9067a1499
Tarot game: invalid cards management
Goffi <goffi@goffi.org>
parents:
98
diff
changeset
|
396 error(_('Internal error: unmanaged game stage')) |
63c9067a1499
Tarot game: invalid cards management
Goffi <goffi@goffi.org>
parents:
98
diff
changeset
|
397 return forbidden_cards |
63c9067a1499
Tarot game: invalid cards management
Goffi <goffi@goffi.org>
parents:
98
diff
changeset
|
398 |
98
dd556233a1b1
Tarot Plugin: Garde Sans and Garde Contre are now managed
Goffi <goffi@goffi.org>
parents:
96
diff
changeset
|
399 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
|
400 """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
|
401 game_data['stage'] = "play" |
594
e629371a28d3
Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
402 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
|
403 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
|
404 to_jid = jid.JID(room_jid.userhost() + "/" + next_player) # FIXME: gof: |
98
dd556233a1b1
Tarot Plugin: Garde Sans and Garde Contre are now managed
Goffi <goffi@goffi.org>
parents:
96
diff
changeset
|
405 mess = self.createGameElt(to_jid) |
dd556233a1b1
Tarot Plugin: Garde Sans and Garde Contre are now managed
Goffi <goffi@goffi.org>
parents:
96
diff
changeset
|
406 yourturn_elt = mess.firstChildElement().addElement('your_turn') |
dd556233a1b1
Tarot Plugin: Garde Sans and Garde Contre are now managed
Goffi <goffi@goffi.org>
parents:
96
diff
changeset
|
407 self.host.profiles[profile].xmlstream.send(mess) |
dd556233a1b1
Tarot Plugin: Garde Sans and Garde Contre are now managed
Goffi <goffi@goffi.org>
parents:
96
diff
changeset
|
408 |
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
|
409 def userJoinedTrigger(self, room, user, profile): |
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
|
410 """This trigger is used to check if we are waiting people in this room, |
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
|
411 and to create a game if everybody is here""" |
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
|
412 _room_jid = room.occupantJID.userhostJID() |
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
|
413 if _room_jid in self.waiting_inv and len(room.roster) == 4: |
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
|
414 #When we have 4 people in the room, we create the game |
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
|
415 #TODO: check people identity |
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
|
416 players = room.roster.keys() |
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
|
417 del self.waiting_inv[_room_jid] |
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
|
418 self.createGame(_room_jid.userhost(), players, profile_key=profile) |
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
|
419 return True |
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
|
420 |
563 | 421 def launchGame(self, players, profile_key='@NONE@'): |
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
|
422 """Launch a game: helper method to create a room, invite players, and create the tarot game |
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
|
423 @param players: list for players jid""" |
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
|
424 debug(_('Launching tarot game')) |
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
|
425 profile = self.host.memory.getProfileName(profile_key) |
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
|
426 if not profile: |
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
|
427 error(_("Unknown profile")) |
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
|
428 return |
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
|
429 |
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
|
430 def tarotRoomJoined(room): |
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
|
431 _room = room.occupantJID.userhostJID() |
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
|
432 for player in players: |
594
e629371a28d3
Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
433 self.host.plugins["XEP-0249"].invite(jid.JID(player), room.occupantJID.userhostJID(), {"game": "Tarot"}, profile) |
e629371a28d3
Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
434 self.waiting_inv[_room] = (time(), players) # TODO: remove invitation waiting for too long, using the time data |
587
952322b1d490
Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
572
diff
changeset
|
435 |
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
|
436 def after_init(ignore): |
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
|
437 room_name = "sat_tarot_%s" % self.host.plugins["XEP-0045"].getUniqueName(profile_key) |
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
|
438 print "\n\n===> room_name:", room_name |
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
|
439 #muc_service = self.host.memory.getServerServiceEntity("conference", "text", profile) |
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
|
440 muc_service = None |
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
|
441 for service in self.host.memory.getServerServiceEntities("conference", "text", profile): |
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
|
442 if not ".irc." in service.userhost(): |
587
952322b1d490
Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
572
diff
changeset
|
443 #FIXME: |
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
|
444 #This awfull ugly hack is here to avoid an issue with openfire: the irc gateway |
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
|
445 #use "conference/text" identity (instead of "conference/irc"), there is certainly a better way |
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
|
446 #to manage this, but this hack fill do it for test purpose |
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
|
447 muc_service = service |
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
|
448 break |
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
|
449 if not muc_service: |
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
|
450 error(_("Can't find a MUC service")) |
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
|
451 return |
587
952322b1d490
Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
572
diff
changeset
|
452 |
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
|
453 _jid, xmlstream = self.host.getJidNStream(profile) |
563 | 454 d = self.host.plugins["XEP-0045"].join(jid.JID("%s@%s" % (room_name, muc_service.userhost())), _jid.user, {}, profile).addCallback(tarotRoomJoined) |
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
|
455 |
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
|
456 client = self.host.getClient(profile) |
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
|
457 if not client: |
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
|
458 error(_('No client for this profile key: %s') % profile_key) |
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
|
459 return |
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
|
460 client.client_initialized.addCallback(after_init) |
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
|
461 |
563 | 462 def createGame(self, room_jid_param, players, profile_key='@NONE@'): |
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
|
463 """Create a new game |
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
|
464 @param room_jid_param: jid of the room |
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
|
465 @param players: list of players nick (nick must exist in the room) |
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
|
466 @param profile_key: %(doc_profile_key)s""" |
594
e629371a28d3
Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
467 debug(_("Creating Tarot game")) |
90 | 468 room_jid = jid.JID(room_jid_param) |
88 | 469 profile = self.host.memory.getProfileName(profile_key) |
470 if not profile: | |
594
e629371a28d3
Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
471 error(_("profile %s is unknown") % profile_key) |
88 | 472 return |
594
e629371a28d3
Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
473 if room_jid in self.games: |
e629371a28d3
Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
474 warning(_("Tarot game already started in room %s") % room_jid.userhost()) |
88 | 475 else: |
291 | 476 room_nick = self.host.plugins["XEP-0045"].getRoomNick(room_jid.userhost(), profile) |
93 | 477 if not room_nick: |
594
e629371a28d3
Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
478 error('Internal error') |
93 | 479 return |
480 referee = room_jid.userhost() + '/' + room_nick | |
90 | 481 status = {} |
91 | 482 players_data = {} |
90 | 483 for player in players: |
594
e629371a28d3
Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
484 players_data[player] = {'score': 0} |
90 | 485 status[player] = "init" |
594
e629371a28d3
Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
486 self.games[room_jid.userhost()] = {'referee': referee, 'players': players, 'status': status, 'players_data': players_data, 'hand_size': 18, 'init_player': 0, 'current_player': None, 'contrat': None, 'stage': None} |
90 | 487 for player in players: |
594
e629371a28d3
Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
488 mess = self.createGameElt(jid.JID(room_jid.userhost() + '/' + player)) |
90 | 489 mess.firstChildElement().addChild(self.__create_started_elt(players)) |
490 self.host.profiles[profile].xmlstream.send(mess) | |
491 | |
563 | 492 def newPlayerReady(self, player, referee, profile_key='@NONE@'): |
90 | 493 """Must be called when player is ready to start a new game""" |
494 profile = self.host.memory.getProfileName(profile_key) | |
495 if not profile: | |
594
e629371a28d3
Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
496 error(_("profile %s is unknown") % profile_key) |
90 | 497 return |
594
e629371a28d3
Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
498 debug('new player ready: %s' % profile) |
90 | 499 mess = self.createGameElt(jid.JID(referee)) |
91 | 500 ready_elt = mess.firstChildElement().addElement('player_ready') |
92 | 501 ready_elt['player'] = player |
91 | 502 self.host.profiles[profile].xmlstream.send(mess) |
503 | |
563 | 504 def contratChoosed(self, player, referee, contrat, profile_key='@NONE@'): |
91 | 505 """Must be call by player when the contrat is selected |
92 | 506 @param player: player's name |
91 | 507 @param referee: arbiter jid |
508 @contrat: contrat choosed (must be the exact same string than in the give list options) | |
509 @profile_key: profile | |
510 """ | |
511 profile = self.host.memory.getProfileName(profile_key) | |
512 if not profile: | |
594
e629371a28d3
Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
513 error(_("profile %s is unknown") % profile_key) |
91 | 514 return |
594
e629371a28d3
Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
515 debug(_('contrat [%(contrat)s] choosed by %(profile)s') % {'contrat': contrat, 'profile': profile}) |
91 | 516 mess = self.createGameElt(jid.JID(referee)) |
594
e629371a28d3
Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
517 contrat_elt = mess.firstChildElement().addElement(('', 'contrat_choosed'), content=contrat) |
92 | 518 contrat_elt['player'] = player |
90 | 519 self.host.profiles[profile].xmlstream.send(mess) |
88 | 520 |
563 | 521 def play_cards(self, player, referee, cards, profile_key='@NONE@'): |
92 | 522 """Must be call by player when the contrat is selected |
523 @param player: player's name | |
524 @param referee: arbiter jid | |
525 @cards: cards played (list of tuples) | |
526 @profile_key: profile | |
527 """ | |
528 profile = self.host.memory.getProfileName(profile_key) | |
529 if not profile: | |
594
e629371a28d3
Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
530 error(_("profile %s is unknown") % profile_key) |
92 | 531 return |
594
e629371a28d3
Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
532 debug(_('Cards played by %(profile)s: [%(cards)s]') % {'profile': profile, 'cards': cards}) |
92 | 533 mess = self.createGameElt(jid.JID(referee)) |
141
8c80d4dec7a8
mover Card class to tools/games and renamed it in TarotCard
Goffi <goffi@goffi.org>
parents:
134
diff
changeset
|
534 playcard_elt = mess.firstChildElement().addChild(self.__card_list_to_xml(TarotCard.from_tuples(cards), 'cards_played')) |
92 | 535 playcard_elt['player'] = player |
536 self.host.profiles[profile].xmlstream.send(mess) | |
88 | 537 |
92 | 538 def newGame(self, room_jid, profile): |
88 | 539 """Launch a new round""" |
594
e629371a28d3
Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
540 debug(_('new Tarot game')) |
88 | 541 deck = self.deck_ordered[:] |
542 random.shuffle(deck) | |
91 | 543 game_data = self.games[room_jid.userhost()] |
544 players = game_data['players'] | |
545 players_data = game_data['players_data'] | |
546 current_player = game_data['current_player'] | |
92 | 547 game_data['stage'] = "init" |
594
e629371a28d3
Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
548 game_data['first_player'] = None # first player for the current trick |
95 | 549 game_data['contrat'] = None |
91 | 550 hand = game_data['hand'] = {} |
551 hand_size = game_data['hand_size'] | |
552 chien = game_data['chien'] = [] | |
328 | 553 for i in range(4): |
88 | 554 hand[players[i]] = deck[0:hand_size] |
555 del deck[0:hand_size] | |
92 | 556 chien.extend(deck) |
88 | 557 del(deck[:]) |
558 | |
559 for player in players: | |
594
e629371a28d3
Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
560 to_jid = jid.JID(room_jid.userhost() + "/" + player) # FIXME: gof: |
90 | 561 mess = self.createGameElt(to_jid) |
98
dd556233a1b1
Tarot Plugin: Garde Sans and Garde Contre are now managed
Goffi <goffi@goffi.org>
parents:
96
diff
changeset
|
562 mess.firstChildElement().addChild(self.__card_list_to_xml(hand[player], 'hand')) |
92 | 563 self.host.profiles[profile].xmlstream.send(mess) |
91 | 564 players_data[player]['contrat'] = None |
594
e629371a28d3
Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
565 players_data[player]['levees'] = [] # cards won |
e629371a28d3
Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
566 players_data[player]['played'] = None # card on the table |
e629371a28d3
Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
567 players_data[player]['wait_for_low'] = None # Used when a player wait for a low card because of excuse |
91 | 568 |
594
e629371a28d3
Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
569 pl_idx = game_data['current_player'] = (game_data['init_player'] + 1) % len(players) # the player after the dealer start |
91 | 570 player = players[pl_idx] |
594
e629371a28d3
Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
571 to_jid = jid.JID(room_jid.userhost() + "/" + player) # FIXME: gof: |
91 | 572 mess = self.createGameElt(to_jid) |
573 mess.firstChildElement().addChild(self.__ask_contrat()) | |
92 | 574 self.host.profiles[profile].xmlstream.send(mess) |
587
952322b1d490
Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
572
diff
changeset
|
575 |
90 | 576 def card_game_cmd(self, mess_elt, profile): |
587
952322b1d490
Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
572
diff
changeset
|
577 from_jid = jid.JID(mess_elt['from']) |
93 | 578 room_jid = jid.JID(from_jid.userhost()) |
90 | 579 game_elt = mess_elt.firstChildElement() |
92 | 580 game_data = self.games[room_jid.userhost()] |
581 players_data = game_data['players_data'] | |
587
952322b1d490
Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
572
diff
changeset
|
582 |
92 | 583 for elt in game_elt.elements(): |
587
952322b1d490
Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
572
diff
changeset
|
584 |
594
e629371a28d3
Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
585 if elt.name == 'started': # new game created |
90 | 586 players = [] |
587 for player in elt.elements(): | |
588 players.append(unicode(player)) | |
93 | 589 self.host.bridge.tarotGameStarted(room_jid.userhost(), from_jid.full(), players, profile) |
587
952322b1d490
Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
572
diff
changeset
|
590 |
594
e629371a28d3
Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
591 elif elt.name == 'player_ready': # ready to play |
92 | 592 player = elt['player'] |
90 | 593 status = self.games[room_jid.userhost()]['status'] |
594 nb_players = len(self.games[room_jid.userhost()]['players']) | |
595 status[player] = 'ready' | |
594
e629371a28d3
Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
596 debug(_('Player %(player)s is ready to start [status: %(status)s]') % {'player': player, 'status': status}) |
e629371a28d3
Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
597 if status.values().count('ready') == nb_players: # everybody is ready, we can start the game |
92 | 598 self.newGame(room_jid, profile) |
587
952322b1d490
Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
572
diff
changeset
|
599 |
594
e629371a28d3
Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
600 elif elt.name == 'hand': # a new hand has been received |
92 | 601 self.host.bridge.tarotGameNew(room_jid.userhost(), self.__xml_to_list(elt), profile) |
91 | 602 |
594
e629371a28d3
Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
603 elif elt.name == 'contrat': # it's time to choose contrat |
91 | 604 form = data_form.Form.fromElement(elt.firstChildElement()) |
102 | 605 xml_data = dataForm2xml(form) |
92 | 606 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
|
607 |
92 | 608 elif elt.name == 'contrat_choosed': |
91 | 609 #TODO: check we receive the contrat from the right person |
92 | 610 #TODO: use proper XEP-0004 way for answering form |
611 player = elt['player'] | |
612 players_data[player]['contrat'] = unicode(elt) | |
91 | 613 contrats = [players_data[player]['contrat'] for player in game_data['players']] |
614 if contrats.count(None): | |
615 #not everybody has choosed his contrat, it's next one turn | |
616 player = self.__next_player(game_data) | |
594
e629371a28d3
Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
617 to_jid = jid.JID(room_jid.userhost() + "/" + player) # FIXME: gof: |
91 | 618 mess = self.createGameElt(to_jid) |
619 mess.firstChildElement().addChild(self.__ask_contrat()) | |
92 | 620 self.host.profiles[profile].xmlstream.send(mess) |
91 | 621 else: |
622 best_contrat = [None, "Passe"] | |
623 for player in game_data['players']: | |
624 contrat = players_data[player]['contrat'] | |
625 idx_best = self.contrats.index(best_contrat[1]) | |
626 idx_pl = self.contrats.index(contrat) | |
627 if idx_pl > idx_best: | |
628 best_contrat[0] = player | |
629 best_contrat[1] = contrat | |
328 | 630 if best_contrat[1] == "Passe": |
631 debug(_("Everybody is passing, round ended")) | |
632 to_jid = jid.JID(room_jid.userhost()) | |
633 mess = self.createGameElt(to_jid) | |
634 mess.firstChildElement().addChild(self.__give_scores(*self.__draw_game(game_data))) | |
635 self.host.profiles[profile].xmlstream.send(mess) | |
594
e629371a28d3
Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
636 game_data['init_player'] = (game_data['init_player'] + 1) % len(game_data['players']) # we change the dealer |
328 | 637 for player in game_data['players']: |
638 game_data['status'][player] = "init" | |
639 return | |
594
e629371a28d3
Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
640 debug(_("%(player)s win the bid with %(contrat)s") % {'player': best_contrat[0], 'contrat': best_contrat[1]}) |
95 | 641 game_data['contrat'] = best_contrat[1] |
587
952322b1d490
Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
572
diff
changeset
|
642 |
98
dd556233a1b1
Tarot Plugin: Garde Sans and Garde Contre are now managed
Goffi <goffi@goffi.org>
parents:
96
diff
changeset
|
643 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
|
644 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
|
645 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
|
646 else: |
dd556233a1b1
Tarot Plugin: Garde Sans and Garde Contre are now managed
Goffi <goffi@goffi.org>
parents:
96
diff
changeset
|
647 #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
|
648 to_jid = jid.JID(room_jid.userhost()) # FIXME: gof: |
98
dd556233a1b1
Tarot Plugin: Garde Sans and Garde Contre are now managed
Goffi <goffi@goffi.org>
parents:
96
diff
changeset
|
649 mess = self.createGameElt(to_jid) |
dd556233a1b1
Tarot Plugin: Garde Sans and Garde Contre are now managed
Goffi <goffi@goffi.org>
parents:
96
diff
changeset
|
650 chien_elt = mess.firstChildElement().addChild(self.__card_list_to_xml(game_data['chien'], 'chien')) |
dd556233a1b1
Tarot Plugin: Garde Sans and Garde Contre are now managed
Goffi <goffi@goffi.org>
parents:
96
diff
changeset
|
651 chien_elt['attaquant'] = best_contrat[0] |
dd556233a1b1
Tarot Plugin: Garde Sans and Garde Contre are now managed
Goffi <goffi@goffi.org>
parents:
96
diff
changeset
|
652 self.host.profiles[profile].xmlstream.send(mess) |
dd556233a1b1
Tarot Plugin: Garde Sans and Garde Contre are now managed
Goffi <goffi@goffi.org>
parents:
96
diff
changeset
|
653 #the attacker (attaquant) get the chien |
dd556233a1b1
Tarot Plugin: Garde Sans and Garde Contre are now managed
Goffi <goffi@goffi.org>
parents:
96
diff
changeset
|
654 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
|
655 del game_data['chien'][:] |
587
952322b1d490
Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
572
diff
changeset
|
656 |
98
dd556233a1b1
Tarot Plugin: Garde Sans and Garde Contre are now managed
Goffi <goffi@goffi.org>
parents:
96
diff
changeset
|
657 if game_data['contrat'] == "Garde Sans": |
dd556233a1b1
Tarot Plugin: Garde Sans and Garde Contre are now managed
Goffi <goffi@goffi.org>
parents:
96
diff
changeset
|
658 #The chien go into attaquant's (attacker) levees |
dd556233a1b1
Tarot Plugin: Garde Sans and Garde Contre are now managed
Goffi <goffi@goffi.org>
parents:
96
diff
changeset
|
659 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
|
660 del game_data['chien'][:] |
91 | 661 |
594
e629371a28d3
Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
662 elif elt.name == 'chien': # we have received the chien |
e629371a28d3
Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
663 debug(_("tarot: chien received")) |
e629371a28d3
Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
664 data = {"attaquant": elt['attaquant']} |
92 | 665 game_data['stage'] = "ecart" |
666 game_data['attaquant'] = elt['attaquant'] | |
667 self.host.bridge.tarotGameShowCards(room_jid.userhost(), "chien", self.__xml_to_list(elt), data, profile) | |
668 | |
669 elif elt.name == 'cards_played': | |
670 if game_data['stage'] == "ecart": | |
671 #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
|
672 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
|
673 list_cards = TarotCard.from_tuples(self.__xml_to_list(elt)) |
99
63c9067a1499
Tarot game: invalid cards management
Goffi <goffi@goffi.org>
parents:
98
diff
changeset
|
674 #we now check validity of card |
63c9067a1499
Tarot game: invalid cards management
Goffi <goffi@goffi.org>
parents:
98
diff
changeset
|
675 invalid_cards = self.__invalid_cards(game_data, list_cards) |
63c9067a1499
Tarot game: invalid cards management
Goffi <goffi@goffi.org>
parents:
98
diff
changeset
|
676 if invalid_cards: |
594
e629371a28d3
Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
677 mess = self.createGameElt(jid.JID(room_jid.userhost() + '/' + elt['player'])) |
99
63c9067a1499
Tarot game: invalid cards management
Goffi <goffi@goffi.org>
parents:
98
diff
changeset
|
678 mess.firstChildElement().addChild(self.__invalid_cards_elt(list_cards, invalid_cards, game_data['stage'])) |
63c9067a1499
Tarot game: invalid cards management
Goffi <goffi@goffi.org>
parents:
98
diff
changeset
|
679 self.host.profiles[profile].xmlstream.send(mess) |
63c9067a1499
Tarot game: invalid cards management
Goffi <goffi@goffi.org>
parents:
98
diff
changeset
|
680 return |
63c9067a1499
Tarot game: invalid cards management
Goffi <goffi@goffi.org>
parents:
98
diff
changeset
|
681 |
95 | 682 #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
|
683 players_data[elt['player']]['levees'].extend(list_cards) # we add the chien to attaquant's levées |
95 | 684 for card in list_cards: |
685 game_data['hand'][elt['player']].remove(card) | |
587
952322b1d490
Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
572
diff
changeset
|
686 |
98
dd556233a1b1
Tarot Plugin: Garde Sans and Garde Contre are now managed
Goffi <goffi@goffi.org>
parents:
96
diff
changeset
|
687 self.__start_play(room_jid, game_data, profile) |
587
952322b1d490
Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
572
diff
changeset
|
688 |
93 | 689 elif game_data['stage'] == "play": |
690 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
|
691 cards = TarotCard.from_tuples(self.__xml_to_list(elt)) |
587
952322b1d490
Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
572
diff
changeset
|
692 |
94 | 693 if mess_elt['type'] == 'groupchat': |
694 self.host.bridge.tarotGameCardsPlayed(room_jid.userhost(), elt['player'], self.__xml_to_list(elt), profile) | |
695 else: | |
99
63c9067a1499
Tarot game: invalid cards management
Goffi <goffi@goffi.org>
parents:
98
diff
changeset
|
696 #we first check validity of card |
63c9067a1499
Tarot game: invalid cards management
Goffi <goffi@goffi.org>
parents:
98
diff
changeset
|
697 invalid_cards = self.__invalid_cards(game_data, cards) |
63c9067a1499
Tarot game: invalid cards management
Goffi <goffi@goffi.org>
parents:
98
diff
changeset
|
698 if invalid_cards: |
594
e629371a28d3
Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
699 mess = self.createGameElt(jid.JID(room_jid.userhost() + '/' + current_player)) |
99
63c9067a1499
Tarot game: invalid cards management
Goffi <goffi@goffi.org>
parents:
98
diff
changeset
|
700 mess.firstChildElement().addChild(self.__invalid_cards_elt(cards, invalid_cards, game_data['stage'])) |
63c9067a1499
Tarot game: invalid cards management
Goffi <goffi@goffi.org>
parents:
98
diff
changeset
|
701 self.host.profiles[profile].xmlstream.send(mess) |
63c9067a1499
Tarot game: invalid cards management
Goffi <goffi@goffi.org>
parents:
98
diff
changeset
|
702 return |
93 | 703 #the card played is ok, we forward it to everybody |
94 | 704 #first we remove it from the hand and put in on the table |
93 | 705 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
|
706 players_data[current_player]['played'] = cards[0] |
93 | 707 |
708 #then we forward the message | |
709 mess = self.createGameElt(room_jid) | |
710 playcard_elt = mess.firstChildElement().addChild(elt) | |
711 self.host.profiles[profile].xmlstream.send(mess) | |
587
952322b1d490
Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
572
diff
changeset
|
712 |
94 | 713 #Did everybody played ? |
714 played = [players_data[player]['played'] for player in game_data['players']] | |
95 | 715 if all(played): |
716 #everybody has played | |
94 | 717 winner = self.__winner(game_data) |
594
e629371a28d3
Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
718 debug(_('The winner of this trick is %s') % winner) |
94 | 719 #the winner win the trick |
720 self.__excuse_hack(game_data, played, winner) | |
721 players_data[elt['player']]['levees'].extend(played) | |
722 #nothing left on the table | |
723 for player in game_data['players']: | |
724 players_data[player]['played'] = None | |
725 if len(game_data['hand'][current_player]) == 0: | |
726 #no card lef: the game is finished | |
362
208107419b17
Quiz game: buzzer, timer, answer management
Goffi <goffi@goffi.org>
parents:
361
diff
changeset
|
727 to_jid = room_jid |
95 | 728 mess = self.createGameElt(to_jid) |
729 chien_elt = mess.firstChildElement().addChild(self.__give_scores(*self.__calculate_scores(game_data))) | |
730 self.host.profiles[profile].xmlstream.send(mess) | |
594
e629371a28d3
Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
731 game_data['init_player'] = (game_data['init_player'] + 1) % len(game_data['players']) # we change the dealer |
328 | 732 for player in game_data['players']: |
733 game_data['status'][player] = "init" | |
94 | 734 return |
735 #next player is the winner | |
736 next_player = game_data['first_player'] = self.__next_player(game_data, winner) | |
737 else: | |
738 next_player = self.__next_player(game_data) | |
739 | |
93 | 740 #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
|
741 to_jid = jid.JID(room_jid.userhost() + "/" + next_player) |
93 | 742 mess = self.createGameElt(to_jid) |
743 yourturn_elt = mess.firstChildElement().addElement('your_turn') | |
744 self.host.profiles[profile].xmlstream.send(mess) | |
745 | |
92 | 746 elif elt.name == 'your_turn': |
747 self.host.bridge.tarotGameYourTurn(room_jid.userhost(), profile) | |
91 | 748 |
95 | 749 elif elt.name == 'score': |
594
e629371a28d3
Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
750 form_elt = elt.elements(name='x', uri='jabber:x:data').next() |
95 | 751 winners = [] |
752 loosers = [] | |
563 | 753 for winner in elt.elements(name='winner', uri=NS_CG): |
95 | 754 winners.append(unicode(winner)) |
563 | 755 for looser in elt.elements(name='looser', uri=NS_CG): |
95 | 756 loosers.append(unicode(looser)) |
757 form = data_form.Form.fromElement(form_elt) | |
102 | 758 xml_data = dataForm2xml(form) |
95 | 759 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
|
760 elif elt.name == 'error': |
63c9067a1499
Tarot game: invalid cards management
Goffi <goffi@goffi.org>
parents:
98
diff
changeset
|
761 if elt['type'] == 'invalid_cards': |
594
e629371a28d3
Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
762 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
|
763 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
|
764 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
|
765 else: |
594
e629371a28d3
Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
766 error(_('Unmanaged error type: %s') % elt['type']) |
99
63c9067a1499
Tarot game: invalid cards management
Goffi <goffi@goffi.org>
parents:
98
diff
changeset
|
767 else: |
594
e629371a28d3
Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
768 error(_('Unmanaged card game element: %s') % elt.name) |
587
952322b1d490
Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
572
diff
changeset
|
769 |
90 | 770 def getHandler(self, profile): |
594
e629371a28d3
Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
771 return CardGameHandler(self) |
e629371a28d3
Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
772 |
88 | 773 |
90 | 774 class CardGameHandler (XMPPHandler): |
775 implements(iwokkel.IDisco) | |
587
952322b1d490
Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
572
diff
changeset
|
776 |
90 | 777 def __init__(self, plugin_parent): |
778 self.plugin_parent = plugin_parent | |
779 self.host = plugin_parent.host | |
780 | |
781 def connectionInitialized(self): | |
594
e629371a28d3
Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
782 self.xmlstream.addObserver(CG_REQUEST, self.plugin_parent.card_game_cmd, profile=self.parent.profile) |
90 | 783 |
784 def getDiscoInfo(self, requestor, target, nodeIdentifier=''): | |
98
dd556233a1b1
Tarot Plugin: Garde Sans and Garde Contre are now managed
Goffi <goffi@goffi.org>
parents:
96
diff
changeset
|
785 return [disco.DiscoFeature(NS_CG)] |
90 | 786 |
787 def getDiscoItems(self, requestor, target, nodeIdentifier=''): | |
788 return [] |