comparison plugins/plugin_misc_tarot.py @ 98:dd556233a1b1

Tarot Plugin: Garde Sans and Garde Contre are now managed
author Goffi <goffi@goffi.org>
date Thu, 03 Jun 2010 17:43:49 +0930
parents c8518b9a8025
children 63c9067a1499
comparison
equal deleted inserted replaced
97:01d7bd6f0e86 98:dd556233a1b1
132 host.bridge.addSignal("tarotGameCardsPlayed", ".communication", signature='ssa(ss)s') #args: room_jid, player, type ["chien", "poignée",...], cards, data[dict], profile 132 host.bridge.addSignal("tarotGameCardsPlayed", ".communication", signature='ssa(ss)s') #args: room_jid, player, type ["chien", "poignée",...], cards, data[dict], profile
133 host.bridge.addSignal("tarotGameYourTurn", ".communication", signature='ss') #args: room_jid, profile 133 host.bridge.addSignal("tarotGameYourTurn", ".communication", signature='ss') #args: room_jid, profile
134 host.bridge.addSignal("tarotGameScore", ".communication", signature='ssasass') #args: room_jid, xml_data, winners (list of nicks), loosers (list of nicks), profile 134 host.bridge.addSignal("tarotGameScore", ".communication", signature='ssasass') #args: room_jid, xml_data, winners (list of nicks), loosers (list of nicks), profile
135 self.deck_ordered = [] 135 self.deck_ordered = []
136 for value in ['excuse']+map(str,range(1,22)): 136 for value in ['excuse']+map(str,range(1,22)):
137 self.deck_ordered.append(("atout",value)) 137 self.deck_ordered.append(Card(("atout",value)))
138 for suit in ["pique", "coeur", "carreau", "trefle"]: 138 for suit in ["pique", "coeur", "carreau", "trefle"]:
139 for value in map(str,range(1,11))+["valet","cavalier","dame","roi"]: 139 for value in map(str,range(1,11))+["valet","cavalier","dame","roi"]:
140 self.deck_ordered.append((suit, value)) 140 self.deck_ordered.append(Card((suit, value)))
141 141
142 def createGameElt(self, to_jid, type="normal"): 142 def createGameElt(self, to_jid, type="normal"):
143 type = "normal" if to_jid.resource else "groupchat" 143 type = "normal" if to_jid.resource else "groupchat"
144 elt = domish.Element(('jabber:client','message')) 144 elt = domish.Element(('jabber:client','message'))
145 elt["to"] = to_jid.full() 145 elt["to"] = to_jid.full()
146 elt["type"] = type 146 elt["type"] = type
147 elt.addElement((NS_CG, CG_TAG)) 147 elt.addElement((NS_CG, CG_TAG))
148 return elt 148 return elt
149 149
150 def __list_to_xml(self, cards_list, elt_name): 150 def __card_list_to_xml(self, cards_list, elt_name):
151 """Convert a card list (list of tuples) to domish element""" 151 """Convert a card list to domish element"""
152 cards_list_elt = domish.Element(('',elt_name)) 152 cards_list_elt = domish.Element(('',elt_name))
153 for suit, value in cards_list: 153 for card in cards_list:
154 card_elt = domish.Element(('','card')) 154 card_elt = domish.Element(('','card'))
155 card_elt['suit'] = suit 155 card_elt['suit'] = card.suit
156 card_elt['value'] = value 156 card_elt['value'] = card.value
157 cards_list_elt.addChild(card_elt) 157 cards_list_elt.addChild(card_elt)
158 return cards_list_elt 158 return cards_list_elt
159 159
160 def __xml_to_list(self, cards_list_elt): 160 def __xml_to_list(self, cards_list_elt):
161 """Convert a domish element with cards to a list of tuples""" 161 """Convert a domish element with cards to a list of tuples"""
320 defenseurs = game_data['players'][:] 320 defenseurs = game_data['players'][:]
321 defenseurs.remove(game_data['attaquant']) 321 defenseurs.remove(game_data['attaquant'])
322 for defenseur in defenseurs: 322 for defenseur in defenseurs:
323 for card in players_data[defenseur]['levees']: 323 for card in players_data[defenseur]['levees']:
324 check_score+=card.points 324 check_score+=card.points
325 if game_data['contrat'] == "Garde Contre":
326 for card in game_data['chien']:
327 check_score+=card.points
328 if ( score + check_score != 91 ):
329 pdb.set_trace()
325 assert (score + check_score == 91) 330 assert (score + check_score == 91)
326 331
327 point_limit = None 332 point_limit = None
328 if nb_bouts == 3: 333 if nb_bouts == 3:
329 point_limit = 36 334 point_limit = 36
365 for player in game_data['players']: 370 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']} 371 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) 372 debug(scores_str)
368 373
369 return (scores_str, winners, loosers) 374 return (scores_str, winners, loosers)
375
376 def __start_play(self, room_jid, game_data, profile):
377 """Start the game (tell to the first player after dealer to play"""
378 game_data['stage'] = "play"
379 next_player_idx = game_data['current_player'] = (game_data['init_player'] + 1) % len(game_data['players']) #the player after the dealer start
380 game_data['first_player'] = next_player = game_data['players'][next_player_idx]
381 to_jid = jid.JID(room_jid.userhost()+"/"+next_player) #FIXME: gof:
382 mess = self.createGameElt(to_jid)
383 yourturn_elt = mess.firstChildElement().addElement('your_turn')
384 self.host.profiles[profile].xmlstream.send(mess)
370 385
371 386
372 def createGame(self, room_jid_param, players, profile_key='@DEFAULT@'): 387 def createGame(self, room_jid_param, players, profile_key='@DEFAULT@'):
373 """Create a new game""" 388 """Create a new game"""
374 debug (_("Creating Tarot game")) 389 debug (_("Creating Tarot game"))
436 if not profile: 451 if not profile:
437 error (_("profile %s is unknown") % profile_key) 452 error (_("profile %s is unknown") % profile_key)
438 return 453 return
439 debug (_('Cards played by %(profile)s: [%(cards)s]') % {'profile':profile,'cards':cards}) 454 debug (_('Cards played by %(profile)s: [%(cards)s]') % {'profile':profile,'cards':cards})
440 mess = self.createGameElt(jid.JID(referee)) 455 mess = self.createGameElt(jid.JID(referee))
441 playcard_elt = mess.firstChildElement().addChild(self.__list_to_xml(cards, 'cards_played')) 456 playcard_elt = mess.firstChildElement().addChild(self.__card_list_to_xml(Card.from_tuples(cards), 'cards_played'))
442 playcard_elt['player'] = player 457 playcard_elt['player'] = player
443 self.host.profiles[profile].xmlstream.send(mess) 458 self.host.profiles[profile].xmlstream.send(mess)
444 459
445 def newGame(self, room_jid, profile): 460 def newGame(self, room_jid, profile):
446 """Launch a new round""" 461 """Launch a new round"""
464 del(deck[:]) 479 del(deck[:])
465 480
466 for player in players: 481 for player in players:
467 to_jid = jid.JID(room_jid.userhost()+"/"+player) #FIXME: gof: 482 to_jid = jid.JID(room_jid.userhost()+"/"+player) #FIXME: gof:
468 mess = self.createGameElt(to_jid) 483 mess = self.createGameElt(to_jid)
469 mess.firstChildElement().addChild(self.__list_to_xml(hand[player], 'hand')) 484 mess.firstChildElement().addChild(self.__card_list_to_xml(hand[player], 'hand'))
470 self.host.profiles[profile].xmlstream.send(mess) 485 self.host.profiles[profile].xmlstream.send(mess)
471 players_data[player]['contrat'] = None 486 players_data[player]['contrat'] = None
472 players_data[player]['levees'] = [] #cards won 487 players_data[player]['levees'] = [] #cards won
473 players_data[player]['played'] = None #card on the table 488 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 489 players_data[player]['wait_for_low'] = None #Used when a player wait for a low card because of excuse
537 if idx_pl > idx_best: 552 if idx_pl > idx_best:
538 best_contrat[0] = player 553 best_contrat[0] = player
539 best_contrat[1] = contrat 554 best_contrat[1] = contrat
540 debug (_("%(player)s win the bid with %(contrat)s") % {'player':best_contrat[0],'contrat':best_contrat[1]}) 555 debug (_("%(player)s win the bid with %(contrat)s") % {'player':best_contrat[0],'contrat':best_contrat[1]})
541 game_data['contrat'] = best_contrat[1] 556 game_data['contrat'] = best_contrat[1]
542 #Time to show the chien to everybody 557
543 to_jid = jid.JID(room_jid.userhost()) #FIXME: gof: 558 if game_data['contrat'] == "Garde Sans" or game_data['contrat'] == "Garde Contre":
544 mess = self.createGameElt(to_jid) 559 self.__start_play(room_jid, game_data, profile)
545 chien_elt = mess.firstChildElement().addChild(self.__list_to_xml(game_data['chien'], 'chien')) 560 game_data['attaquant'] = best_contrat[0]
546 chien_elt['attaquant'] = best_contrat[0] 561 else:
547 self.host.profiles[profile].xmlstream.send(mess) 562 #Time to show the chien to everybody
548 563 to_jid = jid.JID(room_jid.userhost()) #FIXME: gof:
549 #the attacker (attaquant) get the chien 564 mess = self.createGameElt(to_jid)
550 game_data['hand'][best_contrat[0]].extend(game_data['chien']) 565 chien_elt = mess.firstChildElement().addChild(self.__card_list_to_xml(game_data['chien'], 'chien'))
551 del game_data['chien'][:] 566 chien_elt['attaquant'] = best_contrat[0]
567 self.host.profiles[profile].xmlstream.send(mess)
568 #the attacker (attaquant) get the chien
569 game_data['hand'][best_contrat[0]].extend(game_data['chien'])
570 del game_data['chien'][:]
571
572 if game_data['contrat'] == "Garde Sans":
573 #The chien go into attaquant's (attacker) levees
574 players_data[best_contrat[0]]['levees'].extend(game_data['chien'])
575 del game_data['chien'][:]
576
552 577
553 elif elt.name == 'chien': #we have received the chien 578 elif elt.name == 'chien': #we have received the chien
554 debug (_("tarot: chien received")) 579 debug (_("tarot: chien received"))
555 data = {"attaquant":elt['attaquant']} 580 data = {"attaquant":elt['attaquant']}
556 game_data['stage'] = "ecart" 581 game_data['stage'] = "ecart"
560 elif elt.name == 'cards_played': 585 elif elt.name == 'cards_played':
561 if game_data['stage'] == "ecart": 586 if game_data['stage'] == "ecart":
562 #TODO: check validity of écart (no king, no oulder, cards must be in player hand) 587 #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 588 #TODO: show atouts (trumps) if player put some in écart
564 assert (game_data['attaquant'] == elt['player']) #TODO: throw an xml error here 589 assert (game_data['attaquant'] == elt['player']) #TODO: throw an xml error here
565 list_cards = self.__xml_to_list(elt) 590 list_cards = Card.from_tuples(self.__xml_to_list(elt))
566 #FIXME: gof: manage Garde Sans & Garde Contre cases 591 #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 592 players_data[elt['player']]['levees'].extend(list_cards) #we add the chien to attaquant's levées
568 for card in list_cards: 593 for card in list_cards:
569 game_data['hand'][elt['player']].remove(card) 594 game_data['hand'][elt['player']].remove(card)
570 game_data['stage'] = "play" 595
571 next_player_idx = game_data['current_player'] = (game_data['init_player'] + 1) % len(game_data['players']) #the player after the dealer start 596 self.__start_play(room_jid, game_data, profile)
572 game_data['first_player'] = next_player = game_data['players'][next_player_idx] 597
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)
577 elif game_data['stage'] == "play": 598 elif game_data['stage'] == "play":
578 current_player = game_data['players'][game_data['current_player']] 599 current_player = game_data['players'][game_data['current_player']]
579 cards = self.__xml_to_list(elt) 600 cards = Card.from_tuples(self.__xml_to_list(elt))
580 601
581 if mess_elt['type'] == 'groupchat': 602 if mess_elt['type'] == 'groupchat':
582 self.host.bridge.tarotGameCardsPlayed(room_jid.userhost(), elt['player'], self.__xml_to_list(elt), profile) 603 self.host.bridge.tarotGameCardsPlayed(room_jid.userhost(), elt['player'], self.__xml_to_list(elt), profile)
583 else: 604 else:
584 #TODO: check card validity and send error mess if necessary 605 #TODO: check card validity and send error mess if necessary
585 #the card played is ok, we forward it to everybody 606 #the card played is ok, we forward it to everybody
586 #first we remove it from the hand and put in on the table 607 #first we remove it from the hand and put in on the table
587 game_data['hand'][current_player].remove(cards[0]) 608 game_data['hand'][current_player].remove(cards[0])
588 players_data[current_player]['played'] = Card(cards[0]) 609 players_data[current_player]['played'] = cards[0]
589 610
590 #then we forward the message 611 #then we forward the message
591 mess = self.createGameElt(room_jid) 612 mess = self.createGameElt(room_jid)
592 playcard_elt = mess.firstChildElement().addChild(elt) 613 playcard_elt = mess.firstChildElement().addChild(elt)
593 self.host.profiles[profile].xmlstream.send(mess) 614 self.host.profiles[profile].xmlstream.send(mess)
649 670
650 def connectionInitialized(self): 671 def connectionInitialized(self):
651 self.xmlstream.addObserver(CG_REQUEST, self.plugin_parent.card_game_cmd, profile = self.parent.profile) 672 self.xmlstream.addObserver(CG_REQUEST, self.plugin_parent.card_game_cmd, profile = self.parent.profile)
652 673
653 def getDiscoInfo(self, requestor, target, nodeIdentifier=''): 674 def getDiscoInfo(self, requestor, target, nodeIdentifier=''):
654 return [disco.DiscoFeature(NS_CB)] 675 return [disco.DiscoFeature(NS_CG)]
655 676
656 def getDiscoItems(self, requestor, target, nodeIdentifier=''): 677 def getDiscoItems(self, requestor, target, nodeIdentifier=''):
657 return [] 678 return []
658 679