comparison src/plugins/plugin_misc_tarot.py @ 328:809733b8d9be

Tarot game: - draw game managed - we can now play continuously \o/
author Goffi <goffi@goffi.org>
date Mon, 23 May 2011 00:46:51 +0200
parents 5fc5e6a7e5c3
children 141eeb7cd9e6
comparison
equal deleted inserted replaced
327:7c9784658163 328:809733b8d9be
203 """give a low card to other team and keep excuse if trick is lost 203 """give a low card to other team and keep excuse if trick is lost
204 @param game_data: data of the game 204 @param game_data: data of the game
205 @param played: cards currently on the table 205 @param played: cards currently on the table
206 @param winner: nick of the trick winner""" 206 @param winner: nick of the trick winner"""
207 #TODO: manage the case where excuse is played on the last trick (and lost) 207 #TODO: manage the case where excuse is played on the last trick (and lost)
208 #TODO: gof: manage excuse (fool)
209 players_data = game_data['players_data'] 208 players_data = game_data['players_data']
210 excuse = TarotCard(("atout","excuse")) 209 excuse = TarotCard(("atout","excuse"))
211 210
212 #we first check if the Excuse was already played 211 #we first check if the Excuse was already played
213 #and if somebody is waiting for a card 212 #and if somebody is waiting for a card
258 #TODO: manage case when player never win a trick with low card 257 #TODO: manage case when player never win a trick with low card
259 players_data[excuse_player]['wait_for_low'] = winner 258 players_data[excuse_player]['wait_for_low'] = winner
260 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}) 259 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})
261 260
262 261
262 def __draw_game(self, game_data):
263 """The game is draw, no score change
264 @param game_data: data of the game
265 @return: tuple with (string victory message, list of winners, list of loosers)"""
266 players_data = game_data['players_data']
267 scores_str = _('Draw game')
268 scores_str+='\n'
269 for player in game_data['players']:
270 scores_str+=_("\n--\n%(player)s:\nscore for this game ==> %(score_game)i\ntotal score ==> %(total_score)i") % {'player':player, 'score_game':0, 'total_score': players_data[player]['score']}
271 debug(scores_str)
272
273 return (scores_str, [], [])
274
275
263 def __calculate_scores(self, game_data): 276 def __calculate_scores(self, game_data):
264 """The game is finished, time to know who won :) 277 """The game is finished, time to know who won :)
265 @param game_data: data of the game 278 @param game_data: data of the game
266 @return: tuple with (string victory message, list of winners, list of loosers)""" 279 @return: tuple with (string victory message, list of winners, list of loosers)"""
267 players_data = game_data['players_data'] 280 players_data = game_data['players_data']
545 game_data['first_player'] = None #first player for the current trick 558 game_data['first_player'] = None #first player for the current trick
546 game_data['contrat'] = None 559 game_data['contrat'] = None
547 hand = game_data['hand'] = {} 560 hand = game_data['hand'] = {}
548 hand_size = game_data['hand_size'] 561 hand_size = game_data['hand_size']
549 chien = game_data['chien'] = [] 562 chien = game_data['chien'] = []
550 for i in range(4): #TODO: distribute according to real Tarot rules (3 by 3 counter-clockwise, 1 card at once to chien) 563 for i in range(4):
551 hand[players[i]] = deck[0:hand_size] 564 hand[players[i]] = deck[0:hand_size]
552 del deck[0:hand_size] 565 del deck[0:hand_size]
553 chien.extend(deck) 566 chien.extend(deck)
554 del(deck[:]) 567 del(deck[:])
555 568
615 to_jid = jid.JID(room_jid.userhost()+"/"+player) #FIXME: gof: 628 to_jid = jid.JID(room_jid.userhost()+"/"+player) #FIXME: gof:
616 mess = self.createGameElt(to_jid) 629 mess = self.createGameElt(to_jid)
617 mess.firstChildElement().addChild(self.__ask_contrat()) 630 mess.firstChildElement().addChild(self.__ask_contrat())
618 self.host.profiles[profile].xmlstream.send(mess) 631 self.host.profiles[profile].xmlstream.send(mess)
619 else: 632 else:
620 #TODO: gof: manage "everybody pass" case
621 best_contrat = [None, "Passe"] 633 best_contrat = [None, "Passe"]
622 for player in game_data['players']: 634 for player in game_data['players']:
623 contrat = players_data[player]['contrat'] 635 contrat = players_data[player]['contrat']
624 idx_best = self.contrats.index(best_contrat[1]) 636 idx_best = self.contrats.index(best_contrat[1])
625 idx_pl = self.contrats.index(contrat) 637 idx_pl = self.contrats.index(contrat)
626 if idx_pl > idx_best: 638 if idx_pl > idx_best:
627 best_contrat[0] = player 639 best_contrat[0] = player
628 best_contrat[1] = contrat 640 best_contrat[1] = contrat
641 if best_contrat[1] == "Passe":
642 debug(_("Everybody is passing, round ended"))
643 to_jid = jid.JID(room_jid.userhost())
644 mess = self.createGameElt(to_jid)
645 mess.firstChildElement().addChild(self.__give_scores(*self.__draw_game(game_data)))
646 self.host.profiles[profile].xmlstream.send(mess)
647 game_data['init_player'] = (game_data['init_player'] + 1) % len(game_data['players']) #we change the dealer
648 for player in game_data['players']:
649 game_data['status'][player] = "init"
650 return
629 debug (_("%(player)s win the bid with %(contrat)s") % {'player':best_contrat[0],'contrat':best_contrat[1]}) 651 debug (_("%(player)s win the bid with %(contrat)s") % {'player':best_contrat[0],'contrat':best_contrat[1]})
630 game_data['contrat'] = best_contrat[1] 652 game_data['contrat'] = best_contrat[1]
631 653
632 if game_data['contrat'] == "Garde Sans" or game_data['contrat'] == "Garde Contre": 654 if game_data['contrat'] == "Garde Sans" or game_data['contrat'] == "Garde Contre":
633 self.__start_play(room_jid, game_data, profile) 655 self.__start_play(room_jid, game_data, profile)
716 #no card lef: the game is finished 738 #no card lef: the game is finished
717 to_jid = jid.JID(room_jid.userhost()) #FIXME: gof: 739 to_jid = jid.JID(room_jid.userhost()) #FIXME: gof:
718 mess = self.createGameElt(to_jid) 740 mess = self.createGameElt(to_jid)
719 chien_elt = mess.firstChildElement().addChild(self.__give_scores(*self.__calculate_scores(game_data))) 741 chien_elt = mess.firstChildElement().addChild(self.__give_scores(*self.__calculate_scores(game_data)))
720 self.host.profiles[profile].xmlstream.send(mess) 742 self.host.profiles[profile].xmlstream.send(mess)
743 game_data['init_player'] = (game_data['init_player'] + 1) % len(game_data['players']) #we change the dealer
744 for player in game_data['players']:
745 game_data['status'][player] = "init"
721 return 746 return
722 #next player is the winner 747 #next player is the winner
723 next_player = game_data['first_player'] = self.__next_player(game_data, winner) 748 next_player = game_data['first_player'] = self.__next_player(game_data, winner)
724 else: 749 else:
725 next_player = self.__next_player(game_data) 750 next_player = self.__next_player(game_data)