Skip to content

Commit

Permalink
integrations: Remove !avatar from game handler.
Browse files Browse the repository at this point in the history
The issue linked to this commit suggest suggests to replace the avatar
with the username only, I just needed to remove !avatar as the code
already shows the username.

Fixes part of #632.
  • Loading branch information
ahmedabuamra authored and timabbott committed Feb 19, 2021
1 parent e995e52 commit 9ce2ea5
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ def test_quit_invite(self) -> None:
}
}
self.verify_response(
'quit', 'Game cancelled.\n!avatar([email protected]) **foo** quit.', 0, bot, 'foo')
'quit', 'Game cancelled.\n**foo** quit.', 0, bot, 'foo')

def test_user_already_in_game_errors(self) -> None:
bot = self.setup_game()
Expand Down Expand Up @@ -313,14 +313,14 @@ def test_draw(self) -> None:

def test_normal_turns(self) -> None:
bot = self.setup_game()
self.verify_response('move 3', '**foo** moved in column 3\n\nfoo\n\n!avatar([email protected]) It\'s **baz**\'s (:red_circle:) turn.',
self.verify_response('move 3', '**foo** moved in column 3\n\nfoo\n\nIt\'s **baz**\'s (:red_circle:) turn.',
0, bot=bot, stream='test', subject='test game')
self.verify_response('move 3', '**baz** moved in column 3\n\nfoo\n\n!avatar([email protected]) It\'s **foo**\'s (:blue_circle:) turn.',
self.verify_response('move 3', '**baz** moved in column 3\n\nfoo\n\nIt\'s **foo**\'s (:blue_circle:) turn.',
0, bot=bot, stream='test', subject='test game', user_name='baz')

def test_wrong_turn(self) -> None:
bot = self.setup_game()
self.verify_response('move 5', '!avatar([email protected]) It\'s **foo**\'s (:blue_circle:) turn.', 0,
self.verify_response('move 5', 'It\'s **foo**\'s (:blue_circle:) turn.', 0,
bot=bot, stream='test', subject='test game', user_name='baz')

def test_private_message_error(self) -> None:
Expand Down Expand Up @@ -390,7 +390,7 @@ def test_game_over_and_leaderboard(self) -> None:
bot = self.setup_game()
bot.put_user_cache()
with patch('zulip_bots.bots.game_handler_bot.game_handler_bot.MockModel.determine_game_over', return_value='[email protected]'):
self.verify_response('move 3', '!avatar([email protected]) **foo** won! :tada:',
self.verify_response('move 3', '**foo** won! :tada:',
1, bot=bot, stream='test', subject='test game')
leaderboard = '**Most wins**\n\n\
Player | Games Won | Games Drawn | Games Lost | Total Games\n\
Expand All @@ -403,12 +403,12 @@ def test_game_over_and_leaderboard(self) -> None:
def test_current_turn_winner(self) -> None:
bot = self.setup_game()
with patch('zulip_bots.bots.game_handler_bot.game_handler_bot.MockModel.determine_game_over', return_value='current turn'):
self.verify_response('move 3', '!avatar([email protected]) **foo** won! :tada:',
self.verify_response('move 3', '**foo** won! :tada:',
1, bot=bot, stream='test', subject='test game')

def test_computer_turn(self) -> None:
bot = self.setup_computer_game()
self.verify_response('move 3', '**foo** moved in column 3\n\nfoo\n\n!avatar([email protected]) It\'s **test-bot**\'s (:red_circle:) turn.',
self.verify_response('move 3', '**foo** moved in column 3\n\nfoo\n\nIt\'s **test-bot**\'s (:red_circle:) turn.',
0, bot=bot, stream='test', subject='test game')
with patch('zulip_bots.bots.game_handler_bot.game_handler_bot.MockModel.determine_game_over', return_value='[email protected]'):
self.verify_response('move 5', 'I won! Well Played!',
Expand Down Expand Up @@ -462,7 +462,6 @@ def test_get_game_info(self) -> None:
def test_parse_message(self) -> None:
bot = self.setup_game()
self.verify_response('move 3', 'Join your game using the link below!\n\n> **Game `abc123`**\n\
> !avatar([email protected])\n\
> foo test game\n\
> 2/2 players\n\
> **[Join Game](/#narrow/stream/test/topic/test game)**', 0, bot=bot)
Expand All @@ -471,7 +470,6 @@ def test_parse_message(self) -> None:
To move subjects, send your message again, otherwise join the game using the link below.
> **Game `abc123`**
> !avatar([email protected])
> foo test game
> 2/2 players
> **[Join Game](/#narrow/stream/test/topic/test game)**''', 0, bot=bot, stream='test 2', subject='game 2')
Expand All @@ -485,7 +483,6 @@ def test_change_game_subject(self) -> None:
To move subjects, send your message again, otherwise join the game using the link below.
> **Game `abcdefg`**
> !avatar([email protected])
> foo test game
> 2/2 players
> **[Join Game](/#narrow/stream/test2/topic/test game 2)**''', 0, bot=bot, user_name='bar', stream='test game', subject='test2')
Expand Down
30 changes: 9 additions & 21 deletions zulip_bots/zulip_bots/game_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,9 @@ def play_with_computer_help(self) -> str:

def alert_new_invitation(self, game_id: str) -> str:
# Since the first player invites, the challenger is always the first player
return '**' + self.get_host(game_id) + ' has invited you to play a game of ' + self.game_name + '.**\n' +\
player_email = self.get_players(game_id)[0]
sender_name = self.get_username_by_email(player_email)
return '**' + sender_name + ' has invited you to play a game of ' + self.game_name + '.**\n' +\
self.get_formatted_game_object(game_id) + '\n\n' +\
'Type ```accept``` to accept the game invitation\n' +\
'Type ```decline``` to decline the game invitation.'
Expand Down Expand Up @@ -381,9 +383,8 @@ def command_quit(self, message: Dict[str, Any], sender: str, content: str) -> No
if game_id == '':
self.send_reply(
message, 'You are not in a game. Type `help` for all commands.')
sender_avatar = "!avatar({})".format(sender)
sender_name = self.get_username_by_email(sender)
self.cancel_game(game_id, reason='{} **{}** quit.'.format(sender_avatar, sender_name))
self.cancel_game(game_id, reason='**{}** quit.'.format(sender_name))

def command_join(self, message: Dict[str, Any], sender: str, content: str) -> None:
if not self.is_user_not_player(sender, message):
Expand Down Expand Up @@ -485,8 +486,7 @@ def start_game(self, game_id: str) -> None:
def get_formatted_game_object(self, game_id: str) -> str:
object = '''> **Game `{}`**
> {}
> {}
> {}/{} players'''.format(game_id, self.get_host(game_id), self.game_name, self.get_number_of_players(game_id), self.max_players)
> {}/{} players'''.format(game_id, self.game_name, self.get_number_of_players(game_id), self.max_players)
if game_id in self.instances.keys():
instance = self.instances[game_id]
if not self.is_single_player:
Expand Down Expand Up @@ -554,11 +554,6 @@ def get_number_of_players(self, game_id: str) -> int:
num = len(self.get_players(game_id))
return num

def get_host(self, game_id: str) -> str:
player_email = self.get_players(game_id)[0]
player_avatar = "!avatar({})".format(player_email)
return player_avatar

def parse_message(self, message: Dict[str, Any]) -> None:
game_id = self.is_user_in_game(message['sender_email'])
game = self.get_game_info(game_id)
Expand Down Expand Up @@ -825,9 +820,7 @@ def handle_message(self, content: str, player_email: str) -> None:
if self.gameAdapter.is_single_player:
self.broadcast('It\'s your turn')
else:
user_turn_avatar = "!avatar({})".format(self.players[self.turn])
self.broadcast('{} It\'s **{}**\'s ({}) turn.'.format(
user_turn_avatar,
self.broadcast('It\'s **{}**\'s ({}) turn.'.format(
self.gameAdapter.get_username_by_email(
self.players[self.turn]),
self.gameAdapter.gameMessageHandler.get_player_color(self.turn)))
Expand Down Expand Up @@ -889,9 +882,7 @@ def same_player_turn(self, content: str, message: str, is_computer: bool) -> Non
game_over = self.players[self.turn]
self.end_game(game_over)
return
user_turn_avatar = "!avatar({})".format(self.players[self.turn])
self.current_messages.append('{} It\'s **{}**\'s ({}) turn.'.format(
user_turn_avatar,
self.current_messages.append('It\'s **{}**\'s ({}) turn.'.format(
self.gameAdapter.get_username_by_email(self.players[self.turn]),
self.gameAdapter.gameMessageHandler.get_player_color(self.turn)
))
Expand All @@ -906,9 +897,7 @@ def next_turn(self) -> None:
if self.gameAdapter.is_single_player:
self.current_messages.append('It\'s your turn.')
else:
user_turn_avatar = "!avatar({})".format(self.players[self.turn])
self.current_messages.append('{} It\'s **{}**\'s ({}) turn.'.format(
user_turn_avatar,
self.current_messages.append('It\'s **{}**\'s ({}) turn.'.format(
self.gameAdapter.get_username_by_email(self.players[self.turn]),
self.gameAdapter.gameMessageHandler.get_player_color(self.turn)
))
Expand All @@ -931,9 +920,8 @@ def end_game(self, winner: str) -> None:
elif winner.startswith('except:'):
loser = winner.lstrip('except:')
else:
winner_avatar = "!avatar({})".format(winner)
winner_name = self.gameAdapter.get_username_by_email(winner)
self.broadcast('{} **{}** won! :tada:'.format(winner_avatar, winner_name))
self.broadcast('**{}** won! :tada:'.format(winner_name))
for u in self.players:
values = {'total_games': 1, 'games_won': 0,
'games_lost': 0, 'games_drawn': 0}
Expand Down

0 comments on commit 9ce2ea5

Please sign in to comment.