Skip to content
This repository has been archived by the owner on Aug 7, 2024. It is now read-only.

Tests for Workers updated #765

Merged
merged 6 commits into from
Aug 28, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions aimmo-game/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
from simulation import map_generator
from simulation.avatar.avatar_manager import AvatarManager
from simulation.worker_managers import WORKER_MANAGERS
from simulation.communicator import Communicator
from simulation.game_runner import GameRunner

eventlet.sleep()
Expand Down Expand Up @@ -61,8 +60,6 @@ def player_data(player_id):
def register_world_update_on_connect(self):
@socketio_server.on('connect')
def world_update_on_connect(sid, environ):
self._sid_to_avatar_id[sid] = None

query = environ['QUERY_STRING']
self._find_avatar_id_from_query(sid, query)
self.send_updates()
Expand Down Expand Up @@ -101,9 +98,12 @@ def _find_avatar_id_from_query(self, session_id, query_string):
LOGGER.error("Avatar ID could not be casted into an integer")
except KeyError:
LOGGER.error("No avatar ID found. User may not be authorised ")
LOGGER.error("query_string: " + query_string)

def _send_logs(self, player_id_to_workers):
def should_send_logs(logs):
LOGGER.info("should_send_logs: " + str(logs))

return logs is not None and logs != ''

for sid, player_id in self._sid_to_avatar_id.iteritems():
Expand Down
9 changes: 6 additions & 3 deletions aimmo-game/simulation/game_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,12 @@ def update_simulation(self, player_id_to_serialised_actions):
self.simulation_runner.run_single_turn(player_id_to_serialised_actions)
self.end_turn_callback()

def update(self):
self.update_workers()
self.update_simulation(self.worker_manager.get_player_id_to_serialised_actions())
self.worker_manager.clear_logs()

def run(self):
while True:
self.update_workers()
self.update_simulation(self.worker_manager.get_player_id_to_serialised_actions())
self.worker_manager.clear_logs()
self.update()
time.sleep(TURN_TIME)
4 changes: 2 additions & 2 deletions aimmo-game/simulation/simulation_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class SimulationRunner(object):
"""
daemon = True

def __init__(self, communicator, game_state,):
def __init__(self, game_state, communicator):
self.game_state = game_state
self.communicator = communicator

Expand Down Expand Up @@ -60,7 +60,7 @@ def run_turn(self, player_id_to_serialised_actions):
avatars = self.game_state.avatar_manager.active_avatars

for avatar in avatars:
self._run_turn_for_avatar(avatar, player_id_to_serialised_actions[avatar.id])
self._run_turn_for_avatar(avatar, player_id_to_serialised_actions[avatar.player_id])
location_to_clear = avatar.action.target_location
avatar.action.process(self.game_state.world_map)
self.game_state.world_map.clear_cell_actions(location_to_clear)
Expand Down
16 changes: 5 additions & 11 deletions aimmo-game/tests/functional/mock_world.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import random

from simulation import map_generator
from simulation.turn_manager import SequentialTurnManager
from simulation.logs import Logs
from simulation.simulation_runner import SequentialSimulationRunner
from tests.test_simulation.mock_communicator import MockCommunicator
from tests.test_simulation.dummy_avatar import DummyAvatarManager, MoveEastDummy

Expand All @@ -21,19 +20,14 @@ class MockWorld(object):

By default, the first avatar added to the world will be a MoveEastDummy.
"""
def __init__(self, settings=SETTINGS, dummies_list=None, map_generator_class=map_generator.Main, logs=None):
def __init__(self, settings=SETTINGS, dummies_list=None, map_generator_class=map_generator.Main,
simulation_runner_class=SequentialSimulationRunner):
random.seed(0)
if dummies_list is None:
dummies_list = [MoveEastDummy]

if logs is None:
logs = Logs()

self.generator = map_generator_class(settings)
self.avatar_manager = DummyAvatarManager(dummies_list)
self.game_state = self.generator.get_game_state(self.avatar_manager)
self.turn_manager = SequentialTurnManager(game_state=self.game_state,
end_turn_callback=lambda: None,
communicator=MockCommunicator(),
logs=logs,
have_avatars_code_updated={})
self.simulation_runner = simulation_runner_class(game_state=self.game_state,
communicator=MockCommunicator())
12 changes: 6 additions & 6 deletions aimmo-game/tests/functional/test_damage_pickups_and_effects.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def setUp(self):
avatar manager, game state, turn manager and a map generator.
"""
self.game = MockWorld()
self.game.game_state.add_avatar(1, None, Location(0, 0))
self.game.game_state.add_avatar(1, Location(0, 0))
_avatar_spawn_cell = self.game.game_state.world_map.get_cell(Location(0, 0))
self.initial_attack_strength = _avatar_spawn_cell.avatar.attack_strength
self.cell = self.game.game_state.world_map.get_cell(Location(1, 0))
Expand All @@ -24,7 +24,7 @@ def test_damage_boost_pickup_can_be_picked_up_default(self):
pickup_created = DamageBoostPickup(self.cell)
self.cell.pickup = pickup_created

self.game.turn_manager._run_single_turn()
self.game.simulation_runner.run_single_turn(self.game.avatar_manager.get_player_id_to_serialised_action())

self.assertEqual(self.cell.avatar, self.game.avatar_manager.get_avatar(1))
self.assertEqual(len(self.game.avatar_manager.get_avatar(1).effects), 1)
Expand All @@ -37,7 +37,7 @@ def test_damage_boost_pickup_can_be_picked_up_custom_integer(self, boost_value):
pickup_created = DamageBoostPickup(self.cell, boost_value)
self.cell.pickup = pickup_created

self.game.turn_manager._run_single_turn()
self.game.simulation_runner.run_single_turn(self.game.avatar_manager.get_player_id_to_serialised_action())

self.assertEqual(self.cell.avatar, self.game.avatar_manager.get_avatar(1))
self.assertEqual(len(self.game.avatar_manager.get_avatar(1).effects), 1)
Expand All @@ -51,7 +51,7 @@ def test_damage_boost_pickup_can_be_picked_up_custom_floats(self, boost_value):
pickup_created = DamageBoostPickup(self.cell, boost_value)
self.cell.pickup = pickup_created

self.game.turn_manager._run_single_turn()
self.game.simulation_runner.run_single_turn(self.game.avatar_manager.get_player_id_to_serialised_action())

self.assertEqual(self.cell.avatar, self.game.avatar_manager.get_avatar(1))
self.assertEqual(len(self.game.avatar_manager.get_avatar(1).effects), 1)
Expand All @@ -65,7 +65,7 @@ def test_damage_boost_increases_attack_strength_with_default_integer(self):
pickup_created = DamageBoostPickup(self.cell)
self.cell.pickup = pickup_created

self.game.turn_manager._run_single_turn()
self.game.simulation_runner.run_single_turn(self.game.avatar_manager.get_player_id_to_serialised_action())

self.assertTrue(self.cell.avatar.attack_strength, self.initial_attack_strength + DAMAGE_BOOST_DEFAULT)

Expand All @@ -78,6 +78,6 @@ def test_damage_boost_increases_attack_strength_with_custom_integers(self, boost
pickup_created = DamageBoostPickup(self.cell, boost_value)
self.cell.pickup = pickup_created

self.game.turn_manager._run_single_turn()
self.game.simulation_runner.run_single_turn(self.game.avatar_manager.get_player_id_to_serialised_action())

self.assertTrue(self.cell.avatar.attack_strength, self.initial_attack_strength + boost_value)
46 changes: 33 additions & 13 deletions aimmo-game/tests/functional/test_effect_expiry.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def setUp(self):
}

self.game = MockWorld(SETTINGS)
self.game.game_state.add_avatar(1, None, Location(0, 0))
self.game.game_state.add_avatar(1, Location(0, 0))
self.avatar = self.game.avatar_manager.get_avatar(1)
self.cell = self.game.game_state.world_map.get_cell(Location(1, 0))

Expand All @@ -40,15 +40,19 @@ def test_single_damage_boost_pickup_expiry(self):

# Avatar moves EAST to (1,0) where pickup is located, then repeats it 5 times.
for i in range(6):
self.game.turn_manager._run_single_turn()
self.game.simulation_runner.run_single_turn(
self.game.avatar_manager.get_player_id_to_serialised_action()
)

self.assertTrue(isinstance(list(self.avatar.effects)[0], pickup_created.EFFECT))
self.assertEqual(list(self.avatar.effects)[0]._time_remaining, 5)
self.assertEqual(self.avatar.attack_strength, 11)

# Run 5 more turns and expect the effect to expire.
for i in range(5):
self.game.turn_manager._run_single_turn()
self.game.simulation_runner.run_single_turn(
self.game.avatar_manager.get_player_id_to_serialised_action()
)

self.assertEqual(len(self.avatar.effects), 0)
self.assertEqual(self.avatar.attack_strength, 1)
Expand All @@ -64,15 +68,15 @@ def test_single_invulnerability_pickup_pickup_expiry(self):

# Avatar moves EAST to (1,0) where pickup is located, then repeats it 5 times.
for i in range(6):
self.game.turn_manager._run_single_turn()
self.game.simulation_runner.run_single_turn(self.game.avatar_manager.get_player_id_to_serialised_action())

self.assertTrue(isinstance(list(self.avatar.effects)[0], pickup_created.EFFECT))
self.assertEqual(list(self.avatar.effects)[0]._time_remaining, 5)
self.assertEqual(self.avatar.resistance, INVULNERABILITY_RESISTANCE)

# Run 5 more turns and expect the effect to expire.
for i in range(5):
self.game.turn_manager._run_single_turn()
self.game.simulation_runner.run_single_turn(self.game.avatar_manager.get_player_id_to_serialised_action())

self.assertEqual(len(self.avatar.effects), 0)
self.assertEqual(self.avatar.resistance, 0)
Expand All @@ -93,7 +97,9 @@ def test_multiple_damage_boost_pickup_expiry(self):
self.assertEqual(self.avatar.attack_strength, 1)

# Avatar moves EAST to (1,0) where pickup one is located.
self.game.turn_manager._run_single_turn()
self.game.simulation_runner.run_single_turn(
self.game.avatar_manager.get_player_id_to_serialised_action()
)

self.assertTrue(isinstance(list(self.avatar.effects)[0], pickup_created_one.EFFECT))
self.assertEqual(len(self.avatar.effects), 1)
Expand All @@ -102,23 +108,29 @@ def test_multiple_damage_boost_pickup_expiry(self):

# Move twice to the second pickup.
for i in range(2):
self.game.turn_manager._run_single_turn()
self.game.simulation_runner.run_single_turn(
self.game.avatar_manager.get_player_id_to_serialised_action()
)

self.assertTrue(isinstance(list(self.avatar.effects)[1], pickup_created_two.EFFECT))
self.assertEqual(len(self.avatar.effects), 2)
self.assertEqual(self.avatar.attack_strength, 26)

# Eight turns later, we expect the first effect to expire.
for i in range(8):
self.game.turn_manager._run_single_turn()
self.game.simulation_runner.run_single_turn(
self.game.avatar_manager.get_player_id_to_serialised_action()
)

self.assertEqual(len(self.avatar.effects), 1)
self.assertEqual(list(self.avatar.effects)[0]._time_remaining, 2)
self.assertEqual(self.avatar.attack_strength, 16)

# Two turns later, the second pickup expires too.
for i in range(2):
self.game.turn_manager._run_single_turn()
self.game.simulation_runner.run_single_turn(
self.game.avatar_manager.get_player_id_to_serialised_action()
)

self.assertEqual(len(self.avatar.effects), 0)
self.assertEqual(self.avatar.attack_strength, 1)
Expand All @@ -139,7 +151,9 @@ def test_multiple_invulnerability_boost_pickup_expiry(self):
self.assertEqual(self.avatar.resistance, 0)

# Avatar moves EAST to (1,0) where pickup one is located.
self.game.turn_manager._run_single_turn()
self.game.simulation_runner.run_single_turn(
self.game.avatar_manager.get_player_id_to_serialised_action()
)

self.assertTrue(isinstance(list(self.avatar.effects)[0], pickup_created_one.EFFECT))
self.assertEqual(len(self.avatar.effects), 1)
Expand All @@ -148,23 +162,29 @@ def test_multiple_invulnerability_boost_pickup_expiry(self):

# Move twice to the second pickup.
for i in range(2):
self.game.turn_manager._run_single_turn()
self.game.simulation_runner.run_single_turn(
self.game.avatar_manager.get_player_id_to_serialised_action()
)

self.assertTrue(isinstance(list(self.avatar.effects)[1], pickup_created_two.EFFECT))
self.assertEqual(len(self.avatar.effects), 2)
self.assertEqual(self.avatar.resistance, INVULNERABILITY_RESISTANCE * 2)

# Eight turns later, we expect the first effect to expire.
for i in range(8):
self.game.turn_manager._run_single_turn()
self.game.simulation_runner.run_single_turn(
self.game.avatar_manager.get_player_id_to_serialised_action()
)

self.assertEqual(len(self.avatar.effects), 1)
self.assertEqual(list(self.avatar.effects)[0]._time_remaining, 2)
self.assertEqual(self.avatar.resistance, INVULNERABILITY_RESISTANCE)

# Two turns later, the second pickup expires too.
for i in range(2):
self.game.turn_manager._run_single_turn()
self.game.simulation_runner.run_single_turn(
self.game.avatar_manager.get_player_id_to_serialised_action()
)

self.assertEqual(len(self.avatar.effects), 0)
self.assertEqual(self.avatar.resistance, 0)
10 changes: 5 additions & 5 deletions aimmo-game/tests/functional/test_health_pickups_and_effects.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def setUp(self):
avatar manager, game state, turn manager and a map generator.
"""
self.game = MockWorld()
self.game.game_state.add_avatar(1, None, Location(0, 0))
self.game.game_state.add_avatar(1, Location(0, 0))
self.cell = self.game.game_state.world_map.get_cell(Location(1, 0))
self.initial_health = self.game.avatar_manager.get_avatar(1).health

Expand All @@ -29,7 +29,7 @@ def test_health_pickups_and_effects_apply_default(self):
"""
self.cell.pickup = HealthPickup(self.cell)

self.game.turn_manager._run_single_turn()
self.game.simulation_runner.run_single_turn(self.game.avatar_manager.get_player_id_to_serialised_action())

self.assertEqual(self.cell.avatar, self.game.avatar_manager.get_avatar(1))
self.assertEqual(self.cell.avatar.health, self.initial_health +
Expand All @@ -43,7 +43,7 @@ def test_health_pickups_and_effects_apply_custom_integers(self, restore_value):
self.setUp()
self.cell.pickup = HealthPickup(self.cell, restore_value)

self.game.turn_manager._run_single_turn()
self.game.simulation_runner.run_single_turn(self.game.avatar_manager.get_player_id_to_serialised_action())
self.assertEqual(self.cell.avatar, self.game.avatar_manager.get_avatar(1))

if self.initial_health + restore_value > HEALTH_RESTORE_MAX:
Expand All @@ -61,7 +61,7 @@ def test_health_pickups_and_effects_apply_custom_floats(self, restore_value):
self.setUp()
self.cell.pickup = HealthPickup(self.cell, restore_value)

self.game.turn_manager._run_single_turn()
self.game.simulation_runner.run_single_turn(self.game.avatar_manager.get_player_id_to_serialised_action())
self.assertEqual(self.cell.avatar, self.game.avatar_manager.get_avatar(1))

if self.initial_health + restore_value > HEALTH_RESTORE_MAX:
Expand All @@ -81,7 +81,7 @@ def test_health_effect_is_capped_at_HEALTH_RESTORE_MAX(self, restore_value):
self.setUp()
self.cell.pickup = HealthPickup(self.cell, restore_value)

self.game.turn_manager._run_single_turn()
self.game.simulation_runner.run_single_turn(self.game.avatar_manager.get_player_id_to_serialised_action())

self.assertEqual(self.cell.avatar, self.game.avatar_manager.get_avatar(1))
self.assertEqual(self.cell.avatar.health, AVATAR_HEALTH_MAX)
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def setUp(self):
avatar manager, game state, turn manager and a map generator.
"""
self.game = MockWorld()
self.game.game_state.add_avatar(1, None, Location(0, 0))
self.game.game_state.add_avatar(1, Location(0, 0))
self.cell = self.game.game_state.world_map.get_cell(Location(1, 0))

def test_invulnerability_pickups_increase_resistance_of_avatar(self):
Expand All @@ -25,7 +25,7 @@ def test_invulnerability_pickups_increase_resistance_of_avatar(self):
self.cell.pickup = InvulnerabilityPickup(self.cell)
self.assertEqual(self.game.avatar_manager.get_avatar(1).resistance, 0)

self.game.turn_manager._run_single_turn()
self.game.simulation_runner.run_single_turn(self.game.avatar_manager.get_player_id_to_serialised_action())

self.assertEqual(self.cell.avatar, self.game.avatar_manager.get_avatar(1))
self.assertEqual(self.cell.avatar.resistance, 1000)
Expand All @@ -37,13 +37,13 @@ def test_invulnerability_pickups_can_increase_resistance_to_2000(self):
then picks up the pickup, and moves to 2,0 to do the same.
"""
self.cell.pickup = InvulnerabilityPickup(self.cell)
self.game.turn_manager._run_single_turn()
self.game.simulation_runner.run_single_turn(self.game.avatar_manager.get_player_id_to_serialised_action())
self.assertEqual(self.cell.avatar, self.game.avatar_manager.get_avatar(1))
self.assertEqual(self.cell.avatar.resistance, 1000)

self.cell = self.game.game_state.world_map.get_cell(Location(2, 0))
self.cell.pickup = InvulnerabilityPickup(self.cell)
self.game.turn_manager._run_single_turn()
self.game.simulation_runner.run_single_turn(self.game.avatar_manager.get_player_id_to_serialised_action())

self.assertEqual(self.cell.avatar.resistance, 2000)
self.assertEqual(self.cell.avatar, self.game.avatar_manager.get_avatar(1))
Loading