comparison plugins/plugin_misc_tarot.py @ 93:2f87651a5ad8

Tarot game: basic trick - plugin xep-0045: new method getRoomNick - plugin tarot: basic trick - wix: played card are shown in CardPanel
author Goffi <goffi@goffi.org>
date Sun, 30 May 2010 15:33:08 +0930
parents 2503de7fb4c7
children 1eb5ccead43c
comparison
equal deleted inserted replaced
92:2503de7fb4c7 93:2f87651a5ad8
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.addMethod("tarotGamePlayCards", ".communication", in_sign='ssa(ss)s', out_sign='', method=self.play_cards) #args: player, referee, cards, profile
73 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
74 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
75 host.bridge.addSignal("tarotGameChooseContrat", ".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 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("tarotGameCardsPlayed", ".communication", signature='ssa(ss)s') #args: room_jid, player, type ["chien", "poignée",...], cards, data[dict], profile
77 host.bridge.addSignal("tarotGameYourTurn", ".communication", signature='ss') #args: room_jid, profile 78 host.bridge.addSignal("tarotGameYourTurn", ".communication", signature='ss') #args: room_jid, profile
78 self.deck_ordered = [] 79 self.deck_ordered = []
79 for value in ['excuse']+map(str,range(1,22)): 80 for value in ['excuse']+map(str,range(1,22)):
80 self.deck_ordered.append(("atout",value)) 81 self.deck_ordered.append(("atout",value))
81 for suit in ["pique", "coeur", "carreau", "trefle"]: 82 for suit in ["pique", "coeur", "carreau", "trefle"]:
143 error (_("profile %s is unknown") % profile_key) 144 error (_("profile %s is unknown") % profile_key)
144 return 145 return
145 if False: #gof: self.games.has_key(room_jid): 146 if False: #gof: self.games.has_key(room_jid):
146 warning (_("Tarot game already started in room %s") % room_jid.userhost()) 147 warning (_("Tarot game already started in room %s") % room_jid.userhost())
147 else: 148 else:
149 room_nick = self.host.plugins["XEP_0045"].getRoomNick(room_jid.userhost(), profile)
150 if not room_nick:
151 error ('Internal error')
152 return
153 referee = room_jid.userhost() + '/' + room_nick
148 status = {} 154 status = {}
149 players_data = {} 155 players_data = {}
150 for player in players: 156 for player in players:
151 players_data[player] = {} 157 players_data[player] = {}
152 status[player] = "init" 158 status[player] = "init"
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} 159 self.games[room_jid.userhost()] = {'referee':referee, 'players':players, 'status':status, 'players_data':players_data, 'hand_size':18, 'init_player':0, 'current_player': None, 'stage': None}
154 for player in players: 160 for player in players:
155 mess = self.createGameElt(jid.JID(room_jid.userhost()+'/'+player)) 161 mess = self.createGameElt(jid.JID(room_jid.userhost()+'/'+player))
156 mess.firstChildElement().addChild(self.__create_started_elt(players)) 162 mess.firstChildElement().addChild(self.__create_started_elt(players))
157 self.host.profiles[profile].xmlstream.send(mess) 163 self.host.profiles[profile].xmlstream.send(mess)
158 164
237 self.host.profiles[profile].xmlstream.send(mess) 243 self.host.profiles[profile].xmlstream.send(mess)
238 244
239 245
240 def card_game_cmd(self, mess_elt, profile): 246 def card_game_cmd(self, mess_elt, profile):
241 print "\n\nCARD GAME command received (profile=%s): %s" % (profile, mess_elt.toXml()) 247 print "\n\nCARD GAME command received (profile=%s): %s" % (profile, mess_elt.toXml())
242 room_jid = jid.JID(mess_elt['from']) 248 from_jid = jid.JID(mess_elt['from'])
249 room_jid = jid.JID(from_jid.userhost())
243 game_elt = mess_elt.firstChildElement() 250 game_elt = mess_elt.firstChildElement()
244 game_data = self.games[room_jid.userhost()] 251 game_data = self.games[room_jid.userhost()]
245 players_data = game_data['players_data'] 252 players_data = game_data['players_data']
246 253
247 for elt in game_elt.elements(): 254 for elt in game_elt.elements():
248 255
249 if elt.name == 'started': #new game created 256 if elt.name == 'started': #new game created
250 players = [] 257 players = []
251 for player in elt.elements(): 258 for player in elt.elements():
252 players.append(unicode(player)) 259 players.append(unicode(player))
253 self.host.bridge.tarotGameStarted(room_jid.userhost(), room_jid.full(), players, profile) 260 self.host.bridge.tarotGameStarted(room_jid.userhost(), from_jid.full(), players, profile)
254 261
255 elif elt.name == 'player_ready': #ready to play 262 elif elt.name == 'player_ready': #ready to play
256 player = elt['player'] 263 player = elt['player']
257 status = self.games[room_jid.userhost()]['status'] 264 status = self.games[room_jid.userhost()]['status']
258 nb_players = len(self.games[room_jid.userhost()]['players']) 265 nb_players = len(self.games[room_jid.userhost()]['players'])
321 game_data['stage'] = "play" 328 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 329 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] 330 next_player = game_data['players'][next_player_idx]
324 to_jid = jid.JID(room_jid.userhost()+"/"+next_player) #FIXME: gof: 331 to_jid = jid.JID(room_jid.userhost()+"/"+next_player) #FIXME: gof:
325 mess = self.createGameElt(to_jid) 332 mess = self.createGameElt(to_jid)
326 self.host.profiles[profile].xmlstream.send(mess)
327 yourturn_elt = mess.firstChildElement().addElement('your_turn') 333 yourturn_elt = mess.firstChildElement().addElement('your_turn')
328 self.host.profiles[profile].xmlstream.send(mess) 334 self.host.profiles[profile].xmlstream.send(mess)
335 elif game_data['stage'] == "play":
336 current_player = game_data['players'][game_data['current_player']]
337 #assert (elt['player'] == current_player) #TODO: throw xml error here
338 cards = self.__xml_to_list(elt)
339 #TODO: check card validity and send error mess if necessary
340 if mess_elt['type'] != 'groupchat':
341 #the card played is ok, we forward it to everybody
342 #first we remove it from the hand
343 game_data['hand'][current_player].remove(cards[0])
344
345 #then we forward the message
346 mess = self.createGameElt(room_jid)
347 playcard_elt = mess.firstChildElement().addChild(elt)
348 self.host.profiles[profile].xmlstream.send(mess)
349
350 #finally, we tell to the next player to play
351 next_player = self.__next_player(game_data)
352 to_jid = jid.JID(room_jid.userhost()+"/"+next_player) #FIXME: gof:
353 mess = self.createGameElt(to_jid)
354 yourturn_elt = mess.firstChildElement().addElement('your_turn')
355 self.host.profiles[profile].xmlstream.send(mess)
356 else:
357 self.host.bridge.tarotGameCardsPlayed(room_jid.userhost(), elt['player'], self.__xml_to_list(elt), profile)
358
359
360
329 361
330 elif elt.name == 'your_turn': 362 elif elt.name == 'your_turn':
331 self.host.bridge.tarotGameYourTurn(room_jid.userhost(), profile) 363 self.host.bridge.tarotGameYourTurn(room_jid.userhost(), profile)
332 364
333 365