Mercurial > libervia-backend
annotate plugins/plugin_misc_tarot.py @ 155:61f1e509e684
SàT: fixed exception when using unmanaged IQ
author | Goffi <goffi@goffi.org> |
---|---|
date | Wed, 04 Aug 2010 11:58:54 +0800 |
parents | dc692acde155 |
children | c37826d80f2a |
rev | line source |
---|---|
88 | 1 #!/usr/bin/python |
2 # -*- coding: utf-8 -*- | |
3 | |
4 """ | |
5 SAT plugin for managing xep-0045 | |
6 Copyright (C) 2009, 2010 Jérôme Poisson (goffi@goffi.org) | |
7 | |
8 This program is free software: you can redistribute it and/or modify | |
9 it under the terms of the GNU General Public License as published by | |
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 | |
16 GNU General Public License for more details. | |
17 | |
18 You should have received a copy of the GNU General Public License | |
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 | |
24 from twisted.internet import protocol, defer, threads, reactor | |
25 from twisted.words.protocols.jabber import client, jid, xmlstream | |
26 from twisted.words.protocols.jabber import error as jab_error | |
27 from twisted.words.protocols.jabber.xmlstream import IQ | |
28 import os.path | |
29 import pdb | |
30 import random | |
31 | |
32 from zope.interface import implements | |
33 | |
91 | 34 from wokkel import disco, iwokkel, data_form |
102 | 35 from tools.xml_tools import dataForm2xml |
141
8c80d4dec7a8
mover Card class to tools/games and renamed it in TarotCard
Goffi <goffi@goffi.org>
parents:
134
diff
changeset
|
36 from tools.games import TarotCard |
88 | 37 |
38 try: | |
39 from twisted.words.protocols.xmlstream import XMPPHandler | |
40 except ImportError: | |
41 from wokkel.subprotocols import XMPPHandler | |
42 | |
90 | 43 MESSAGE = '/message' |
44 NS_CG = 'http://www.goffi.org/protocol/card_game' | |
45 CG_TAG = 'card_game' | |
46 CG_REQUEST = MESSAGE + '/' + CG_TAG + '[@xmlns="' + NS_CG + '"]' | |
88 | 47 |
48 PLUGIN_INFO = { | |
49 "name": "Tarot cards plugin", | |
50 "import_name": "Tarot", | |
51 "type": "Misc", | |
52 "protocols": [], | |
53 "dependencies": ["XEP_0045"], | |
54 "main": "Tarot", | |
90 | 55 "handler": "yes", |
88 | 56 "description": _("""Implementation of Tarot card game""") |
57 } | |
58 | |
94 | 59 |
88 | 60 class Tarot(): |
61 | |
62 def __init__(self, host): | |
63 info(_("Plugin Tarot initialization")) | |
64 self.host = host | |
65 self.games={} | |
91 | 66 self.contrats = [_('Passe'), _('Petite'), _('Garde'), _('Garde Sans'), _('Garde Contre')] |
90 | 67 host.bridge.addMethod("tarotGameCreate", ".communication", in_sign='sass', out_sign='', method=self.createGame) #args: room_jid, players, profile |
92 | 68 host.bridge.addMethod("tarotGameReady", ".communication", in_sign='sss', out_sign='', method=self.newPlayerReady) #args: player, referee, profile |
69 host.bridge.addMethod("tarotGameContratChoosed", ".communication", in_sign='ssss', out_sign='', method=self.contratChoosed) #args: player, referee, contrat, profile | |
70 host.bridge.addMethod("tarotGamePlayCards", ".communication", in_sign='ssa(ss)s', out_sign='', method=self.play_cards) #args: player, referee, cards, profile | |
90 | 71 host.bridge.addSignal("tarotGameStarted", ".communication", signature='ssass') #args: room_jid, referee, players, profile |
72 host.bridge.addSignal("tarotGameNew", ".communication", signature='sa(ss)s') #args: room_jid, hand, profile | |
92 | 73 host.bridge.addSignal("tarotGameChooseContrat", ".communication", signature='sss') #args: room_jid, xml_data, profile |
74 host.bridge.addSignal("tarotGameShowCards", ".communication", signature='ssa(ss)a{ss}s') #args: room_jid, type ["chien", "poignée",...], cards, data[dict], profile | |
93 | 75 host.bridge.addSignal("tarotGameCardsPlayed", ".communication", signature='ssa(ss)s') #args: room_jid, player, type ["chien", "poignée",...], cards, data[dict], profile |
92 | 76 host.bridge.addSignal("tarotGameYourTurn", ".communication", signature='ss') #args: room_jid, profile |
95 | 77 host.bridge.addSignal("tarotGameScore", ".communication", signature='ssasass') #args: room_jid, xml_data, winners (list of nicks), loosers (list of nicks), profile |
99
63c9067a1499
Tarot game: invalid cards management
Goffi <goffi@goffi.org>
parents:
98
diff
changeset
|
78 host.bridge.addSignal("tarotGameInvalidCards", ".communication", signature='ssa(ss)a(ss)s') #args: room_jid, game phase, played_cards, invalid_cards, profile |
88 | 79 self.deck_ordered = [] |
92 | 80 for value in ['excuse']+map(str,range(1,22)): |
141
8c80d4dec7a8
mover Card class to tools/games and renamed it in TarotCard
Goffi <goffi@goffi.org>
parents:
134
diff
changeset
|
81 self.deck_ordered.append(TarotCard(("atout",value))) |
92 | 82 for suit in ["pique", "coeur", "carreau", "trefle"]: |
88 | 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" | |
90 | 88 elt = domish.Element(('jabber:client','message')) |
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""" |
92 | 96 cards_list_elt = domish.Element(('',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: |
90 | 98 card_elt = domish.Element(('','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 |
92 | 101 cards_list_elt.addChild(card_elt) |
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 | |
90 | 110 |
111 def __create_started_elt(self, players): | |
112 """Create a game_started domish element""" | |
113 started_elt = domish.Element(('','started')) | |
114 idx = 0 | |
115 for player in players: | |
116 player_elt = domish.Element(('','player')) | |
117 player_elt.addContent(player) | |
118 player_elt['index'] = str(idx) | |
119 idx+=1 | |
120 started_elt.addChild(player_elt) | |
121 return started_elt | |
122 | |
91 | 123 def __ask_contrat(self): |
124 """Create a element for asking contrat""" | |
125 contrat_elt = domish.Element(('','contrat')) | |
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 | |
138 score_elt = domish.Element(('','score')) | |
139 form = data_form.Form('form', title=_('scores')) | |
140 for line in scores.split('\n'): | |
141 field = data_form.Field('fixed', value = line) | |
142 form.addField(field) | |
143 score_elt.addChild(form.toElement()) | |
144 for winner in winners: | |
145 winner_elt = domish.Element(('','winner')) | |
146 winner_elt.addContent(winner) | |
147 score_elt.addChild(winner_elt) | |
148 for looser in loosers: | |
149 looser_elt = domish.Element(('','looser')) | |
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']""" |
63c9067a1499
Tarot game: invalid cards management
Goffi <goffi@goffi.org>
parents:
98
diff
changeset
|
158 error_elt = domish.Element(('','error')) |
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 |
63c9067a1499
Tarot game: invalid cards management
Goffi <goffi@goffi.org>
parents:
98
diff
changeset
|
166 |
94 | 167 def __next_player(self, game_data, next_pl = None): |
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'] | |
182 first_idx = game_data['players'].index(first) | |
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 | |
191 if suit_asked == None: | |
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 #TODO: gof: manage excuse (fool) | |
206 players_data = game_data['players_data'] | |
141
8c80d4dec7a8
mover Card class to tools/games and renamed it in TarotCard
Goffi <goffi@goffi.org>
parents:
134
diff
changeset
|
207 excuse = TarotCard(("atout","excuse")) |
95 | 208 |
209 #we first check if the Excuse was already player | |
210 #and if somebody is waiting for a card | |
94 | 211 for player in game_data['players']: |
212 if players_data[player]['wait_for_low']: | |
213 #the excuse owner has to give a card to somebody | |
214 if winner == player: | |
215 #the excuse owner win the trick, we check if we have something to give | |
216 for card in played: | |
217 if card.points == 0.5: | |
218 pl_waiting = players_data[player]['wait_for_low'] | |
219 played.remove(card) | |
220 players_data[pl_waiting]['levees'].append(card) | |
221 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 | 222 return |
94 | 223 return |
224 | |
225 if not excuse in played: | |
95 | 226 #the Excuse is not on the table, nothing to do |
94 | 227 return |
228 | |
95 | 229 excuse_player = None #Who has played the Excuse ? |
94 | 230 for player in game_data['players']: |
231 if players_data[player]['played'] == excuse: | |
232 excuse_player = player | |
233 break | |
234 | |
235 if excuse_player == winner: | |
236 return #the excuse player win the trick, nothing to do | |
237 | |
238 #first we remove the excuse from played cards | |
239 played.remove(excuse) | |
240 #then we give it back to the original owner | |
241 owner_levees = players_data[excuse_player]['levees'] | |
242 owner_levees.append(excuse) | |
243 #finally we give a low card to the trick winner | |
244 low_card = None | |
95 | 245 #We look backward in cards won by the Excuse owner to |
246 #find a low value card | |
94 | 247 for card_idx in range(len(owner_levees)-1, -1, -1): |
248 if owner_levees[card_idx].points == 0.5: | |
249 low_card = owner_levees[card_idx] | |
250 del owner_levees[card_idx] | |
251 players_data[winner]['levees'].append(low_card) | |
252 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}) | |
253 break | |
254 if not low_card: #The player has no low card yet | |
255 #TODO: manage case when player never win a trick with low card | |
256 players_data[excuse_player]['wait_for_low'] = winner | |
257 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}) | |
258 | |
259 | |
260 def __calculate_scores(self, game_data): | |
95 | 261 """The game is finished, time to know who won :) |
262 @param game_data: data of the game | |
263 @return: tuple with (string victory message, list of winners, list of loosers)""" | |
94 | 264 players_data = game_data['players_data'] |
265 levees = players_data[game_data['attaquant']]['levees'] | |
266 score = 0 | |
267 nb_bouts = 0 | |
95 | 268 bouts = [] |
94 | 269 for card in levees: |
270 if card.bout: | |
271 nb_bouts +=1 | |
95 | 272 bouts.append(card.value) |
94 | 273 score += card.points |
95 | 274 |
275 #We now check if there is no bug in score calculation | |
276 check_score = 0 | |
277 defenseurs = game_data['players'][:] | |
278 defenseurs.remove(game_data['attaquant']) | |
279 for defenseur in defenseurs: | |
280 for card in players_data[defenseur]['levees']: | |
281 check_score+=card.points | |
98
dd556233a1b1
Tarot Plugin: Garde Sans and Garde Contre are now managed
Goffi <goffi@goffi.org>
parents:
96
diff
changeset
|
282 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
|
283 for card in game_data['chien']: |
dd556233a1b1
Tarot Plugin: Garde Sans and Garde Contre are now managed
Goffi <goffi@goffi.org>
parents:
96
diff
changeset
|
284 check_score+=card.points |
95 | 285 assert (score + check_score == 91) |
286 | |
94 | 287 point_limit = None |
288 if nb_bouts == 3: | |
289 point_limit = 36 | |
290 elif nb_bouts == 2: | |
291 point_limit = 41 | |
292 elif nb_bouts == 1: | |
293 point_limit = 51 | |
294 else: | |
295 point_limit = 56 | |
95 | 296 if game_data['contrat'] == 'Petite': |
297 contrat_mult = 1 | |
298 elif game_data['contrat'] == 'Garde': | |
299 contrat_mult = 2 | |
300 elif game_data['contrat'] == 'Garde Sans': | |
301 contrat_mult = 4 | |
302 elif game_data['contrat'] == 'Garde Contre': | |
303 contrat_mult = 6 | |
304 else: | |
305 error(_('Internal error: contrat not managed (mispelled ?)')) | |
306 | |
94 | 307 victory = (score >= point_limit) |
95 | 308 margin = score - point_limit |
309 points_defenseur = (-margin + 25) * contrat_mult | |
310 winners = [] | |
311 loosers = [] | |
312 player_score = {} | |
313 for player in game_data['players']: | |
314 #TODO: adjust this for 3 and 5 players variants | |
315 #TODO: manage bonuses (petit au bout, poignée, chelem) | |
316 player_score[player] = points_defenseur if player != game_data['attaquant'] else points_defenseur * -3 | |
317 players_data[player]['score'] += player_score[player] #we add score of this game to the global score | |
318 if player_score[player] > 0: | |
319 winners.append(player) | |
320 else: | |
321 loosers.append(player) | |
94 | 322 |
147
dc692acde155
plugin Tarot: separator doen't appear anymore when attacked has no bout in scores
Goffi <goffi@goffi.org>
parents:
141
diff
changeset
|
323 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'} |
95 | 324 scores_str+='\n' |
325 for player in game_data['players']: | |
326 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']} | |
327 debug(scores_str) | |
328 | |
329 return (scores_str, winners, loosers) | |
94 | 330 |
99
63c9067a1499
Tarot game: invalid cards management
Goffi <goffi@goffi.org>
parents:
98
diff
changeset
|
331 def __invalid_cards(self, game_data, cards): |
63c9067a1499
Tarot game: invalid cards management
Goffi <goffi@goffi.org>
parents:
98
diff
changeset
|
332 """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
|
333 @param game_data: Game data |
63c9067a1499
Tarot game: invalid cards management
Goffi <goffi@goffi.org>
parents:
98
diff
changeset
|
334 @param cards: cards the player want to play |
63c9067a1499
Tarot game: invalid cards management
Goffi <goffi@goffi.org>
parents:
98
diff
changeset
|
335 @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
|
336 forbidden_cards = [] |
63c9067a1499
Tarot game: invalid cards management
Goffi <goffi@goffi.org>
parents:
98
diff
changeset
|
337 if game_data['stage'] == 'ecart': |
63c9067a1499
Tarot game: invalid cards management
Goffi <goffi@goffi.org>
parents:
98
diff
changeset
|
338 for card in cards: |
63c9067a1499
Tarot game: invalid cards management
Goffi <goffi@goffi.org>
parents:
98
diff
changeset
|
339 if card.bout or card.value=="roi": |
63c9067a1499
Tarot game: invalid cards management
Goffi <goffi@goffi.org>
parents:
98
diff
changeset
|
340 forbidden_cards.append(card) |
63c9067a1499
Tarot game: invalid cards management
Goffi <goffi@goffi.org>
parents:
98
diff
changeset
|
341 #TODO: manage case where atouts (trumps) are in the dog |
63c9067a1499
Tarot game: invalid cards management
Goffi <goffi@goffi.org>
parents:
98
diff
changeset
|
342 elif game_data['stage'] == 'play': |
63c9067a1499
Tarot game: invalid cards management
Goffi <goffi@goffi.org>
parents:
98
diff
changeset
|
343 biggest_atout = None |
63c9067a1499
Tarot game: invalid cards management
Goffi <goffi@goffi.org>
parents:
98
diff
changeset
|
344 suit_asked = None |
63c9067a1499
Tarot game: invalid cards management
Goffi <goffi@goffi.org>
parents:
98
diff
changeset
|
345 players = game_data['players'] |
63c9067a1499
Tarot game: invalid cards management
Goffi <goffi@goffi.org>
parents:
98
diff
changeset
|
346 players_data = game_data['players_data'] |
63c9067a1499
Tarot game: invalid cards management
Goffi <goffi@goffi.org>
parents:
98
diff
changeset
|
347 idx = players.index(game_data['first_player']) |
63c9067a1499
Tarot game: invalid cards management
Goffi <goffi@goffi.org>
parents:
98
diff
changeset
|
348 current_idx = game_data['current_player'] |
63c9067a1499
Tarot game: invalid cards management
Goffi <goffi@goffi.org>
parents:
98
diff
changeset
|
349 current_player = players[current_idx] |
63c9067a1499
Tarot game: invalid cards management
Goffi <goffi@goffi.org>
parents:
98
diff
changeset
|
350 if idx == current_idx: |
63c9067a1499
Tarot game: invalid cards management
Goffi <goffi@goffi.org>
parents:
98
diff
changeset
|
351 #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
|
352 return forbidden_cards |
63c9067a1499
Tarot game: invalid cards management
Goffi <goffi@goffi.org>
parents:
98
diff
changeset
|
353 while (idx != current_idx): |
63c9067a1499
Tarot game: invalid cards management
Goffi <goffi@goffi.org>
parents:
98
diff
changeset
|
354 player = players[idx] |
63c9067a1499
Tarot game: invalid cards management
Goffi <goffi@goffi.org>
parents:
98
diff
changeset
|
355 played_card = players_data[player]['played'] |
63c9067a1499
Tarot game: invalid cards management
Goffi <goffi@goffi.org>
parents:
98
diff
changeset
|
356 if not suit_asked and played_card.value != "excuse": |
63c9067a1499
Tarot game: invalid cards management
Goffi <goffi@goffi.org>
parents:
98
diff
changeset
|
357 suit_asked = played_card.suit |
63c9067a1499
Tarot game: invalid cards management
Goffi <goffi@goffi.org>
parents:
98
diff
changeset
|
358 if played_card.suit == "atout" and played_card > biggest_atout: |
63c9067a1499
Tarot game: invalid cards management
Goffi <goffi@goffi.org>
parents:
98
diff
changeset
|
359 biggest_atout = played_card |
63c9067a1499
Tarot game: invalid cards management
Goffi <goffi@goffi.org>
parents:
98
diff
changeset
|
360 idx = (idx + 1) % len(players) |
63c9067a1499
Tarot game: invalid cards management
Goffi <goffi@goffi.org>
parents:
98
diff
changeset
|
361 has_suit = False #True if there is one card of the asked suit in the hand of the player |
63c9067a1499
Tarot game: invalid cards management
Goffi <goffi@goffi.org>
parents:
98
diff
changeset
|
362 has_atout = False |
63c9067a1499
Tarot game: invalid cards management
Goffi <goffi@goffi.org>
parents:
98
diff
changeset
|
363 biggest_hand_atout = None |
63c9067a1499
Tarot game: invalid cards management
Goffi <goffi@goffi.org>
parents:
98
diff
changeset
|
364 |
63c9067a1499
Tarot game: invalid cards management
Goffi <goffi@goffi.org>
parents:
98
diff
changeset
|
365 for hand_card in game_data['hand'][current_player]: |
63c9067a1499
Tarot game: invalid cards management
Goffi <goffi@goffi.org>
parents:
98
diff
changeset
|
366 if hand_card.suit == suit_asked: |
63c9067a1499
Tarot game: invalid cards management
Goffi <goffi@goffi.org>
parents:
98
diff
changeset
|
367 has_suit = True |
63c9067a1499
Tarot game: invalid cards management
Goffi <goffi@goffi.org>
parents:
98
diff
changeset
|
368 if hand_card.suit == "atout": |
63c9067a1499
Tarot game: invalid cards management
Goffi <goffi@goffi.org>
parents:
98
diff
changeset
|
369 has_atout = True |
63c9067a1499
Tarot game: invalid cards management
Goffi <goffi@goffi.org>
parents:
98
diff
changeset
|
370 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
|
371 biggest_hand_atout = hand_card |
63c9067a1499
Tarot game: invalid cards management
Goffi <goffi@goffi.org>
parents:
98
diff
changeset
|
372 |
63c9067a1499
Tarot game: invalid cards management
Goffi <goffi@goffi.org>
parents:
98
diff
changeset
|
373 assert len(cards) == 1 |
63c9067a1499
Tarot game: invalid cards management
Goffi <goffi@goffi.org>
parents:
98
diff
changeset
|
374 card = cards[0] |
63c9067a1499
Tarot game: invalid cards management
Goffi <goffi@goffi.org>
parents:
98
diff
changeset
|
375 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
|
376 forbidden_cards.append(card) |
63c9067a1499
Tarot game: invalid cards management
Goffi <goffi@goffi.org>
parents:
98
diff
changeset
|
377 return forbidden_cards |
63c9067a1499
Tarot game: invalid cards management
Goffi <goffi@goffi.org>
parents:
98
diff
changeset
|
378 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
|
379 forbidden_cards.append(card) |
63c9067a1499
Tarot game: invalid cards management
Goffi <goffi@goffi.org>
parents:
98
diff
changeset
|
380 return forbidden_cards |
63c9067a1499
Tarot game: invalid cards management
Goffi <goffi@goffi.org>
parents:
98
diff
changeset
|
381 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
|
382 forbidden_cards.append(card) |
63c9067a1499
Tarot game: invalid cards management
Goffi <goffi@goffi.org>
parents:
98
diff
changeset
|
383 else: |
63c9067a1499
Tarot game: invalid cards management
Goffi <goffi@goffi.org>
parents:
98
diff
changeset
|
384 error(_('Internal error: unmanaged game stage')) |
63c9067a1499
Tarot game: invalid cards management
Goffi <goffi@goffi.org>
parents:
98
diff
changeset
|
385 return forbidden_cards |
63c9067a1499
Tarot game: invalid cards management
Goffi <goffi@goffi.org>
parents:
98
diff
changeset
|
386 |
63c9067a1499
Tarot game: invalid cards management
Goffi <goffi@goffi.org>
parents:
98
diff
changeset
|
387 |
98
dd556233a1b1
Tarot Plugin: Garde Sans and Garde Contre are now managed
Goffi <goffi@goffi.org>
parents:
96
diff
changeset
|
388 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
|
389 """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
|
390 game_data['stage'] = "play" |
dd556233a1b1
Tarot Plugin: Garde Sans and Garde Contre are now managed
Goffi <goffi@goffi.org>
parents:
96
diff
changeset
|
391 next_player_idx = game_data['current_player'] = (game_data['init_player'] + 1) % len(game_data['players']) #the player after the dealer start |
dd556233a1b1
Tarot Plugin: Garde Sans and Garde Contre are now managed
Goffi <goffi@goffi.org>
parents:
96
diff
changeset
|
392 game_data['first_player'] = next_player = game_data['players'][next_player_idx] |
dd556233a1b1
Tarot Plugin: Garde Sans and Garde Contre are now managed
Goffi <goffi@goffi.org>
parents:
96
diff
changeset
|
393 to_jid = jid.JID(room_jid.userhost()+"/"+next_player) #FIXME: gof: |
dd556233a1b1
Tarot Plugin: Garde Sans and Garde Contre are now managed
Goffi <goffi@goffi.org>
parents:
96
diff
changeset
|
394 mess = self.createGameElt(to_jid) |
dd556233a1b1
Tarot Plugin: Garde Sans and Garde Contre are now managed
Goffi <goffi@goffi.org>
parents:
96
diff
changeset
|
395 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
|
396 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
|
397 |
91 | 398 |
90 | 399 def createGame(self, room_jid_param, players, profile_key='@DEFAULT@'): |
88 | 400 """Create a new game""" |
401 debug (_("Creating Tarot game")) | |
90 | 402 room_jid = jid.JID(room_jid_param) |
88 | 403 profile = self.host.memory.getProfileName(profile_key) |
404 if not profile: | |
405 error (_("profile %s is unknown") % profile_key) | |
406 return | |
407 if False: #gof: self.games.has_key(room_jid): | |
90 | 408 warning (_("Tarot game already started in room %s") % room_jid.userhost()) |
88 | 409 else: |
93 | 410 room_nick = self.host.plugins["XEP_0045"].getRoomNick(room_jid.userhost(), profile) |
411 if not room_nick: | |
412 error ('Internal error') | |
413 return | |
414 referee = room_jid.userhost() + '/' + room_nick | |
90 | 415 status = {} |
91 | 416 players_data = {} |
90 | 417 for player in players: |
95 | 418 players_data[player] = {'score':0} |
90 | 419 status[player] = "init" |
95 | 420 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 | 421 for player in players: |
422 mess = self.createGameElt(jid.JID(room_jid.userhost()+'/'+player)) | |
423 mess.firstChildElement().addChild(self.__create_started_elt(players)) | |
424 self.host.profiles[profile].xmlstream.send(mess) | |
425 | |
92 | 426 def newPlayerReady(self, player, referee, profile_key='@DEFAULT@'): |
90 | 427 """Must be called when player is ready to start a new game""" |
428 profile = self.host.memory.getProfileName(profile_key) | |
429 if not profile: | |
430 error (_("profile %s is unknown") % profile_key) | |
431 return | |
432 debug ('new player ready: %s' % profile) | |
433 mess = self.createGameElt(jid.JID(referee)) | |
91 | 434 ready_elt = mess.firstChildElement().addElement('player_ready') |
92 | 435 ready_elt['player'] = player |
91 | 436 self.host.profiles[profile].xmlstream.send(mess) |
437 | |
92 | 438 def contratChoosed(self, player, referee, contrat, profile_key='@DEFAULT@'): |
91 | 439 """Must be call by player when the contrat is selected |
92 | 440 @param player: player's name |
91 | 441 @param referee: arbiter jid |
442 @contrat: contrat choosed (must be the exact same string than in the give list options) | |
443 @profile_key: profile | |
444 """ | |
445 profile = self.host.memory.getProfileName(profile_key) | |
446 if not profile: | |
447 error (_("profile %s is unknown") % profile_key) | |
448 return | |
449 debug (_('contrat [%(contrat)s] choosed by %(profile)s') % {'contrat':contrat, 'profile':profile}) | |
450 mess = self.createGameElt(jid.JID(referee)) | |
451 contrat_elt = mess.firstChildElement().addElement(('','contrat_choosed'), content=contrat) | |
92 | 452 contrat_elt['player'] = player |
90 | 453 self.host.profiles[profile].xmlstream.send(mess) |
88 | 454 |
92 | 455 def play_cards(self, player, referee, cards, profile_key='@DEFAULT@'): |
456 """Must be call by player when the contrat is selected | |
457 @param player: player's name | |
458 @param referee: arbiter jid | |
459 @cards: cards played (list of tuples) | |
460 @profile_key: profile | |
461 """ | |
462 profile = self.host.memory.getProfileName(profile_key) | |
463 if not profile: | |
464 error (_("profile %s is unknown") % profile_key) | |
465 return | |
466 debug (_('Cards played by %(profile)s: [%(cards)s]') % {'profile':profile,'cards':cards}) | |
467 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
|
468 playcard_elt = mess.firstChildElement().addChild(self.__card_list_to_xml(TarotCard.from_tuples(cards), 'cards_played')) |
92 | 469 playcard_elt['player'] = player |
470 self.host.profiles[profile].xmlstream.send(mess) | |
88 | 471 |
92 | 472 def newGame(self, room_jid, profile): |
88 | 473 """Launch a new round""" |
474 debug (_('new Tarot game')) | |
475 deck = self.deck_ordered[:] | |
476 random.shuffle(deck) | |
91 | 477 game_data = self.games[room_jid.userhost()] |
478 players = game_data['players'] | |
479 players_data = game_data['players_data'] | |
480 current_player = game_data['current_player'] | |
92 | 481 game_data['stage'] = "init" |
94 | 482 game_data['first_player'] = None #first player for the current trick |
95 | 483 game_data['contrat'] = None |
91 | 484 hand = game_data['hand'] = {} |
485 hand_size = game_data['hand_size'] | |
486 chien = game_data['chien'] = [] | |
88 | 487 for i in range(4): #TODO: distribute according to real Tarot rules (3 by 3 counter-clockwise, 1 card at once to chien) |
488 hand[players[i]] = deck[0:hand_size] | |
489 del deck[0:hand_size] | |
92 | 490 chien.extend(deck) |
88 | 491 del(deck[:]) |
492 | |
493 for player in players: | |
90 | 494 to_jid = jid.JID(room_jid.userhost()+"/"+player) #FIXME: gof: |
495 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
|
496 mess.firstChildElement().addChild(self.__card_list_to_xml(hand[player], 'hand')) |
92 | 497 self.host.profiles[profile].xmlstream.send(mess) |
91 | 498 players_data[player]['contrat'] = None |
92 | 499 players_data[player]['levees'] = [] #cards won |
94 | 500 players_data[player]['played'] = None #card on the table |
501 players_data[player]['wait_for_low'] = None #Used when a player wait for a low card because of excuse | |
91 | 502 |
503 pl_idx = game_data['current_player'] = (game_data['init_player'] + 1) % len(players) #the player after the dealer start | |
504 player = players[pl_idx] | |
505 to_jid = jid.JID(room_jid.userhost()+"/"+player) #FIXME: gof: | |
506 mess = self.createGameElt(to_jid) | |
507 mess.firstChildElement().addChild(self.__ask_contrat()) | |
92 | 508 self.host.profiles[profile].xmlstream.send(mess) |
90 | 509 |
510 | |
511 def card_game_cmd(self, mess_elt, profile): | |
93 | 512 from_jid = jid.JID(mess_elt['from']) |
513 room_jid = jid.JID(from_jid.userhost()) | |
90 | 514 game_elt = mess_elt.firstChildElement() |
92 | 515 game_data = self.games[room_jid.userhost()] |
516 players_data = game_data['players_data'] | |
517 | |
518 for elt in game_elt.elements(): | |
91 | 519 |
92 | 520 if elt.name == 'started': #new game created |
90 | 521 players = [] |
522 for player in elt.elements(): | |
523 players.append(unicode(player)) | |
93 | 524 self.host.bridge.tarotGameStarted(room_jid.userhost(), from_jid.full(), players, profile) |
91 | 525 |
92 | 526 elif elt.name == 'player_ready': #ready to play |
527 player = elt['player'] | |
90 | 528 status = self.games[room_jid.userhost()]['status'] |
529 nb_players = len(self.games[room_jid.userhost()]['players']) | |
530 status[player] = 'ready' | |
531 debug (_('Player %(player)s is ready to start [status: %(status)s]') % {'player':player, 'status':status}) | |
91 | 532 if status.values().count('ready') == nb_players: #everybody is ready, we can start the game |
92 | 533 self.newGame(room_jid, profile) |
88 | 534 |
90 | 535 elif elt.name == 'hand': #a new hand has been received |
92 | 536 self.host.bridge.tarotGameNew(room_jid.userhost(), self.__xml_to_list(elt), profile) |
91 | 537 |
538 elif elt.name == 'contrat': #it's time to choose contrat | |
539 form = data_form.Form.fromElement(elt.firstChildElement()) | |
102 | 540 xml_data = dataForm2xml(form) |
92 | 541 self.host.bridge.tarotGameChooseContrat(room_jid.userhost(), xml_data, profile) |
91 | 542 |
92 | 543 elif elt.name == 'contrat_choosed': |
91 | 544 #TODO: check we receive the contrat from the right person |
92 | 545 #TODO: use proper XEP-0004 way for answering form |
546 player = elt['player'] | |
547 players_data[player]['contrat'] = unicode(elt) | |
91 | 548 contrats = [players_data[player]['contrat'] for player in game_data['players']] |
549 if contrats.count(None): | |
550 #not everybody has choosed his contrat, it's next one turn | |
551 player = self.__next_player(game_data) | |
552 to_jid = jid.JID(room_jid.userhost()+"/"+player) #FIXME: gof: | |
553 mess = self.createGameElt(to_jid) | |
554 mess.firstChildElement().addChild(self.__ask_contrat()) | |
92 | 555 self.host.profiles[profile].xmlstream.send(mess) |
91 | 556 else: |
94 | 557 #TODO: gof: manage "everybody pass" case |
91 | 558 best_contrat = [None, "Passe"] |
559 for player in game_data['players']: | |
560 contrat = players_data[player]['contrat'] | |
561 idx_best = self.contrats.index(best_contrat[1]) | |
562 idx_pl = self.contrats.index(contrat) | |
563 if idx_pl > idx_best: | |
564 best_contrat[0] = player | |
565 best_contrat[1] = contrat | |
566 debug (_("%(player)s win the bid with %(contrat)s") % {'player':best_contrat[0],'contrat':best_contrat[1]}) | |
95 | 567 game_data['contrat'] = best_contrat[1] |
98
dd556233a1b1
Tarot Plugin: Garde Sans and Garde Contre are now managed
Goffi <goffi@goffi.org>
parents:
96
diff
changeset
|
568 |
dd556233a1b1
Tarot Plugin: Garde Sans and Garde Contre are now managed
Goffi <goffi@goffi.org>
parents:
96
diff
changeset
|
569 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
|
570 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
|
571 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
|
572 else: |
dd556233a1b1
Tarot Plugin: Garde Sans and Garde Contre are now managed
Goffi <goffi@goffi.org>
parents:
96
diff
changeset
|
573 #Time to show the chien to everybody |
dd556233a1b1
Tarot Plugin: Garde Sans and Garde Contre are now managed
Goffi <goffi@goffi.org>
parents:
96
diff
changeset
|
574 to_jid = jid.JID(room_jid.userhost()) #FIXME: gof: |
dd556233a1b1
Tarot Plugin: Garde Sans and Garde Contre are now managed
Goffi <goffi@goffi.org>
parents:
96
diff
changeset
|
575 mess = self.createGameElt(to_jid) |
dd556233a1b1
Tarot Plugin: Garde Sans and Garde Contre are now managed
Goffi <goffi@goffi.org>
parents:
96
diff
changeset
|
576 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
|
577 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
|
578 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
|
579 #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
|
580 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
|
581 del game_data['chien'][:] |
dd556233a1b1
Tarot Plugin: Garde Sans and Garde Contre are now managed
Goffi <goffi@goffi.org>
parents:
96
diff
changeset
|
582 |
dd556233a1b1
Tarot Plugin: Garde Sans and Garde Contre are now managed
Goffi <goffi@goffi.org>
parents:
96
diff
changeset
|
583 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
|
584 #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
|
585 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
|
586 del game_data['chien'][:] |
91 | 587 |
92 | 588 |
589 elif elt.name == 'chien': #we have received the chien | |
590 debug (_("tarot: chien received")) | |
591 data = {"attaquant":elt['attaquant']} | |
592 game_data['stage'] = "ecart" | |
593 game_data['attaquant'] = elt['attaquant'] | |
594 self.host.bridge.tarotGameShowCards(room_jid.userhost(), "chien", self.__xml_to_list(elt), data, profile) | |
595 | |
596 elif elt.name == 'cards_played': | |
597 if game_data['stage'] == "ecart": | |
598 #TODO: show atouts (trumps) if player put some in écart | |
599 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
|
600 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
|
601 #we now check validity of card |
63c9067a1499
Tarot game: invalid cards management
Goffi <goffi@goffi.org>
parents:
98
diff
changeset
|
602 invalid_cards = self.__invalid_cards(game_data, list_cards) |
63c9067a1499
Tarot game: invalid cards management
Goffi <goffi@goffi.org>
parents:
98
diff
changeset
|
603 if invalid_cards: |
63c9067a1499
Tarot game: invalid cards management
Goffi <goffi@goffi.org>
parents:
98
diff
changeset
|
604 mess = self.createGameElt(jid.JID(room_jid.userhost()+'/'+elt['player'])) |
63c9067a1499
Tarot game: invalid cards management
Goffi <goffi@goffi.org>
parents:
98
diff
changeset
|
605 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
|
606 self.host.profiles[profile].xmlstream.send(mess) |
63c9067a1499
Tarot game: invalid cards management
Goffi <goffi@goffi.org>
parents:
98
diff
changeset
|
607 return |
63c9067a1499
Tarot game: invalid cards management
Goffi <goffi@goffi.org>
parents:
98
diff
changeset
|
608 |
95 | 609 #FIXME: gof: manage Garde Sans & Garde Contre cases |
98
dd556233a1b1
Tarot Plugin: Garde Sans and Garde Contre are now managed
Goffi <goffi@goffi.org>
parents:
96
diff
changeset
|
610 players_data[elt['player']]['levees'].extend(list_cards) #we add the chien to attaquant's levées |
95 | 611 for card in list_cards: |
612 game_data['hand'][elt['player']].remove(card) | |
98
dd556233a1b1
Tarot Plugin: Garde Sans and Garde Contre are now managed
Goffi <goffi@goffi.org>
parents:
96
diff
changeset
|
613 |
dd556233a1b1
Tarot Plugin: Garde Sans and Garde Contre are now managed
Goffi <goffi@goffi.org>
parents:
96
diff
changeset
|
614 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
|
615 |
93 | 616 elif game_data['stage'] == "play": |
617 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
|
618 cards = TarotCard.from_tuples(self.__xml_to_list(elt)) |
94 | 619 |
620 if mess_elt['type'] == 'groupchat': | |
621 self.host.bridge.tarotGameCardsPlayed(room_jid.userhost(), elt['player'], self.__xml_to_list(elt), profile) | |
622 else: | |
99
63c9067a1499
Tarot game: invalid cards management
Goffi <goffi@goffi.org>
parents:
98
diff
changeset
|
623 #we first check validity of card |
63c9067a1499
Tarot game: invalid cards management
Goffi <goffi@goffi.org>
parents:
98
diff
changeset
|
624 invalid_cards = self.__invalid_cards(game_data, cards) |
63c9067a1499
Tarot game: invalid cards management
Goffi <goffi@goffi.org>
parents:
98
diff
changeset
|
625 if invalid_cards: |
63c9067a1499
Tarot game: invalid cards management
Goffi <goffi@goffi.org>
parents:
98
diff
changeset
|
626 mess = self.createGameElt(jid.JID(room_jid.userhost()+'/'+current_player)) |
63c9067a1499
Tarot game: invalid cards management
Goffi <goffi@goffi.org>
parents:
98
diff
changeset
|
627 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
|
628 self.host.profiles[profile].xmlstream.send(mess) |
63c9067a1499
Tarot game: invalid cards management
Goffi <goffi@goffi.org>
parents:
98
diff
changeset
|
629 return |
93 | 630 #the card played is ok, we forward it to everybody |
94 | 631 #first we remove it from the hand and put in on the table |
93 | 632 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
|
633 players_data[current_player]['played'] = cards[0] |
93 | 634 |
635 #then we forward the message | |
636 mess = self.createGameElt(room_jid) | |
637 playcard_elt = mess.firstChildElement().addChild(elt) | |
638 self.host.profiles[profile].xmlstream.send(mess) | |
639 | |
94 | 640 #Did everybody played ? |
641 played = [players_data[player]['played'] for player in game_data['players']] | |
95 | 642 if all(played): |
643 #everybody has played | |
94 | 644 winner = self.__winner(game_data) |
645 debug (_('The winner of this trick is %s') % winner) | |
646 #the winner win the trick | |
647 self.__excuse_hack(game_data, played, winner) | |
648 players_data[elt['player']]['levees'].extend(played) | |
649 #nothing left on the table | |
650 for player in game_data['players']: | |
651 players_data[player]['played'] = None | |
652 if len(game_data['hand'][current_player]) == 0: | |
653 #no card lef: the game is finished | |
95 | 654 to_jid = jid.JID(room_jid.userhost()) #FIXME: gof: |
655 mess = self.createGameElt(to_jid) | |
656 chien_elt = mess.firstChildElement().addChild(self.__give_scores(*self.__calculate_scores(game_data))) | |
657 self.host.profiles[profile].xmlstream.send(mess) | |
94 | 658 return |
659 #next player is the winner | |
660 next_player = game_data['first_player'] = self.__next_player(game_data, winner) | |
661 else: | |
662 next_player = self.__next_player(game_data) | |
663 | |
93 | 664 #finally, we tell to the next player to play |
665 to_jid = jid.JID(room_jid.userhost()+"/"+next_player) #FIXME: gof: | |
666 mess = self.createGameElt(to_jid) | |
667 yourturn_elt = mess.firstChildElement().addElement('your_turn') | |
668 self.host.profiles[profile].xmlstream.send(mess) | |
669 | |
92 | 670 elif elt.name == 'your_turn': |
671 self.host.bridge.tarotGameYourTurn(room_jid.userhost(), profile) | |
91 | 672 |
95 | 673 elif elt.name == 'score': |
674 form_elt = elt.elements(name='x',uri='jabber:x:data').next() | |
675 winners = [] | |
676 loosers = [] | |
677 for winner in elt.elements(name='winner', uri=''): | |
678 winners.append(unicode(winner)) | |
679 for looser in elt.elements(name='looser', uri=''): | |
680 loosers.append(unicode(looser)) | |
681 form = data_form.Form.fromElement(form_elt) | |
102 | 682 xml_data = dataForm2xml(form) |
95 | 683 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
|
684 elif elt.name == 'error': |
63c9067a1499
Tarot game: invalid cards management
Goffi <goffi@goffi.org>
parents:
98
diff
changeset
|
685 if elt['type'] == 'invalid_cards': |
63c9067a1499
Tarot game: invalid cards management
Goffi <goffi@goffi.org>
parents:
98
diff
changeset
|
686 played_cards = self.__xml_to_list(elt.elements(name='played',uri='').next()) |
63c9067a1499
Tarot game: invalid cards management
Goffi <goffi@goffi.org>
parents:
98
diff
changeset
|
687 invalid_cards = self.__xml_to_list(elt.elements(name='invalid',uri='').next()) |
63c9067a1499
Tarot game: invalid cards management
Goffi <goffi@goffi.org>
parents:
98
diff
changeset
|
688 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
|
689 else: |
63c9067a1499
Tarot game: invalid cards management
Goffi <goffi@goffi.org>
parents:
98
diff
changeset
|
690 error (_('Unmanaged error type: %s') % elt['type']) |
63c9067a1499
Tarot game: invalid cards management
Goffi <goffi@goffi.org>
parents:
98
diff
changeset
|
691 else: |
63c9067a1499
Tarot game: invalid cards management
Goffi <goffi@goffi.org>
parents:
98
diff
changeset
|
692 error (_('Unmanaged card game element: %s') % elt.name) |
95 | 693 |
90 | 694 def getHandler(self, profile): |
695 return CardGameHandler(self) | |
88 | 696 |
90 | 697 class CardGameHandler (XMPPHandler): |
698 implements(iwokkel.IDisco) | |
699 | |
700 def __init__(self, plugin_parent): | |
701 self.plugin_parent = plugin_parent | |
702 self.host = plugin_parent.host | |
703 | |
704 def connectionInitialized(self): | |
705 self.xmlstream.addObserver(CG_REQUEST, self.plugin_parent.card_game_cmd, profile = self.parent.profile) | |
706 | |
707 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
|
708 return [disco.DiscoFeature(NS_CG)] |
90 | 709 |
710 def getDiscoItems(self, requestor, target, nodeIdentifier=''): | |
711 return [] | |
712 |