def EntityGroupChanged(self, controller, event, callback): entity_service = EntityService_Stub(channels.entity_channel) browserquest_stub = BrowserQuest_Stub(channels.browserquest_channel) world_service = WorldService_Stub(channels.world_channel) entity = Entity() entity.id = event.entity_id entity = entity_service.GetEntity(controller, entity) list_request = ListRequest() id_list = world_service.GetRelevantEntityList(controller, entity) list_request.entity_ids.extend(id_list.ids) controller.player_ids = [entity.id] browserquest_stub.list(controller, list_request) spawn = SpawnRequest() spawn.entity_id = entity.id spawn.kind = entity.kind spawn.x = entity.x spawn.y = entity.y spawn.name = entity.name spawn.orientation = 1 spawn.armor = entity.armor spawn.weapon = entity.weapon id_list = world_service.GetAdjacentPlayerList(controller, entity) controller.player_ids = id_list.ids browserquest_stub.spawn(controller, spawn) destroy = DestroyRequest() destroy.entity_id = entity.id id_list = world_service.GetRecentlyLeftGroupPlayerList(controller, entity) controller.player_ids = id_list.ids browserquest_stub.destroy(controller, destroy)
def Zone(self, controller, request, callback): # service entity_service = EntityService_Stub(channels.entity_channel) event_service = EventService_Stub(channels.event_channel) # model entity = Entity() entity.id = request.entity_id entity = entity_service.GetEntity(controller, entity) world = self.worlds.get(entity.world_id, None) # event event_controller = Controller() entity_group_changed_event = EntityGroupChangedEvent() # pre entity_group_changed_event.entity_id = entity.id entity_group_changed_event.pre_group_id = entity.group_id # has_changed_groups = self.handle_entity_group_membership(world, entity) entity_service.UpdateEntity(controller, entity) if has_changed_groups: # post entity_group_changed_event.group_id = entity.group_id # broadcast event event_service.EntityGroupChanged(event_controller, entity_group_changed_event) response = ZoneResponse() return response
def Teleport(self, controller, request, callback): # service entity_service = EntityService_Stub(channels.entity_channel) event_service = EventService_Stub(channels.event_channel) # model entity = Entity() entity.id = request.entity_id entity = entity_service.GetEntity(controller, entity) world = self.worlds[entity.world_id] # event event_controller = Controller() entity_group_changed_event = EntityGroupChangedEvent() # pre entity_group_changed_event.entity_id = entity.id entity_group_changed_event.pre_group_id = entity.group_id # entity.x = request.x entity.y = request.y self.handle_entity_group_membership(world, entity) entity_service.UpdateEntity(controller, entity) # post entity_group_changed_event.group_id = entity.group_id # broadcast event event_service.EntityGroupChanged(event_controller, entity_group_changed_event) response = TeleportResponse() return response
def Move(self, controller, request, callback): # service entity_service = EntityService_Stub(channels.entity_channel) event_service = EventService_Stub(channels.event_channel) # model entity = Entity() entity.id = request.entity_id entity = entity_service.GetEntity(controller, entity) # event entity_move_event = EntityMoveEvent() # pre event_controller = Controller() entity_move_event.entity_id = entity.id entity_move_event.pre_x = entity.x entity_move_event.pre_y = entity.y # entity.x = request.x entity.y = request.y entity_service.UpdateEntity(controller, entity) # post entity_move_event.x = entity.x entity_move_event.y = entity.y # broadcast event event_service.EntityMove(event_controller, entity_move_event) response = MoveResponse() return response
def PlayerJoin(self, controller, request, callback): # service list entity_service = EntityService_Stub(channels.entity_channel) event_service = EventService_Stub(channels.event_channel) # model player = Player() player.id = request.player_id player = entity_service.GetPlayer(controller, player) player.kind = 1 # Wariior checkpoint = self.world_map.checkpoints.get(player.last_check_point, None) world = self.get_random_world() entity_service.UpdateEntity(controller, player) # event list event_controller = Controller() entity_join_world_event = EntityJoinWorldEvent() population_update_event = PopulationUpdateEvent() entity_group_changed_event = EntityGroupChangedEvent() # pre entity_join_world_event.entity_id = player.id entity_group_changed_event.entity_id = player.id entity_group_changed_event.pre_group_id = player.group_id population_update_event.world_id = world.id population_update_event.pre_population = len(world.player_ids) # if not checkpoint: random_start = randint(0, len(self.world_map.starting_areas) - 1) checkpoint_id = self.world_map.starting_areas[random_start] checkpoint = self.world_map.checkpoints[checkpoint_id] player.world_id = world.id player.x = checkpoint.x player.y = checkpoint.y player.hitpoints = 100 world.player_ids.append(player.id) self.handle_entity_group_membership(world, player) entity_service.UpdateEntity(controller, player) # post entity_join_world_event.world_id = player.world_id entity_group_changed_event.group_id = player.group_id population_update_event.population = len(world.player_ids) # broadcast event event_service.EntityJoinWorld(event_controller, entity_join_world_event) event_service.PopulationUpdate(event_controller, population_update_event) event_service.EntityGroupChanged(event_controller, entity_group_changed_event) response = PlayerJoinResponse() response.player_id = player.id response.x = player.x response.y = player.y response.hitpoints = player.hitpoints return response
def GetRelevantEntityList(self, controller, entity, callback): entity_service = EntityService_Stub(channels.entity_channel) entity = entity_service.GetEntity(controller, entity) id_list = IDList() group = self.groups.get('%s-%s' % (entity.world_id, entity.group_id)) id_list.ids.extend(group.entity_ids) if entity.id in id_list.ids: id_list.ids.remove(entity.id) return id_list
def Check(self, controller, request, callback): entity_service = EntityService_Stub(channels.entity_channel) event_service = EventService_Stub(channels.event_channel) entity = Entity() entity.id = request.entity_id entity = entity_service.GetEntity(controller, entity) entity.last_check_point = request.checkpoint entity_service.UpdateEntity(controller, entity) response = CheckResponse() return response
def GetAdjacentPlayerList(self, controller, entity, callback): entity_service = EntityService_Stub(channels.entity_channel) entity = entity_service.GetEntity(controller, entity) id_list = IDList() for position in self.world_map.get_adjacent_group_positions(entity.group_id): group_index = "%s-%s-%s" % (entity.world_id, position['x'], position['y']) group = self.groups.get(group_index, None) if group: id_list.ids.extend(group.player_ids) if entity.id in id_list.ids: id_list.ids.remove(entity.id) return id_list
def Kill(self, controller, event, callback): world_service = WorldService_Stub(channels.world_channel) entity_service = EntityService_Stub(channels.entity_channel) browserquest_stub = BrowserQuest_Stub(channels.browserquest_channel) killed_entity = Entity() killed_entity.id = event.killed_id killed_entity = entity_service.GetEntity(controller, killed_entity) kill_request = KillRequest() kill_request.mob_kind = killed_entity.kind controller.player_ids = [event.killer_id] browserquest_stub.kill(controller, kill_request) despawn_request = DespawnRequest() despawn_request.entity_id = event.killed_id id_list = world_service.GetAdjacentPlayerList(controller, killed_entity) controller.player_ids = id_list.ids browserquest_stub.despawn(controller, despawn_request)
def Damage(self, controller, request, callback): entity_service = EntityService_Stub(channels.entity_channel) event_service = EventService_Stub(channels.event_channel) attack = Entity() attack.id = request.attack_id target = Entity() target.id = request.target_id attack = entity_service.GetEntity(controller, attack) target = entity_service.GetEntity(controller, target) event_controller = Controller() damage_event = DamageEvent() kill_event = KillEvent() damage_event.attack_id = attack.id damage_event.attack_weapon = attack.weapon damage_event.target_armor = target.armor damage_event.pre_target_hitpoints = target.hitpoints kill_event.killer_id = attack.id kill_event.killed_id = target.id damage = randint(0, 3) dealt = attack.weapon * randint(5, 10) absorbed = target.armor * randint(1, 3) if dealt - absorbed > 0: damage = dealt - absorbed target.hitpoints -= damage if target.hitpoints <= 0: target.is_dead = True entity_service.UpdateEntity(controller, target) damage_event.target_hitpoints = target.hitpoints event_service.Damage(event_controller, damage_event) if target.hitpoints <= 0: event_service.Kill(event_controller, kill_event) response = DamageResponse() response.attack_id = request.attack_id response.target_id = request.target_id response.damage = damage return response
def spawn_mobs(self, world, mob_area): entity_service = EntityService_Stub(channels.entity_channel) controller = Controller() mob_property = self.mob_properties[mob_area.type] for i in xrange(mob_area.nb): position = self.get_random_position_inside_area(mob_area) mob = Mob() mob.x = position['x'] mob.y = position['y'] mob.kind = self.get_entity_number_by_name(mob_area.type) mob.area_id = mob_area.id mob.world_id = mob_area.world_id mob.hitpoints = mob_property['hp'] mob.weapon = mob_property['weapon'] mob.armor = mob_property['armor'] mob_area.entity_ids.append(mob.id) mob = entity_service.CreateMob(controller, mob) world.entity_ids.append(mob.id) self.handle_entity_group_membership(world, mob)
def who(self, controller, request, callback): entity_service = EntityService_Stub(channels.entity_channel) browserquest_stub = BrowserQuest_Stub(channels.browserquest_channel) for entity_id in request.entity_ids: entity = Entity() entity.id = entity_id entity = entity_service.GetEntity(controller, entity) if entity and not getattr(entity, 'is_dead', False): spawn = SpawnRequest() spawn.entity_id = entity.id spawn.kind = entity.kind spawn.x = entity.x spawn.y = entity.y spawn.orientation = entity.orientation spawn.armor = entity.armor spawn.weapon = entity.weapon spawn.name = getattr(entity, 'name', '') controller.player_ids = [controller.environ['player_id']] browserquest_stub.spawn(controller, spawn)
def Attack(self, controller, request, callback): entity_service = EntityService_Stub(channels.entity_channel) event_service = EventService_Stub(channels.event_channel) attack = Entity() attack.id = request.attack_id target = Entity() target.id = request.target_id attack = entity_service.GetEntity(controller, attack) target = entity_service.GetEntity(controller, target) event_controller = Controller() attack_event = AttackEvent() attack_event.attack_id = request.attack_id attack_event.target_id = request.target_id event_service.Attack(event_controller, attack_event) response = AttackResponse() response.attack_id = request.attack_id response.target_id = request.target_id return response
def hello(self, controller, request, callback): world_service = WorldService_Stub(channels.world_channel) event_service = EventService_Stub(channels.event_channel) browserquest_stub = BrowserQuest_Stub(channels.browserquest_channel) entity_service = EntityService_Stub(channels.entity_channel) _player = Player() _player.name = request.name player = entity_service.GetPlayer(controller, _player) if not player: player = entity_service.CreatePlayer(controller, _player) player.armor = 1 player.weapon = 1 entity_service.UpdateEntity(controller, player) # regist connection connections = channels.browserquest_channel.connections connections[player.id] = controller.connection controller.environ['player_id'] = player.id event_controller = Controller() player_logined_event = PlayerLoginedEvent() player_logined_event.player_id = player.id event_service.PlayerLogined(event_controller, player_logined_event) player_join_request = world_service_pb2.PlayerJoinRequest() player_join_request.player_id = player.id player_join_response = world_service.PlayerJoin( controller, player_join_request) welcome = WelcomeRequest() welcome.player_id = player.id welcome.name = request.name welcome.x = player_join_response.x welcome.y = player_join_response.y welcome.hitpoints = player_join_response.hitpoints controller.player_ids = [player_join_response.player_id] browserquest_stub.welcome(controller, welcome)
def hurt(self, controller, request, callback): world_service = WorldService_Stub(channels.world_channel) browserquest_stub = BrowserQuest_Stub(channels.browserquest_channel) entity_service = EntityService_Stub(channels.entity_channel) damage_request = world_service_pb2.DamageRequest() damage_request.attack_id = request.mob_id damage_request.target_id = controller.environ['player_id'] damage_response = world_service.Damage(controller, damage_request) player = Player() player.id = controller.environ['player_id'] playerentity_service.GetPlayer(controller, player) health_request = HealthRequest() health_request.hitpoints = player.hitpoints controller.player_ids = [player.id] browserquest_stub.health(controller, health_request)
def EntityJoinWorld(self, controller, event, callback): entity_service = EntityService_Stub(channels.entity_channel) entity = Entity() entity.id = event.entity_id entity = entity_service.GetEntity(controller, entity)
def PlayerLogined(self, controller, event, callback): entity_service = EntityService_Stub(channels.entity_channel) player = Player() player.id = event.player_id player = entity_service.GetPlayer(controller, player)