comparison src/plugins/plugin_misc_radiocol.py @ 826:71f8e996f765

plugins radiocol_tarot: do not process received messages for MUC users that are actually not playing the game + fix for testing purpose
author souliane <souliane@mailoo.org>
date Wed, 15 Jan 2014 23:09:39 +0100
parents e3f4d80f987d
children 215a2cb15e2d
comparison
equal deleted inserted replaced
825:e3f4d80f987d 826:71f8e996f765
109 'album': song.get("album", ["Unknown"])[0], 109 'album': song.get("album", ["Unknown"])[0],
110 'length': str(song.info.length) 110 'length': str(song.info.length)
111 } 111 }
112 radio_data = self.games[jid.JID(referee).userhost()] # FIXME: referee comes from Libervia's client side, it's unsecure 112 radio_data = self.games[jid.JID(referee).userhost()] # FIXME: referee comes from Libervia's client side, it's unsecure
113 radio_data['to_delete'][attrs['filename']] = song_path # FIXME: works only because of the same host trick, see the note under the docstring 113 radio_data['to_delete'][attrs['filename']] = song_path # FIXME: works only because of the same host trick, see the note under the docstring
114 return threads.deferToThread(self.send, jid.JID(referee), ('', 'song_added'), attrs, profile=profile) 114
115 # XXX: avoid deferToThread which is causing testing troubles. When using deferToThread,
116 # the callbacks that are added (by the test) to the Deferred instance returned by this
117 # method are not run. And if you run d.callback again, you get a AlreadyCalledError.
118 d = defer.Deferred()
119 d.addCallback(self.send, ('', 'song_added'), attrs, profile=profile)
120 d.callback(jid.JID(referee))
121 return d
115 122
116 def playNext(self, room_jid, profile): 123 def playNext(self, room_jid, profile):
117 """"Play next song in queue if exists, and put a timer 124 """"Play next song in queue if exists, and put a timer
118 which trigger after the song has been played to play next one""" 125 which trigger after the song has been played to play next one"""
119 #TODO: songs need to be erased once played or found invalids 126 #TODO: songs need to be erased once played or found invalids
128 queue = radio_data['queue'] 135 queue = radio_data['queue']
129 if not queue: 136 if not queue:
130 #nothing left to play, we need to wait for uploads 137 #nothing left to play, we need to wait for uploads
131 radio_data['playing'] = False 138 radio_data['playing'] = False
132 return 139 return
133
134 song = queue.pop(0) 140 song = queue.pop(0)
135 filename, length = song['filename'], float(song['length']) 141 filename, length = song['filename'], float(song['length'])
136 self.send(room_jid, ('', 'play'), {'filename': filename}, profile=profile) 142 self.send(room_jid, ('', 'play'), {'filename': filename}, profile=profile)
137 143
138 if not radio_data['upload'] and len(queue) < QUEUE_LIMIT: 144 if not radio_data['upload'] and len(queue) < QUEUE_LIMIT:
167 return True 173 return True
168 174
169 def room_game_cmd(self, mess_elt, profile): 175 def room_game_cmd(self, mess_elt, profile):
170 from_jid = jid.JID(mess_elt['from']) 176 from_jid = jid.JID(mess_elt['from'])
171 room_jid = jid.JID(from_jid.userhost()) 177 room_jid = jid.JID(from_jid.userhost())
178 nick = self.host.plugins["XEP-0045"].getRoomNick(room_jid.userhost(), profile)
179
172 radio_elt = mess_elt.firstChildElement() 180 radio_elt = mess_elt.firstChildElement()
173 radio_data = self.games[room_jid.userhost()] 181 radio_data = self.games[room_jid.userhost()]
174 if 'queue' in radio_data: 182 if 'queue' in radio_data:
175 queue = radio_data['queue'] 183 queue = radio_data['queue']
176 184
177 from_referee = self.isReferee(room_jid.userhost(), from_jid.resource) 185 from_referee = self.isReferee(room_jid.userhost(), from_jid.resource)
178 to_referee = self.isReferee(room_jid.userhost(), jid.JID(mess_elt['to']).user) 186 to_referee = self.isReferee(room_jid.userhost(), jid.JID(mess_elt['to']).user)
187 is_player = self.isPlayer(room_jid.userhost(), nick)
179 for elt in radio_elt.elements(): 188 for elt in radio_elt.elements():
180 if not from_referee and not (to_referee and elt.name == 'song_added'): 189 if not from_referee and not (to_referee and elt.name == 'song_added'):
181 continue # sender must be referee, expect when a song is submitted 190 continue # sender must be referee, expect when a song is submitted
182 191 if not is_player and (elt.name not in ('started', 'players')):
183 if elt.name == 'started' or elt.name == 'players': # new game created 192 continue # user is in the room but not playing
193
194 if elt.name in ('started', 'players'): # new game created and/or players list updated
184 players = [] 195 players = []
185 for player in elt.elements(): 196 for player in elt.elements():
186 players.append(unicode(player)) 197 players.append(unicode(player))
187 signal = self.host.bridge.radiocolStarted if elt.name == 'started' else self.host.bridge.radiocolPlayers 198 signal = self.host.bridge.radiocolStarted if elt.name == 'started' else self.host.bridge.radiocolPlayers
188 signal(room_jid.userhost(), from_jid.full(), players, [QUEUE_TO_START, QUEUE_LIMIT], profile) 199 signal(room_jid.userhost(), from_jid.full(), players, [QUEUE_TO_START, QUEUE_LIMIT], profile)
197 elif elt.name == 'upload_ok': 208 elif elt.name == 'upload_ok':
198 self.host.bridge.radiocolUploadOk(room_jid.userhost(), profile) 209 self.host.bridge.radiocolUploadOk(room_jid.userhost(), profile)
199 elif elt.name == 'song_added': # a song has been added 210 elif elt.name == 'song_added': # a song has been added
200 #FIXME: we are KISS for the proof of concept: every song is added, to a limit of 3 in queue. 211 #FIXME: we are KISS for the proof of concept: every song is added, to a limit of 3 in queue.
201 # Need to manage some sort of rules to allow peoples to send songs 212 # Need to manage some sort of rules to allow peoples to send songs
202
203 if len(queue) >= QUEUE_LIMIT: 213 if len(queue) >= QUEUE_LIMIT:
204 #there are already too many songs in queue, we reject this one 214 #there are already too many songs in queue, we reject this one
205 #FIXME: add an error code 215 #FIXME: add an error code
206 self.send(from_jid, ('', 'song_rejected'), {'reason': "Too many songs in queue"}, profile=profile) 216 self.send(from_jid, ('', 'song_rejected'), {'reason': "Too many songs in queue"}, profile=profile)
207 return 217 return