comparison src/plugins/plugin_misc_radiocol.py @ 714:ecc5a5b34ee1

plugins (games): add a method to send messages more easily
author souliane <souliane@mailoo.org>
date Mon, 18 Nov 2013 14:25:40 +0100
parents f610864eb7a5
children 358018c5c398
comparison
equal deleted inserted replaced
713:8bd63daecdbf 714:ecc5a5b34ee1
106 reject_elt['sender'] = client.jid 106 reject_elt['sender'] = client.jid
107 reject_elt['reason'] = "Uploaded file is not Ogg Vorbis song, only Ogg Vorbis songs are acceptable" 107 reject_elt['reason'] = "Uploaded file is not Ogg Vorbis song, only Ogg Vorbis songs are acceptable"
108 #FIXME: add an error code 108 #FIXME: add an error code
109 self.host.profiles[profile].xmlstream.send(mess)""" 109 self.host.profiles[profile].xmlstream.send(mess)"""
110 return 110 return
111 title = song.get("title", ["Unknown"])[0] 111 attrs = {'filename': os.path.basename(song_path),
112 artist = song.get("artist", ["Unknown"])[0] 112 'title': song.get("title", ["Unknown"])[0],
113 album = song.get("album", ["Unknown"])[0] 113 'artist': song.get("artist", ["Unknown"])[0],
114 length = song.info.length 114 'album': song.get("album", ["Unknown"])[0],
115 mess = self.createGameElt(jid.JID(referee)) 115 'length': str(song.info.length)
116 added_elt = mess.firstChildElement().addElement(('', 'song_added')) 116 }
117 added_elt['filename'] = filename = os.path.basename(song_path) 117 self.send(jid.JID(referee), ('', 'song_added'), attrs, profile=profile)
118 added_elt['title'] = title
119 added_elt['artist'] = artist
120 added_elt['album'] = album
121 added_elt['length'] = str(length)
122 self.host.profiles[profile].xmlstream.send(mess)
123 118
124 radio_data = self.games[jid.JID(referee).userhost()] # FIXME: referee comes from Libervia's client side, it's unsecure 119 radio_data = self.games[jid.JID(referee).userhost()] # FIXME: referee comes from Libervia's client side, it's unsecure
125 radio_data['to_delete'][filename] = song_path # FIXME: works only because of the same host trick, see the note under the docstring 120 radio_data['to_delete'][attrs['filename']] = song_path # FIXME: works only because of the same host trick, see the note under the docstring
126 121
127 def playNext(self, room_jid, profile): 122 def playNext(self, room_jid, profile):
128 """"Play next sont in queue if exists, and put a timer 123 """"Play next sont in queue if exists, and put a timer
129 which trigger after the song has been played to play next one""" 124 which trigger after the song has been played to play next one"""
130 #TODO: need to check that there are still peoples in the room 125 #TODO: need to check that there are still peoples in the room
137 #nothing left to play, we need to wait for uploads 132 #nothing left to play, we need to wait for uploads
138 radio_data['playing'] = False 133 radio_data['playing'] = False
139 return 134 return
140 135
141 filename, length = queue.pop(0) 136 filename, length = queue.pop(0)
142 mess = self.createGameElt(room_jid) 137 self.send(room_jid, ('', 'play'), {'filename': filename}, profile=profile)
143 play_elt = mess.firstChildElement().addElement(('', 'play'))
144 play_elt['filename'] = filename
145 self.host.profiles[profile].xmlstream.send(mess)
146 138
147 if not radio_data['upload'] and len(queue) < QUEUE_LIMIT: 139 if not radio_data['upload'] and len(queue) < QUEUE_LIMIT:
148 #upload is blocked and we now have resources to get more, we reactivate it 140 #upload is blocked and we now have resources to get more, we reactivate it
149 mess = self.createGameElt(room_jid) 141 self.send(room_jid, ('', 'upload_ok'), profile=profile)
150 no_upload_elt = mess.firstChildElement().addElement(('', 'upload_ok'))
151 self.host.profiles[profile].xmlstream.send(mess)
152 radio_data['upload'] = True 142 radio_data['upload'] = True
153 143
154 reactor.callLater(length, self.playNext, room_jid, profile) 144 reactor.callLater(length, self.playNext, room_jid, profile)
155 try: 145 try:
156 file_to_delete = radio_data['to_delete'][filename] 146 file_to_delete = radio_data['to_delete'][filename]
187 #FIXME: we are KISS for the proof of concept: every song is added, to a limit of 3 in queue. 177 #FIXME: we are KISS for the proof of concept: every song is added, to a limit of 3 in queue.
188 # Need to manage some sort of rules to allow peoples to send songs 178 # Need to manage some sort of rules to allow peoples to send songs
189 179
190 if len(queue) >= QUEUE_LIMIT: 180 if len(queue) >= QUEUE_LIMIT:
191 #there are already too many songs in queue, we reject this one 181 #there are already too many songs in queue, we reject this one
192 mess = self.createGameElt(room_jid) 182 attrs = {'sender': from_jid.resource,
193 reject_elt = mess.firstChildElement().addElement(('', 'song_rejected')) 183 'reason': "Too many songs in queue"
194 reject_elt['sender'] = from_jid.resource 184 }
195 reject_elt['reason'] = "Too many songs in queue"
196 #FIXME: add an error code 185 #FIXME: add an error code
197 self.host.profiles[profile].xmlstream.send(mess) 186 self.send(room_jid, ('', 'song_rejected'), attrs, profile=profile)
198 return 187 return
199 188
200 #The song is accepted and added in queue 189 #The song is accepted and added in queue
201 queue.append((elt['filename'], float(elt['length']))) 190 queue.append((elt['filename'], float(elt['length'])))
202 191
203 if len(queue) >= QUEUE_LIMIT: 192 if len(queue) >= QUEUE_LIMIT:
204 #We are at the limit, we refuse new upload until next play 193 #We are at the limit, we refuse new upload until next play
205 mess = self.createGameElt(room_jid)
206 no_upload_elt = mess.firstChildElement().addElement(('', 'no_upload'))
207 #FIXME: add an error code 194 #FIXME: add an error code
208 self.host.profiles[profile].xmlstream.send(mess) 195 self.send(room_jid, ('', 'no_upload'), profile=profile)
209 radio_data['upload'] = False 196 radio_data['upload'] = False
210 197
211 mess = self.createGameElt(room_jid)
212 preload_elt = self.__create_preload_elt(from_jid.resource, 198 preload_elt = self.__create_preload_elt(from_jid.resource,
213 elt['filename'], 199 elt['filename'],
214 elt['title'], 200 elt['title'],
215 elt['artist'], 201 elt['artist'],
216 elt['album']) 202 elt['album'])
217 mess.firstChildElement().addChild(preload_elt) 203 self.send(room_jid, preload_elt, profile=profile)
218 self.host.profiles[profile].xmlstream.send(mess)
219 if not radio_data['playing'] and len(queue) == 2: 204 if not radio_data['playing'] and len(queue) == 2:
220 #we have not started playing yet, and we have 2 songs in queue 205 #we have not started playing yet, and we have 2 songs in queue
221 #we can now start the party :) 206 #we can now start the party :)
222 radio_data['playing'] = True 207 radio_data['playing'] = True
223 self.playNext(room_jid, profile) 208 self.playNext(room_jid, profile)