Mercurial > libervia-backend
comparison plugins/plugin_misc_tarot.py @ 90:4020931569b8
Tarot Game: session initialization
author | Goffi <goffi@goffi.org> |
---|---|
date | Sun, 23 May 2010 16:39:05 +0930 |
parents | 23caf1051099 |
children | 39c672544593 |
comparison
equal
deleted
inserted
replaced
89:23caf1051099 | 90:4020931569b8 |
---|---|
40 try: | 40 try: |
41 from twisted.words.protocols.xmlstream import XMPPHandler | 41 from twisted.words.protocols.xmlstream import XMPPHandler |
42 except ImportError: | 42 except ImportError: |
43 from wokkel.subprotocols import XMPPHandler | 43 from wokkel.subprotocols import XMPPHandler |
44 | 44 |
45 MESSAGE = '/message' | |
46 NS_CG = 'http://www.goffi.org/protocol/card_game' | |
47 CG_TAG = 'card_game' | |
48 CG_REQUEST = MESSAGE + '/' + CG_TAG + '[@xmlns="' + NS_CG + '"]' | |
45 | 49 |
46 PLUGIN_INFO = { | 50 PLUGIN_INFO = { |
47 "name": "Tarot cards plugin", | 51 "name": "Tarot cards plugin", |
48 "import_name": "Tarot", | 52 "import_name": "Tarot", |
49 "type": "Misc", | 53 "type": "Misc", |
50 "protocols": [], | 54 "protocols": [], |
51 "dependencies": ["XEP_0045"], | 55 "dependencies": ["XEP_0045"], |
52 "main": "Tarot", | 56 "main": "Tarot", |
53 "handler": "no", | 57 "handler": "yes", |
54 "description": _("""Implementation of Tarot card game""") | 58 "description": _("""Implementation of Tarot card game""") |
55 } | 59 } |
56 | 60 |
57 class Tarot(): | 61 class Tarot(): |
58 | 62 |
59 def __init__(self, host): | 63 def __init__(self, host): |
60 info(_("Plugin Tarot initialization")) | 64 info(_("Plugin Tarot initialization")) |
61 self.host = host | 65 self.host = host |
62 self.games={} | 66 self.games={} |
63 host.bridge.addMethod("createTarotGame", ".communication", in_sign='sass', out_sign='', method=self.createGame) | 67 host.bridge.addMethod("tarotGameCreate", ".communication", in_sign='sass', out_sign='', method=self.createGame) #args: room_jid, players, profile |
64 host.bridge.addSignal("tarotGameStarted", ".communication", signature='sass') #args: room_id, players, profile | 68 host.bridge.addMethod("tarotGameReady", ".communication", in_sign='sss', out_sign='', method=self.newPlayerReady) #args: user, referee, profile |
65 host.bridge.addSignal("tarotGameNew", ".communication", signature='sa(ss)s') #args: room_id, hand, profile | 69 host.bridge.addSignal("tarotGameStarted", ".communication", signature='ssass') #args: room_jid, referee, players, profile |
70 host.bridge.addSignal("tarotGameNew", ".communication", signature='sa(ss)s') #args: room_jid, hand, profile | |
66 self.deck_ordered = [] | 71 self.deck_ordered = [] |
67 for value in map(str,range(1,22))+['excuse']: | 72 for value in map(str,range(1,22))+['excuse']: |
68 self.deck_ordered.append(("atout",value)) | 73 self.deck_ordered.append(("atout",value)) |
69 for family in ["pique", "coeur", "carreau", "trefle"]: | 74 for family in ["pique", "coeur", "carreau", "trefle"]: |
70 for value in map(str,range(1,11))+["valet","cavalier","dame","roi"]: | 75 for value in map(str,range(1,11))+["valet","cavalier","dame","roi"]: |
71 self.deck_ordered.append((family, value)) | 76 self.deck_ordered.append((family, value)) |
72 | 77 |
73 def createGame(self, room_jid, players, profile_key='@DEFAULT@'): | 78 def createGameElt(self, to_jid): |
79 elt = domish.Element(('jabber:client','message')) | |
80 elt["to"] = to_jid.full() | |
81 elt.addElement((NS_CG, CG_TAG)) | |
82 return elt | |
83 | |
84 def __hand_to_xml(self, hand): | |
85 """Convert a hand (list of tuples) to domish element""" | |
86 hand_elt = domish.Element(('','hand')) | |
87 for family, value in hand: | |
88 card_elt = domish.Element(('','card')) | |
89 card_elt['family'] = family | |
90 card_elt['value'] = value | |
91 hand_elt.addChild(card_elt) | |
92 return hand_elt | |
93 | |
94 def __xml_to_hand(self, hand_elt): | |
95 """Convert a hand domish element to a list of tuples""" | |
96 hand = [] | |
97 assert (hand_elt.name == 'hand') | |
98 for card in hand_elt.elements(): | |
99 hand.append((card['family'], card['value'])) | |
100 return hand | |
101 | |
102 def __create_started_elt(self, players): | |
103 """Create a game_started domish element""" | |
104 started_elt = domish.Element(('','started')) | |
105 idx = 0 | |
106 for player in players: | |
107 player_elt = domish.Element(('','player')) | |
108 player_elt.addContent(player) | |
109 player_elt['index'] = str(idx) | |
110 idx+=1 | |
111 started_elt.addChild(player_elt) | |
112 return started_elt | |
113 | |
114 def createGame(self, room_jid_param, players, profile_key='@DEFAULT@'): | |
74 """Create a new game""" | 115 """Create a new game""" |
75 debug (_("Creating Tarot game")) | 116 debug (_("Creating Tarot game")) |
117 room_jid = jid.JID(room_jid_param) | |
76 profile = self.host.memory.getProfileName(profile_key) | 118 profile = self.host.memory.getProfileName(profile_key) |
77 if not profile: | 119 if not profile: |
78 error (_("profile %s is unknown") % profile_key) | 120 error (_("profile %s is unknown") % profile_key) |
79 return | 121 return |
80 if False: #gof: self.games.has_key(room_jid): | 122 if False: #gof: self.games.has_key(room_jid): |
81 warning (_("Tarot game already started in room %s") % room_jid) | 123 warning (_("Tarot game already started in room %s") % room_jid.userhost()) |
82 else: | 124 else: |
83 self.games[room_jid] = {'players':players, 'profile':profile, 'hand_size':18, 'player_start':0} | 125 status = {} |
84 self.host.bridge.tarotGameStarted(room_jid, players, profile) | 126 for player in players: |
85 self.newGame(room_jid) | 127 status[player] = "init" |
128 self.games[room_jid.userhost()] = {'players':players, 'status':status, 'profile':profile, 'hand_size':18, 'player_start':0} | |
129 for player in players: | |
130 mess = self.createGameElt(jid.JID(room_jid.userhost()+'/'+player)) | |
131 mess.firstChildElement().addChild(self.__create_started_elt(players)) | |
132 self.host.profiles[profile].xmlstream.send(mess) | |
133 | |
134 def newPlayerReady(self, user, referee, profile_key='@DEFAULT@'): | |
135 """Must be called when player is ready to start a new game""" | |
136 profile = self.host.memory.getProfileName(profile_key) | |
137 if not profile: | |
138 error (_("profile %s is unknown") % profile_key) | |
139 return | |
140 debug ('new player ready: %s' % profile) | |
141 mess = self.createGameElt(jid.JID(referee)) | |
142 mess.firstChildElement().addElement('player_ready', content=user) | |
143 self.host.profiles[profile].xmlstream.send(mess) | |
86 | 144 |
87 | 145 |
88 def newGame(self, room_jid): | 146 def newGame(self, room_jid): |
89 """Launch a new round""" | 147 """Launch a new round""" |
90 debug (_('new Tarot game')) | 148 debug (_('new Tarot game')) |
91 deck = self.deck_ordered[:] | 149 deck = self.deck_ordered[:] |
92 random.shuffle(deck) | 150 random.shuffle(deck) |
93 profile = self.games[room_jid]['profile'] | 151 profile = self.games[room_jid.userhost()]['profile'] |
94 players = self.games[room_jid]['players'] | 152 players = self.games[room_jid.userhost()]['players'] |
95 hand = self.games[room_jid]['hand'] = {} | 153 hand = self.games[room_jid.userhost()]['hand'] = {} |
96 hand_size = self.games[room_jid]['hand_size'] | 154 hand_size = self.games[room_jid.userhost()]['hand_size'] |
97 chien = self.games[room_jid]['chien'] = [] | 155 chien = self.games[room_jid.userhost()]['chien'] = [] |
98 for i in range(4): #TODO: distribute according to real Tarot rules (3 by 3 counter-clockwise, 1 card at once to chien) | 156 for i in range(4): #TODO: distribute according to real Tarot rules (3 by 3 counter-clockwise, 1 card at once to chien) |
99 hand[players[i]] = deck[0:hand_size] | 157 hand[players[i]] = deck[0:hand_size] |
100 del deck[0:hand_size] | 158 del deck[0:hand_size] |
101 chien = deck[:] | 159 chien = deck[:] |
102 del(deck[:]) | 160 del(deck[:]) |
103 | 161 |
104 for player in players: | 162 for player in players: |
105 self.host.sendMessage(room_jid+"/"+player, "/hand: %s" % str(hand[player])) | 163 to_jid = jid.JID(room_jid.userhost()+"/"+player) #FIXME: gof: |
164 mess = self.createGameElt(to_jid) | |
165 mess.firstChildElement().addChild(self.__hand_to_xml(hand[player])) | |
166 self.host.profiles[profile].xmlstream.send(mess) | |
106 | 167 |
107 self.host.bridge.tarotGameNew(room_jid, hand[players[0]], profile) | 168 |
108 | 169 def card_game_cmd(self, mess_elt, profile): |
109 | 170 print "\n\nCARD GAME command received (profile=%s): %s" % (profile, mess_elt.toXml()) |
171 room_jid = jid.JID(mess_elt['from']) | |
172 game_elt = mess_elt.firstChildElement() | |
173 for elt in game_elt.elements(): #new game created | |
174 if elt.name == 'started': | |
175 players = [] | |
176 for player in elt.elements(): | |
177 players.append(unicode(player)) | |
178 self.host.bridge.tarotGameStarted(room_jid.userhost(), room_jid.full(), players, profile) | |
179 elif elt.name == 'player_ready': | |
180 player = unicode(elt) | |
181 status = self.games[room_jid.userhost()]['status'] | |
182 nb_players = len(self.games[room_jid.userhost()]['players']) | |
183 status[player] = 'ready' | |
184 debug (_('Player %(player)s is ready to start [status: %(status)s]') % {'player':player, 'status':status}) | |
185 if status.values().count('ready') == 2: #gof: nb_players: #everybody is ready, we can start the game | |
186 self.newGame(room_jid) | |
187 | |
188 elif elt.name == 'hand': #a new hand has been received | |
189 self.host.bridge.tarotGameNew(room_jid.userhost(), self.__xml_to_hand(elt), profile) | |
190 | |
191 | |
192 def getHandler(self, profile): | |
193 return CardGameHandler(self) | |
194 | |
195 | |
196 | |
197 class CardGameHandler (XMPPHandler): | |
198 implements(iwokkel.IDisco) | |
199 | |
200 def __init__(self, plugin_parent): | |
201 self.plugin_parent = plugin_parent | |
202 self.host = plugin_parent.host | |
203 | |
204 def connectionInitialized(self): | |
205 print "gof: ajout d'observer", CG_REQUEST | |
206 self.xmlstream.addObserver(CG_REQUEST, self.plugin_parent.card_game_cmd, profile = self.parent.profile) | |
207 | |
208 def getDiscoInfo(self, requestor, target, nodeIdentifier=''): | |
209 return [disco.DiscoFeature(NS_CB)] | |
210 | |
211 def getDiscoItems(self, requestor, target, nodeIdentifier=''): | |
212 return [] | |
213 |