def setUp(self): super(TestPlayer, self).setUp() path = os.path.dirname(os.path.os.path.realpath(__file__)) path = os.path.join(path, 'assets/test1.tmx') self.world = World(path) self.spawn_point = self.world.get_spawn_points()[0] self.world_update_task = task.LoopingCall(self.world.processing_step) self.world_update_task.clock = self.clock self.world_update_task.start(1.0 / 30) self.player = Archer(self.world, reactor=self.clock) self.player.spawn(self.spawn_point)
def setUp(self): super(ProfileInterface, self).setUp() self.pr = cProfile.Profile() self.path = os.path.dirname(os.path.os.path.realpath(__file__)) path = os.path.join(self.path, '../../resources/map.tmx') self.world = World(path) self.cache = MessageCache(self.world) self.spawn_point = self.world.get_object_by_name('spawn1') self.world_update_task = task.LoopingCall(self.world.processing_step) self.world_update_task.clock = self.clock self.world_update_task.start(1.0/30) self.networking_task = task.LoopingCall(self.world.networking_step) self.networking_task.clock = self.clock self.networking_task.start(1.0/30)
def setUp(self): super(TestInterface, self).setUp() path = os.path.dirname(os.path.os.path.realpath(__file__)) path = os.path.join(path, 'assets/test1.tmx') self.world = World(path) self.spawn_point = self.world.get_object_by_name('spawn1') self.world_update_task = task.LoopingCall(self.world.processing_step) self.world_update_task.clock = self.clock self.world_update_task.start(1.0 / 30) self.networking_task = task.LoopingCall(self.world.networking_step) self.networking_task.clock = self.clock self.networking_task.start(1.0 / 30) self.connection = Connection(self.world) self.last_callback_data = None
def setUp(self): super(TestArcher, self).setUp() path = os.path.dirname(os.path.os.path.realpath(__file__)) path = os.path.join(path, 'assets/test1.tmx') self.world = World(path) self.spawn_point = self.world.get_spawn_points()[0] self.world_update_task = task.LoopingCall(self.world.step) self.world_update_task.clock = self.clock self.world_update_task.start(settings.TIME_STEP) self.archer = Archer(self.world, reactor=self.clock) self.archer.spawn(self.spawn_point)
def init_world(self): if (self.config['map'][-3:] != 'tmx'): self.config['map'] = self.config['map'] + ".tmx" self.world = World('../resources/{}'.format(self.config['map'])) logging.info("Initialised map {}".format( self.world.map.properties['name'])) task.LoopingCall(self.world.networking_step).start( settings.NETWORKING_STEP) task.LoopingCall(self.world.processing_step).start( settings.PROCESSING_STEP) task.clock = self.reactor
class TestWorldGeneration(BaseTestCase): def setUp(self): path = os.path.dirname(os.path.os.path.realpath(__file__)) path = os.path.join(path, 'assets/test1.tmx') self.world = World(path) def test_spawn_points_found(self): spawn_point = self.world.get_spawn_points()[0] self.assertEqual(spawn_point.x, 2) self.assertEqual(spawn_point.y, 3) def test_collidables_created(self): barrel_width = 1.0 barrel_height = 1.312 barrel_x = 6.0 barrel_y = 2.688 barrel = self.world.get_objects_by_type('collidable')[0] self.assertAlmostEqual(barrel.get_position()['x'], barrel_x+0.5*barrel_width, places=3) self.assertAlmostEqual(barrel.get_position()['y'], barrel_y+0.5*barrel_height, places=3)
def setUp(self): super(TestInterface, self).setUp() path = os.path.dirname(os.path.os.path.realpath(__file__)) path = os.path.join(path, 'assets/test1.tmx') self.world = World(path) self.spawn_point = self.world.get_object_by_name('spawn1') self.world_update_task = task.LoopingCall(self.world.processing_step) self.world_update_task.clock = self.clock self.world_update_task.start(1.0/30) self.networking_task = task.LoopingCall(self.world.networking_step) self.networking_task.clock = self.clock self.networking_task.start(1.0/30) self.connection = Connection(self.world) self.last_callback_data = None
class ProfileInterface(BaseTestCase): def setUp(self): super(ProfileInterface, self).setUp() self.pr = cProfile.Profile() self.path = os.path.dirname(os.path.os.path.realpath(__file__)) path = os.path.join(self.path, '../../resources/map.tmx') self.world = World(path) self.cache = MessageCache(self.world) self.spawn_point = self.world.get_object_by_name('spawn1') self.world_update_task = task.LoopingCall(self.world.processing_step) self.world_update_task.clock = self.clock self.world_update_task.start(1.0/30) self.networking_task = task.LoopingCall(self.world.networking_step) self.networking_task.clock = self.clock self.networking_task.start(1.0/30) def process_stats(self, filename): self.ps = pstats.Stats(self.pr) f = open(os.path.join(self.path, 'data', filename), 'w') pstats.Stats(self.pr, stream=f).strip_dirs().sort_stats('cumulative').print_stats() f.close() return "%i calls" % self.ps.total_calls, "%.3f sec" % self.ps.total_tt def test_get_frame(self): """ Test raw execution performance of the 'get_frame'""" self.connection = Connection(self.world, self.cache) archers = list() for i in range(50): archer = Archer(self.world, reactor=self.clock) archer.spawn(self.spawn_point) archer.want_move(directions['south']) archers.append(archer) self.pr.enable() for i in range(100): self.connection.get_frame() self.pr.disable() print self.process_stats('test_get_frame.txt') def test_building_frames(self): """ Test performance of building frame for many players """ connections = list for i in range(100): connection = Connection(self.world, self.cache) connection.get_update() self.pr.enable() self.clock.advance(1) self.pr.disable() print self.process_stats('test_building_frames.txt') def test_get_update(self): """ Test raw execution performance of the 'get_update'""" self.connection = Connection(self.world, self.cache) archers = list() for i in range(50): archer = Archer(self.world, reactor=self.clock) archer.spawn(self.spawn_point) archer.want_move(directions['south']) archers.append(archer) self.pr.enable() for i in range(100): self.connection.get_update() self.pr.disable() print self.process_stats('test_get_update.txt') def test_buidling_updates(self): """ Test performance of building update messages for many players""" connections = list for i in range(100): connection = Connection(self.world, self.cache) connection.get_frame() self.pr.enable() self.clock.advance(1) self.pr.disable() print self.process_stats('test_buidling_updates.txt')
class TestArcher(BaseTestCase): def setUp(self): super(TestArcher, self).setUp() path = os.path.dirname(os.path.os.path.realpath(__file__)) path = os.path.join(path, 'assets/test1.tmx') self.world = World(path) self.spawn_point = self.world.get_spawn_points()[0] self.world_update_task = task.LoopingCall(self.world.step) self.world_update_task.clock = self.clock self.world_update_task.start(settings.TIME_STEP) self.archer = Archer(self.world, reactor=self.clock) self.archer.spawn(self.spawn_point) def tearDown(self): self.archer.destroy() self.world_update_task.stop() def count_archer_arrows(self, archer): arrows = self.world.get_objects_by_type('arrow') count = 0 for arrow in arrows: if(arrow.owner == archer): count = count + 1 return count def get_archer_arrows(self, archer): arrows = self.world.get_objects_by_type('arrow') archer_arrows = list() for arrow in arrows: if(arrow.owner == archer): archer_arrows.append(arrow) return archer_arrows def test_archer_spawned(self): self.assertEqual(self.spawn_point.x, self.archer.physics.position.x) self.assertEqual(self.spawn_point.y, self.archer.physics.position.y) def test_archer_moved(self): self.archer.want_move(directions['north']) self.advance_clock(1) self.assertLess(self.archer.physics.position.y, self.spawn_point.y) def test_archer_collides(self): self.archer.want_move(directions['east']) self.advance_clock(100) self.assertLess(self.archer.physics.position.x, 6.0) self.assertGreater(self.archer.physics.position.x, self.spawn_point.x) def test_archer_shoots(self): self.archer.want_attack(directions['south']) self.advance_clock(40) self.assertEqual(self.count_archer_arrows(self.archer), 1) self.advance_clock(1000) self.assertEqual(self.count_archer_arrows(self.archer), 0) def test_arrow_flies(self): self.archer.want_attack(directions['south']) self.advance_clock(40) arrow = self.get_archer_arrows(self.archer)[0] self.assertEqual(self.archer.physics.position.x, arrow.physics.position.x) archer_position_plus_2m = self.archer.physics.position + directions['south']*2 self.assertGreater(arrow.physics.position.y, archer_position_plus_2m.y) def test_arrow_collides(self): self.archer.want_attack(directions['east']) self.advance_clock(40) arrow = self.get_archer_arrows(self.archer)[0] self.assertLess(arrow.physics.position.x, 6.0) self.assertGreater(arrow.physics.position.x, self.archer.physics.position.x) def test_archer_kills_archer(self): attacker_spawn = self.world.get_object_by_name('spawn2') defender_spawn = self.world.get_object_by_name('spawn3') attacker = Archer(self.world, name="attacker", reactor=self.clock) defender = Archer(self.world, name="defender", reactor=self.clock) attacker.spawn(attacker_spawn) defender.spawn(defender_spawn) attacker.want_attack(directions['south']) self.advance_clock(1) self.assertEqual(attacker.state, "shooting") self.assertEqual(defender.state, "standing") self.advance_clock(50) self.assertEqual(attacker.state, "standing") self.assertEqual(defender.state, "dying") self.advance_clock(250) self.assertEqual(defender.state, "dead")
class TestPlayer(BaseTestCase): def setUp(self): super(TestPlayer, self).setUp() path = os.path.dirname(os.path.os.path.realpath(__file__)) path = os.path.join(path, 'assets/test1.tmx') self.world = World(path) self.spawn_point = self.world.get_spawn_points()[0] self.world_update_task = task.LoopingCall(self.world.processing_step) self.world_update_task.clock = self.clock self.world_update_task.start(1.0 / 30) self.player = Archer(self.world, reactor=self.clock) self.player.spawn(self.spawn_point) def tearDown(self): self.player.destroy() self.world_update_task.stop() def count_player_arrows(self, player): arrows = self.world.get_objects_by_type('arrow') count = 0 for arrow in arrows: if (arrow.owner == player): count = count + 1 return count def get_player_arrows(self, player): arrows = self.world.get_objects_by_type('arrow') player_arrows = list() for arrow in arrows: if (arrow.owner == player): player_arrows.append(arrow) return player_arrows def test_player_spawned(self): self.assertEqual(self.spawn_point.x, self.player.physics.position.x) self.assertEqual(self.spawn_point.y, self.player.physics.position.y) def test_player_moved(self): self.player.want_move(directions['north']) self.advance_clock(1) self.assertLess(self.player.physics.position.y, self.spawn_point.y) def test_player_collides(self): self.player.want_move(directions['east']) self.advance_clock(100) self.assertLess(self.player.physics.position.x, 6.0) self.assertGreater(self.player.physics.position.x, self.spawn_point.x) def test_player_shoots(self): self.player.want_attack(directions['south']) self.advance_clock(40) self.assertEqual(self.count_player_arrows(self.player), 1) self.advance_clock(1000) self.assertEqual(self.count_player_arrows(self.player), 0) def test_arrow_flies(self): self.player.want_attack(directions['south']) self.advance_clock(40) arrow = self.get_player_arrows(self.player)[0] self.assertEqual(self.player.physics.position.x, arrow.physics.position.x) player_position_plus_2m = self.player.physics.position + directions[ 'south'] * 2 self.assertGreater(arrow.physics.position.y, player_position_plus_2m.y) def test_arrow_collides(self): self.player.want_attack(directions['south']) self.advance_clock(40) arrow = self.get_player_arrows(self.player)[0] self.assertLess(arrow.physics.position.x, 6.0) self.assertGreater(arrow.physics.position.y, self.player.physics.position.y) def test_player_kills_player(self): attacker_spawn = self.world.get_object_by_name('spawn2') defender_spawn = self.world.get_object_by_name('spawn3') attacker = Archer(self.world, name="attacker", reactor=self.clock) defender = Archer(self.world, name="defender", reactor=self.clock) attacker.spawn(attacker_spawn) defender.spawn(defender_spawn) attacker.want_attack(directions['south']) self.advance_clock(1) self.assertEqual(attacker.state, "shooting") self.assertEqual(defender.state, "standing") self.advance_clock(50) self.assertEqual(attacker.state, "standing") self.assertEqual(defender.state, "dying") self.advance_clock(250) self.assertEqual(defender.state, "dead")
class TestInterface(BaseTestCase): def setUp(self): super(TestInterface, self).setUp() path = os.path.dirname(os.path.os.path.realpath(__file__)) path = os.path.join(path, 'assets/test1.tmx') self.world = World(path) self.spawn_point = self.world.get_object_by_name('spawn1') self.world_update_task = task.LoopingCall(self.world.processing_step) self.world_update_task.clock = self.clock self.world_update_task.start(1.0/30) self.networking_task = task.LoopingCall(self.world.networking_step) self.networking_task.clock = self.clock self.networking_task.start(1.0/30) self.connection = Connection(self.world) self.last_callback_data = None def tearDown(self): self.world_update_task.stop() self.networking_task.stop() def get_items_expected(self, expected_attr): # items_expected = self.world.object_lookup_by_name items_expected = self.world.object_index items_expected = {k: v for k, v in items_expected.items() if hasattr(v, expected_attr)} return items_expected def callback(self, *args, **kwargs): self.last_callback_data = list(*args) def get_last_callback_arguments(self): tmp = self.last_callback_data self.last_callback_data = None return tmp def test_get_frame(self): self.archer = Archer(self.world, reactor=self.clock) self.archer.spawn(self.spawn_point) self.connection.on('frame', self.callback) self.clock.advance(1) frame = self.get_last_callback_arguments() items_expected = self.get_items_expected('get_frame_message') self.assertEqual(len(frame), len(items_expected)) for message in frame: matching_expected_item = items_expected.pop(message['id']) self.assertEqual(message['x'], int(settings.PPM*matching_expected_item.get_position()['x'])) self.assertEqual(message['y'], int(settings.PPM*matching_expected_item.get_position()['y'])) self.assertEqual(message['direction'], matching_expected_item.get_direction()) self.assertEqual(len(items_expected), 0) self.clock.advance(1) frame = self.get_last_callback_arguments() # Nothing has changed! self.assertEqual(len(frame), 0) self.archer.want_move(directions['south']) self.advance_clock(1) frame = self.get_last_callback_arguments() self.assertEqual(len(frame), 1) message = frame.pop() self.assertEqual(message['x'], int(settings.PPM*self.archer.get_position()['x'])) self.assertEqual(message['y'], int(settings.PPM*self.archer.get_position()['y'])) self.assertEqual(message['direction'], self.archer.get_direction()) def test_get_update(self): self.connection.on('update', self.callback) self.clock.advance(1) items_expected = self.get_items_expected('get_update_message') update = self.get_last_callback_arguments() self.assertEqual(len(update), len(items_expected)) for message in update: self.assertIsInstance(message, UpdateMessage) matching_expected_item = items_expected.pop(message['id']) self.assertEqual(message['id'], matching_expected_item.id) # self.assertEqual(message['center'], False) #TEST MORE self.assertEqual(len(items_expected), 0) self.clock.advance(1) update = self.get_last_callback_arguments() self.assertIsNone(update) self.archer = Archer(self.world, reactor=self.clock) self.archer.spawn(self.spawn_point) self.clock.advance(1) update = self.get_last_callback_arguments() self.assertEqual(len(update), 1) def get_fake_msg(self, x=1, y=3.33, direction=directions['south']): msg = FakeMessage() msg['x'] = x msg['y'] = y msg['direction'] = direction return msg def test_packing(self): msg = self.get_fake_msg() packed = msg.pack() self.assertEqual(packed[0:4], struct.pack('!I', 1)) self.assertEqual(packed[4:8], struct.pack('!f', 3.33)) self.assertEqual(packed[8:9], struct.pack('!B', DirectionMessageMixin.direction_lookup[directions['south']])) def test_dehydration(self): x = 12 y = 4.44 dir = DirectionMessageMixin.direction_lookup[directions['west']] dehydrated_item = [x, y, dir] msg = FakeMessage.from_dehydrated(dehydrated_item) self.assertEqual(msg['x'], x) self.assertEqual(msg['y'], y) self.assertEqual(msg['direction'], directions['west']) def test_unpacking(self): # packed = '\x00\x00\x00\x01\x00\x00\x00\x01\x00\x00\x00\x02\x02\n' x = 12 y = 7.77 dir = DirectionMessageMixin.direction_lookup[directions['west']] packed = struct.pack("!I", x) + struct.pack("!f", y) + struct.pack("!B", dir) + "\n" msg = FakeMessage.from_packed(packed) self.assertEqual(msg['x'], x) self.assertAlmostEqual(msg['y'], y, places=4) self.assertEqual(msg['direction'], directions['west']) def test_message_packing(self): msg = self.get_fake_msg() msg2 = self.get_fake_msg(x=5, direction=directions['east']) result = pack_messages([msg, msg2]) expected_byte_length = 1 + 2 * msg.get_byte_length() self.assertEqual(len(result), expected_byte_length) def test_message_unpacking(self): x1 = 12 y1 = 4.44 dir1 = DirectionMessageMixin.direction_lookup[directions['west']] packed = struct.pack("!I", x1) + struct.pack("!f", y1) + struct.pack("!B", dir1) x2 = 42 y2 = 9.987654 dir2 = DirectionMessageMixin.direction_lookup[directions['north']] packed = packed + struct.pack("!I", x2) + struct.pack("!f", y2) + struct.pack("!B", dir2) + "\n" packed = struct.pack("!B", 255) + packed messages = unpack_mesages(packed, message_types=message_types) self.assertEqual(len(messages), 2) self.assertEqual(messages[0]['x'], x1) self.assertAlmostEqual(messages[0]['y'], y1, places=4) self.assertEqual(messages[0]['direction'], directions['west']) self.assertEqual(messages[1]['x'], x2) self.assertAlmostEqual(messages[1]['y'], y2, places=4) self.assertEqual(messages[1]['direction'], directions['north']) def test_eating_own_dog_food(self): msg = self.get_fake_msg(y=1.0) packed = msg.pack() unpacked = FakeMessage.from_packed(packed) self.assertEqual(msg, unpacked)
def setUp(self): path = os.path.dirname(os.path.os.path.realpath(__file__)) path = os.path.join(path, 'assets/test1.tmx') self.world = World(path)
class TestInterface(BaseTestCase): def setUp(self): super(TestInterface, self).setUp() path = os.path.dirname(os.path.os.path.realpath(__file__)) path = os.path.join(path, 'assets/test1.tmx') self.world = World(path) self.spawn_point = self.world.get_object_by_name('spawn1') self.world_update_task = task.LoopingCall(self.world.processing_step) self.world_update_task.clock = self.clock self.world_update_task.start(1.0 / 30) self.networking_task = task.LoopingCall(self.world.networking_step) self.networking_task.clock = self.clock self.networking_task.start(1.0 / 30) self.connection = Connection(self.world) self.last_callback_data = None def tearDown(self): self.world_update_task.stop() self.networking_task.stop() def get_items_expected(self, expected_attr): # items_expected = self.world.object_lookup_by_name items_expected = self.world.object_index items_expected = { k: v for k, v in items_expected.items() if hasattr(v, expected_attr) } return items_expected def callback(self, *args, **kwargs): self.last_callback_data = list(*args) def get_last_callback_arguments(self): tmp = self.last_callback_data self.last_callback_data = None return tmp def test_get_frame(self): self.archer = Archer(self.world, reactor=self.clock) self.archer.spawn(self.spawn_point) self.connection.on('frame', self.callback) self.clock.advance(1) frame = self.get_last_callback_arguments() items_expected = self.get_items_expected('get_frame_message') self.assertEqual(len(frame), len(items_expected)) for message in frame: matching_expected_item = items_expected.pop(message['id']) self.assertEqual( message['x'], int(settings.PPM * matching_expected_item.get_position()['x'])) self.assertEqual( message['y'], int(settings.PPM * matching_expected_item.get_position()['y'])) self.assertEqual(message['direction'], matching_expected_item.get_direction()) self.assertEqual(len(items_expected), 0) self.clock.advance(1) frame = self.get_last_callback_arguments() # Nothing has changed! self.assertEqual(len(frame), 0) self.archer.want_move(directions['south']) self.advance_clock(1) frame = self.get_last_callback_arguments() self.assertEqual(len(frame), 1) message = frame.pop() self.assertEqual(message['x'], int(settings.PPM * self.archer.get_position()['x'])) self.assertEqual(message['y'], int(settings.PPM * self.archer.get_position()['y'])) self.assertEqual(message['direction'], self.archer.get_direction()) def test_get_update(self): self.connection.on('update', self.callback) self.clock.advance(1) items_expected = self.get_items_expected('get_update_message') update = self.get_last_callback_arguments() self.assertEqual(len(update), len(items_expected)) for message in update: self.assertIsInstance(message, UpdateMessage) matching_expected_item = items_expected.pop(message['id']) self.assertEqual(message['id'], matching_expected_item.id) # self.assertEqual(message['center'], False) #TEST MORE self.assertEqual(len(items_expected), 0) self.clock.advance(1) update = self.get_last_callback_arguments() self.assertIsNone(update) self.archer = Archer(self.world, reactor=self.clock) self.archer.spawn(self.spawn_point) self.clock.advance(1) update = self.get_last_callback_arguments() self.assertEqual(len(update), 1) def get_fake_msg(self, x=1, y=3.33, direction=directions['south']): msg = FakeMessage() msg['x'] = x msg['y'] = y msg['direction'] = direction return msg def test_packing(self): msg = self.get_fake_msg() packed = msg.pack() self.assertEqual(packed[0:4], struct.pack('!I', 1)) self.assertEqual(packed[4:8], struct.pack('!f', 3.33)) self.assertEqual( packed[8:9], struct.pack( '!B', DirectionMessageMixin.direction_lookup[directions['south']])) def test_dehydration(self): x = 12 y = 4.44 dir = DirectionMessageMixin.direction_lookup[directions['west']] dehydrated_item = [x, y, dir] msg = FakeMessage.from_dehydrated(dehydrated_item) self.assertEqual(msg['x'], x) self.assertEqual(msg['y'], y) self.assertEqual(msg['direction'], directions['west']) def test_unpacking(self): # packed = '\x00\x00\x00\x01\x00\x00\x00\x01\x00\x00\x00\x02\x02\n' x = 12 y = 7.77 dir = DirectionMessageMixin.direction_lookup[directions['west']] packed = struct.pack("!I", x) + struct.pack("!f", y) + struct.pack( "!B", dir) + "\n" msg = FakeMessage.from_packed(packed) self.assertEqual(msg['x'], x) self.assertAlmostEqual(msg['y'], y, places=4) self.assertEqual(msg['direction'], directions['west']) def test_message_packing(self): msg = self.get_fake_msg() msg2 = self.get_fake_msg(x=5, direction=directions['east']) result = pack_messages([msg, msg2]) expected_byte_length = 1 + 2 * msg.get_byte_length() self.assertEqual(len(result), expected_byte_length) def test_message_unpacking(self): x1 = 12 y1 = 4.44 dir1 = DirectionMessageMixin.direction_lookup[directions['west']] packed = struct.pack("!I", x1) + struct.pack("!f", y1) + struct.pack( "!B", dir1) x2 = 42 y2 = 9.987654 dir2 = DirectionMessageMixin.direction_lookup[directions['north']] packed = packed + struct.pack("!I", x2) + struct.pack( "!f", y2) + struct.pack("!B", dir2) + "\n" packed = struct.pack("!B", 255) + packed messages = unpack_mesages(packed, message_types=message_types) self.assertEqual(len(messages), 2) self.assertEqual(messages[0]['x'], x1) self.assertAlmostEqual(messages[0]['y'], y1, places=4) self.assertEqual(messages[0]['direction'], directions['west']) self.assertEqual(messages[1]['x'], x2) self.assertAlmostEqual(messages[1]['y'], y2, places=4) self.assertEqual(messages[1]['direction'], directions['north']) def test_eating_own_dog_food(self): msg = self.get_fake_msg(y=1.0) packed = msg.pack() unpacked = FakeMessage.from_packed(packed) self.assertEqual(msg, unpacked)