Mercurial > libervia-backend
comparison plugins/plugin_misc_tarot.py @ 92:2503de7fb4c7
Tarot game: chien/écart stage
- tarot plugin: new methods/signals tarotGamePlayCards, tarotGameShowCards, tarotGameYourTurn
- tarot plugin: protocole update
- tarot plugin: family renamed in suit
- wix: card_game: card can be selected for écart, card move when mouse is over only if it's our turn
author | Goffi <goffi@goffi.org> |
---|---|
date | Sat, 29 May 2010 20:53:03 +0930 |
parents | 39c672544593 |
children | 2f87651a5ad8 |
comparison
equal
deleted
inserted
replaced
91:39c672544593 | 92:2503de7fb4c7 |
---|---|
65 info(_("Plugin Tarot initialization")) | 65 info(_("Plugin Tarot initialization")) |
66 self.host = host | 66 self.host = host |
67 self.games={} | 67 self.games={} |
68 self.contrats = [_('Passe'), _('Petite'), _('Garde'), _('Garde Sans'), _('Garde Contre')] | 68 self.contrats = [_('Passe'), _('Petite'), _('Garde'), _('Garde Sans'), _('Garde Contre')] |
69 host.bridge.addMethod("tarotGameCreate", ".communication", in_sign='sass', out_sign='', method=self.createGame) #args: room_jid, players, profile | 69 host.bridge.addMethod("tarotGameCreate", ".communication", in_sign='sass', out_sign='', method=self.createGame) #args: room_jid, players, profile |
70 host.bridge.addMethod("tarotGameReady", ".communication", in_sign='sss', out_sign='', method=self.newPlayerReady) #args: user, referee, profile | 70 host.bridge.addMethod("tarotGameReady", ".communication", in_sign='sss', out_sign='', method=self.newPlayerReady) #args: player, referee, profile |
71 host.bridge.addMethod("tarotGameContratChoosed", ".communication", in_sign='ssss', out_sign='', method=self.contratChoosed) #args: user, referee, contrat, profile | 71 host.bridge.addMethod("tarotGameContratChoosed", ".communication", in_sign='ssss', out_sign='', method=self.contratChoosed) #args: player, referee, contrat, profile |
72 host.bridge.addMethod("tarotGamePlayCards", ".communication", in_sign='ssa(ss)s', out_sign='', method=self.play_cards) #args: player, referee, cards, profile | |
72 host.bridge.addSignal("tarotGameStarted", ".communication", signature='ssass') #args: room_jid, referee, players, profile | 73 host.bridge.addSignal("tarotGameStarted", ".communication", signature='ssass') #args: room_jid, referee, players, profile |
73 host.bridge.addSignal("tarotGameNew", ".communication", signature='sa(ss)s') #args: room_jid, hand, profile | 74 host.bridge.addSignal("tarotGameNew", ".communication", signature='sa(ss)s') #args: room_jid, hand, profile |
74 host.bridge.addSignal("tarotChooseContrat", ".communication", signature='sss') #args: room_jid, xml_data, profile | 75 host.bridge.addSignal("tarotGameChooseContrat", ".communication", signature='sss') #args: room_jid, xml_data, profile |
76 host.bridge.addSignal("tarotGameShowCards", ".communication", signature='ssa(ss)a{ss}s') #args: room_jid, type ["chien", "poignée",...], cards, data[dict], profile | |
77 host.bridge.addSignal("tarotGameYourTurn", ".communication", signature='ss') #args: room_jid, profile | |
75 self.deck_ordered = [] | 78 self.deck_ordered = [] |
76 for value in map(str,range(1,22))+['excuse']: | 79 for value in ['excuse']+map(str,range(1,22)): |
77 self.deck_ordered.append(("atout",value)) | 80 self.deck_ordered.append(("atout",value)) |
78 for family in ["pique", "coeur", "carreau", "trefle"]: | 81 for suit in ["pique", "coeur", "carreau", "trefle"]: |
79 for value in map(str,range(1,11))+["valet","cavalier","dame","roi"]: | 82 for value in map(str,range(1,11))+["valet","cavalier","dame","roi"]: |
80 self.deck_ordered.append((family, value)) | 83 self.deck_ordered.append((suit, value)) |
81 | 84 |
82 def createGameElt(self, to_jid): | 85 def createGameElt(self, to_jid, type="normal"): |
86 type = "normal" if to_jid.resource else "groupchat" | |
83 elt = domish.Element(('jabber:client','message')) | 87 elt = domish.Element(('jabber:client','message')) |
84 elt["to"] = to_jid.full() | 88 elt["to"] = to_jid.full() |
89 elt["type"] = type | |
85 elt.addElement((NS_CG, CG_TAG)) | 90 elt.addElement((NS_CG, CG_TAG)) |
86 return elt | 91 return elt |
87 | 92 |
88 def __hand_to_xml(self, hand): | 93 def __list_to_xml(self, cards_list, elt_name): |
89 """Convert a hand (list of tuples) to domish element""" | 94 """Convert a card list (list of tuples) to domish element""" |
90 hand_elt = domish.Element(('','hand')) | 95 cards_list_elt = domish.Element(('',elt_name)) |
91 for family, value in hand: | 96 for suit, value in cards_list: |
92 card_elt = domish.Element(('','card')) | 97 card_elt = domish.Element(('','card')) |
93 card_elt['family'] = family | 98 card_elt['suit'] = suit |
94 card_elt['value'] = value | 99 card_elt['value'] = value |
95 hand_elt.addChild(card_elt) | 100 cards_list_elt.addChild(card_elt) |
96 return hand_elt | 101 return cards_list_elt |
97 | 102 |
98 def __xml_to_hand(self, hand_elt): | 103 def __xml_to_list(self, cards_list_elt): |
99 """Convert a hand domish element to a list of tuples""" | 104 """Convert a domish element with cards to a list of tuples""" |
100 hand = [] | 105 cards_list = [] |
101 assert (hand_elt.name == 'hand') | 106 for card in cards_list_elt.elements(): |
102 for card in hand_elt.elements(): | 107 cards_list.append((card['suit'], card['value'])) |
103 hand.append((card['family'], card['value'])) | 108 return cards_list |
104 return hand | |
105 | 109 |
106 def __create_started_elt(self, players): | 110 def __create_started_elt(self, players): |
107 """Create a game_started domish element""" | 111 """Create a game_started domish element""" |
108 started_elt = domish.Element(('','started')) | 112 started_elt = domish.Element(('','started')) |
109 idx = 0 | 113 idx = 0 |
121 form = data_form.Form('form', title=_('contrat selection')) | 125 form = data_form.Form('form', title=_('contrat selection')) |
122 field = data_form.Field('list-single', 'contrat', options=map(data_form.Option, self.contrats), required=True) | 126 field = data_form.Field('list-single', 'contrat', options=map(data_form.Option, self.contrats), required=True) |
123 form.addField(field) | 127 form.addField(field) |
124 contrat_elt.addChild(form.toElement()) | 128 contrat_elt.addChild(form.toElement()) |
125 return contrat_elt | 129 return contrat_elt |
126 | |
127 | |
128 | |
129 | 130 |
130 def __next_player(self, game_data): | 131 def __next_player(self, game_data): |
131 """It's next player turn | 132 """It's next player turn |
132 Increment player number & return player name""" | 133 Increment player number & return player name""" |
133 pl_idx = game_data['current_player'] = (game_data['current_player'] + 1) % len(game_data['players']) | 134 pl_idx = game_data['current_player'] = (game_data['current_player'] + 1) % len(game_data['players']) |
147 status = {} | 148 status = {} |
148 players_data = {} | 149 players_data = {} |
149 for player in players: | 150 for player in players: |
150 players_data[player] = {} | 151 players_data[player] = {} |
151 status[player] = "init" | 152 status[player] = "init" |
152 self.games[room_jid.userhost()] = {'players':players, 'status':status, 'players_data':players_data, 'referee_profile':profile, 'hand_size':18, 'init_player':0, 'current_player': None} | 153 self.games[room_jid.userhost()] = {'players':players, 'status':status, 'players_data':players_data, 'hand_size':18, 'init_player':0, 'current_player': None, 'stage': None} |
153 for player in players: | 154 for player in players: |
154 mess = self.createGameElt(jid.JID(room_jid.userhost()+'/'+player)) | 155 mess = self.createGameElt(jid.JID(room_jid.userhost()+'/'+player)) |
155 mess.firstChildElement().addChild(self.__create_started_elt(players)) | 156 mess.firstChildElement().addChild(self.__create_started_elt(players)) |
156 self.host.profiles[profile].xmlstream.send(mess) | 157 self.host.profiles[profile].xmlstream.send(mess) |
157 | 158 |
158 def newPlayerReady(self, user, referee, profile_key='@DEFAULT@'): | 159 def newPlayerReady(self, player, referee, profile_key='@DEFAULT@'): |
159 """Must be called when player is ready to start a new game""" | 160 """Must be called when player is ready to start a new game""" |
160 profile = self.host.memory.getProfileName(profile_key) | 161 profile = self.host.memory.getProfileName(profile_key) |
161 if not profile: | 162 if not profile: |
162 error (_("profile %s is unknown") % profile_key) | 163 error (_("profile %s is unknown") % profile_key) |
163 return | 164 return |
164 debug ('new player ready: %s' % profile) | 165 debug ('new player ready: %s' % profile) |
165 mess = self.createGameElt(jid.JID(referee)) | 166 mess = self.createGameElt(jid.JID(referee)) |
166 ready_elt = mess.firstChildElement().addElement('player_ready') | 167 ready_elt = mess.firstChildElement().addElement('player_ready') |
167 ready_elt['user'] = user | 168 ready_elt['player'] = player |
168 self.host.profiles[profile].xmlstream.send(mess) | 169 self.host.profiles[profile].xmlstream.send(mess) |
169 | 170 |
170 def contratChoosed(self, user, referee, contrat, profile_key='@DEFAULT@'): | 171 def contratChoosed(self, player, referee, contrat, profile_key='@DEFAULT@'): |
171 """Must be call by player when the contrat is selected | 172 """Must be call by player when the contrat is selected |
172 @param user: player's name | 173 @param player: player's name |
173 @param referee: arbiter jid | 174 @param referee: arbiter jid |
174 @contrat: contrat choosed (must be the exact same string than in the give list options) | 175 @contrat: contrat choosed (must be the exact same string than in the give list options) |
175 @profile_key: profile | 176 @profile_key: profile |
176 """ | 177 """ |
177 profile = self.host.memory.getProfileName(profile_key) | 178 profile = self.host.memory.getProfileName(profile_key) |
179 error (_("profile %s is unknown") % profile_key) | 180 error (_("profile %s is unknown") % profile_key) |
180 return | 181 return |
181 debug (_('contrat [%(contrat)s] choosed by %(profile)s') % {'contrat':contrat, 'profile':profile}) | 182 debug (_('contrat [%(contrat)s] choosed by %(profile)s') % {'contrat':contrat, 'profile':profile}) |
182 mess = self.createGameElt(jid.JID(referee)) | 183 mess = self.createGameElt(jid.JID(referee)) |
183 contrat_elt = mess.firstChildElement().addElement(('','contrat_choosed'), content=contrat) | 184 contrat_elt = mess.firstChildElement().addElement(('','contrat_choosed'), content=contrat) |
184 contrat_elt['user'] = user | 185 contrat_elt['player'] = player |
185 self.host.profiles[profile].xmlstream.send(mess) | 186 self.host.profiles[profile].xmlstream.send(mess) |
186 | 187 |
187 | 188 def play_cards(self, player, referee, cards, profile_key='@DEFAULT@'): |
188 def newGame(self, room_jid): | 189 """Must be call by player when the contrat is selected |
190 @param player: player's name | |
191 @param referee: arbiter jid | |
192 @cards: cards played (list of tuples) | |
193 @profile_key: profile | |
194 """ | |
195 profile = self.host.memory.getProfileName(profile_key) | |
196 if not profile: | |
197 error (_("profile %s is unknown") % profile_key) | |
198 return | |
199 debug (_('Cards played by %(profile)s: [%(cards)s]') % {'profile':profile,'cards':cards}) | |
200 mess = self.createGameElt(jid.JID(referee)) | |
201 playcard_elt = mess.firstChildElement().addChild(self.__list_to_xml(cards, 'cards_played')) | |
202 playcard_elt['player'] = player | |
203 self.host.profiles[profile].xmlstream.send(mess) | |
204 | |
205 def newGame(self, room_jid, profile): | |
189 """Launch a new round""" | 206 """Launch a new round""" |
190 debug (_('new Tarot game')) | 207 debug (_('new Tarot game')) |
191 deck = self.deck_ordered[:] | 208 deck = self.deck_ordered[:] |
192 random.shuffle(deck) | 209 random.shuffle(deck) |
193 game_data = self.games[room_jid.userhost()] | 210 game_data = self.games[room_jid.userhost()] |
194 referee_profile = game_data['referee_profile'] | |
195 players = game_data['players'] | 211 players = game_data['players'] |
196 players_data = game_data['players_data'] | 212 players_data = game_data['players_data'] |
197 current_player = game_data['current_player'] | 213 current_player = game_data['current_player'] |
214 game_data['stage'] = "init" | |
198 hand = game_data['hand'] = {} | 215 hand = game_data['hand'] = {} |
199 hand_size = game_data['hand_size'] | 216 hand_size = game_data['hand_size'] |
200 chien = game_data['chien'] = [] | 217 chien = game_data['chien'] = [] |
201 for i in range(4): #TODO: distribute according to real Tarot rules (3 by 3 counter-clockwise, 1 card at once to chien) | 218 for i in range(4): #TODO: distribute according to real Tarot rules (3 by 3 counter-clockwise, 1 card at once to chien) |
202 hand[players[i]] = deck[0:hand_size] | 219 hand[players[i]] = deck[0:hand_size] |
203 del deck[0:hand_size] | 220 del deck[0:hand_size] |
204 chien = deck[:] | 221 chien.extend(deck) |
205 del(deck[:]) | 222 del(deck[:]) |
206 | 223 |
207 for player in players: | 224 for player in players: |
208 to_jid = jid.JID(room_jid.userhost()+"/"+player) #FIXME: gof: | 225 to_jid = jid.JID(room_jid.userhost()+"/"+player) #FIXME: gof: |
209 mess = self.createGameElt(to_jid) | 226 mess = self.createGameElt(to_jid) |
210 mess.firstChildElement().addChild(self.__hand_to_xml(hand[player])) | 227 mess.firstChildElement().addChild(self.__list_to_xml(hand[player], 'hand')) |
211 self.host.profiles[referee_profile].xmlstream.send(mess) | 228 self.host.profiles[profile].xmlstream.send(mess) |
212 players_data[player]['contrat'] = None | 229 players_data[player]['contrat'] = None |
230 players_data[player]['levees'] = [] #cards won | |
213 | 231 |
214 pl_idx = game_data['current_player'] = (game_data['init_player'] + 1) % len(players) #the player after the dealer start | 232 pl_idx = game_data['current_player'] = (game_data['init_player'] + 1) % len(players) #the player after the dealer start |
215 player = players[pl_idx] | 233 player = players[pl_idx] |
216 to_jid = jid.JID(room_jid.userhost()+"/"+player) #FIXME: gof: | 234 to_jid = jid.JID(room_jid.userhost()+"/"+player) #FIXME: gof: |
217 mess = self.createGameElt(to_jid) | 235 mess = self.createGameElt(to_jid) |
218 mess.firstChildElement().addChild(self.__ask_contrat()) | 236 mess.firstChildElement().addChild(self.__ask_contrat()) |
219 self.host.profiles[referee_profile].xmlstream.send(mess) | 237 self.host.profiles[profile].xmlstream.send(mess) |
220 | 238 |
221 | 239 |
222 def card_game_cmd(self, mess_elt, profile): | 240 def card_game_cmd(self, mess_elt, profile): |
223 print "\n\nCARD GAME command received (profile=%s): %s" % (profile, mess_elt.toXml()) | 241 print "\n\nCARD GAME command received (profile=%s): %s" % (profile, mess_elt.toXml()) |
224 room_jid = jid.JID(mess_elt['from']) | 242 room_jid = jid.JID(mess_elt['from']) |
225 game_elt = mess_elt.firstChildElement() | 243 game_elt = mess_elt.firstChildElement() |
226 for elt in game_elt.elements(): #new game created | 244 game_data = self.games[room_jid.userhost()] |
227 | 245 players_data = game_data['players_data'] |
228 if elt.name == 'started': | 246 |
247 for elt in game_elt.elements(): | |
248 | |
249 if elt.name == 'started': #new game created | |
229 players = [] | 250 players = [] |
230 for player in elt.elements(): | 251 for player in elt.elements(): |
231 players.append(unicode(player)) | 252 players.append(unicode(player)) |
232 self.host.bridge.tarotGameStarted(room_jid.userhost(), room_jid.full(), players, profile) | 253 self.host.bridge.tarotGameStarted(room_jid.userhost(), room_jid.full(), players, profile) |
233 | 254 |
234 elif elt.name == 'player_ready': | 255 elif elt.name == 'player_ready': #ready to play |
235 player = elt['user'] | 256 player = elt['player'] |
236 status = self.games[room_jid.userhost()]['status'] | 257 status = self.games[room_jid.userhost()]['status'] |
237 nb_players = len(self.games[room_jid.userhost()]['players']) | 258 nb_players = len(self.games[room_jid.userhost()]['players']) |
238 status[player] = 'ready' | 259 status[player] = 'ready' |
239 debug (_('Player %(player)s is ready to start [status: %(status)s]') % {'player':player, 'status':status}) | 260 debug (_('Player %(player)s is ready to start [status: %(status)s]') % {'player':player, 'status':status}) |
240 if status.values().count('ready') == nb_players: #everybody is ready, we can start the game | 261 if status.values().count('ready') == nb_players: #everybody is ready, we can start the game |
241 self.newGame(room_jid) | 262 self.newGame(room_jid, profile) |
242 | 263 |
243 elif elt.name == 'hand': #a new hand has been received | 264 elif elt.name == 'hand': #a new hand has been received |
244 self.host.bridge.tarotGameNew(room_jid.userhost(), self.__xml_to_hand(elt), profile) | 265 self.host.bridge.tarotGameNew(room_jid.userhost(), self.__xml_to_list(elt), profile) |
245 | 266 |
246 elif elt.name == 'contrat': #it's time to choose contrat | 267 elif elt.name == 'contrat': #it's time to choose contrat |
247 form = data_form.Form.fromElement(elt.firstChildElement()) | 268 form = data_form.Form.fromElement(elt.firstChildElement()) |
248 xml_data = XMLTools.dataForm2xml(form) | 269 xml_data = XMLTools.dataForm2xml(form) |
249 self.host.bridge.tarotChooseContrat(room_jid.userhost(), xml_data, profile) | 270 self.host.bridge.tarotGameChooseContrat(room_jid.userhost(), xml_data, profile) |
250 | 271 |
251 elif elt.name == 'contrat_choosed': #the player has chooser a contrat | 272 elif elt.name == 'contrat_choosed': |
252 #TODO: check we receive the contrat from the right person | 273 #TODO: check we receive the contrat from the right person |
253 #TODO: user proper XEP-0004 way for answering form | 274 #TODO: use proper XEP-0004 way for answering form |
254 user = elt['user'] | 275 player = elt['player'] |
255 game_data = self.games[room_jid.userhost()] | 276 players_data[player]['contrat'] = unicode(elt) |
256 players_data = game_data['players_data'] | |
257 players_data[user]['contrat'] = unicode(elt) | |
258 contrats = [players_data[player]['contrat'] for player in game_data['players']] | 277 contrats = [players_data[player]['contrat'] for player in game_data['players']] |
259 if contrats.count(None): | 278 if contrats.count(None): |
260 #not everybody has choosed his contrat, it's next one turn | 279 #not everybody has choosed his contrat, it's next one turn |
261 player = self.__next_player(game_data) | 280 player = self.__next_player(game_data) |
262 to_jid = jid.JID(room_jid.userhost()+"/"+player) #FIXME: gof: | 281 to_jid = jid.JID(room_jid.userhost()+"/"+player) #FIXME: gof: |
263 mess = self.createGameElt(to_jid) | 282 mess = self.createGameElt(to_jid) |
264 mess.firstChildElement().addChild(self.__ask_contrat()) | 283 mess.firstChildElement().addChild(self.__ask_contrat()) |
265 self.host.profiles[game_data['referee_profile']].xmlstream.send(mess) | 284 self.host.profiles[profile].xmlstream.send(mess) |
266 else: | 285 else: |
267 #TODO: manage "everybody pass" case | 286 #TODO: manage "everybody pass" case |
268 best_contrat = [None, "Passe"] | 287 best_contrat = [None, "Passe"] |
269 for player in game_data['players']: | 288 for player in game_data['players']: |
270 contrat = players_data[player]['contrat'] | 289 contrat = players_data[player]['contrat'] |
272 idx_pl = self.contrats.index(contrat) | 291 idx_pl = self.contrats.index(contrat) |
273 if idx_pl > idx_best: | 292 if idx_pl > idx_best: |
274 best_contrat[0] = player | 293 best_contrat[0] = player |
275 best_contrat[1] = contrat | 294 best_contrat[1] = contrat |
276 debug (_("%(player)s win the bid with %(contrat)s") % {'player':best_contrat[0],'contrat':best_contrat[1]}) | 295 debug (_("%(player)s win the bid with %(contrat)s") % {'player':best_contrat[0],'contrat':best_contrat[1]}) |
277 | 296 #Time to show the chien to everybody |
297 to_jid = jid.JID(room_jid.userhost()) #FIXME: gof: | |
298 mess = self.createGameElt(to_jid) | |
299 chien_elt = mess.firstChildElement().addChild(self.__list_to_xml(game_data['chien'], 'chien')) | |
300 chien_elt['attaquant'] = best_contrat[0] | |
301 self.host.profiles[profile].xmlstream.send(mess) | |
302 | |
303 #the attacker (attaquant) get the chien | |
304 game_data['hand'][best_contrat[0]].extend(game_data['chien']) | |
305 del game_data['chien'][:] | |
306 | |
307 elif elt.name == 'chien': #we have received the chien | |
308 debug (_("tarot: chien received")) | |
309 data = {"attaquant":elt['attaquant']} | |
310 players_data = game_data['players_data'] | |
311 game_data['stage'] = "ecart" | |
312 game_data['attaquant'] = elt['attaquant'] | |
313 self.host.bridge.tarotGameShowCards(room_jid.userhost(), "chien", self.__xml_to_list(elt), data, profile) | |
314 | |
315 elif elt.name == 'cards_played': | |
316 if game_data['stage'] == "ecart": | |
317 #TODO: check validity of écart (no king, no oulder, cards must be in player hand) | |
318 #TODO: show atouts (trumps) if player put some in écart | |
319 assert (game_data['attaquant'] == elt['player']) #TODO: throw an xml error here | |
320 players_data[elt['player']]['levees'].extend(self.__xml_to_list(elt)) | |
321 game_data['stage'] = "play" | |
322 next_player_idx = game_data['current_player'] = (game_data['init_player'] + 1) % len(game_data['players']) #the player after the dealer start | |
323 next_player = game_data['players'][next_player_idx] | |
324 to_jid = jid.JID(room_jid.userhost()+"/"+next_player) #FIXME: gof: | |
325 mess = self.createGameElt(to_jid) | |
326 self.host.profiles[profile].xmlstream.send(mess) | |
327 yourturn_elt = mess.firstChildElement().addElement('your_turn') | |
328 self.host.profiles[profile].xmlstream.send(mess) | |
329 | |
330 elif elt.name == 'your_turn': | |
331 self.host.bridge.tarotGameYourTurn(room_jid.userhost(), profile) | |
278 | 332 |
279 | 333 |
280 def getHandler(self, profile): | 334 def getHandler(self, profile): |
281 return CardGameHandler(self) | 335 return CardGameHandler(self) |
282 | 336 |