Skip to content

Commit

Permalink
Merge b9d5f96 into 0956515
Browse files Browse the repository at this point in the history
  • Loading branch information
TheseusGrey committed Sep 5, 2018
2 parents 0956515 + b9d5f96 commit 4b80a50
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 87 deletions.
2 changes: 1 addition & 1 deletion Pipfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ verify_ssl = true
name = "pypi"

[dev-packages]
pylint = "*"
pylint = "<2.0.0"

[packages]
"e1839a8" = {editable = true, path = "."}
Expand Down
104 changes: 23 additions & 81 deletions Pipfile.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 9 additions & 3 deletions aimmo-game-worker/avatar_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@

from six import StringIO

from simulation.action import WaitAction
from simulation.action import WaitAction, Action
from user_exceptions import InvalidActionException

LOGGER = logging.getLogger(__name__)

Expand Down Expand Up @@ -41,8 +42,7 @@ def process_avatar_turn(self, world_map, avatar_state, src_code):
sys.stderr = output_log
self._update_avatar(src_code)

action = self.avatar.handle_turn(world_map, avatar_state)
action = action.serialise()
action = self.decide_action(world_map, avatar_state)

except Exception as e:
traceback.print_exc()
Expand All @@ -56,3 +56,9 @@ def process_avatar_turn(self, world_map, avatar_state, src_code):

logs = output_log.getvalue()
return {'action': action, 'log': logs, 'avatar_updated': avatar_updated}

def decide_action(self, world_map, avatar_state):
action = self.avatar.handle_turn(world_map, avatar_state)
if not isinstance(action, Action):
raise InvalidActionException(action)
return action.serialise()
1 change: 1 addition & 0 deletions aimmo-game-worker/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
],
tests_require=[
'httmock',
'mock'
],
test_suite='tests',
zip_safe=False,
Expand Down
9 changes: 7 additions & 2 deletions aimmo-game-worker/simulation/action.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
class Action(object):
from abc import ABCMeta, abstractmethod


class Action:
__metaclass__ = ABCMeta

@abstractmethod
def serialise(self):
raise NotImplementedError
pass


class WaitAction(Action):
Expand Down
16 changes: 16 additions & 0 deletions aimmo-game-worker/tests/test_avatar_runner.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
from unittest import TestCase

import mock

from avatar_runner import AvatarRunner
from user_exceptions import InvalidActionException

NORTH = {'x': 0, 'y': 1}
SOUTH = {'x': 0, 'y': -1}
Expand Down Expand Up @@ -100,3 +104,15 @@ def test_update_code_flag_with_syntax_errors(self):
self.assertTrue(response['avatar_updated'])
response = runner.process_avatar_turn(world_map={}, avatar_state={}, src_code=avatar)
self.assertFalse(response['avatar_updated'])

def test_invalid_action_exception(self):
avatar = '''class Avatar(object):
def handle_turn(self, world_map, avatar_state):
from simulation.action import MoveAction
from simulation.direction import NORTH
'''
runner = AvatarRunner()
runner._update_avatar(src_code=avatar)
with self.assertRaises(InvalidActionException):
runner.decide_action(world_map={}, avatar_state={})
4 changes: 4 additions & 0 deletions aimmo-game-worker/user_exceptions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
class InvalidActionException(Exception):
def __init__(self, invalid_action_object):
message = '"{}" is not a valid action object.'.format(invalid_action_object)
super(InvalidActionException, self).__init__(message)

0 comments on commit 4b80a50

Please sign in to comment.