comparison src/tools/plugins/games.py @ 712:f610864eb7a5

plugins (MUC, tools, games): generalize the generation of a unique room name when joining a MUC and no room is specified: - method to get the MUC service moved to plugin_xep_0045 - joinMUC returns the muc name (given by user or generated) - getUniqueName can receive a specified service in argument
author souliane <souliane@mailoo.org>
date Sun, 17 Nov 2013 16:59:12 +0100
parents c9792d0be499
children ecc5a5b34ee1
comparison
equal deleted inserted replaced
711:c9792d0be499 712:f610864eb7a5
42 self.collectiveGame = self.player_init_data == {} 42 self.collectiveGame = self.player_init_data == {}
43 self.options = options 43 self.options = options
44 self.games = {} 44 self.games = {}
45 self.waiting_inv = {} # Invitation waiting for people to join to launch a game 45 self.waiting_inv = {} # Invitation waiting for people to join to launch a game
46 46
47 def prepareRoom(self, other_players, profile_key='@NONE@'): 47 def getUniqueName(self, muc_service="", profile_key='@DEFAULT@'):
48 room = self.host.plugins["XEP-0045"].getUniqueName(muc_service, profile_key=profile_key)
49 return "sat_%s_%s" % (self.name.lower(), room) if room != "" else ""
50
51 def prepareRoom(self, other_players, room_jid=None, profile_key='@NONE@'):
48 """Prepare the room for a game: create it and invite players. 52 """Prepare the room for a game: create it and invite players.
49 @param other_players: list for other players JID userhosts 53 @param other_players: list for other players JID userhosts
54 @param room_jid: JID of the room to reuse or None to create a new room
50 """ 55 """
51 debug(_('Preparing room for %s game') % self.name) 56 debug(_('Preparing room for %s game') % self.name)
52 profile = self.host.memory.getProfileName(profile_key) 57 profile = self.host.memory.getProfileName(profile_key)
53 if not profile: 58 if not profile:
54 error(_("Unknown profile")) 59 error(_("Unknown profile"))
67 else: 72 else:
68 self.waiting_inv[_room] = (time(), players) # TODO: remove invitation waiting for too long, using the time data 73 self.waiting_inv[_room] = (time(), players) # TODO: remove invitation waiting for too long, using the time data
69 for player in other_players: 74 for player in other_players:
70 self.host.plugins["XEP-0249"].invite(JID(player), room.occupantJID.userhostJID(), {"game": self.name}, profile) 75 self.host.plugins["XEP-0249"].invite(JID(player), room.occupantJID.userhostJID(), {"game": self.name}, profile)
71 76
72 def after_init(ignore): 77 def after_init(room_jid):
73 room_name = "sat_%s_%s" % (self.name.lower(), self.host.plugins["XEP-0045"].getUniqueName(profile_key)) 78 if room_jid is not None and room_jid != "":
74 print "\n\n===> room_name:", room_name 79 # a room name has been specified...
75 muc_service = None 80 if room_jid in self.host.plugins["XEP-0045"].clients[profile].joined_rooms:
76 for service in self.host.memory.getServerServiceEntities("conference", "text", profile): 81 # and we're already in
77 if not ".irc." in service.userhost(): 82 roomJoined(self.host.plugins["XEP-0045"].clients[profile].joined_rooms[room_jid])
78 #FIXME: 83 return
79 #This awfull ugly hack is here to avoid an issue with openfire: the irc gateway 84 else:
80 #use "conference/text" identity (instead of "conference/irc"), there is certainly a better way 85 room_jid = self.getUniqueName(profile_key=profile_key)
81 #to manage this, but this hack fill do it for test purpose 86 if room_jid == "":
82 muc_service = service 87 return
83 break 88 d = self.host.plugins["XEP-0045"].join(JID(room_jid), _jid.user, {}, profile)
84 if not muc_service:
85 error(_("Can't find a MUC service"))
86 return
87
88 d = self.host.plugins["XEP-0045"].join(JID("%s@%s" % (room_name, muc_service.userhost())), _jid.user, {}, profile)
89 d.addCallback(roomJoined) 89 d.addCallback(roomJoined)
90 90
91 client = self.host.getClient(profile) 91 client = self.host.getClient(profile)
92 if not client: 92 if not client:
93 error(_('No client for this profile key: %s') % profile_key) 93 error(_('No client for this profile key: %s') % profile_key)
94 return 94 return
95 client.client_initialized.addCallback(after_init) 95 client.client_initialized.addCallback(lambda ignore: after_init(room_jid))
96 96
97 def userJoinedTrigger(self, room, user, profile): 97 def userJoinedTrigger(self, room, user, profile):
98 """This trigger is used to check if we are waiting for people in this room, 98 """This trigger is used to check if we are waiting for people in this room,
99 and to create a game if everybody is here. 99 and to create a game if everybody is here.
100 @room: wokkel.muc.Room object. room.roster is a dict{wokkel.muc.User.nick: wokkel.muc.User} 100 @room: wokkel.muc.Room object. room.roster is a dict{wokkel.muc.User.nick: wokkel.muc.User}