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