def step_create_new_battle(context): """Create a battle. This step will create a battle on our Redis using the generated battle_id from the current context. ... Parameters ---------- context : behave context The behave context that is being used in this feature test. """ if not hasattr(context, 'board_size'): context.board_size = 50 for request in context.requests: battle_id = request['battleId'] if not battle_id: continue battle = dict() battle.setdefault('id', battle_id) battle.setdefault('board_size', context.board_size) model = BattleSchema() model.dumps(battle) assert redis.instance.get(battle_id)
def step_close_dinossaur(context): """Add a dinossaur close to the robot. This step will add a dinossaur to the battle close to the robot on the current request. ... Parameters ---------- context : behave context The behave context that is being used in this feature test. """ battle_model = BattleSchema() dino_model = DinossaurSchema() ops = (add, sub) for request in context.requests: battle_id = request.get('battleId') robot_id = request.get('robot') battle = battle_model.get_battle(battle_id) robot_position = battle.get('entities').get(robot_id).get('position') position = [random.choice(ops)(robot_position[i], 1) for i in range(2)] dino = dict() dino.setdefault('battle_id', battle_id) dino.setdefault('position', tuple(position)) dino_model.load(dino) board_with_dino = battle_model.get_battle(battle_id).get('board') assert board_with_dino.get('state')[position[0] - 1][position[1] - 1]
def step_block_robot_move(context): """Block the robot move. This step will add two dinossaurs. One in front of the robot and other one in its backwards. ... Parameters ---------- context : behave context The behave context that is being used in this feature test. """ battle_model = BattleSchema() dino_model = DinossaurSchema() ops = (add, sub) for request in context.requests: battle_id = request.get('battleId') robot_id = request.get('robot') battle = battle_model.get_battle(battle_id) robot_data = battle.get('entities').get(robot_id) robot_position = robot_data.get('position') robot_direction = robot_data.get('direction') to_reverse = dict() to_reverse.setdefault('north', False) to_reverse.setdefault('south', False) to_reverse.setdefault('east', True) to_reverse.setdefault('west', True) is_reversed = to_reverse.get(robot_direction) def _calculate_position(pos, i, reversed): return (ops[i](pos[i], 1), pos[i]) if not reversed else (pos[i], ops[i](pos[i], 1)) pos = [ _calculate_position(robot_position, i, is_reversed) for i in range(2) ] dino_one = dict() dino_one.setdefault('battle_id', battle_id) dino_one.setdefault('position', pos[0]) dino_two = dict() dino_two.setdefault('battle_id', battle_id) dino_two.setdefault('position', pos[1]) dino_model.load(dino_one) dino_model.load(dino_two) board_with_dino = battle_model.get_battle(battle_id).get('board') assert board_with_dino.get('state')[pos[0][0] - 1][pos[0][1] - 1] assert board_with_dino.get('state')[pos[1][0] - 1][pos[1][1] - 1]
def route_state(): battle_id = request.args.get('battleId') if not battle_id: abort(404) battle_model = BattleSchema() battle = battle_model.get_battle(battle_id=battle_id) if not battle: abort(404) default_title = current_app.config.get('BATTLE_STATUS_TITLE_DEFAULT') page_title = default_title.format(battle_id) board = battle.get('board').get('state') entities = battle.get('entities') return render_template('state.html', title=page_title, battle_id=battle_id, board=board, entities=entities)
def step_create_new_non_existing_battle(context): """Create an untracked battle. This step will create a battle on our Redis using an untracked battleId in order to create a battle that you can't interact with ... Parameters ---------- context : behave context The behave context that is being used in this feature test. """ battle = dict() battle.setdefault('id', 9999) battle.setdefault('board_size', context.board_size) model = BattleSchema() model.dumps(battle) assert redis.instance.get(9999)
def step_check_dino_was_destroyed(context): """Check if all dinos were destroyed. This step will check if every dino has been destroyed after an attack from our robots. ... Parameters ---------- context : behave context The behave context that is being used in this feature test. """ battle_model = BattleSchema() for request in context.requests: battle_id = request.get('battleId') battle = battle_model.get_battle(battle_id) entities = battle.get('entities').keys() existing_dinos = [dino for dino in entities if dino[:2] == 'D-'] assert not existing_dinos
def command_robot(battle_id, robot_id, action): """Command a specific robot. This handler will command an specific robot, at a specific battle, to do some given action. It must guarantee that the action is also valid and return any errors if it is not. ... Parameters ---------- battle_id : int The ID of your current battle. robot_id : string The ID of the robot that will receive the action. action: string An action that can be: turn-left, turn-right, move-forward, move-backwards or attack. Returns ------- errors : dict A dict containing every error that happened during the robot action (if any). message : string A message to the user about the action of that robot. """ def _default_error(msg): return msg, None battle_model = BattleSchema() battle_state_original = battle_model.get_battle(battle_id) if not battle_state_original: return _default_error('This battle does not exist') entities = battle_state_original.get('entities') selected_robot = entities.get(robot_id) if not selected_robot: return _default_error('This robot does not exist') if action in constants.ACTIONS_TURNED: robot_model = RobotSchema() previous_direction = selected_robot.get('direction') new_robot_direction = robot_model.change_direction( previous_direction, action) new_battle_state = deepcopy(battle_state_original) robot = new_battle_state.get('entities').get(robot_id) new_direction = dict() new_direction.setdefault('direction', new_robot_direction) robot.update(new_direction) battle_model.update_battle(battle_id, new_battle_state) if action in constants.ACTIONS_MOVED: new_battle_state = battle_model.robot_move(battle_state_original, robot_id, action) if not new_battle_state: return _default_error('There is another entity there') battle_model.update_battle(battle_id, new_battle_state) if action == 'attack': new_battle_state = battle_model.robot_attack(battle_state_original, robot_id) battle_model.update_battle(battle_id, new_battle_state) return None, 'Robot commanded'