plugins (games): add a method to send messages more easily

This commit is contained in:
souliane 2013-11-18 14:25:40 +01:00
commit d3c12524c9
2 changed files with 67 additions and 92 deletions

View file

@ -108,21 +108,16 @@ class Radiocol(RoomGame):
#FIXME: add an error code
self.host.profiles[profile].xmlstream.send(mess)"""
return
title = song.get("title", ["Unknown"])[0]
artist = song.get("artist", ["Unknown"])[0]
album = song.get("album", ["Unknown"])[0]
length = song.info.length
mess = self.createGameElt(jid.JID(referee))
added_elt = mess.firstChildElement().addElement(('', 'song_added'))
added_elt['filename'] = filename = os.path.basename(song_path)
added_elt['title'] = title
added_elt['artist'] = artist
added_elt['album'] = album
added_elt['length'] = str(length)
self.host.profiles[profile].xmlstream.send(mess)
attrs = {'filename': os.path.basename(song_path),
'title': song.get("title", ["Unknown"])[0],
'artist': song.get("artist", ["Unknown"])[0],
'album': song.get("album", ["Unknown"])[0],
'length': str(song.info.length)
}
self.send(jid.JID(referee), ('', 'song_added'), attrs, profile=profile)
radio_data = self.games[jid.JID(referee).userhost()] # FIXME: referee comes from Libervia's client side, it's unsecure
radio_data['to_delete'][filename] = song_path # FIXME: works only because of the same host trick, see the note under the docstring
radio_data['to_delete'][attrs['filename']] = song_path # FIXME: works only because of the same host trick, see the note under the docstring
def playNext(self, room_jid, profile):
""""Play next sont in queue if exists, and put a timer
@ -139,16 +134,11 @@ class Radiocol(RoomGame):
return
filename, length = queue.pop(0)
mess = self.createGameElt(room_jid)
play_elt = mess.firstChildElement().addElement(('', 'play'))
play_elt['filename'] = filename
self.host.profiles[profile].xmlstream.send(mess)
self.send(room_jid, ('', 'play'), {'filename': filename}, profile=profile)
if not radio_data['upload'] and len(queue) < QUEUE_LIMIT:
#upload is blocked and we now have resources to get more, we reactivate it
mess = self.createGameElt(room_jid)
no_upload_elt = mess.firstChildElement().addElement(('', 'upload_ok'))
self.host.profiles[profile].xmlstream.send(mess)
self.send(room_jid, ('', 'upload_ok'), profile=profile)
radio_data['upload'] = True
reactor.callLater(length, self.playNext, room_jid, profile)
@ -189,12 +179,11 @@ class Radiocol(RoomGame):
if len(queue) >= QUEUE_LIMIT:
#there are already too many songs in queue, we reject this one
mess = self.createGameElt(room_jid)
reject_elt = mess.firstChildElement().addElement(('', 'song_rejected'))
reject_elt['sender'] = from_jid.resource
reject_elt['reason'] = "Too many songs in queue"
attrs = {'sender': from_jid.resource,
'reason': "Too many songs in queue"
}
#FIXME: add an error code
self.host.profiles[profile].xmlstream.send(mess)
self.send(room_jid, ('', 'song_rejected'), attrs, profile=profile)
return
#The song is accepted and added in queue
@ -202,20 +191,16 @@ class Radiocol(RoomGame):
if len(queue) >= QUEUE_LIMIT:
#We are at the limit, we refuse new upload until next play
mess = self.createGameElt(room_jid)
no_upload_elt = mess.firstChildElement().addElement(('', 'no_upload'))
#FIXME: add an error code
self.host.profiles[profile].xmlstream.send(mess)
self.send(room_jid, ('', 'no_upload'), profile=profile)
radio_data['upload'] = False
mess = self.createGameElt(room_jid)
preload_elt = self.__create_preload_elt(from_jid.resource,
elt['filename'],
elt['title'],
elt['artist'],
elt['album'])
mess.firstChildElement().addChild(preload_elt)
self.host.profiles[profile].xmlstream.send(mess)
self.send(room_jid, preload_elt, profile=profile)
if not radio_data['playing'] and len(queue) == 2:
#we have not started playing yet, and we have 2 songs in queue
#we can now start the party :)

View file

@ -380,9 +380,7 @@ class Tarot(RoomGame):
next_player_idx = game_data['current_player'] = (game_data['init_player'] + 1) % len(game_data['players']) # the player after the dealer start
game_data['first_player'] = next_player = game_data['players'][next_player_idx]
to_jid = jid.JID(room_jid.userhost() + "/" + next_player) # FIXME: gof:
mess = self.createGameElt(to_jid)
yourturn_elt = mess.firstChildElement().addElement('your_turn')
self.host.profiles[profile].xmlstream.send(mess)
self.send(to_jid, 'your_turn', profile=profile)
def contratChoosed(self, player, referee, contrat, profile_key='@NONE@'):
"""Must be call by player when the contrat is selected
@ -396,10 +394,7 @@ class Tarot(RoomGame):
error(_("profile %s is unknown") % profile_key)
return
debug(_('contrat [%(contrat)s] choosed by %(profile)s') % {'contrat': contrat, 'profile': profile})
mess = self.createGameElt(jid.JID(referee))
contrat_elt = mess.firstChildElement().addElement(('', 'contrat_choosed'), content=contrat)
contrat_elt['player'] = player
self.host.profiles[profile].xmlstream.send(mess)
self.send(jid.JID(referee), ('', 'contrat_choosed'), {'player': player}, content=contrat, profile=profile)
def play_cards(self, player, referee, cards, profile_key='@NONE@'):
"""Must be call by player when the contrat is selected
@ -413,10 +408,8 @@ class Tarot(RoomGame):
error(_("profile %s is unknown") % profile_key)
return
debug(_('Cards played by %(profile)s: [%(cards)s]') % {'profile': profile, 'cards': cards})
mess = self.createGameElt(jid.JID(referee))
playcard_elt = mess.firstChildElement().addChild(self.__card_list_to_xml(TarotCard.from_tuples(cards), 'cards_played'))
playcard_elt['player'] = player
self.host.profiles[profile].xmlstream.send(mess)
elem = self.__card_list_to_xml(TarotCard.from_tuples(cards), 'cards_played')
self.send(jid.JID(referee), elem, {'player': player}, profile=profile)
def newRound(self, room_jid, profile):
game_data = self.games[room_jid.userhost()]
@ -448,9 +441,7 @@ class Tarot(RoomGame):
pl_idx = game_data['current_player'] = (game_data['init_player'] + 1) % len(players) # the player after the dealer start
player = players[pl_idx]
to_jid = jid.JID(room_jid.userhost() + "/" + player) # FIXME: gof:
mess = self.createGameElt(to_jid)
mess.firstChildElement().addChild(self.__ask_contrat())
self.host.profiles[profile].xmlstream.send(mess)
self.send(to_jid, self.__ask_contrat(), profile=profile)
def card_game_cmd(self, mess_elt, profile):
"""
@ -497,9 +488,7 @@ class Tarot(RoomGame):
#not everybody has choosed his contrat, it's next one turn
player = self.__next_player(game_data)
to_jid = jid.JID(room_jid.userhost() + "/" + player) # FIXME: gof:
mess = self.createGameElt(to_jid)
mess.firstChildElement().addChild(self.__ask_contrat())
self.host.profiles[profile].xmlstream.send(mess)
self.send(to_jid, self.__ask_contrat(), profile=profile)
else:
best_contrat = [None, "Passe"]
for player in game_data['players']:
@ -512,9 +501,7 @@ class Tarot(RoomGame):
if best_contrat[1] == "Passe":
debug(_("Everybody is passing, round ended"))
to_jid = jid.JID(room_jid.userhost())
mess = self.createGameElt(to_jid)
mess.firstChildElement().addChild(self.__give_scores(*self.__draw_game(game_data)))
self.host.profiles[profile].xmlstream.send(mess)
self.send(to_jid, self.__give_scores(*self.__draw_game(game_data)), profile=profile)
game_data['init_player'] = (game_data['init_player'] + 1) % len(game_data['players']) # we change the dealer
for player in game_data['players']:
game_data['status'][player] = "init"
@ -528,10 +515,8 @@ class Tarot(RoomGame):
else:
#Time to show the chien to everybody
to_jid = jid.JID(room_jid.userhost()) # FIXME: gof:
mess = self.createGameElt(to_jid)
chien_elt = mess.firstChildElement().addChild(self.__card_list_to_xml(game_data['chien'], 'chien'))
chien_elt['attaquant'] = best_contrat[0]
self.host.profiles[profile].xmlstream.send(mess)
elem = self.__card_list_to_xml(game_data['chien'], 'chien')
self.send(to_jid, elem, {'attaquant': best_contrat[0]}, profile=profile)
#the attacker (attaquant) get the chien
game_data['hand'][best_contrat[0]].extend(game_data['chien'])
del game_data['chien'][:]
@ -556,9 +541,8 @@ class Tarot(RoomGame):
#we now check validity of card
invalid_cards = self.__invalid_cards(game_data, list_cards)
if invalid_cards:
mess = self.createGameElt(jid.JID(room_jid.userhost() + '/' + elt['player']))
mess.firstChildElement().addChild(self.__invalid_cards_elt(list_cards, invalid_cards, game_data['stage']))
self.host.profiles[profile].xmlstream.send(mess)
elem = self.__invalid_cards_elt(list_cards, invalid_cards, game_data['stage'])
self.send(jid.JID(room_jid.userhost() + '/' + elt['player']), elem, profile=profile)
return
#FIXME: gof: manage Garde Sans & Garde Contre cases
@ -578,9 +562,8 @@ class Tarot(RoomGame):
#we first check validity of card
invalid_cards = self.__invalid_cards(game_data, cards)
if invalid_cards:
mess = self.createGameElt(jid.JID(room_jid.userhost() + '/' + current_player))
mess.firstChildElement().addChild(self.__invalid_cards_elt(cards, invalid_cards, game_data['stage']))
self.host.profiles[profile].xmlstream.send(mess)
elem = self.__invalid_cards_elt(cards, invalid_cards, game_data['stage'])
self.send(jid.JID(room_jid.userhost() + '/' + current_player), elem, profile=profile)
return
#the card played is ok, we forward it to everybody
#first we remove it from the hand and put in on the table
@ -588,9 +571,7 @@ class Tarot(RoomGame):
players_data[current_player]['played'] = cards[0]
#then we forward the message
mess = self.createGameElt(room_jid)
playcard_elt = mess.firstChildElement().addChild(elt)
self.host.profiles[profile].xmlstream.send(mess)
self.send(room_jid, elt, profile=profile)
#Did everybody played ?
played = [players_data[player]['played'] for player in game_data['players']]
@ -606,10 +587,8 @@ class Tarot(RoomGame):
players_data[player]['played'] = None
if len(game_data['hand'][current_player]) == 0:
#no card lef: the game is finished
to_jid = room_jid
mess = self.createGameElt(to_jid)
chien_elt = mess.firstChildElement().addChild(self.__give_scores(*self.__calculate_scores(game_data)))
self.host.profiles[profile].xmlstream.send(mess)
elem = self.__give_scores(*self.__calculate_scores(game_data))
self.send(room_jid, elem, profile=profile)
game_data['init_player'] = (game_data['init_player'] + 1) % len(game_data['players']) # we change the dealer
for player in game_data['players']:
game_data['status'][player] = "init"
@ -621,9 +600,7 @@ class Tarot(RoomGame):
#finally, we tell to the next player to play
to_jid = jid.JID(room_jid.userhost() + "/" + next_player)
mess = self.createGameElt(to_jid)
yourturn_elt = mess.firstChildElement().addElement('your_turn')
self.host.profiles[profile].xmlstream.send(mess)
self.send(to_jid, 'your_turn', profile=profile)
elif elt.name == 'your_turn':
self.host.bridge.tarotGameYourTurn(room_jid.userhost(), profile)