Mercurial > libervia-backend
annotate plugins/plugin_misc_tarot.py @ 111:6c927140ba82
sortilege fix: it launches again now
author | Goffi <goffi@goffi.org> |
---|---|
date | Tue, 29 Jun 2010 16:07:51 +0800 |
parents | 94011f553cd0 |
children | 7201851d9aed |
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 |
88 | 36 |
37 try: | |
38 from twisted.words.protocols.xmlstream import XMPPHandler | |
39 except ImportError: | |
40 from wokkel.subprotocols import XMPPHandler | |
41 | |
90 | 42 MESSAGE = '/message' |
43 NS_CG = 'http://www.goffi.org/protocol/card_game' | |
44 CG_TAG = 'card_game' | |
45 CG_REQUEST = MESSAGE + '/' + CG_TAG + '[@xmlns="' + NS_CG + '"]' | |
88 | 46 |
47 PLUGIN_INFO = { | |
48 "name": "Tarot cards plugin", | |
49 "import_name": "Tarot", | |
50 "type": "Misc", | |
51 "protocols": [], | |
52 "dependencies": ["XEP_0045"], | |
53 "main": "Tarot", | |
90 | 54 "handler": "yes", |
88 | 55 "description": _("""Implementation of Tarot card game""") |
56 } | |
57 | |
102 | 58 suits_order = ['pique', 'coeur', 'trefle', 'carreau', 'atout'] #I have switched the usual order 'trefle' and 'carreau' because card are more easy to see if suit colour change (black, red, black, red) |
94 | 59 values_order = map(str,range(1,11))+["valet","cavalier","dame","roi"] |
60 | |
61 class Card(): | |
62 """This class is used to represent a car logically""" | |
63 #TODO: move this in a library in tools, and share this with frontends (e.g. card_game in wix use the same class) | |
64 | |
65 def __init__(self, tuple_card): | |
66 """@param tuple_card: tuple (suit, value)""" | |
67 self.suit, self.value = tuple_card | |
68 self.bout = True if self.suit=="atout" and self.value in ["1","21","excuse"] else False | |
69 if self.bout or self.value == "roi": | |
70 self.points = 4.5 | |
71 elif self.value == "dame": | |
72 self.points = 3.5 | |
73 elif self.value == "cavalier": | |
74 self.points = 2.5 | |
75 elif self.value == "valet": | |
76 self.points = 1.5 | |
77 else: | |
78 self.points = 0.5 | |
79 | |
80 def get_tuple(self): | |
81 return (self.suit,self.value) | |
82 | |
83 @staticmethod | |
84 def from_tuples(tuple_list): | |
85 result = [] | |
86 for card_tuple in tuple_list: | |
87 result.append(Card(card_tuple)) | |
88 return result | |
89 | |
90 def __cmp__(self, other): | |
91 if other == None: | |
92 return 1 | |
93 if self.suit != other.suit: | |
94 idx1 = suits_order.index(self.suit) | |
95 idx2 = suits_order.index(other.suit) | |
96 return idx1.__cmp__(idx2) | |
97 if self.suit == 'atout': | |
98 if self.value == other.value == 'excuse': | |
99 return 0 | |
100 if self.value == 'excuse': | |
101 return -1 | |
102 if other.value == 'excuse': | |
103 return 1 | |
104 return int(self.value).__cmp__(int(other.value)) | |
105 #at this point we have the same suit which is not 'atout' | |
106 idx1 = values_order.index(self.value) | |
107 idx2 = values_order.index(other.value) | |
108 return idx1.__cmp__(idx2) | |
109 | |
110 def __str__(self): | |
111 return "[%s,%s]" % (self.suit, self.value) | |
112 | |
88 | 113 class Tarot(): |
114 | |
115 def __init__(self, host): | |
116 info(_("Plugin Tarot initialization")) | |
117 self.host = host | |
118 self.games={} | |
91 | 119 self.contrats = [_('Passe'), _('Petite'), _('Garde'), _('Garde Sans'), _('Garde Contre')] |
90 | 120 host.bridge.addMethod("tarotGameCreate", ".communication", in_sign='sass', out_sign='', method=self.createGame) #args: room_jid, players, profile |
92 | 121 host.bridge.addMethod("tarotGameReady", ".communication", in_sign='sss', out_sign='', method=self.newPlayerReady) #args: player, referee, profile |
122 host.bridge.addMethod("tarotGameContratChoosed", ".communication", in_sign='ssss', out_sign='', method=self.contratChoosed) #args: player, referee, contrat, profile | |
123 host.bridge.addMethod("tarotGamePlayCards", ".communication", in_sign='ssa(ss)s', out_sign='', method=self.play_cards) #args: player, referee, cards, profile | |
90 | 124 host.bridge.addSignal("tarotGameStarted", ".communication", signature='ssass') #args: room_jid, referee, players, profile |
125 host.bridge.addSignal("tarotGameNew", ".communication", signature='sa(ss)s') #args: room_jid, hand, profile | |
92 | 126 host.bridge.addSignal("tarotGameChooseContrat", ".communication", signature='sss') #args: room_jid, xml_data, profile |
127 host.bridge.addSignal("tarotGameShowCards", ".communication", signature='ssa(ss)a{ss}s') #args: room_jid, type ["chien", "poignée",...], cards, data[dict], profile | |
93 | 128 host.bridge.addSignal("tarotGameCardsPlayed", ".communication", signature='ssa(ss)s') #args: room_jid, player, type ["chien", "poignée",...], cards, data[dict], profile |
92 | 129 host.bridge.addSignal("tarotGameYourTurn", ".communication", signature='ss') #args: room_jid, profile |
95 | 130 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
|
131 host.bridge.addSignal("tarotGameInvalidCards", ".communication", signature='ssa(ss)a(ss)s') #args: room_jid, game phase, played_cards, invalid_cards, profile |
88 | 132 self.deck_ordered = [] |
92 | 133 for value in ['excuse']+map(str,range(1,22)): |
98
dd556233a1b1
Tarot Plugin: Garde Sans and Garde Contre are now managed
Goffi <goffi@goffi.org>
parents:
96
diff
changeset
|
134 self.deck_ordered.append(Card(("atout",value))) |
92 | 135 for suit in ["pique", "coeur", "carreau", "trefle"]: |
88 | 136 for value in map(str,range(1,11))+["valet","cavalier","dame","roi"]: |
98
dd556233a1b1
Tarot Plugin: Garde Sans and Garde Contre are now managed
Goffi <goffi@goffi.org>
parents:
96
diff
changeset
|
137 self.deck_ordered.append(Card((suit, value))) |
88 | 138 |
92 | 139 def createGameElt(self, to_jid, type="normal"): |
140 type = "normal" if to_jid.resource else "groupchat" | |
90 | 141 elt = domish.Element(('jabber:client','message')) |
142 elt["to"] = to_jid.full() | |
92 | 143 elt["type"] = type |
90 | 144 elt.addElement((NS_CG, CG_TAG)) |
145 return elt | |
146 | |
98
dd556233a1b1
Tarot Plugin: Garde Sans and Garde Contre are now managed
Goffi <goffi@goffi.org>
parents:
96
diff
changeset
|
147 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
|
148 """Convert a card list to domish element""" |
92 | 149 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
|
150 for card in cards_list: |
90 | 151 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
|
152 card_elt['suit'] = card.suit |
dd556233a1b1
Tarot Plugin: Garde Sans and Garde Contre are now managed
Goffi <goffi@goffi.org>
parents:
96
diff
changeset
|
153 card_elt['value'] = card.value |
92 | 154 cards_list_elt.addChild(card_elt) |
155 return cards_list_elt | |
90 | 156 |
92 | 157 def __xml_to_list(self, cards_list_elt): |
158 """Convert a domish element with cards to a list of tuples""" | |
159 cards_list = [] | |
160 for card in cards_list_elt.elements(): | |
161 cards_list.append((card['suit'], card['value'])) | |
162 return cards_list | |
90 | 163 |
164 def __create_started_elt(self, players): | |
165 """Create a game_started domish element""" | |
166 started_elt = domish.Element(('','started')) | |
167 idx = 0 | |
168 for player in players: | |
169 player_elt = domish.Element(('','player')) | |
170 player_elt.addContent(player) | |
171 player_elt['index'] = str(idx) | |
172 idx+=1 | |
173 started_elt.addChild(player_elt) | |
174 return started_elt | |
175 | |
91 | 176 def __ask_contrat(self): |
177 """Create a element for asking contrat""" | |
178 contrat_elt = domish.Element(('','contrat')) | |
179 form = data_form.Form('form', title=_('contrat selection')) | |
180 field = data_form.Field('list-single', 'contrat', options=map(data_form.Option, self.contrats), required=True) | |
181 form.addField(field) | |
182 contrat_elt.addChild(form.toElement()) | |
183 return contrat_elt | |
184 | |
95 | 185 def __give_scores(self, scores, winners, loosers): |
186 """Create an element to give scores | |
187 @param scores: unicode (can contain line feed) | |
188 @param winners: list of unicode nicks of winners | |
189 @param loosers: list of unicode nicks of loosers""" | |
190 | |
191 score_elt = domish.Element(('','score')) | |
192 form = data_form.Form('form', title=_('scores')) | |
193 for line in scores.split('\n'): | |
194 field = data_form.Field('fixed', value = line) | |
195 form.addField(field) | |
196 score_elt.addChild(form.toElement()) | |
197 for winner in winners: | |
198 winner_elt = domish.Element(('','winner')) | |
199 winner_elt.addContent(winner) | |
200 score_elt.addChild(winner_elt) | |
201 for looser in loosers: | |
202 looser_elt = domish.Element(('','looser')) | |
203 looser_elt.addContent(looser) | |
204 score_elt.addChild(looser_elt) | |
205 return score_elt | |
206 | |
99
63c9067a1499
Tarot game: invalid cards management
Goffi <goffi@goffi.org>
parents:
98
diff
changeset
|
207 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
|
208 """Create a element for invalid_cards error |
63c9067a1499
Tarot game: invalid cards management
Goffi <goffi@goffi.org>
parents:
98
diff
changeset
|
209 @param list_cards: list of Card |
63c9067a1499
Tarot game: invalid cards management
Goffi <goffi@goffi.org>
parents:
98
diff
changeset
|
210 @param game_phase: phase of the game ['ecart', 'play']""" |
63c9067a1499
Tarot game: invalid cards management
Goffi <goffi@goffi.org>
parents:
98
diff
changeset
|
211 error_elt = domish.Element(('','error')) |
63c9067a1499
Tarot game: invalid cards management
Goffi <goffi@goffi.org>
parents:
98
diff
changeset
|
212 played_elt = self.__card_list_to_xml(played_cards, 'played') |
63c9067a1499
Tarot game: invalid cards management
Goffi <goffi@goffi.org>
parents:
98
diff
changeset
|
213 invalid_elt = self.__card_list_to_xml(invalid_cards, 'invalid') |
63c9067a1499
Tarot game: invalid cards management
Goffi <goffi@goffi.org>
parents:
98
diff
changeset
|
214 error_elt['type'] = 'invalid_cards' |
63c9067a1499
Tarot game: invalid cards management
Goffi <goffi@goffi.org>
parents:
98
diff
changeset
|
215 error_elt['phase'] = game_phase |
63c9067a1499
Tarot game: invalid cards management
Goffi <goffi@goffi.org>
parents:
98
diff
changeset
|
216 error_elt.addChild(played_elt) |
63c9067a1499
Tarot game: invalid cards management
Goffi <goffi@goffi.org>
parents:
98
diff
changeset
|
217 error_elt.addChild(invalid_elt) |
63c9067a1499
Tarot game: invalid cards management
Goffi <goffi@goffi.org>
parents:
98
diff
changeset
|
218 return error_elt |
63c9067a1499
Tarot game: invalid cards management
Goffi <goffi@goffi.org>
parents:
98
diff
changeset
|
219 |
94 | 220 def __next_player(self, game_data, next_pl = None): |
221 """Increment player number & return player name | |
222 @param next_pl: if given, then next_player is forced to this one | |
223 """ | |
224 if next_pl: | |
225 game_data['current_player'] = game_data['players'].index(next_pl) | |
226 return next_pl | |
227 else: | |
228 pl_idx = game_data['current_player'] = (game_data['current_player'] + 1) % len(game_data['players']) | |
229 return game_data['players'][pl_idx] | |
230 | |
231 def __winner(self, game_data): | |
232 """give the nick of the player who win this trick""" | |
233 players_data = game_data['players_data'] | |
234 first = game_data['first_player'] | |
235 first_idx = game_data['players'].index(first) | |
236 suit_asked = None | |
237 strongest = None | |
238 winner = None | |
239 for idx in [(first_idx + i) % 4 for i in range(4)]: | |
240 player = game_data['players'][idx] | |
241 card = players_data[player]['played'] | |
242 if card.value == "excuse": | |
243 continue | |
244 if suit_asked == None: | |
245 suit_asked = card.suit | |
246 if (card.suit == suit_asked or card.suit == "atout") and card > strongest: | |
247 strongest = card | |
248 winner = player | |
95 | 249 assert winner |
94 | 250 return winner |
251 | |
252 def __excuse_hack(self, game_data, played, winner): | |
95 | 253 """give a low card to other team and keep excuse if trick is lost |
254 @param game_data: data of the game | |
255 @param played: cards currently on the table | |
256 @param winner: nick of the trick winner""" | |
94 | 257 #TODO: manage the case where excuse is played on the last trick (and lost) |
258 #TODO: gof: manage excuse (fool) | |
259 players_data = game_data['players_data'] | |
260 excuse = Card(("atout","excuse")) | |
95 | 261 |
262 #we first check if the Excuse was already player | |
263 #and if somebody is waiting for a card | |
94 | 264 for player in game_data['players']: |
265 if players_data[player]['wait_for_low']: | |
266 #the excuse owner has to give a card to somebody | |
267 if winner == player: | |
268 #the excuse owner win the trick, we check if we have something to give | |
269 for card in played: | |
270 if card.points == 0.5: | |
271 pl_waiting = players_data[player]['wait_for_low'] | |
272 played.remove(card) | |
273 players_data[pl_waiting]['levees'].append(card) | |
274 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 | 275 return |
94 | 276 return |
277 | |
278 if not excuse in played: | |
95 | 279 #the Excuse is not on the table, nothing to do |
94 | 280 return |
281 | |
95 | 282 excuse_player = None #Who has played the Excuse ? |
94 | 283 for player in game_data['players']: |
284 if players_data[player]['played'] == excuse: | |
285 excuse_player = player | |
286 break | |
287 | |
288 if excuse_player == winner: | |
289 return #the excuse player win the trick, nothing to do | |
290 | |
291 #first we remove the excuse from played cards | |
292 played.remove(excuse) | |
293 #then we give it back to the original owner | |
294 owner_levees = players_data[excuse_player]['levees'] | |
295 owner_levees.append(excuse) | |
296 #finally we give a low card to the trick winner | |
297 low_card = None | |
95 | 298 #We look backward in cards won by the Excuse owner to |
299 #find a low value card | |
94 | 300 for card_idx in range(len(owner_levees)-1, -1, -1): |
301 if owner_levees[card_idx].points == 0.5: | |
302 low_card = owner_levees[card_idx] | |
303 del owner_levees[card_idx] | |
304 players_data[winner]['levees'].append(low_card) | |
305 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}) | |
306 break | |
307 if not low_card: #The player has no low card yet | |
308 #TODO: manage case when player never win a trick with low card | |
309 players_data[excuse_player]['wait_for_low'] = winner | |
310 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}) | |
311 | |
312 | |
313 def __calculate_scores(self, game_data): | |
95 | 314 """The game is finished, time to know who won :) |
315 @param game_data: data of the game | |
316 @return: tuple with (string victory message, list of winners, list of loosers)""" | |
94 | 317 players_data = game_data['players_data'] |
318 levees = players_data[game_data['attaquant']]['levees'] | |
319 score = 0 | |
320 nb_bouts = 0 | |
95 | 321 bouts = [] |
94 | 322 for card in levees: |
323 if card.bout: | |
324 nb_bouts +=1 | |
95 | 325 bouts.append(card.value) |
94 | 326 score += card.points |
95 | 327 |
328 #We now check if there is no bug in score calculation | |
329 check_score = 0 | |
330 defenseurs = game_data['players'][:] | |
331 defenseurs.remove(game_data['attaquant']) | |
332 for defenseur in defenseurs: | |
333 for card in players_data[defenseur]['levees']: | |
334 check_score+=card.points | |
98
dd556233a1b1
Tarot Plugin: Garde Sans and Garde Contre are now managed
Goffi <goffi@goffi.org>
parents:
96
diff
changeset
|
335 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
|
336 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
|
337 check_score+=card.points |
95 | 338 assert (score + check_score == 91) |
339 | |
94 | 340 point_limit = None |
341 if nb_bouts == 3: | |
342 point_limit = 36 | |
343 elif nb_bouts == 2: | |
344 point_limit = 41 | |
345 elif nb_bouts == 1: | |
346 point_limit = 51 | |
347 else: | |
348 point_limit = 56 | |
95 | 349 if game_data['contrat'] == 'Petite': |
350 contrat_mult = 1 | |
351 elif game_data['contrat'] == 'Garde': | |
352 contrat_mult = 2 | |
353 elif game_data['contrat'] == 'Garde Sans': | |
354 contrat_mult = 4 | |
355 elif game_data['contrat'] == 'Garde Contre': | |
356 contrat_mult = 6 | |
357 else: | |
358 error(_('Internal error: contrat not managed (mispelled ?)')) | |
359 | |
94 | 360 victory = (score >= point_limit) |
95 | 361 margin = score - point_limit |
362 points_defenseur = (-margin + 25) * contrat_mult | |
363 winners = [] | |
364 loosers = [] | |
365 player_score = {} | |
366 for player in game_data['players']: | |
367 #TODO: adjust this for 3 and 5 players variants | |
368 #TODO: manage bonuses (petit au bout, poignée, chelem) | |
369 player_score[player] = points_defenseur if player != game_data['attaquant'] else points_defenseur * -3 | |
370 players_data[player]['score'] += player_score[player] #we add score of this game to the global score | |
371 if player_score[player] > 0: | |
372 winners.append(player) | |
373 else: | |
374 loosers.append(player) | |
94 | 375 |
95 | 376 scores_str = _('The attacker (%(attaquant)s) makes %(points)i and needs to make %(point_limit)i (%(nb_bouts)s oulder%(plural)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 '', 'bouts':','.join(map(str,bouts)), 'victory': 'win' if victory else 'loose'} |
377 scores_str+='\n' | |
378 for player in game_data['players']: | |
379 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']} | |
380 debug(scores_str) | |
381 | |
382 return (scores_str, winners, loosers) | |
94 | 383 |
99
63c9067a1499
Tarot game: invalid cards management
Goffi <goffi@goffi.org>
parents:
98
diff
changeset
|
384 def __invalid_cards(self, game_data, cards): |
63c9067a1499
Tarot game: invalid cards management
Goffi <goffi@goffi.org>
parents:
98
diff
changeset
|
385 """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
|
386 @param game_data: Game data |
63c9067a1499
Tarot game: invalid cards management
Goffi <goffi@goffi.org>
parents:
98
diff
changeset
|
387 @param cards: cards the player want to play |
63c9067a1499
Tarot game: invalid cards management
Goffi <goffi@goffi.org>
parents:
98
diff
changeset
|
388 @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
|
389 forbidden_cards = [] |
63c9067a1499
Tarot game: invalid cards management
Goffi <goffi@goffi.org>
parents:
98
diff
changeset
|
390 if game_data['stage'] == 'ecart': |
63c9067a1499
Tarot game: invalid cards management
Goffi <goffi@goffi.org>
parents:
98
diff
changeset
|
391 for card in cards: |
63c9067a1499
Tarot game: invalid cards management
Goffi <goffi@goffi.org>
parents:
98
diff
changeset
|
392 if card.bout or card.value=="roi": |
63c9067a1499
Tarot game: invalid cards management
Goffi <goffi@goffi.org>
parents:
98
diff
changeset
|
393 forbidden_cards.append(card) |
63c9067a1499
Tarot game: invalid cards management
Goffi <goffi@goffi.org>
parents:
98
diff
changeset
|
394 #TODO: manage case where atouts (trumps) are in the dog |
63c9067a1499
Tarot game: invalid cards management
Goffi <goffi@goffi.org>
parents:
98
diff
changeset
|
395 elif game_data['stage'] == 'play': |
63c9067a1499
Tarot game: invalid cards management
Goffi <goffi@goffi.org>
parents:
98
diff
changeset
|
396 biggest_atout = None |
63c9067a1499
Tarot game: invalid cards management
Goffi <goffi@goffi.org>
parents:
98
diff
changeset
|
397 suit_asked = None |
63c9067a1499
Tarot game: invalid cards management
Goffi <goffi@goffi.org>
parents:
98
diff
changeset
|
398 players = game_data['players'] |
63c9067a1499
Tarot game: invalid cards management
Goffi <goffi@goffi.org>
parents:
98
diff
changeset
|
399 players_data = game_data['players_data'] |
63c9067a1499
Tarot game: invalid cards management
Goffi <goffi@goffi.org>
parents:
98
diff
changeset
|
400 idx = players.index(game_data['first_player']) |
63c9067a1499
Tarot game: invalid cards management
Goffi <goffi@goffi.org>
parents:
98
diff
changeset
|
401 current_idx = game_data['current_player'] |
63c9067a1499
Tarot game: invalid cards management
Goffi <goffi@goffi.org>
parents:
98
diff
changeset
|
402 current_player = players[current_idx] |
63c9067a1499
Tarot game: invalid cards management
Goffi <goffi@goffi.org>
parents:
98
diff
changeset
|
403 if idx == current_idx: |
63c9067a1499
Tarot game: invalid cards management
Goffi <goffi@goffi.org>
parents:
98
diff
changeset
|
404 #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
|
405 return forbidden_cards |
63c9067a1499
Tarot game: invalid cards management
Goffi <goffi@goffi.org>
parents:
98
diff
changeset
|
406 while (idx != current_idx): |
63c9067a1499
Tarot game: invalid cards management
Goffi <goffi@goffi.org>
parents:
98
diff
changeset
|
407 player = players[idx] |
63c9067a1499
Tarot game: invalid cards management
Goffi <goffi@goffi.org>
parents:
98
diff
changeset
|
408 played_card = players_data[player]['played'] |
63c9067a1499
Tarot game: invalid cards management
Goffi <goffi@goffi.org>
parents:
98
diff
changeset
|
409 if not suit_asked and played_card.value != "excuse": |
63c9067a1499
Tarot game: invalid cards management
Goffi <goffi@goffi.org>
parents:
98
diff
changeset
|
410 suit_asked = played_card.suit |
63c9067a1499
Tarot game: invalid cards management
Goffi <goffi@goffi.org>
parents:
98
diff
changeset
|
411 if played_card.suit == "atout" and played_card > biggest_atout: |
63c9067a1499
Tarot game: invalid cards management
Goffi <goffi@goffi.org>
parents:
98
diff
changeset
|
412 biggest_atout = played_card |
63c9067a1499
Tarot game: invalid cards management
Goffi <goffi@goffi.org>
parents:
98
diff
changeset
|
413 idx = (idx + 1) % len(players) |
63c9067a1499
Tarot game: invalid cards management
Goffi <goffi@goffi.org>
parents:
98
diff
changeset
|
414 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
|
415 has_atout = False |
63c9067a1499
Tarot game: invalid cards management
Goffi <goffi@goffi.org>
parents:
98
diff
changeset
|
416 biggest_hand_atout = None |
63c9067a1499
Tarot game: invalid cards management
Goffi <goffi@goffi.org>
parents:
98
diff
changeset
|
417 |
63c9067a1499
Tarot game: invalid cards management
Goffi <goffi@goffi.org>
parents:
98
diff
changeset
|
418 for hand_card in game_data['hand'][current_player]: |
63c9067a1499
Tarot game: invalid cards management
Goffi <goffi@goffi.org>
parents:
98
diff
changeset
|
419 if hand_card.suit == suit_asked: |
63c9067a1499
Tarot game: invalid cards management
Goffi <goffi@goffi.org>
parents:
98
diff
changeset
|
420 has_suit = True |
63c9067a1499
Tarot game: invalid cards management
Goffi <goffi@goffi.org>
parents:
98
diff
changeset
|
421 if hand_card.suit == "atout": |
63c9067a1499
Tarot game: invalid cards management
Goffi <goffi@goffi.org>
parents:
98
diff
changeset
|
422 has_atout = True |
63c9067a1499
Tarot game: invalid cards management
Goffi <goffi@goffi.org>
parents:
98
diff
changeset
|
423 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
|
424 biggest_hand_atout = hand_card |
63c9067a1499
Tarot game: invalid cards management
Goffi <goffi@goffi.org>
parents:
98
diff
changeset
|
425 |
63c9067a1499
Tarot game: invalid cards management
Goffi <goffi@goffi.org>
parents:
98
diff
changeset
|
426 assert len(cards) == 1 |
63c9067a1499
Tarot game: invalid cards management
Goffi <goffi@goffi.org>
parents:
98
diff
changeset
|
427 card = cards[0] |
63c9067a1499
Tarot game: invalid cards management
Goffi <goffi@goffi.org>
parents:
98
diff
changeset
|
428 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
|
429 forbidden_cards.append(card) |
63c9067a1499
Tarot game: invalid cards management
Goffi <goffi@goffi.org>
parents:
98
diff
changeset
|
430 return forbidden_cards |
63c9067a1499
Tarot game: invalid cards management
Goffi <goffi@goffi.org>
parents:
98
diff
changeset
|
431 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
|
432 forbidden_cards.append(card) |
63c9067a1499
Tarot game: invalid cards management
Goffi <goffi@goffi.org>
parents:
98
diff
changeset
|
433 return forbidden_cards |
63c9067a1499
Tarot game: invalid cards management
Goffi <goffi@goffi.org>
parents:
98
diff
changeset
|
434 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
|
435 forbidden_cards.append(card) |
63c9067a1499
Tarot game: invalid cards management
Goffi <goffi@goffi.org>
parents:
98
diff
changeset
|
436 else: |
63c9067a1499
Tarot game: invalid cards management
Goffi <goffi@goffi.org>
parents:
98
diff
changeset
|
437 error(_('Internal error: unmanaged game stage')) |
63c9067a1499
Tarot game: invalid cards management
Goffi <goffi@goffi.org>
parents:
98
diff
changeset
|
438 return forbidden_cards |
63c9067a1499
Tarot game: invalid cards management
Goffi <goffi@goffi.org>
parents:
98
diff
changeset
|
439 |
63c9067a1499
Tarot game: invalid cards management
Goffi <goffi@goffi.org>
parents:
98
diff
changeset
|
440 |
98
dd556233a1b1
Tarot Plugin: Garde Sans and Garde Contre are now managed
Goffi <goffi@goffi.org>
parents:
96
diff
changeset
|
441 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
|
442 """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
|
443 game_data['stage'] = "play" |
dd556233a1b1
Tarot Plugin: Garde Sans and Garde Contre are now managed
Goffi <goffi@goffi.org>
parents:
96
diff
changeset
|
444 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
|
445 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
|
446 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
|
447 mess = self.createGameElt(to_jid) |
dd556233a1b1
Tarot Plugin: Garde Sans and Garde Contre are now managed
Goffi <goffi@goffi.org>
parents:
96
diff
changeset
|
448 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
|
449 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
|
450 |
91 | 451 |
90 | 452 def createGame(self, room_jid_param, players, profile_key='@DEFAULT@'): |
88 | 453 """Create a new game""" |
454 debug (_("Creating Tarot game")) | |
90 | 455 room_jid = jid.JID(room_jid_param) |
88 | 456 profile = self.host.memory.getProfileName(profile_key) |
457 if not profile: | |
458 error (_("profile %s is unknown") % profile_key) | |
459 return | |
460 if False: #gof: self.games.has_key(room_jid): | |
90 | 461 warning (_("Tarot game already started in room %s") % room_jid.userhost()) |
88 | 462 else: |
93 | 463 room_nick = self.host.plugins["XEP_0045"].getRoomNick(room_jid.userhost(), profile) |
464 if not room_nick: | |
465 error ('Internal error') | |
466 return | |
467 referee = room_jid.userhost() + '/' + room_nick | |
90 | 468 status = {} |
91 | 469 players_data = {} |
90 | 470 for player in players: |
95 | 471 players_data[player] = {'score':0} |
90 | 472 status[player] = "init" |
95 | 473 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 | 474 for player in players: |
475 mess = self.createGameElt(jid.JID(room_jid.userhost()+'/'+player)) | |
476 mess.firstChildElement().addChild(self.__create_started_elt(players)) | |
477 self.host.profiles[profile].xmlstream.send(mess) | |
478 | |
92 | 479 def newPlayerReady(self, player, referee, profile_key='@DEFAULT@'): |
90 | 480 """Must be called when player is ready to start a new game""" |
481 profile = self.host.memory.getProfileName(profile_key) | |
482 if not profile: | |
483 error (_("profile %s is unknown") % profile_key) | |
484 return | |
485 debug ('new player ready: %s' % profile) | |
486 mess = self.createGameElt(jid.JID(referee)) | |
91 | 487 ready_elt = mess.firstChildElement().addElement('player_ready') |
92 | 488 ready_elt['player'] = player |
91 | 489 self.host.profiles[profile].xmlstream.send(mess) |
490 | |
92 | 491 def contratChoosed(self, player, referee, contrat, profile_key='@DEFAULT@'): |
91 | 492 """Must be call by player when the contrat is selected |
92 | 493 @param player: player's name |
91 | 494 @param referee: arbiter jid |
495 @contrat: contrat choosed (must be the exact same string than in the give list options) | |
496 @profile_key: profile | |
497 """ | |
498 profile = self.host.memory.getProfileName(profile_key) | |
499 if not profile: | |
500 error (_("profile %s is unknown") % profile_key) | |
501 return | |
502 debug (_('contrat [%(contrat)s] choosed by %(profile)s') % {'contrat':contrat, 'profile':profile}) | |
503 mess = self.createGameElt(jid.JID(referee)) | |
504 contrat_elt = mess.firstChildElement().addElement(('','contrat_choosed'), content=contrat) | |
92 | 505 contrat_elt['player'] = player |
90 | 506 self.host.profiles[profile].xmlstream.send(mess) |
88 | 507 |
92 | 508 def play_cards(self, player, referee, cards, profile_key='@DEFAULT@'): |
509 """Must be call by player when the contrat is selected | |
510 @param player: player's name | |
511 @param referee: arbiter jid | |
512 @cards: cards played (list of tuples) | |
513 @profile_key: profile | |
514 """ | |
515 profile = self.host.memory.getProfileName(profile_key) | |
516 if not profile: | |
517 error (_("profile %s is unknown") % profile_key) | |
518 return | |
519 debug (_('Cards played by %(profile)s: [%(cards)s]') % {'profile':profile,'cards':cards}) | |
520 mess = self.createGameElt(jid.JID(referee)) | |
98
dd556233a1b1
Tarot Plugin: Garde Sans and Garde Contre are now managed
Goffi <goffi@goffi.org>
parents:
96
diff
changeset
|
521 playcard_elt = mess.firstChildElement().addChild(self.__card_list_to_xml(Card.from_tuples(cards), 'cards_played')) |
92 | 522 playcard_elt['player'] = player |
523 self.host.profiles[profile].xmlstream.send(mess) | |
88 | 524 |
92 | 525 def newGame(self, room_jid, profile): |
88 | 526 """Launch a new round""" |
527 debug (_('new Tarot game')) | |
528 deck = self.deck_ordered[:] | |
529 random.shuffle(deck) | |
91 | 530 game_data = self.games[room_jid.userhost()] |
531 players = game_data['players'] | |
532 players_data = game_data['players_data'] | |
533 current_player = game_data['current_player'] | |
92 | 534 game_data['stage'] = "init" |
94 | 535 game_data['first_player'] = None #first player for the current trick |
95 | 536 game_data['contrat'] = None |
91 | 537 hand = game_data['hand'] = {} |
538 hand_size = game_data['hand_size'] | |
539 chien = game_data['chien'] = [] | |
88 | 540 for i in range(4): #TODO: distribute according to real Tarot rules (3 by 3 counter-clockwise, 1 card at once to chien) |
541 hand[players[i]] = deck[0:hand_size] | |
542 del deck[0:hand_size] | |
92 | 543 chien.extend(deck) |
88 | 544 del(deck[:]) |
545 | |
546 for player in players: | |
90 | 547 to_jid = jid.JID(room_jid.userhost()+"/"+player) #FIXME: gof: |
548 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
|
549 mess.firstChildElement().addChild(self.__card_list_to_xml(hand[player], 'hand')) |
92 | 550 self.host.profiles[profile].xmlstream.send(mess) |
91 | 551 players_data[player]['contrat'] = None |
92 | 552 players_data[player]['levees'] = [] #cards won |
94 | 553 players_data[player]['played'] = None #card on the table |
554 players_data[player]['wait_for_low'] = None #Used when a player wait for a low card because of excuse | |
91 | 555 |
556 pl_idx = game_data['current_player'] = (game_data['init_player'] + 1) % len(players) #the player after the dealer start | |
557 player = players[pl_idx] | |
558 to_jid = jid.JID(room_jid.userhost()+"/"+player) #FIXME: gof: | |
559 mess = self.createGameElt(to_jid) | |
560 mess.firstChildElement().addChild(self.__ask_contrat()) | |
92 | 561 self.host.profiles[profile].xmlstream.send(mess) |
90 | 562 |
563 | |
564 def card_game_cmd(self, mess_elt, profile): | |
565 print "\n\nCARD GAME command received (profile=%s): %s" % (profile, mess_elt.toXml()) | |
93 | 566 from_jid = jid.JID(mess_elt['from']) |
567 room_jid = jid.JID(from_jid.userhost()) | |
90 | 568 game_elt = mess_elt.firstChildElement() |
92 | 569 game_data = self.games[room_jid.userhost()] |
570 players_data = game_data['players_data'] | |
571 | |
572 for elt in game_elt.elements(): | |
91 | 573 |
92 | 574 if elt.name == 'started': #new game created |
90 | 575 players = [] |
576 for player in elt.elements(): | |
577 players.append(unicode(player)) | |
93 | 578 self.host.bridge.tarotGameStarted(room_jid.userhost(), from_jid.full(), players, profile) |
91 | 579 |
92 | 580 elif elt.name == 'player_ready': #ready to play |
581 player = elt['player'] | |
90 | 582 status = self.games[room_jid.userhost()]['status'] |
583 nb_players = len(self.games[room_jid.userhost()]['players']) | |
584 status[player] = 'ready' | |
585 debug (_('Player %(player)s is ready to start [status: %(status)s]') % {'player':player, 'status':status}) | |
91 | 586 if status.values().count('ready') == nb_players: #everybody is ready, we can start the game |
92 | 587 self.newGame(room_jid, profile) |
88 | 588 |
90 | 589 elif elt.name == 'hand': #a new hand has been received |
92 | 590 self.host.bridge.tarotGameNew(room_jid.userhost(), self.__xml_to_list(elt), profile) |
91 | 591 |
592 elif elt.name == 'contrat': #it's time to choose contrat | |
593 form = data_form.Form.fromElement(elt.firstChildElement()) | |
102 | 594 xml_data = dataForm2xml(form) |
92 | 595 self.host.bridge.tarotGameChooseContrat(room_jid.userhost(), xml_data, profile) |
91 | 596 |
92 | 597 elif elt.name == 'contrat_choosed': |
91 | 598 #TODO: check we receive the contrat from the right person |
92 | 599 #TODO: use proper XEP-0004 way for answering form |
600 player = elt['player'] | |
601 players_data[player]['contrat'] = unicode(elt) | |
91 | 602 contrats = [players_data[player]['contrat'] for player in game_data['players']] |
603 if contrats.count(None): | |
604 #not everybody has choosed his contrat, it's next one turn | |
605 player = self.__next_player(game_data) | |
606 to_jid = jid.JID(room_jid.userhost()+"/"+player) #FIXME: gof: | |
607 mess = self.createGameElt(to_jid) | |
608 mess.firstChildElement().addChild(self.__ask_contrat()) | |
92 | 609 self.host.profiles[profile].xmlstream.send(mess) |
91 | 610 else: |
94 | 611 #TODO: gof: manage "everybody pass" case |
91 | 612 best_contrat = [None, "Passe"] |
613 for player in game_data['players']: | |
614 contrat = players_data[player]['contrat'] | |
615 idx_best = self.contrats.index(best_contrat[1]) | |
616 idx_pl = self.contrats.index(contrat) | |
617 if idx_pl > idx_best: | |
618 best_contrat[0] = player | |
619 best_contrat[1] = contrat | |
620 debug (_("%(player)s win the bid with %(contrat)s") % {'player':best_contrat[0],'contrat':best_contrat[1]}) | |
95 | 621 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
|
622 |
dd556233a1b1
Tarot Plugin: Garde Sans and Garde Contre are now managed
Goffi <goffi@goffi.org>
parents:
96
diff
changeset
|
623 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
|
624 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
|
625 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
|
626 else: |
dd556233a1b1
Tarot Plugin: Garde Sans and Garde Contre are now managed
Goffi <goffi@goffi.org>
parents:
96
diff
changeset
|
627 #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
|
628 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
|
629 mess = self.createGameElt(to_jid) |
dd556233a1b1
Tarot Plugin: Garde Sans and Garde Contre are now managed
Goffi <goffi@goffi.org>
parents:
96
diff
changeset
|
630 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
|
631 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
|
632 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
|
633 #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
|
634 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
|
635 del game_data['chien'][:] |
dd556233a1b1
Tarot Plugin: Garde Sans and Garde Contre are now managed
Goffi <goffi@goffi.org>
parents:
96
diff
changeset
|
636 |
dd556233a1b1
Tarot Plugin: Garde Sans and Garde Contre are now managed
Goffi <goffi@goffi.org>
parents:
96
diff
changeset
|
637 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
|
638 #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
|
639 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
|
640 del game_data['chien'][:] |
91 | 641 |
92 | 642 |
643 elif elt.name == 'chien': #we have received the chien | |
644 debug (_("tarot: chien received")) | |
645 data = {"attaquant":elt['attaquant']} | |
646 game_data['stage'] = "ecart" | |
647 game_data['attaquant'] = elt['attaquant'] | |
648 self.host.bridge.tarotGameShowCards(room_jid.userhost(), "chien", self.__xml_to_list(elt), data, profile) | |
649 | |
650 elif elt.name == 'cards_played': | |
651 if game_data['stage'] == "ecart": | |
652 #TODO: show atouts (trumps) if player put some in écart | |
653 assert (game_data['attaquant'] == elt['player']) #TODO: throw an xml error here | |
98
dd556233a1b1
Tarot Plugin: Garde Sans and Garde Contre are now managed
Goffi <goffi@goffi.org>
parents:
96
diff
changeset
|
654 list_cards = Card.from_tuples(self.__xml_to_list(elt)) |
99
63c9067a1499
Tarot game: invalid cards management
Goffi <goffi@goffi.org>
parents:
98
diff
changeset
|
655 #we now check validity of card |
63c9067a1499
Tarot game: invalid cards management
Goffi <goffi@goffi.org>
parents:
98
diff
changeset
|
656 invalid_cards = self.__invalid_cards(game_data, list_cards) |
63c9067a1499
Tarot game: invalid cards management
Goffi <goffi@goffi.org>
parents:
98
diff
changeset
|
657 if invalid_cards: |
63c9067a1499
Tarot game: invalid cards management
Goffi <goffi@goffi.org>
parents:
98
diff
changeset
|
658 mess = self.createGameElt(jid.JID(room_jid.userhost()+'/'+elt['player'])) |
63c9067a1499
Tarot game: invalid cards management
Goffi <goffi@goffi.org>
parents:
98
diff
changeset
|
659 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
|
660 self.host.profiles[profile].xmlstream.send(mess) |
63c9067a1499
Tarot game: invalid cards management
Goffi <goffi@goffi.org>
parents:
98
diff
changeset
|
661 return |
63c9067a1499
Tarot game: invalid cards management
Goffi <goffi@goffi.org>
parents:
98
diff
changeset
|
662 |
95 | 663 #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
|
664 players_data[elt['player']]['levees'].extend(list_cards) #we add the chien to attaquant's levées |
95 | 665 for card in list_cards: |
666 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
|
667 |
dd556233a1b1
Tarot Plugin: Garde Sans and Garde Contre are now managed
Goffi <goffi@goffi.org>
parents:
96
diff
changeset
|
668 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
|
669 |
93 | 670 elif game_data['stage'] == "play": |
671 current_player = game_data['players'][game_data['current_player']] | |
98
dd556233a1b1
Tarot Plugin: Garde Sans and Garde Contre are now managed
Goffi <goffi@goffi.org>
parents:
96
diff
changeset
|
672 cards = Card.from_tuples(self.__xml_to_list(elt)) |
94 | 673 |
674 if mess_elt['type'] == 'groupchat': | |
675 self.host.bridge.tarotGameCardsPlayed(room_jid.userhost(), elt['player'], self.__xml_to_list(elt), profile) | |
676 else: | |
99
63c9067a1499
Tarot game: invalid cards management
Goffi <goffi@goffi.org>
parents:
98
diff
changeset
|
677 #we first check validity of card |
63c9067a1499
Tarot game: invalid cards management
Goffi <goffi@goffi.org>
parents:
98
diff
changeset
|
678 invalid_cards = self.__invalid_cards(game_data, cards) |
63c9067a1499
Tarot game: invalid cards management
Goffi <goffi@goffi.org>
parents:
98
diff
changeset
|
679 if invalid_cards: |
63c9067a1499
Tarot game: invalid cards management
Goffi <goffi@goffi.org>
parents:
98
diff
changeset
|
680 mess = self.createGameElt(jid.JID(room_jid.userhost()+'/'+current_player)) |
63c9067a1499
Tarot game: invalid cards management
Goffi <goffi@goffi.org>
parents:
98
diff
changeset
|
681 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
|
682 self.host.profiles[profile].xmlstream.send(mess) |
63c9067a1499
Tarot game: invalid cards management
Goffi <goffi@goffi.org>
parents:
98
diff
changeset
|
683 return |
93 | 684 #the card played is ok, we forward it to everybody |
94 | 685 #first we remove it from the hand and put in on the table |
93 | 686 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
|
687 players_data[current_player]['played'] = cards[0] |
93 | 688 |
689 #then we forward the message | |
690 mess = self.createGameElt(room_jid) | |
691 playcard_elt = mess.firstChildElement().addChild(elt) | |
692 self.host.profiles[profile].xmlstream.send(mess) | |
693 | |
94 | 694 #Did everybody played ? |
695 played = [players_data[player]['played'] for player in game_data['players']] | |
95 | 696 if all(played): |
697 #everybody has played | |
94 | 698 winner = self.__winner(game_data) |
699 debug (_('The winner of this trick is %s') % winner) | |
700 #the winner win the trick | |
701 self.__excuse_hack(game_data, played, winner) | |
702 players_data[elt['player']]['levees'].extend(played) | |
703 #nothing left on the table | |
704 for player in game_data['players']: | |
705 players_data[player]['played'] = None | |
706 if len(game_data['hand'][current_player]) == 0: | |
707 #no card lef: the game is finished | |
95 | 708 to_jid = jid.JID(room_jid.userhost()) #FIXME: gof: |
709 mess = self.createGameElt(to_jid) | |
710 chien_elt = mess.firstChildElement().addChild(self.__give_scores(*self.__calculate_scores(game_data))) | |
711 self.host.profiles[profile].xmlstream.send(mess) | |
94 | 712 return |
713 #next player is the winner | |
714 next_player = game_data['first_player'] = self.__next_player(game_data, winner) | |
715 else: | |
716 next_player = self.__next_player(game_data) | |
717 | |
93 | 718 #finally, we tell to the next player to play |
719 to_jid = jid.JID(room_jid.userhost()+"/"+next_player) #FIXME: gof: | |
720 mess = self.createGameElt(to_jid) | |
721 yourturn_elt = mess.firstChildElement().addElement('your_turn') | |
722 self.host.profiles[profile].xmlstream.send(mess) | |
723 | |
92 | 724 elif elt.name == 'your_turn': |
725 self.host.bridge.tarotGameYourTurn(room_jid.userhost(), profile) | |
91 | 726 |
95 | 727 elif elt.name == 'score': |
728 form_elt = elt.elements(name='x',uri='jabber:x:data').next() | |
729 winners = [] | |
730 loosers = [] | |
731 for winner in elt.elements(name='winner', uri=''): | |
732 winners.append(unicode(winner)) | |
733 for looser in elt.elements(name='looser', uri=''): | |
734 loosers.append(unicode(looser)) | |
735 form = data_form.Form.fromElement(form_elt) | |
102 | 736 xml_data = dataForm2xml(form) |
95 | 737 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
|
738 elif elt.name == 'error': |
63c9067a1499
Tarot game: invalid cards management
Goffi <goffi@goffi.org>
parents:
98
diff
changeset
|
739 if elt['type'] == 'invalid_cards': |
63c9067a1499
Tarot game: invalid cards management
Goffi <goffi@goffi.org>
parents:
98
diff
changeset
|
740 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
|
741 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
|
742 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
|
743 else: |
63c9067a1499
Tarot game: invalid cards management
Goffi <goffi@goffi.org>
parents:
98
diff
changeset
|
744 error (_('Unmanaged error type: %s') % elt['type']) |
63c9067a1499
Tarot game: invalid cards management
Goffi <goffi@goffi.org>
parents:
98
diff
changeset
|
745 else: |
63c9067a1499
Tarot game: invalid cards management
Goffi <goffi@goffi.org>
parents:
98
diff
changeset
|
746 error (_('Unmanaged card game element: %s') % elt.name) |
95 | 747 |
90 | 748 def getHandler(self, profile): |
749 return CardGameHandler(self) | |
88 | 750 |
90 | 751 class CardGameHandler (XMPPHandler): |
752 implements(iwokkel.IDisco) | |
753 | |
754 def __init__(self, plugin_parent): | |
755 self.plugin_parent = plugin_parent | |
756 self.host = plugin_parent.host | |
757 | |
758 def connectionInitialized(self): | |
759 self.xmlstream.addObserver(CG_REQUEST, self.plugin_parent.card_game_cmd, profile = self.parent.profile) | |
760 | |
761 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
|
762 return [disco.DiscoFeature(NS_CG)] |
90 | 763 |
764 def getDiscoItems(self, requestor, target, nodeIdentifier=''): | |
765 return [] | |
766 |