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 Connection(EventsMixins): def __str__(self): return self.meta['username'] def __init__(self, world, cache): super(Connection, self).__init__() self.session_id = uuid.UUID(bytes=urandom(16)) self.world = world self.cache = cache self.known = dict() self.last_world_index = 0 self.last_frame_index = 0 self.archer = Archer(self.world, player=self) self.meta = { "id": self.archer.id, "username": "******", "gender": "male", "slots": {}, "kills": 0, "deaths": 0, "score": 0, "budget": settings.initial_budget, "attributes": self.archer.attributes } self.archer.interface = self logging.info("New connection %s" % self.session_id) self.world.on('destroy_object', self.on_destroy) self.world.on('step', self.on_step) self.on('useraction', self.on_user_action) self.on('disconnect', self.on_disconnect) self.on('usermsg', self.on_user_message) self.on('kill', self.on_kill) self.on('die', self.on_die) self.on('mob', self.on_mob) self.on('pickup', self.on_pickup) self.on('spawn', self.on_spawn) def on_spawn(self): self.meta['budget'] = settings.initial_budget self.trigger('meta', self.meta) def on_kill(self, prey): self.meta['kills'] = self.meta['kills'] + 1 self.meta['score'] = self.meta['score'] + settings.score['kill'] logging.info("%s fragged %s" % (self.meta['username'], prey.meta['username'])) self.trigger('meta', self.meta) def on_die(self, killer=None): self.meta['deaths'] = self.meta['deaths'] + 1 logging.info("%s died by the hand of %s" % (self.meta['username'], killer or "[unknown]")) self.trigger('meta', self.meta) def on_mob(self): self.meta['score'] = self.meta['score'] + settings.score['mob'] self.trigger('meta', self.meta) def on_pickup(self, bonus): self.meta['budget'] = self.meta["budget"] + bonus.value self.trigger('meta', self.meta) def on_user_message(self, meta): changed = False if ('username' in meta): new_username = meta['username'][0:10] if (new_username != self.meta['username']): self.meta['username'] = new_username logging.info("%s is now known as %s" % (self.session_id, self.meta['username'])) changed = True if ('gender' in meta and meta['gender'] != self.meta['gender']): self.meta['gender'] = meta['gender'] changed = True if ('slots' in meta and meta['slots'] != self.meta['slots']): if (verify_slots(meta['slots'], self.meta['budget'])): self.meta['slots'] = meta['slots'] changed = True else: logging.info( "%s provided incorrect meta msg (%s). Discarding" % (self.meta['username'], meta['slots'])) if (changed): self.archer.update_attributes() # self.trigger('meta', self.meta) def on_disconnect(self): self.world.off('destroy_object', self.on_destroy) self.world.off('step', self.on_step) self.off() self.archer.destroy() def on_user_action(self, message): spawn_points = self.world.get_spawn_points() shuffle(spawn_points) logging.info(message) if (message['action'] == 'spawn'): if (self.archer.can_spawn()): if (verify_slots(self.meta['slots'], self.meta['budget'])): self.archer.spawn(spawn_points[0]) else: logging.info('denying %s spawn, incorrect slots %s' % (self.meta['username'], self.meta['slots'])) if (message['action'] == 'suicide'): self.archer.kill() if (message['action'] == 'stop'): self.archer.want_stop() if (message['action'] == 'move'): self.archer.want_move(message['direction']) if (message['action'] == 'attack'): self.archer.want_attack(message['direction']) def on_destroy(self, world_object): messages = list() if (hasattr(world_object, 'get_destroy_message')): msg = world_object.get_destroy_message() if (msg): messages.append(msg) self.trigger('remove', messages) def on_step(self, world): self.trigger('update', self.get_update()) self.trigger('frame', self.get_frame()) def get_update(self): messages = list() if (self.last_world_index != self.world.object_index.index): for index in range(self.last_world_index, self.world.object_index.index): index = index + 1 try: world_object = self.world.get_object_by_id(index) if (hasattr(world_object, 'get_update_message')): msg = world_object.get_update_message(recipient=self) if (msg): messages.append(msg) except KeyError: pass self.last_world_index = index return messages def get_frame(self): items = self.world.object_index.values() update = self.cache.get_frame_message_from_cache(self.world.step) if (update): return update update = list() for item in items: if (hasattr(item, 'get_frame_message')): data = item.get_frame_message() update.append(data) # if data and not(item in self.known.keys() and self.known[item] == data): # self.known[item] = data self.cache.cache_frame_messages(self.world.step, update) return update
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 Connection(EventsMixins): def __str__(self): return self.meta['username']; def __init__(self, world, cache): super(Connection, self).__init__() self.session_id = uuid.UUID(bytes=urandom(16)) self.world = world self.cache = cache self.known = dict() self.last_world_index = 0 self.last_frame_index = 0 self.archer = Archer(self.world, player=self) self.meta = { "id": self.archer.id, "username": "******", "gender": "male", "slots": {}, "kills": 0, "deaths": 0, "score": 0, "budget": settings.initial_budget, "attributes": self.archer.attributes } self.archer.interface = self logging.info("New connection %s" % self.session_id) self.world.on('destroy_object', self.on_destroy) self.world.on('step', self.on_step) self.on('useraction', self.on_user_action) self.on('disconnect', self.on_disconnect) self.on('usermsg', self.on_user_message) self.on('kill', self.on_kill) self.on('die', self.on_die) self.on('mob', self.on_mob) self.on('pickup', self.on_pickup) self.on('spawn', self.on_spawn) def on_spawn(self): self.meta['budget'] = settings.initial_budget self.trigger('meta', self.meta) def on_kill(self, prey): self.meta['kills'] = self.meta['kills'] + 1 self.meta['score'] = self.meta['score'] + settings.score['kill'] logging.info("%s fragged %s" % (self.meta['username'], prey.meta['username'])) self.trigger('meta', self.meta) def on_die(self, killer=None): self.meta['deaths'] = self.meta['deaths'] + 1 logging.info("%s died by the hand of %s" % (self.meta['username'], killer or "[unknown]")) self.trigger('meta', self.meta) def on_mob(self): self.meta['score'] = self.meta['score'] + settings.score['mob'] self.trigger('meta', self.meta) def on_pickup(self, bonus): self.meta['budget'] = self.meta["budget"] + bonus.value self.trigger('meta', self.meta) def on_user_message(self, meta): changed = False if('username' in meta): new_username = meta['username'][0:10] if(new_username != self.meta['username']): self.meta['username'] = new_username logging.info("%s is now known as %s" % (self.session_id, self.meta['username'])) changed = True if('gender' in meta and meta['gender'] != self.meta['gender']): self.meta['gender'] = meta['gender'] changed = True if('slots' in meta and meta['slots'] != self.meta['slots']): if(verify_slots(meta['slots'], self.meta['budget'])): self.meta['slots'] = meta['slots'] changed = True else: logging.info("%s provided incorrect meta msg (%s). Discarding" % (self.meta['username'], meta['slots'])) if(changed): self.archer.update_attributes() # self.trigger('meta', self.meta) def on_disconnect(self): self.world.off('destroy_object', self.on_destroy) self.world.off('step', self.on_step) self.off() self.archer.destroy() def on_user_action(self, message): spawn_points = self.world.get_spawn_points() shuffle(spawn_points) logging.info(message); if(message['action'] == 'spawn'): if(self.archer.can_spawn()): if(verify_slots(self.meta['slots'], self.meta['budget'])): self.archer.spawn(spawn_points[0]) else: logging.info('denying %s spawn, incorrect slots %s' % (self.meta['username'], self.meta['slots'])) if(message['action'] == 'suicide'): self.archer.kill(); if(message['action'] == 'stop'): self.archer.want_stop() if(message['action'] == 'move'): self.archer.want_move(message['direction']) if(message['action'] == 'attack'): self.archer.want_attack(message['direction']) def on_destroy(self, world_object): messages = list() if(hasattr(world_object, 'get_destroy_message')): msg = world_object.get_destroy_message() if(msg): messages.append(msg) self.trigger('remove', messages) def on_step(self, world): self.trigger('update', self.get_update()) self.trigger('frame', self.get_frame()) def get_update(self): messages = list() if(self.last_world_index != self.world.object_index.index): for index in range(self.last_world_index, self.world.object_index.index): index = index+1 try: world_object = self.world.get_object_by_id(index) if(hasattr(world_object, 'get_update_message')): msg = world_object.get_update_message(recipient=self) if(msg): messages.append(msg) except KeyError: pass self.last_world_index = index return messages def get_frame(self): items = self.world.object_index.values() update = self.cache.get_frame_message_from_cache(self.world.step) if(update): return update update = list() for item in items: if(hasattr(item, 'get_frame_message')): data = item.get_frame_message() update.append(data) # if data and not(item in self.known.keys() and self.known[item] == data): # self.known[item] = data self.cache.cache_frame_messages(self.world.step, update) return update