def test_StartGame_CreatesChannelAndInvitesMafia(): repo = GameStateRepo() game = Game() testpId = 'test' testMafiaChannelId = 'testChannel' game.players = [createMafia(id=testpId)] game.state = States.NIGHT game.meta = {'channel_id': 'channel'} with patch('mafiaManageSlack.WebClient') as slackClientConstructor: with patch( 'mafiaManageSlack.get_state_change_message') as messageBuilder: with patch('data_access.dataRepos.boto3'): with patch('mafiaManageSlack.boto3'): slackClient = slackClientConstructor.return_value slackClient.conversations_create.return_value = { 'channel': { 'id': testMafiaChannelId } } mafiaManageSlack.lambda_handler( createSqsEvent({ 'state': repo._serializeGame(game), 'action': Actions.START_GAME, 'source': 'initiator', 'target': None }), None) slackClient.conversations_create.assert_called_with( name='mafia-secrets', is_private=True) slackClient.conversations_invite.assert_called_with( channel=testMafiaChannelId, users=testpId) slackClient.chat_postMessage.assert_called_with( channel=testMafiaChannelId, text= 'You are members of the local mafia. Rabble-rousers in the village have decided to make a stand against you. It is time you taught them a lesson...\nKill one of them using the command: /mafia kill @who-to-kill\nIf there is more than one member of the mafia you must all /mafia kill the same villager before they will be killed.' )
def test_RecordReceived_GenerateMessageAndBroadcastToChannel(): repo = GameStateRepo() game = Game() testExecutorId = 'source' testTargetId = 'target' testMainChannelId = 'testChannel' game.meta = {'channel_id' : testMainChannelId} with patch('mafiaManageSlack.WebClient') as slackClientConstructor: with patch('mafiaManageSlack.get_state_change_message') as messageBuilder: with patch('mafiaManageSlack.boto3'): slackClient = slackClientConstructor.return_value mafiaManageSlack.lambda_handler(createSqsEvent({'state':repo._serializeGame(game), 'action':'ACTION', 'source': testExecutorId, 'target':testTargetId}), None) slackClient.chat_postMessage.assert_called_with(channel=testMainChannelId, text=messageBuilder.return_value) messageBuilder.assert_called_with(ANY, True, 'ACTION', testExecutorId, testTargetId)
def processRecords(recoredList): repo = GameStateRepo() for r in recoredList: try: body = json.loads(r['body']) state = repo._deserializeGame(body['state']) token = getToken(state.id) client = WebClient(token=token) action = body['action'] sourcePlayer = body['source'] targetPlayer = body['target'] mainChannel = state.meta['channel_id'] message = get_state_change_message(state,True,action,sourcePlayer,targetPlayer) client.chat_postMessage(channel=mainChannel,text=message) if action == Actions.START_GAME: #create a private channel for the mafia mafiaMembers = ','.join([p.id for p in state.players if p.role == Roles.MAFIA]) mafiaChannelName = 'mafia-secrets' response = client.conversations_list(types='private_channel') mafiaChannels = [c for c in response['channels'] if c['name'] == mafiaChannelName] if len(mafiaChannels) > 0: print('Unarchiving mafia channel') channelId = mafiaChannels[0]['id'] response = client.conversations_unarchive(channel=channelId) else: print('Creating mafia channel') response = client.conversations_create(name=mafiaChannelName, is_private=True) print(response) channelId = response['channel']['id'] print(f'Inviting {mafiaMembers} to mafia channel') client.conversations_invite(channel=channelId, users=mafiaMembers) client.chat_postMessage(channel=channelId, text='You are members of the local mafia. Rabble-rousers in the village have decided to make a stand against you. It is time you taught them a lesson...\nKill one of them using the command: /mafiahit @who-to-kill\nIf there is more than one member of the mafia you must all /mafiahit the same villager before they will be killed.') #store the mafia channel state.meta['mafia_channel'] = channelId repo.UpdateGame(state) elif state.state == GameStates.GAME_OVER: #clean up the mafia channel and archive it mafia_channel = state.meta['mafia_channel'] for player_id in [p.id for p in state.players if p.role == Roles.MAFIA]: print(f'kicking {player_id}') client.conversations_kick(channel=mafia_channel, user=player_id) print(f'archiving channel {mafia_channel}') client.conversations_archive(channel=mafia_channel) except SlackApiError as e: print(e)
def lambda_handler(event, context): print(f"Received event:\n{json.dumps(event)}\nWith context:\n{context}") gameRepo = GameStateRepo() game_id, channel_id, action, player_id, target_id = extractParameters( event) if action == HELP: response = { 'statusCode': 200, 'headers': {}, 'body': json.dumps({ 'response_type': 'ephemeral', 'text': get_help_message() }), 'isBase64Encoded': False } elif action == NEW_GAME: state = gameRepo.CreateNewGame(game_id, {'channel_id': channel_id}) if state == None: response_type = 'ephemeral' message = json.dumps('Game already exists.') else: response_type = 'in_channel' message = json.dumps( 'A new game of mafia is about to start. type /mafia join to get in on the action.' ) response = { 'statusCode': 200, 'headers': {}, 'body': json.dumps({ 'response_type': response_type, 'text': message, 'game_id': game_id }), 'isBase64Encoded': False } else: gameState = gameRepo.GetGameState(game_id) if gameState == None: return None manager = getInstance(gameState) success = manager.transition(action, executor=player_id, data=target_id) response_type = 'ephemeral' if success: updateSlack(gameRepo._serializeGame(gameState), action, player_id, target_id) response_text = 'Got it!' else: response_text = get_state_change_message(gameState, False, action, player_id, target_id) print(f'Response message: {response_text}') gameRepo.UpdateGame(gameState) response = { 'statusCode': 200, 'headers': {}, 'body': json.dumps({ 'response_type': response_type, 'text': response_text, 'game_id': game_id }), 'isBase64Encoded': False } return response