comparison src/plugins/plugin_misc_room_game.py @ 993:301b342c697a

core: use of the new core.log module: /!\ this is a massive refactoring and was largely automated, it probably did bring some bugs /!\
author Goffi <goffi@goffi.org>
date Sat, 19 Apr 2014 19:19:19 +0200
parents e1842ebcb2f3
children 83ed877541e3
comparison
equal deleted inserted replaced
992:f51a1895275c 993:301b342c697a
17 # You should have received a copy of the GNU Affero General Public License 17 # You should have received a copy of the GNU Affero General Public License
18 # along with this program. If not, see <http://www.gnu.org/licenses/>. 18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
19 19
20 from sat.core.i18n import _ 20 from sat.core.i18n import _
21 from sat.core.constants import Const as C 21 from sat.core.constants import Const as C
22 from logging import debug, warning, error 22 from sat.core.log import getLogger
23 log = getLogger(__name__)
23 from twisted.words.protocols.jabber.jid import JID 24 from twisted.words.protocols.jabber.jid import JID
24 from twisted.words.xish import domish 25 from twisted.words.xish import domish
25 from twisted.internet import defer 26 from twisted.internet import defer
26 from time import time 27 from time import time
27 from wokkel import disco, iwokkel 28 from wokkel import disco, iwokkel
197 if nick in [JID(invited).user for invited in invitations[1]]: 198 if nick in [JID(invited).user for invited in invitations[1]]:
198 auth = True 199 auth = True
199 break 200 break
200 201
201 if not auth and (verbose or _DEBUG): 202 if not auth and (verbose or _DEBUG):
202 debug(_("%(user)s not allowed to join the game %(game)s in %(room)s") % {'user': user_jid_s or nick, 'game': self.name, 'room': room_jid_s}) 203 log.debug(_("%(user)s not allowed to join the game %(game)s in %(room)s") % {'user': user_jid_s or nick, 'game': self.name, 'room': room_jid_s})
203 return auth 204 return auth
204 205
205 def _updatePlayers(self, room_jid_s, nicks, sync, profile): 206 def _updatePlayers(self, room_jid_s, nicks, sync, profile):
206 """Update the list of players and signal to the room that some players joined the game. 207 """Update the list of players and signal to the room that some players joined the game.
207 If sync is True, the news players are synchronized with the game data they have missed. 208 If sync is True, the news players are synchronized with the game data they have missed.
333 elif self.invite_mode == self.FROM_REFEREE: 334 elif self.invite_mode == self.FROM_REFEREE:
334 auth = self.isReferee(room_jid_s, nick) 335 auth = self.isReferee(room_jid_s, nick)
335 elif self.invite_mode == self.FROM_PLAYERS: 336 elif self.invite_mode == self.FROM_PLAYERS:
336 auth = self.isPlayer(room_jid_s, nick) 337 auth = self.isPlayer(room_jid_s, nick)
337 if not auth and (verbose or _DEBUG): 338 if not auth and (verbose or _DEBUG):
338 debug(_("%(user)s not allowed to invite for the game %(game)s in %(room)s") % {'user': nick, 'game': self.name, 'room': room_jid_s}) 339 log.debug(_("%(user)s not allowed to invite for the game %(game)s in %(room)s") % {'user': nick, 'game': self.name, 'room': room_jid_s})
339 return auth 340 return auth
340 341
341 def isReferee(self, room_jid_s, nick): 342 def isReferee(self, room_jid_s, nick):
342 """Checks if the player with this nick is the referee for the game in this room" 343 """Checks if the player with this nick is the referee for the game in this room"
343 @param room_jid_s: room userhost 344 @param room_jid_s: room userhost
379 else: 380 else:
380 # TODO: find a way to make it secure 381 # TODO: find a way to make it secure
381 (nicks, missing) = self.host.plugins["XEP-0045"].getRoomNicksOfUsers(room, other_players, secure=False) 382 (nicks, missing) = self.host.plugins["XEP-0045"].getRoomNicksOfUsers(room, other_players, secure=False)
382 result = (len(nicks) == len(other_players), nicks, missing) 383 result = (len(nicks) == len(other_players), nicks, missing)
383 if not result[0] and (verbose or _DEBUG): 384 if not result[0] and (verbose or _DEBUG):
384 debug(_("Still waiting for %(users)s before starting the game %(game)s in %(room)s") % {'users': result[2], 'game': self.name, 'room': room.occupantJID.userhost()}) 385 log.debug(_("Still waiting for %(users)s before starting the game %(game)s in %(room)s") % {'users': result[2], 'game': self.name, 'room': room.occupantJID.userhost()})
385 return result 386 return result
386 387
387 def getUniqueName(self, muc_service=None, profile_key=C.PROF_KEY_NONE): 388 def getUniqueName(self, muc_service=None, profile_key=C.PROF_KEY_NONE):
388 """Generate unique room name 389 """Generate unique room name
389 390
400 401
401 @param other_players: list for other players JID userhosts 402 @param other_players: list for other players JID userhosts
402 @param room_jid_s: JID userhost of the room, or None to generate a unique name 403 @param room_jid_s: JID userhost of the room, or None to generate a unique name
403 @param profile_key 404 @param profile_key
404 """ 405 """
405 debug(_('Preparing room for %s game') % self.name) 406 log.debug(_('Preparing room for %s game') % self.name)
406 profile = self.host.memory.getProfileName(profile_key) 407 profile = self.host.memory.getProfileName(profile_key)
407 if not profile: 408 if not profile:
408 error(_("Unknown profile")) 409 log.error(_("Unknown profile"))
409 return 410 return
410 if other_players is None: 411 if other_players is None:
411 other_players = [] 412 other_players = []
412 413
413 def roomJoined(room): 414 def roomJoined(room):
444 return True 445 return True
445 if self.wait_mode == self.FOR_ALL: 446 if self.wait_mode == self.FOR_ALL:
446 # considering the last batch of invitations 447 # considering the last batch of invitations
447 batch = len(self.invitations[room_jid_s]) - 1 448 batch = len(self.invitations[room_jid_s]) - 1
448 if batch < 0: 449 if batch < 0:
449 error("Invitations from %s to play %s in %s have been lost!" % (profile_nick, self.name, room_jid_s)) 450 log.error("Invitations from %s to play %s in %s have been lost!" % (profile_nick, self.name, room_jid_s))
450 return True 451 return True
451 other_players = self.invitations[room_jid_s][batch][1] 452 other_players = self.invitations[room_jid_s][batch][1]
452 (auth, nicks, dummy) = self._checkWaitAuth(room, other_players) 453 (auth, nicks, dummy) = self._checkWaitAuth(room, other_players)
453 if auth: 454 if auth:
454 del self.invitations[room_jid_s][batch] 455 del self.invitations[room_jid_s][batch]
498 - create: set to True to allow the game creation 499 - create: set to True to allow the game creation
499 - sync: set to True to advice a game synchronization 500 - sync: set to True to advice a game synchronization
500 """ 501 """
501 user_nick = self.host.plugins["XEP-0045"].getRoomNick(room_jid_s, profile) 502 user_nick = self.host.plugins["XEP-0045"].getRoomNick(room_jid_s, profile)
502 if not user_nick: 503 if not user_nick:
503 error('Internal error: profile %s has not joined the room %s' % (profile, room_jid_s)) 504 log.error('Internal error: profile %s has not joined the room %s' % (profile, room_jid_s))
504 return False, False 505 return False, False
505 if self._gameExists(room_jid_s): 506 if self._gameExists(room_jid_s):
506 is_referee = self.isReferee(room_jid_s, user_nick) 507 is_referee = self.isReferee(room_jid_s, user_nick)
507 if self._gameExists(room_jid_s, started=True): 508 if self._gameExists(room_jid_s, started=True):
508 warning(_("%(game)s game already created in room %(room)s") % {'game': self.name, 'room': room_jid_s}) 509 log.warning(_("%(game)s game already created in room %(room)s") % {'game': self.name, 'room': room_jid_s})
509 return False, is_referee 510 return False, is_referee
510 elif not is_referee: 511 elif not is_referee:
511 warning(_("%(game)s game in room %(room)s can only be created by %(user)s") % {'game': self.name, 'room': room_jid_s, 'user': user_nick}) 512 log.warning(_("%(game)s game in room %(room)s can only be created by %(user)s") % {'game': self.name, 'room': room_jid_s, 'user': user_nick})
512 return False, False 513 return False, False
513 else: 514 else:
514 self._initGame(room_jid_s, user_nick) 515 self._initGame(room_jid_s, user_nick)
515 return True, False 516 return True, False
516 517
521 but the game must not exist and all the players must be in the room already. 522 but the game must not exist and all the players must be in the room already.
522 @param room_jid: JID userhost of the room 523 @param room_jid: JID userhost of the room
523 @param nicks: list of players nicks in the room (referee included, in first position) 524 @param nicks: list of players nicks in the room (referee included, in first position)
524 @param profile_key: %(doc_profile_key)s 525 @param profile_key: %(doc_profile_key)s
525 """ 526 """
526 debug(_("Creating %(game)s game in room %(room)s") % {'game': self.name, 'room': room_jid_s}) 527 log.debug(_("Creating %(game)s game in room %(room)s") % {'game': self.name, 'room': room_jid_s})
527 profile = self.host.memory.getProfileName(profile_key) 528 profile = self.host.memory.getProfileName(profile_key)
528 if not profile: 529 if not profile:
529 error(_("profile %s is unknown") % profile_key) 530 log.error(_("profile %s is unknown") % profile_key)
530 return 531 return
531 (create, sync) = self._checkCreateGameAndInit(room_jid_s, profile) 532 (create, sync) = self._checkCreateGameAndInit(room_jid_s, profile)
532 if nicks is None: 533 if nicks is None:
533 nicks = [] 534 nicks = []
534 if not create: 535 if not create:
550 @param player: the player nick in the room 551 @param player: the player nick in the room
551 @param referee: referee userhost 552 @param referee: referee userhost
552 """ 553 """
553 profile = self.host.memory.getProfileName(profile_key) 554 profile = self.host.memory.getProfileName(profile_key)
554 if not profile: 555 if not profile:
555 error(_("profile %s is unknown") % profile_key) 556 log.error(_("profile %s is unknown") % profile_key)
556 return 557 return
557 debug('new player ready: %s' % profile) 558 log.debug('new player ready: %s' % profile)
558 # TODO: we probably need to add the game and room names in the sent message 559 # TODO: we probably need to add the game and room names in the sent message
559 self.send(JID(referee), 'player_ready', {'player': player}, profile=profile) 560 self.send(JID(referee), 'player_ready', {'player': player}, profile=profile)
560 561
561 def newRound(self, room_jid, data, profile): 562 def newRound(self, room_jid, data, profile):
562 """Launch a new round (reinit the user data) 563 """Launch a new round (reinit the user data)
565 @param data: a couple (common_data, msg_elts) with: 566 @param data: a couple (common_data, msg_elts) with:
566 - common_data: backend initialization data for the new round 567 - common_data: backend initialization data for the new round
567 - msg_elts: dict to map each user to his specific initialization message 568 - msg_elts: dict to map each user to his specific initialization message
568 @param profile 569 @param profile
569 """ 570 """
570 debug(_('new round for %s game') % self.name) 571 log.debug(_('new round for %s game') % self.name)
571 game_data = self.games[room_jid.userhost()] 572 game_data = self.games[room_jid.userhost()]
572 players = game_data['players'] 573 players = game_data['players']
573 players_data = game_data['players_data'] 574 players_data = game_data['players_data']
574 game_data['stage'] = "init" 575 game_data['stage'] = "init"
575 576
633 - content: unicode that is appended to the child content 634 - content: unicode that is appended to the child content
634 @param profile: the profile from which the message is sent 635 @param profile: the profile from which the message is sent
635 @return: a Deferred instance 636 @return: a Deferred instance
636 """ 637 """
637 if profile is None: 638 if profile is None:
638 error(_("Message can not be sent without a sender profile")) 639 log.error(_("Message can not be sent without a sender profile"))
639 return defer.fail(None) 640 return defer.fail(None)
640 msg = self._createGameElt(to_jid) 641 msg = self._createGameElt(to_jid)
641 for elem, attrs, content in data: 642 for elem, attrs, content in data:
642 if elem is not None: 643 if elem is not None:
643 if isinstance(elem, domish.Element): 644 if isinstance(elem, domish.Element):