Mercurial > libervia-backend
comparison src/plugins/plugin_misc_room_game.py @ 1970:200cd707a46d
plugin XEP-0045, quick_frontend + primitivus (chat): cleaning of XEP-0045 (first pass):
- bridge methods/signals now all start with "muc" to follow new convention
- internal method use client instead of profile to follow new convention
- removed excetpions from plugin XEP-0045 in favor of core.exceptions, NotReady added
- cleaned/simplified several part of the code. checkClient removed as it is not needed anymore
- self.clients map removed, muc data are now stored directly in client
- getRoomEntityNick and getRoomNicksOfUsers are removed as they don't look sane.
/!\ This break all room game plugins for the moment
- use of uuid4 instead of uuid1 for getUniqueName, as host ID and current time are used for uuid1
author | Goffi <goffi@goffi.org> |
---|---|
date | Mon, 27 Jun 2016 21:45:11 +0200 |
parents | 2daf7b4c6756 |
children | 6a66c8c5a567 |
comparison
equal
deleted
inserted
replaced
1969:5fbe09b9b568 | 1970:200cd707a46d |
---|---|
46 "handler": "no", # handler MUST be "no" (dynamic inheritance) | 46 "handler": "no", # handler MUST be "no" (dynamic inheritance) |
47 "description": _("""Base class for MUC games""") | 47 "description": _("""Base class for MUC games""") |
48 } | 48 } |
49 | 49 |
50 | 50 |
51 # FIXME: this plugin is broken, need to be fixed | |
52 | |
51 class RoomGame(object): | 53 class RoomGame(object): |
52 """This class is used to help launching a MUC game. | 54 """This class is used to help launching a MUC game. |
53 | 55 |
54 Bridge methods callbacks: _prepareRoom, _playerReady, _createGame | 56 Bridge methods callbacks: _prepareRoom, _playerReady, _createGame |
55 Triggered methods: userJoinedTrigger, userLeftTrigger | 57 Triggered methods: userJoinedTrigger, userLeftTrigger |
121 self.testing = False | 123 self.testing = False |
122 | 124 |
123 host.trigger.add("MUC user joined", self.userJoinedTrigger) | 125 host.trigger.add("MUC user joined", self.userJoinedTrigger) |
124 host.trigger.add("MUC user left", self.userLeftTrigger) | 126 host.trigger.add("MUC user left", self.userLeftTrigger) |
125 | 127 |
126 def _createOrInvite(self, room, other_players, profile): | 128 def _createOrInvite(self, room_jid, other_players, profile): |
127 """ | 129 """ |
128 This is called only when someone explicitly wants to play. | 130 This is called only when someone explicitly wants to play. |
129 | 131 |
130 The game will not be created if one already exists in the room, | 132 The game will not be created if one already exists in the room, |
131 also its creation could be postponed until all the expected players | 133 also its creation could be postponed until all the expected players |
132 join the room (in that case it will be created from userJoinedTrigger). | 134 join the room (in that case it will be created from userJoinedTrigger). |
133 @param room (wokkel.muc.Room): the room | 135 @param room (wokkel.muc.Room): the room |
134 @param other_players (list[jid.JID]): list of the other players JID (bare) | 136 @param other_players (list[jid.JID]): list of the other players JID (bare) |
135 """ | 137 """ |
138 # FIXME: broken ! | |
139 raise NotImplementedError("To be fixed") | |
140 client = self.host.getClient(profile) | |
136 user_jid = self.host.getJidNStream(profile)[0] | 141 user_jid = self.host.getJidNStream(profile)[0] |
137 room_jid = room.occupantJID.userhostJID() | 142 nick = self.host.plugins["XEP-0045"].getRoomNick(client, room_jid) |
138 nick = self.host.plugins["XEP-0045"].getRoomNick(room_jid, profile) | |
139 nicks = [nick] | 143 nicks = [nick] |
140 if self._gameExists(room_jid): | 144 if self._gameExists(room_jid): |
141 if not self._checkJoinAuth(room_jid, user_jid, nick): | 145 if not self._checkJoinAuth(room_jid, user_jid, nick): |
142 return | 146 return |
143 nicks.extend(self._invitePlayers(room, other_players, nick, profile)) | 147 nicks.extend(self._invitePlayers(room_jid, other_players, nick, profile)) |
144 self._updatePlayers(room_jid, nicks, True, profile) | 148 self._updatePlayers(room_jid, nicks, True, profile) |
145 else: | 149 else: |
146 self._initGame(room_jid, nick) | 150 self._initGame(room_jid, nick) |
147 (auth, waiting, missing) = self._checkWaitAuth(room, other_players) | 151 (auth, waiting, missing) = self._checkWaitAuth(room_jid, other_players) |
148 nicks.extend(waiting) | 152 nicks.extend(waiting) |
149 nicks.extend(self._invitePlayers(room, missing, nick, profile)) | 153 nicks.extend(self._invitePlayers(room_jid, missing, nick, profile)) |
150 if auth: | 154 if auth: |
151 self.createGame(room_jid, nicks, profile) | 155 self.createGame(room_jid, nicks, profile) |
152 else: | 156 else: |
153 self._updatePlayers(room_jid, nicks, False, profile) | 157 self._updatePlayers(room_jid, nicks, False, profile) |
154 | 158 |
298 @param nick: the nick of the player to be synchronized | 302 @param nick: the nick of the player to be synchronized |
299 @return: a list of elements to synchronize this player with the game. | 303 @return: a list of elements to synchronize this player with the game. |
300 """ | 304 """ |
301 return [] | 305 return [] |
302 | 306 |
303 def _invitePlayers(self, room, other_players, nick, profile): | 307 def _invitePlayers(self, room_jid, other_players, nick, profile): |
304 """Invite players to a room, associated game may exist or not. | 308 """Invite players to a room, associated game may exist or not. |
305 | 309 |
306 @param room (wokkel.muc.Room): the room | |
307 @param other_players (list[jid.JID]): list of the players to invite | 310 @param other_players (list[jid.JID]): list of the players to invite |
308 @param nick (unicode): nick of the user who send the invitation | 311 @param nick (unicode): nick of the user who send the invitation |
309 @return: list[unicode] of room nicks for invited players who are already in the room | 312 @return: list[unicode] of room nicks for invited players who are already in the room |
310 """ | 313 """ |
311 room_jid = room.occupantJID.userhostJID() | 314 raise NotImplementedError("Need to be fixed !") |
315 # FIXME: this is broken and unsecure ! | |
312 if not self._checkInviteAuth(room_jid, nick): | 316 if not self._checkInviteAuth(room_jid, nick): |
313 return [] | 317 return [] |
314 # TODO: remove invitation waiting for too long, using the time data | 318 # TODO: remove invitation waiting for too long, using the time data |
315 self.invitations[room_jid].append((time(), [player.userhostJID() for player in other_players])) | 319 self.invitations[room_jid].append((time(), [player.userhostJID() for player in other_players])) |
316 nicks = [] | 320 nicks = [] |
317 for player_jid in [player.userhostJID() for player in other_players]: | 321 for player_jid in [player.userhostJID() for player in other_players]: |
318 # TODO: find a way to make it secure | 322 # TODO: find a way to make it secure |
319 other_nick = self.host.plugins["XEP-0045"].getRoomNickOfUser(room, player_jid, secure=self.testing) | 323 other_nick = self.host.plugins["XEP-0045"].getRoomEntityNick(room_jid, player_jid, secure=self.testing) |
320 if other_nick is None: | 324 if other_nick is None: |
321 self.host.plugins["XEP-0249"].invite(player_jid, room_jid, {"game": self.name}, profile) | 325 self.host.plugins["XEP-0249"].invite(player_jid, room_jid, {"game": self.name}, profile) |
322 else: | 326 else: |
323 nicks.append(other_nick) | 327 nicks.append(other_nick) |
324 return nicks | 328 return nicks |
395 | 399 |
396 @param muc_service (jid.JID): you can leave empty to autofind the muc service | 400 @param muc_service (jid.JID): you can leave empty to autofind the muc service |
397 @param profile_key (unicode): %(doc_profile_key)s | 401 @param profile_key (unicode): %(doc_profile_key)s |
398 @return: jid.JID (unique name for a new room to be created) | 402 @return: jid.JID (unique name for a new room to be created) |
399 """ | 403 """ |
404 client = self.host.getClient(profile_key) | |
400 # FIXME: jid.JID must be used instead of strings | 405 # FIXME: jid.JID must be used instead of strings |
401 room = self.host.plugins["XEP-0045"].getUniqueName(muc_service, profile_key=profile_key) | 406 room = self.host.plugins["XEP-0045"].getUniqueName(client, muc_service) |
402 return jid.JID("sat_%s_%s" % (self.name.lower(), room.userhost())) | 407 return jid.JID("sat_%s_%s" % (self.name.lower(), room.userhost())) |
403 | 408 |
404 def _prepareRoom(self, other_players=None, room_jid_s='', profile_key=C.PROF_KEY_NONE): | 409 def _prepareRoom(self, other_players=None, room_jid_s='', profile_key=C.PROF_KEY_NONE): |
405 room_jid = jid.JID(room_jid_s) if room_jid_s else None | 410 room_jid = jid.JID(room_jid_s) if room_jid_s else None |
406 other_players = [jid.JID(player).userhostJID() for player in other_players] | 411 other_players = [jid.JID(player).userhostJID() for player in other_players] |
411 | 416 |
412 @param other_players (list[JID]): list of other players JID (bare) | 417 @param other_players (list[JID]): list of other players JID (bare) |
413 @param room_jid (jid.JID): JID of the room, or None to generate a unique name | 418 @param room_jid (jid.JID): JID of the room, or None to generate a unique name |
414 @param profile_key (unicode): %(doc_profile_key)s | 419 @param profile_key (unicode): %(doc_profile_key)s |
415 """ | 420 """ |
421 # FIXME: need to be refactored | |
422 client = self.host.getClient(profile_key) | |
416 log.debug(_(u'Preparing room for %s game') % self.name) | 423 log.debug(_(u'Preparing room for %s game') % self.name) |
417 profile = self.host.memory.getProfileName(profile_key) | 424 profile = self.host.memory.getProfileName(profile_key) |
418 if not profile: | 425 if not profile: |
419 log.error(_("Unknown profile")) | 426 log.error(_("Unknown profile")) |
420 return defer.succeed(None) | 427 return defer.succeed(None) |
421 if other_players is None: | 428 if other_players is None: |
422 other_players = [] | 429 other_players = [] |
423 | 430 |
424 def roomJoined(room): | |
425 """@param room: instance of wokkel.muc.Room""" | |
426 self._createOrInvite(room, other_players, profile) | |
427 | |
428 # Create/join the given room, or a unique generated one if no room is specified. | 431 # Create/join the given room, or a unique generated one if no room is specified. |
429 if room_jid is None: | 432 if room_jid is None: |
430 room_jid = self.getUniqueName(profile_key=profile_key) | 433 room_jid = self.getUniqueName(profile_key=profile_key) |
431 else: | 434 else: |
432 if room_jid in self.host.plugins["XEP-0045"].clients[profile].joined_rooms: | 435 self.host.plugins["XEP-0045"].checkRoomJoined(client, room_jid) |
433 roomJoined(self.host.plugins["XEP-0045"].clients[profile].joined_rooms[room_jid]) | 436 self._createOrInvite(client, room_jid, other_players) |
434 return defer.succeed(None) | 437 return defer.succeed(None) |
435 | 438 |
436 user_jid = self.host.getJidNStream(profile)[0] | 439 user_jid = self.host.getJidNStream(profile)[0] |
437 d = self.host.plugins["XEP-0045"].join(room_jid, user_jid.user, {}, profile) | 440 d = self.host.plugins["XEP-0045"].join(room_jid, user_jid.user, {}, profile) |
438 return d.addCallback(roomJoined) | 441 return d.addCallback(lambda dummy: self._createOrInvite(client, room_jid, other_players)) |
439 | 442 |
440 def userJoinedTrigger(self, room, user, profile): | 443 def userJoinedTrigger(self, room, user, profile): |
441 """This trigger is used to check if the new user can take part of a game, create the game if we were waiting for him or just update the players list. | 444 """This trigger is used to check if the new user can take part of a game, create the game if we were waiting for him or just update the players list. |
442 | 445 |
443 @room: wokkel.muc.Room object. room.roster is a dict{wokkel.muc.User.nick: wokkel.muc.User} | 446 @room: wokkel.muc.Room object. room.roster is a dict{wokkel.muc.User.nick: wokkel.muc.User} |