comparison src/plugins/plugin_misc_tarot.py @ 714:ecc5a5b34ee1

plugins (games): add a method to send messages more easily
author souliane <souliane@mailoo.org>
date Mon, 18 Nov 2013 14:25:40 +0100
parents f610864eb7a5
children 358018c5c398
comparison
equal deleted inserted replaced
713:8bd63daecdbf 714:ecc5a5b34ee1
378 """Start the game (tell to the first player after dealer to play""" 378 """Start the game (tell to the first player after dealer to play"""
379 game_data['stage'] = "play" 379 game_data['stage'] = "play"
380 next_player_idx = game_data['current_player'] = (game_data['init_player'] + 1) % len(game_data['players']) # the player after the dealer start 380 next_player_idx = game_data['current_player'] = (game_data['init_player'] + 1) % len(game_data['players']) # the player after the dealer start
381 game_data['first_player'] = next_player = game_data['players'][next_player_idx] 381 game_data['first_player'] = next_player = game_data['players'][next_player_idx]
382 to_jid = jid.JID(room_jid.userhost() + "/" + next_player) # FIXME: gof: 382 to_jid = jid.JID(room_jid.userhost() + "/" + next_player) # FIXME: gof:
383 mess = self.createGameElt(to_jid) 383 self.send(to_jid, 'your_turn', profile=profile)
384 yourturn_elt = mess.firstChildElement().addElement('your_turn')
385 self.host.profiles[profile].xmlstream.send(mess)
386 384
387 def contratChoosed(self, player, referee, contrat, profile_key='@NONE@'): 385 def contratChoosed(self, player, referee, contrat, profile_key='@NONE@'):
388 """Must be call by player when the contrat is selected 386 """Must be call by player when the contrat is selected
389 @param player: player's name 387 @param player: player's name
390 @param referee: arbiter jid 388 @param referee: arbiter jid
394 profile = self.host.memory.getProfileName(profile_key) 392 profile = self.host.memory.getProfileName(profile_key)
395 if not profile: 393 if not profile:
396 error(_("profile %s is unknown") % profile_key) 394 error(_("profile %s is unknown") % profile_key)
397 return 395 return
398 debug(_('contrat [%(contrat)s] choosed by %(profile)s') % {'contrat': contrat, 'profile': profile}) 396 debug(_('contrat [%(contrat)s] choosed by %(profile)s') % {'contrat': contrat, 'profile': profile})
399 mess = self.createGameElt(jid.JID(referee)) 397 self.send(jid.JID(referee), ('', 'contrat_choosed'), {'player': player}, content=contrat, profile=profile)
400 contrat_elt = mess.firstChildElement().addElement(('', 'contrat_choosed'), content=contrat)
401 contrat_elt['player'] = player
402 self.host.profiles[profile].xmlstream.send(mess)
403 398
404 def play_cards(self, player, referee, cards, profile_key='@NONE@'): 399 def play_cards(self, player, referee, cards, profile_key='@NONE@'):
405 """Must be call by player when the contrat is selected 400 """Must be call by player when the contrat is selected
406 @param player: player's name 401 @param player: player's name
407 @param referee: arbiter jid 402 @param referee: arbiter jid
411 profile = self.host.memory.getProfileName(profile_key) 406 profile = self.host.memory.getProfileName(profile_key)
412 if not profile: 407 if not profile:
413 error(_("profile %s is unknown") % profile_key) 408 error(_("profile %s is unknown") % profile_key)
414 return 409 return
415 debug(_('Cards played by %(profile)s: [%(cards)s]') % {'profile': profile, 'cards': cards}) 410 debug(_('Cards played by %(profile)s: [%(cards)s]') % {'profile': profile, 'cards': cards})
416 mess = self.createGameElt(jid.JID(referee)) 411 elem = self.__card_list_to_xml(TarotCard.from_tuples(cards), 'cards_played')
417 playcard_elt = mess.firstChildElement().addChild(self.__card_list_to_xml(TarotCard.from_tuples(cards), 'cards_played')) 412 self.send(jid.JID(referee), elem, {'player': player}, profile=profile)
418 playcard_elt['player'] = player
419 self.host.profiles[profile].xmlstream.send(mess)
420 413
421 def newRound(self, room_jid, profile): 414 def newRound(self, room_jid, profile):
422 game_data = self.games[room_jid.userhost()] 415 game_data = self.games[room_jid.userhost()]
423 players = game_data['players'] 416 players = game_data['players']
424 game_data['first_player'] = None # first player for the current trick 417 game_data['first_player'] = None # first player for the current trick
446 RoomGame.newRound(self, room_jid, (common_data, msg_elts), profile) 439 RoomGame.newRound(self, room_jid, (common_data, msg_elts), profile)
447 440
448 pl_idx = game_data['current_player'] = (game_data['init_player'] + 1) % len(players) # the player after the dealer start 441 pl_idx = game_data['current_player'] = (game_data['init_player'] + 1) % len(players) # the player after the dealer start
449 player = players[pl_idx] 442 player = players[pl_idx]
450 to_jid = jid.JID(room_jid.userhost() + "/" + player) # FIXME: gof: 443 to_jid = jid.JID(room_jid.userhost() + "/" + player) # FIXME: gof:
451 mess = self.createGameElt(to_jid) 444 self.send(to_jid, self.__ask_contrat(), profile=profile)
452 mess.firstChildElement().addChild(self.__ask_contrat())
453 self.host.profiles[profile].xmlstream.send(mess)
454 445
455 def card_game_cmd(self, mess_elt, profile): 446 def card_game_cmd(self, mess_elt, profile):
456 """ 447 """
457 @param mess_elt: instance of twisted.words.xish.domish.Element 448 @param mess_elt: instance of twisted.words.xish.domish.Element
458 """ 449 """
495 contrats = [players_data[player]['contrat'] for player in game_data['players']] 486 contrats = [players_data[player]['contrat'] for player in game_data['players']]
496 if contrats.count(None): 487 if contrats.count(None):
497 #not everybody has choosed his contrat, it's next one turn 488 #not everybody has choosed his contrat, it's next one turn
498 player = self.__next_player(game_data) 489 player = self.__next_player(game_data)
499 to_jid = jid.JID(room_jid.userhost() + "/" + player) # FIXME: gof: 490 to_jid = jid.JID(room_jid.userhost() + "/" + player) # FIXME: gof:
500 mess = self.createGameElt(to_jid) 491 self.send(to_jid, self.__ask_contrat(), profile=profile)
501 mess.firstChildElement().addChild(self.__ask_contrat())
502 self.host.profiles[profile].xmlstream.send(mess)
503 else: 492 else:
504 best_contrat = [None, "Passe"] 493 best_contrat = [None, "Passe"]
505 for player in game_data['players']: 494 for player in game_data['players']:
506 contrat = players_data[player]['contrat'] 495 contrat = players_data[player]['contrat']
507 idx_best = self.contrats.index(best_contrat[1]) 496 idx_best = self.contrats.index(best_contrat[1])
510 best_contrat[0] = player 499 best_contrat[0] = player
511 best_contrat[1] = contrat 500 best_contrat[1] = contrat
512 if best_contrat[1] == "Passe": 501 if best_contrat[1] == "Passe":
513 debug(_("Everybody is passing, round ended")) 502 debug(_("Everybody is passing, round ended"))
514 to_jid = jid.JID(room_jid.userhost()) 503 to_jid = jid.JID(room_jid.userhost())
515 mess = self.createGameElt(to_jid) 504 self.send(to_jid, self.__give_scores(*self.__draw_game(game_data)), profile=profile)
516 mess.firstChildElement().addChild(self.__give_scores(*self.__draw_game(game_data)))
517 self.host.profiles[profile].xmlstream.send(mess)
518 game_data['init_player'] = (game_data['init_player'] + 1) % len(game_data['players']) # we change the dealer 505 game_data['init_player'] = (game_data['init_player'] + 1) % len(game_data['players']) # we change the dealer
519 for player in game_data['players']: 506 for player in game_data['players']:
520 game_data['status'][player] = "init" 507 game_data['status'][player] = "init"
521 return 508 return
522 debug(_("%(player)s win the bid with %(contrat)s") % {'player': best_contrat[0], 'contrat': best_contrat[1]}) 509 debug(_("%(player)s win the bid with %(contrat)s") % {'player': best_contrat[0], 'contrat': best_contrat[1]})
526 self.__start_play(room_jid, game_data, profile) 513 self.__start_play(room_jid, game_data, profile)
527 game_data['attaquant'] = best_contrat[0] 514 game_data['attaquant'] = best_contrat[0]
528 else: 515 else:
529 #Time to show the chien to everybody 516 #Time to show the chien to everybody
530 to_jid = jid.JID(room_jid.userhost()) # FIXME: gof: 517 to_jid = jid.JID(room_jid.userhost()) # FIXME: gof:
531 mess = self.createGameElt(to_jid) 518 elem = self.__card_list_to_xml(game_data['chien'], 'chien')
532 chien_elt = mess.firstChildElement().addChild(self.__card_list_to_xml(game_data['chien'], 'chien')) 519 self.send(to_jid, elem, {'attaquant': best_contrat[0]}, profile=profile)
533 chien_elt['attaquant'] = best_contrat[0]
534 self.host.profiles[profile].xmlstream.send(mess)
535 #the attacker (attaquant) get the chien 520 #the attacker (attaquant) get the chien
536 game_data['hand'][best_contrat[0]].extend(game_data['chien']) 521 game_data['hand'][best_contrat[0]].extend(game_data['chien'])
537 del game_data['chien'][:] 522 del game_data['chien'][:]
538 523
539 if game_data['contrat'] == "Garde Sans": 524 if game_data['contrat'] == "Garde Sans":
554 assert (game_data['attaquant'] == elt['player']) # TODO: throw an xml error here 539 assert (game_data['attaquant'] == elt['player']) # TODO: throw an xml error here
555 list_cards = TarotCard.from_tuples(self.__xml_to_list(elt)) 540 list_cards = TarotCard.from_tuples(self.__xml_to_list(elt))
556 #we now check validity of card 541 #we now check validity of card
557 invalid_cards = self.__invalid_cards(game_data, list_cards) 542 invalid_cards = self.__invalid_cards(game_data, list_cards)
558 if invalid_cards: 543 if invalid_cards:
559 mess = self.createGameElt(jid.JID(room_jid.userhost() + '/' + elt['player'])) 544 elem = self.__invalid_cards_elt(list_cards, invalid_cards, game_data['stage'])
560 mess.firstChildElement().addChild(self.__invalid_cards_elt(list_cards, invalid_cards, game_data['stage'])) 545 self.send(jid.JID(room_jid.userhost() + '/' + elt['player']), elem, profile=profile)
561 self.host.profiles[profile].xmlstream.send(mess)
562 return 546 return
563 547
564 #FIXME: gof: manage Garde Sans & Garde Contre cases 548 #FIXME: gof: manage Garde Sans & Garde Contre cases
565 players_data[elt['player']]['levees'].extend(list_cards) # we add the chien to attaquant's levées 549 players_data[elt['player']]['levees'].extend(list_cards) # we add the chien to attaquant's levées
566 for card in list_cards: 550 for card in list_cards:
576 self.host.bridge.tarotGameCardsPlayed(room_jid.userhost(), elt['player'], self.__xml_to_list(elt), profile) 560 self.host.bridge.tarotGameCardsPlayed(room_jid.userhost(), elt['player'], self.__xml_to_list(elt), profile)
577 else: 561 else:
578 #we first check validity of card 562 #we first check validity of card
579 invalid_cards = self.__invalid_cards(game_data, cards) 563 invalid_cards = self.__invalid_cards(game_data, cards)
580 if invalid_cards: 564 if invalid_cards:
581 mess = self.createGameElt(jid.JID(room_jid.userhost() + '/' + current_player)) 565 elem = self.__invalid_cards_elt(cards, invalid_cards, game_data['stage'])
582 mess.firstChildElement().addChild(self.__invalid_cards_elt(cards, invalid_cards, game_data['stage'])) 566 self.send(jid.JID(room_jid.userhost() + '/' + current_player), elem, profile=profile)
583 self.host.profiles[profile].xmlstream.send(mess)
584 return 567 return
585 #the card played is ok, we forward it to everybody 568 #the card played is ok, we forward it to everybody
586 #first we remove it from the hand and put in on the table 569 #first we remove it from the hand and put in on the table
587 game_data['hand'][current_player].remove(cards[0]) 570 game_data['hand'][current_player].remove(cards[0])
588 players_data[current_player]['played'] = cards[0] 571 players_data[current_player]['played'] = cards[0]
589 572
590 #then we forward the message 573 #then we forward the message
591 mess = self.createGameElt(room_jid) 574 self.send(room_jid, elt, profile=profile)
592 playcard_elt = mess.firstChildElement().addChild(elt)
593 self.host.profiles[profile].xmlstream.send(mess)
594 575
595 #Did everybody played ? 576 #Did everybody played ?
596 played = [players_data[player]['played'] for player in game_data['players']] 577 played = [players_data[player]['played'] for player in game_data['players']]
597 if all(played): 578 if all(played):
598 #everybody has played 579 #everybody has played
604 #nothing left on the table 585 #nothing left on the table
605 for player in game_data['players']: 586 for player in game_data['players']:
606 players_data[player]['played'] = None 587 players_data[player]['played'] = None
607 if len(game_data['hand'][current_player]) == 0: 588 if len(game_data['hand'][current_player]) == 0:
608 #no card lef: the game is finished 589 #no card lef: the game is finished
609 to_jid = room_jid 590 elem = self.__give_scores(*self.__calculate_scores(game_data))
610 mess = self.createGameElt(to_jid) 591 self.send(room_jid, elem, profile=profile)
611 chien_elt = mess.firstChildElement().addChild(self.__give_scores(*self.__calculate_scores(game_data)))
612 self.host.profiles[profile].xmlstream.send(mess)
613 game_data['init_player'] = (game_data['init_player'] + 1) % len(game_data['players']) # we change the dealer 592 game_data['init_player'] = (game_data['init_player'] + 1) % len(game_data['players']) # we change the dealer
614 for player in game_data['players']: 593 for player in game_data['players']:
615 game_data['status'][player] = "init" 594 game_data['status'][player] = "init"
616 return 595 return
617 #next player is the winner 596 #next player is the winner
619 else: 598 else:
620 next_player = self.__next_player(game_data) 599 next_player = self.__next_player(game_data)
621 600
622 #finally, we tell to the next player to play 601 #finally, we tell to the next player to play
623 to_jid = jid.JID(room_jid.userhost() + "/" + next_player) 602 to_jid = jid.JID(room_jid.userhost() + "/" + next_player)
624 mess = self.createGameElt(to_jid) 603 self.send(to_jid, 'your_turn', profile=profile)
625 yourturn_elt = mess.firstChildElement().addElement('your_turn')
626 self.host.profiles[profile].xmlstream.send(mess)
627 604
628 elif elt.name == 'your_turn': 605 elif elt.name == 'your_turn':
629 self.host.bridge.tarotGameYourTurn(room_jid.userhost(), profile) 606 self.host.bridge.tarotGameYourTurn(room_jid.userhost(), profile)
630 607
631 elif elt.name == 'score': 608 elif elt.name == 'score':