Tarot Plugin: Garde Sans and Garde Contre are now managed

This commit is contained in:
Goffi 2010-06-03 17:43:49 +09:30
commit 851faff0e3

View file

@ -134,10 +134,10 @@ class Tarot():
host.bridge.addSignal("tarotGameScore", ".communication", signature='ssasass') #args: room_jid, xml_data, winners (list of nicks), loosers (list of nicks), profile
self.deck_ordered = []
for value in ['excuse']+map(str,range(1,22)):
self.deck_ordered.append(("atout",value))
self.deck_ordered.append(Card(("atout",value)))
for suit in ["pique", "coeur", "carreau", "trefle"]:
for value in map(str,range(1,11))+["valet","cavalier","dame","roi"]:
self.deck_ordered.append((suit, value))
self.deck_ordered.append(Card((suit, value)))
def createGameElt(self, to_jid, type="normal"):
type = "normal" if to_jid.resource else "groupchat"
@ -147,13 +147,13 @@ class Tarot():
elt.addElement((NS_CG, CG_TAG))
return elt
def __list_to_xml(self, cards_list, elt_name):
"""Convert a card list (list of tuples) to domish element"""
def __card_list_to_xml(self, cards_list, elt_name):
"""Convert a card list to domish element"""
cards_list_elt = domish.Element(('',elt_name))
for suit, value in cards_list:
for card in cards_list:
card_elt = domish.Element(('','card'))
card_elt['suit'] = suit
card_elt['value'] = value
card_elt['suit'] = card.suit
card_elt['value'] = card.value
cards_list_elt.addChild(card_elt)
return cards_list_elt
@ -322,6 +322,11 @@ class Tarot():
for defenseur in defenseurs:
for card in players_data[defenseur]['levees']:
check_score+=card.points
if game_data['contrat'] == "Garde Contre":
for card in game_data['chien']:
check_score+=card.points
if ( score + check_score != 91 ):
pdb.set_trace()
assert (score + check_score == 91)
point_limit = None
@ -368,6 +373,16 @@ class Tarot():
return (scores_str, winners, loosers)
def __start_play(self, room_jid, game_data, profile):
"""Start the game (tell to the first player after dealer to play"""
game_data['stage'] = "play"
next_player_idx = game_data['current_player'] = (game_data['init_player'] + 1) % len(game_data['players']) #the player after the dealer start
game_data['first_player'] = next_player = game_data['players'][next_player_idx]
to_jid = jid.JID(room_jid.userhost()+"/"+next_player) #FIXME: gof:
mess = self.createGameElt(to_jid)
yourturn_elt = mess.firstChildElement().addElement('your_turn')
self.host.profiles[profile].xmlstream.send(mess)
def createGame(self, room_jid_param, players, profile_key='@DEFAULT@'):
"""Create a new game"""
@ -438,7 +453,7 @@ class Tarot():
return
debug (_('Cards played by %(profile)s: [%(cards)s]') % {'profile':profile,'cards':cards})
mess = self.createGameElt(jid.JID(referee))
playcard_elt = mess.firstChildElement().addChild(self.__list_to_xml(cards, 'cards_played'))
playcard_elt = mess.firstChildElement().addChild(self.__card_list_to_xml(Card.from_tuples(cards), 'cards_played'))
playcard_elt['player'] = player
self.host.profiles[profile].xmlstream.send(mess)
@ -466,7 +481,7 @@ class Tarot():
for player in players:
to_jid = jid.JID(room_jid.userhost()+"/"+player) #FIXME: gof:
mess = self.createGameElt(to_jid)
mess.firstChildElement().addChild(self.__list_to_xml(hand[player], 'hand'))
mess.firstChildElement().addChild(self.__card_list_to_xml(hand[player], 'hand'))
self.host.profiles[profile].xmlstream.send(mess)
players_data[player]['contrat'] = None
players_data[player]['levees'] = [] #cards won
@ -539,16 +554,26 @@ class Tarot():
best_contrat[1] = contrat
debug (_("%(player)s win the bid with %(contrat)s") % {'player':best_contrat[0],'contrat':best_contrat[1]})
game_data['contrat'] = best_contrat[1]
#Time to show the chien to everybody
to_jid = jid.JID(room_jid.userhost()) #FIXME: gof:
mess = self.createGameElt(to_jid)
chien_elt = mess.firstChildElement().addChild(self.__list_to_xml(game_data['chien'], 'chien'))
chien_elt['attaquant'] = best_contrat[0]
self.host.profiles[profile].xmlstream.send(mess)
if game_data['contrat'] == "Garde Sans" or game_data['contrat'] == "Garde Contre":
self.__start_play(room_jid, game_data, profile)
game_data['attaquant'] = best_contrat[0]
else:
#Time to show the chien to everybody
to_jid = jid.JID(room_jid.userhost()) #FIXME: gof:
mess = self.createGameElt(to_jid)
chien_elt = mess.firstChildElement().addChild(self.__card_list_to_xml(game_data['chien'], 'chien'))
chien_elt['attaquant'] = best_contrat[0]
self.host.profiles[profile].xmlstream.send(mess)
#the attacker (attaquant) get the chien
game_data['hand'][best_contrat[0]].extend(game_data['chien'])
del game_data['chien'][:]
if game_data['contrat'] == "Garde Sans":
#The chien go into attaquant's (attacker) levees
players_data[best_contrat[0]]['levees'].extend(game_data['chien'])
del game_data['chien'][:]
#the attacker (attaquant) get the chien
game_data['hand'][best_contrat[0]].extend(game_data['chien'])
del game_data['chien'][:]
elif elt.name == 'chien': #we have received the chien
debug (_("tarot: chien received"))
@ -562,21 +587,17 @@ class Tarot():
#TODO: check validity of écart (no king, no oulder, cards must be in player hand)
#TODO: show atouts (trumps) if player put some in écart
assert (game_data['attaquant'] == elt['player']) #TODO: throw an xml error here
list_cards = self.__xml_to_list(elt)
list_cards = Card.from_tuples(self.__xml_to_list(elt))
#FIXME: gof: manage Garde Sans & Garde Contre cases
players_data[elt['player']]['levees'].extend(Card.from_tuples(list_cards)) #we add the chien to attaquant's levées
players_data[elt['player']]['levees'].extend(list_cards) #we add the chien to attaquant's levées
for card in list_cards:
game_data['hand'][elt['player']].remove(card)
game_data['stage'] = "play"
next_player_idx = game_data['current_player'] = (game_data['init_player'] + 1) % len(game_data['players']) #the player after the dealer start
game_data['first_player'] = next_player = game_data['players'][next_player_idx]
to_jid = jid.JID(room_jid.userhost()+"/"+next_player) #FIXME: gof:
mess = self.createGameElt(to_jid)
yourturn_elt = mess.firstChildElement().addElement('your_turn')
self.host.profiles[profile].xmlstream.send(mess)
self.__start_play(room_jid, game_data, profile)
elif game_data['stage'] == "play":
current_player = game_data['players'][game_data['current_player']]
cards = self.__xml_to_list(elt)
cards = Card.from_tuples(self.__xml_to_list(elt))
if mess_elt['type'] == 'groupchat':
self.host.bridge.tarotGameCardsPlayed(room_jid.userhost(), elt['player'], self.__xml_to_list(elt), profile)
@ -585,7 +606,7 @@ class Tarot():
#the card played is ok, we forward it to everybody
#first we remove it from the hand and put in on the table
game_data['hand'][current_player].remove(cards[0])
players_data[current_player]['played'] = Card(cards[0])
players_data[current_player]['played'] = cards[0]
#then we forward the message
mess = self.createGameElt(room_jid)
@ -651,7 +672,7 @@ class CardGameHandler (XMPPHandler):
self.xmlstream.addObserver(CG_REQUEST, self.plugin_parent.card_game_cmd, profile = self.parent.profile)
def getDiscoInfo(self, requestor, target, nodeIdentifier=''):
return [disco.DiscoFeature(NS_CB)]
return [disco.DiscoFeature(NS_CG)]
def getDiscoItems(self, requestor, target, nodeIdentifier=''):
return []