comparison sat_frontends/quick_frontend/quick_game_tarot.py @ 2624:56f94936df1e

code style reformatting using black
author Goffi <goffi@goffi.org>
date Wed, 27 Jun 2018 20:14:46 +0200
parents 26edcf3a30eb
children 003b8b4b56a7
comparison
equal deleted inserted replaced
2623:49533de4540b 2624:56f94936df1e
16 16
17 # You should have received a copy of the GNU Affero General Public License 17 # You should have received a copy of the GNU Affero General Public License
18 # along with this program. If not, see <http://www.gnu.org/licenses/>. 18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
19 19
20 from sat.core.log import getLogger 20 from sat.core.log import getLogger
21
21 log = getLogger(__name__) 22 log = getLogger(__name__)
22 from sat_frontends.tools.jid import JID 23 from sat_frontends.tools.jid import JID
23 24
24 25
25 class QuickTarotGame(object): 26 class QuickTarotGame(object):
26
27 def __init__(self, parent, referee, players): 27 def __init__(self, parent, referee, players):
28 self._autoplay = None #XXX: use 0 to activate fake play, None else 28 self._autoplay = None # XXX: use 0 to activate fake play, None else
29 self.parent = parent 29 self.parent = parent
30 self.referee = referee 30 self.referee = referee
31 self.players = players 31 self.players = players
32 self.played = {} 32 self.played = {}
33 for player in players: 33 for player in players:
40 idx = (idx + 1) % len(self.players) 40 idx = (idx + 1) % len(self.players)
41 self.top_nick = unicode(self.players[idx]) 41 self.top_nick = unicode(self.players[idx])
42 idx = (idx + 1) % len(self.players) 42 idx = (idx + 1) % len(self.players)
43 self.left_nick = unicode(self.players[idx]) 43 self.left_nick = unicode(self.players[idx])
44 self.bottom_nick = unicode(self.player_nick) 44 self.bottom_nick = unicode(self.player_nick)
45 self.selected = [] #Card choosed by the player (e.g. during ecart) 45 self.selected = [] # Card choosed by the player (e.g. during ecart)
46 self.hand_size = 13 #number of cards in a hand 46 self.hand_size = 13 # number of cards in a hand
47 self.hand = [] 47 self.hand = []
48 self.to_show = [] 48 self.to_show = []
49 self.state = None 49 self.state = None
50 50
51 def resetRound(self): 51 def resetRound(self):
57 for pl in self.played: 57 for pl in self.played:
58 self.played[pl] = None 58 self.played[pl] = None
59 59
60 def getPlayerLocation(self, nick): 60 def getPlayerLocation(self, nick):
61 """return player location (top,bottom,left or right)""" 61 """return player location (top,bottom,left or right)"""
62 for location in ['top','left','bottom','right']: 62 for location in ["top", "left", "bottom", "right"]:
63 if getattr(self,'%s_nick' % location) == nick: 63 if getattr(self, "%s_nick" % location) == nick:
64 return location 64 return location
65 assert(False) 65 assert False
66 66
67 def loadCards(self): 67 def loadCards(self):
68 """Load all the cards in memory 68 """Load all the cards in memory
69 @param dir: directory where the PNG files are""" 69 @param dir: directory where the PNG files are"""
70 self.cards={} 70 self.cards = {}
71 self.deck=[] 71 self.deck = []
72 self.cards["atout"]={} #As Tarot is a french game, it's more handy & logical to keep french names 72 self.cards[
73 self.cards["pique"]={} #spade 73 "atout"
74 self.cards["coeur"]={} #heart 74 ] = {} # As Tarot is a french game, it's more handy & logical to keep french names
75 self.cards["carreau"]={} #diamond 75 self.cards["pique"] = {} # spade
76 self.cards["trefle"]={} #club 76 self.cards["coeur"] = {} # heart
77 self.cards["carreau"] = {} # diamond
78 self.cards["trefle"] = {} # club
77 79
78 def tarotGameNewHandler(self, hand): 80 def tarotGameNewHandler(self, hand):
79 """Start a new game, with given hand""" 81 """Start a new game, with given hand"""
80 assert (len(self.hand) == 0) 82 assert len(self.hand) == 0
81 for suit, value in hand: 83 for suit, value in hand:
82 self.hand.append(self.cards[suit, value]) 84 self.hand.append(self.cards[suit, value])
83 self.hand.sort() 85 self.hand.sort()
84 self.state = "init" 86 self.state = "init"
85 87
91 def tarotGameShowCardsHandler(self, game_stage, cards, data): 93 def tarotGameShowCardsHandler(self, game_stage, cards, data):
92 """Display cards in the middle of the game (to show for e.g. chien ou poignée)""" 94 """Display cards in the middle of the game (to show for e.g. chien ou poignée)"""
93 self.to_show = [] 95 self.to_show = []
94 for suit, value in cards: 96 for suit, value in cards:
95 self.to_show.append(self.cards[suit, value]) 97 self.to_show.append(self.cards[suit, value])
96 if game_stage == "chien" and data['attaquant'] == self.player_nick: 98 if game_stage == "chien" and data["attaquant"] == self.player_nick:
97 self.state = "wait_for_ecart" 99 self.state = "wait_for_ecart"
98 else: 100 else:
99 self.state = "chien" 101 self.state = "chien"
100 102
101 def tarotGameYourTurnHandler(self): 103 def tarotGameYourTurnHandler(self):
111 if self._autoplay == None: 113 if self._autoplay == None:
112 return 114 return
113 if self._autoplay >= len(self.hand): 115 if self._autoplay >= len(self.hand):
114 self._autoplay = 0 116 self._autoplay = 0
115 card = self.hand[self._autoplay] 117 card = self.hand[self._autoplay]
116 self.parent.host.bridge.tarotGamePlayCards(self.player_nick, self.referee, [(card.suit, card.value)], self.parent.profile) 118 self.parent.host.bridge.tarotGamePlayCards(
119 self.player_nick, self.referee, [(card.suit, card.value)], self.parent.profile
120 )
117 del self.hand[self._autoplay] 121 del self.hand[self._autoplay]
118 self.state = "wait" 122 self.state = "wait"
119 self._autoplay+=1 123 self._autoplay += 1
120 124
121 def tarotGameScoreHandler(self, xml_data, winners, loosers): 125 def tarotGameScoreHandler(self, xml_data, winners, loosers):
122 """Called at the end of a game 126 """Called at the end of a game
123 @param xml_data: SàT xml representation of the scores 127 @param xml_data: SàT xml representation of the scores
124 @param winners: list of winners' nicks 128 @param winners: list of winners' nicks
128 def tarotGameCardsPlayedHandler(self, player, cards): 132 def tarotGameCardsPlayedHandler(self, player, cards):
129 """A card has been played by player""" 133 """A card has been played by player"""
130 if self.to_show: 134 if self.to_show:
131 self.to_show = [] 135 self.to_show = []
132 pl_cards = [] 136 pl_cards = []
133 if self.played[player] != None: #FIXME 137 if self.played[player] != None: # FIXME
134 for pl in self.played: 138 for pl in self.played:
135 self.played[pl] = None 139 self.played[pl] = None
136 for suit, value in cards: 140 for suit, value in cards:
137 pl_cards.append(self.cards[suit, value]) 141 pl_cards.append(self.cards[suit, value])
138 self.played[player] = pl_cards[0] 142 self.played[player] = pl_cards[0]
146 if phase == "play": 150 if phase == "play":
147 self.state = "play" 151 self.state = "play"
148 elif phase == "ecart": 152 elif phase == "ecart":
149 self.state = "ecart" 153 self.state = "ecart"
150 else: 154 else:
151 log.error ('INTERNAL ERROR: unmanaged game phase') 155 log.error("INTERNAL ERROR: unmanaged game phase")
152 156
153 for suit, value in played_cards: 157 for suit, value in played_cards:
154 self.hand.append(self.cards[suit, value]) 158 self.hand.append(self.cards[suit, value])
155 159
156 self.hand.sort() 160 self.hand.sort()
157 self.__fakePlay() 161 self.__fakePlay()
158