Beispiel #1
0
def plane_depart(plane_id):
    """
    Handle a plane departing

    :return: API Gateway dictionary response
    """
    player_id = utils.get_username()
    body = request.get_json(force=True)
    logger.info(
        f'Received PUT request from player: "{player_id}" '
        f'for path: "/v1/planes/{plane_id}/depart" with body: "{body}"')

    if not body.get('destination_city_id'):
        return make_response('destination_city_id is a required field', 400)

    success, result = utils.get_player_attributes(
        player_id=player_id, attributes_to_get=['cities', 'planes'])
    if not success:
        return make_response(result, 400)

    player_plane = result.get('planes', {}).get(plane_id)

    if not player_plane:
        return make_response('Invalid plane_id', 400)

    if player_plane.get('eta', 0) > 0:
        return make_response('Plane is currently in flight', 400)

    success, result = utils.depart_plane(player_id, plane_id, player_plane,
                                         body.get('destination_city_id'))
    if not success:
        return make_response(result, 400)

    return make_response(result, 200)
Beispiel #2
0
def create_player():
    """
    Create a player

    :return: API Gateway dictionary response
    """
    player_id = utils.get_username()
    logger.info(f'Received POST request from player: "{player_id}" '
                f'for path: "/v1/player"')

    created, message = utils.create_player(player_id, balance=100000)
    if not created:
        return make_response(message, 400)

    return make_response(message, 201)
Beispiel #3
0
def get_player_cities():
    """
    Get cities for a player

    :return: API Gateway dictionary response
    """
    player_id = utils.get_username()
    logger.info(f'Received GET request from player: "{player_id}" for path: "/v1/cities"')

    success, result = utils.get_player_attributes(player_id=player_id,
                                                  attributes_to_get=['cities'])
    if success:
        return make_response(result, 200)
    else:
        return make_response(result, 404)
Beispiel #4
0
def create_city():
    """
    Create a city for a player

    :return: API Gateway dictionary response
    """
    player_id = utils.get_username()
    body = request.get_json(force=True)
    logger.info(f'Received POST request from player: "{player_id}" '
                f'for path: "/v1/cities" with body: "{body}"')

    requested_city_id = body.get('city')

    success, result = utils.add_city_to_player(player_id=player_id, city_id=requested_city_id)
    if success:
        return make_response({
            'balance': int(result.get('balance'))
        }, 201)
    else:
        return make_response(result, 400)
Beispiel #5
0
def plane_arrive(plane_id):
    """
    Handle a plane arriving

    :return: API Gateway dictionary response
    """
    player_id = utils.get_username()
    logger.info(f'Received PUT request from player: "{player_id}" '
                f'for path: "/v1/planes/{plane_id}/unload"')
    success, result = utils.get_player_attributes(player_id=player_id,
                                                  attributes_to_get=['planes'])
    if not success:
        return make_response(result, 400)

    player_plane = result.get('planes', {}).get(plane_id)

    landed, result = utils.handle_plane_landed(player_id, plane_id,
                                               player_plane)
    if landed:
        return make_response(result, 200)

    return make_response(result, 400)
Beispiel #6
0
def get_player_city_jobs(city_id):
    """
    Get jobs for a player's city

    :return: API Gateway dictionary response
    """
    player_id = utils.get_username()
    logger.info(f'Received GET request from player: "{player_id}" '
                f'for path: "/v1/cities/{city_id}/jobs"')

    success, result = utils.get_player_attributes(player_id=player_id,
                                                  attributes_to_get=['cities'])
    if not success:
        return make_response(result, 400)

    player_cities = result.get('cities', {})
    if not player_cities or len(player_cities) < 2:
        return make_response('Player does not own enough cities', 400)

    player_city = player_cities.get(city_id)
    if not player_city:
        return make_response('Player does not own city', 400)

    if player_city.get('jobs_expire') > time.time():
        logging.info('Jobs have not expired, sending current jobs')
        return make_response({
            'jobs': player_city.get('jobs'),
            'jobs_expire': player_city.get('jobs_expire')
        }, 200)

    logging.info('Jobs have expired, generating new jobs')
    new_jobs, jobs_expire = utils.update_city_with_new_jobs(player_id, city_id, player_cities)

    return make_response({
        'jobs': new_jobs,
        'jobs_expire': jobs_expire
    }, 200)
Beispiel #7
0
def plane_load(plane_id):
    """
    Handle a plane loading

    :return: API Gateway dictionary response
    """
    player_id = utils.get_username()
    body = request.get_json(force=True)
    logger.info(f'Received PUT request from player: "{player_id}" '
                f'for path: "/v1/planes/{plane_id}/load" with body: "{body}"')

    success, result = utils.get_player_attributes(
        player_id=player_id, attributes_to_get=['cities', 'planes'])
    if not success:
        return make_response(result, 400)

    player_cities = result.get('cities', {})
    player_plane = result.get('planes', {}).get(plane_id)

    if not player_plane:
        return make_response('Invalid plane_id', 400)

    if player_plane.get('eta', 0) > 0:
        return make_response('Plane is currently in flight', 400)

    if not body.get('loaded_jobs'):
        return make_response('loaded_jobs is a required field', 400)

    # Handle new jobs
    job_ids = body.get('loaded_jobs')
    source_city = player_cities.get(player_plane.get('current_city_id'))

    if len(job_ids) > player_plane.get('capacity') - len(
            player_plane.get('loaded_jobs')):
        return make_response('Not enough capacity', 400)

    if time.time() > source_city.get('jobs_expire'):
        return make_response('Jobs have expired', 400)

    try:
        # Get job definitions from job ids
        jobs = {job_id: source_city['jobs'][job_id] for job_id in job_ids}
    except KeyError:
        return make_response('One or more job ids is invalid', 400)

    # Check if the jobs match the plane type (P or C)
    if any([
            job for job in jobs.values()
            if job.get('job_type') != player_plane.get('capacity_type')
    ]):
        return make_response(
            'Plane is incompatible with one or more job types', 400)

    # Combine new jobs with existing loaded jobs
    jobs = {**player_plane.get('loaded_jobs'), **jobs}

    updated, result = utils.add_jobs_to_plane(player_id, plane_id, jobs)
    if not updated:
        return make_response(result, 400)

    utils.remove_jobs_from_city(player_id, source_city.get('city_id'), job_ids)

    return make_response('Loaded plane successfully', 200)