def decide_action(self, world_map, avatar_state): try: action = self.avatar.next_turn(world_map, avatar_state) if not isinstance(action, Action): raise InvalidActionException(action) return action.serialise() except TypeError as e: print(e) return WaitAction().serialise()
def handle_turn(self, world_state, events): import random from simulation.action import WaitAction self.world_state = world_state if world_state.map_centred_at_me.get_cell(world_state.my_avatar.location).generates_score: return WaitAction() possible_directions = self.get_possible_directions() directions_to_emphasise = [d for d in possible_directions if self.is_towards(d, self.get_closest_score_location())] return MoveAction(random.choice(possible_directions + (directions_to_emphasise * 5)))
def handle_turn(self, state): try: next_action = self.avatar.handle_turn(state, self.events) except Exception as e: # TODO: tell user their program threw an exception during execution somehow... print('avatar threw exception during handle_turn:', e) traceback.print_exc() next_action = WaitAction() # Reset event log self.events = [] return next_action
def run_users_code(self, world_map, avatar_state, src_code): try: self._update_avatar(src_code) action = self.decide_action(world_map, avatar_state) self.print_logs() except InvalidActionException as e: self.print_logs() print(e) action = WaitAction().serialise() except Exception as e: self.print_logs() user_traceback = self.get_only_user_traceback() for trace in user_traceback: print(trace) LOGGER.info("Code failed to run") LOGGER.info(e) action = WaitAction().serialise() return action
def decide_action(self, serialised_action): try: action = self._construct_action(serialised_action) except (KeyError, ValueError) as err: LOGGER.error('Bad action data supplied: %s', err) except TypeError as err: LOGGER.error('Worker data not received: %s', err) else: self._action = action return True # Returning False here means that the action won't be registered on the cell # (although it will be registered on the avatar) self._action = WaitAction(self) return False
def decide_action(self, state_view): try: data = self._fetch_action(state_view) action = self._construct_action(data) except (KeyError, ValueError) as err: LOGGER.info('Bad action data supplied: %s', err) except requests.exceptions.ConnectionError: LOGGER.info('Could not connect to worker, probably not ready yet') except Exception: LOGGER.exception("Unknown error while fetching turn data") else: self._action = action return True self._action = WaitAction(self) return False
def handle_turn(self, avatar_state, world_state): from simulation.action import MoveAction from simulation import direction import random from simulation.action import WaitAction self.world_state = world_state self.avatar_state = avatar_state if world_state.get_cell(avatar_state.location).generates_score: return WaitAction() possible_directions = self.get_possible_directions() directions_to_emphasise = [ d for d in possible_directions if self.is_towards(d, self.get_closest_pickup_location()) ] return MoveAction( random.choice(possible_directions + (directions_to_emphasise * 5)))
def process_avatar_turn(self, world_map, avatar_state, src_code): avatar_updated = False with capture_output() as output: if self.code_updater.should_update(self.avatar, self.auto_update, src_code): self.avatar, avatar_updated = self.code_updater.update_avatar( src_code) if self.avatar: action = self.run_users_code(world_map, avatar_state, src_code) else: action = WaitAction().serialise() stdout, stderr = output output_log = stdout.getvalue() if not stderr.getvalue() == "": LOGGER.info(stderr.getvalue()) return { "action": action, "log": output_log, "avatar_updated": avatar_updated }
def handle_turn(self, world_map=None, avatar_state=None): return WaitAction(self)
def handle_turn(self, state_view): return WaitAction(self)