def on_message_received(self, message_id, jid, message, timestamp, wants_receipt, push_name, is_broadcast): if message.startswith('!'): # Message sent approximately one minute (or more) before? if (self.creation_time - timestamp) > 60: return try: operation, params = message.split(' ', 1) except ValueError: operation, params = message, None send_msg = self.get_send_msg(jid) TruthOrDare.private_command(operation, params, send_msg, user_jid=jid) # reply ack if wants_receipt and self.send_receipts: self.methods.call("message_ack", (jid, message_id))
def on_group_message_received(self, message_id, group_jid, author, message, timestamp, wants_receipt, push_name): if group_jid not in self.groups: send_msg = self.get_send_msg() game_group = TruthOrDare(group_jid, send_msg) self.groups[group_jid] = game_group if message.startswith('!'): # Message sent approximately one minute (or more) before? if (self.creation_time - timestamp) > 60: return try: operation, params = message.split(' ', 1) except ValueError: operation, params = message, None if (author not in self.groups[group_jid].players) and (operation != '!jogar'): return command = self.groups[group_jid].commands.get(operation) if command: command(push_name=push_name, author=author, message=message, params=params) # reply ack if wants_receipt and self.send_receipts: self.methods.call("message_ack", (group_jid, message_id))
def setUp(self): self.game = TruthOrDare('group', send_msg)
class TruthOrDareTestCase(unittest.TestCase): def setUp(self): self.game = TruthOrDare('group', send_msg) def test_play(self): self.game.players = { '*****@*****.**': 'player1', '*****@*****.**': 'player2', } self.game.play() jid, msg = MESSAGE self.assertEquals(jid, 'group') self.assertTrue('player1' in msg) self.assertTrue('pergunta para' in msg) self.assertTrue('player2' in msg) def test_play_without_players(self): self.game.play() jid, msg = MESSAGE self.assertEquals(jid, 'group') self.assertEquals(msg, 'Vamos jogar galera! Digite !jogar') def test_join(self): self.game.join(push_name='player1', author='*****@*****.**') jid, msg = MESSAGE self.assertEquals(jid, 'group') self.assertEquals(msg, 'player1 entrou no jogo...') self.assertEquals(self.game.players['*****@*****.**'], 'player1') def test_join_with_player_in_game(self): self.game.players = { '*****@*****.**': 'player1', } self.game.join(push_name='player1', author='*****@*****.**') jid, msg = MESSAGE self.assertEquals(jid, 'group') self.assertEquals(msg, 'player1 já está jogando!') def test_left(self): self.game.players = { '*****@*****.**': 'player1', } self.game.left(push_name='player1', author='*****@*****.**') jid, msg = MESSAGE self.assertEquals(jid, 'group') self.assertEquals(msg, 'player1 saiu do jogo.') self.assertTrue('player1' not in self.game.players) def test_left_without_in_the_game(self): self.game.left(push_name='player1', author='*****@*****.**') jid, msg = MESSAGE self.assertEquals(jid, 'group') self.assertEquals(msg, 'player1 não está no jogo.') self.assertTrue('player1' not in self.game.players) def test_list(self): self.game.players = { '*****@*****.**': 'player1', '*****@*****.**': 'player2', } self.game.list_players() jid, msg = MESSAGE self.assertEquals(jid, 'group') self.assertTrue('No jogo agora: ' in msg) self.assertTrue('player1' in msg) self.assertTrue('player2' in msg) def test_list_players_without_players(self): self.game.list_players() jid, msg = MESSAGE self.assertEquals(jid, 'group') self.assertEquals(msg, 'Ninguém está jogando.') def test_throw(self): self.game.throw() jid, msg = MESSAGE self.assertEquals(jid, 'group') self.assertTrue(msg in ['Mentira', 'Verdade']) def test_challenge(self): with open(os.path.join(BASE_DIR, 'challenges.txt'), 'w') as f: f.write("first challenge\n") self.game.challenge() jid, msg = MESSAGE self.assertEquals(jid, 'group') self.assertEquals(msg, "first challenge\n") def test_challenge_without_file(self): filename = os.path.join(BASE_DIR, 'challenges.txt') if os.path.exists(filename): os.remove(filename) self.game.challenge() jid, msg = MESSAGE self.assertEquals(jid, 'group') self.assertEquals(msg, "Não há desafios cadastrados!") def test_challenge_with_empty_file(self): with open(os.path.join(BASE_DIR, 'challenges.txt'), 'w'): pass self.game.challenge() jid, msg = MESSAGE self.assertEquals(jid, 'group') self.assertEquals(msg, "Não há desafios cadastrados!") def test_help(self): self.game.game_help(author="*****@*****.**") jid, msg = MESSAGE self.assertEquals(jid, '*****@*****.**') self.assertEquals(msg, HELP_TEXT) def tearDown(self): self.game = None