コード例 #1
0
ファイル: auth.py プロジェクト: Nukesor/spacesurvival
def login(args):
    """Endpoint for login.

    Check if we can login with the credentials. We try to get the
    user by searching email and nickname for the given identifier.
    """
    identifier = args['identifier']
    password = args['password']

    # Check if the user exists
    user = db.session.query(User) \
        .filter(or_(User.nickname == identifier, User.email == identifier)) \
        .one_or_none()

    if user is None:
        return bad_request('Unknown credentials or wrong password.')

    # Validate password
    valid_password = user.verify_password(password)
    if not valid_password:
        return bad_request('Unknown credentials or wrong password.')

    if not user.has_valid_auth_token:
        user.generate_auth_token()
    user.last_login_at = datetime.utcnow()
    db.session.add(user)
    db.session.commit()

    schema = UserSchema()
    return jsonify(schema.dump(user).data)
コード例 #2
0
ファイル: handlers.py プロジェクト: Nukesor/spacesurvival
    def default_login_required():
        # If this is an error or something else without a proper endpoint, just
        # return straight away.
        if not request.endpoint:
            return

        view = app.view_functions[request.endpoint]

        if getattr(view, 'login_exempt', False):
            return

        token_header = request.headers.get('Authorization')
        if not token_header:
            return bad_request("'Authorization' not found in headers.")

        token = token_header
        user = User.get_user_from_login_token(token)

        if not user:
            return bad_request("Invalid login token.")

        # Require user to re-login if the last action is too long ago.
        if not user.has_valid_auth_token:
            return unauthorized("Auth token too old, please log in again.")

        # At this point the user is considered successfully authenticated.
        user.last_action = datetime.utcnow()
        db.session.add(user)
        db.session.commit()
        g.current_user = db.session.query(User).get(user.id)
コード例 #3
0
ファイル: researches.py プロジェクト: Nukesor/spacesurvival
def begin_pod_research(args, pod_id):
    """Begin a new pod research."""
    from server.data.data import research_data

    pod = db.session.query(Pod).get(pod_id)
    if pod.user_id != g.current_user.id:
        return bad_request(f"Pod doesn't belong to current user.")

    # Check for valid research type
    research_type = args['type']
    if research_type not in ResearchTypes.__members__:
        return bad_request('Unknown research type "{research_type}"')

    research = db.session.query(Research) \
        .filter(Research.pod_id == g.current_user.pod.id) \
        .filter(Research.type == research_type) \
        .first()

    next_level = 0
    if research:
        next_level = research.level + 1
        highest_queue_entry = db.session.query(QueueEntry) \
            .filter(QueueEntry.research == research) \
            .join(Queue) \
            .filter(Queue.pod == pod) \
            .order_by(QueueEntry.level.desc()) \
            .first()
        if highest_queue_entry:
            next_level = highest_queue_entry.level + 1

    # Ensure we didn't reach max level.
    if next_level >= len(research_data[research_type]['levels']):
        return bad_request("Max level reached.")
    research_level = research_data[research_type]['levels'][next_level]

    # Check if we have enough resources
    requirements = research_level['resources']
    enough, missing = pod.enough_resources(requirements)
    if not enough:
        return bad_request(f'Not enough resources: {missing}')

    # Subtract the resources from the pod and create a queue entry.
    pod.subtract_resources(requirements)

    # Create a new research if we don't have it yet.
    if not research:
        research = Research(research_type, pod, 0)

    # Create a new queue entry.
    queue_entry = QueueEntry(pod.queue,
                             next_level,
                             research_level['duration'],
                             research=research)

    pod.queue.next_entry()
    db.session.add(queue_entry)
    db.session.add(research)
    db.session.commit()

    return ok()
コード例 #4
0
def new_pod_module(args, pod_id):
    """Place a new module on the pod grid."""
    from server.data.data import module_data

    pod = db.session.query(Pod).get(pod_id)
    if pod.user_id != g.current_user.id:
        return bad_request(f"Pod doesn't belong to current user.")

    # Check for valid module type
    module_type = args['module_type']
    stationary = args.get('stationary')
    x_pos = args.get('x_pos')
    y_pos = args.get('y_pos')
    if module_type not in ModuleTypes.__members__:
        return bad_request(f'Unknown Module type: {module_type}')

    # Check if we already have a module with this type
    # at the specified position.
    if stationary:
        existing_module = db.session.query(Module) \
            .filter(Module.pod_id == pod.id) \
            .filter(Module.stationary.is_(True)) \
            .filter(Module.type == module_type) \
            .first()

    else:
        existing_module = db.session.query(Module) \
            .filter(Module.pod_id == pod.id) \
            .filter(Module.x_pos == x_pos) \
            .filter(Module.y_pos == y_pos) \
            .first()

    if existing_module:
        return bad_request('There already is a module at this position')

    # Check if we have enough resources
    module_level = module_data[module_type]['levels'][0]
    requirements = module_level['resources']
    enough, missing = pod.enough_resources(requirements)
    if not enough:
        return bad_request(f'Not enough resources: {missing}')

    # Subtract the resources from the pod and create a queue entry.
    pod.subtract_resources(requirements)
    module = Module(module_type, pod, 0, stationary, x_pos, y_pos)
    queue_entry = QueueEntry(pod.queue,
                             0,
                             module_level['duration'],
                             module=module)

    pod.queue.next_entry()
    db.session.add(queue_entry)
    db.session.add(module)
    db.session.commit()

    return created()
コード例 #5
0
def upgrade_pod_module(pod_id, module_id):
    """Update a module on the pod grid."""
    from server.data.data import module_data

    pod = db.session.query(Pod).get(pod_id)
    if pod.user_id != g.current_user.id:
        return bad_request(f"Pod doesn't belong to current user.")

    # Check if we already have a module with this type
    # at the specified position.
    module = db.session.query(Module).get(module_id)

    if not module:
        return bad_request('No module with this id')

    next_level = module.level + 1
    highest_queue_entry = db.session.query(QueueEntry) \
        .filter(QueueEntry.module == module) \
        .join(Queue) \
        .filter(Queue.pod == pod) \
        .order_by(QueueEntry.level.desc()) \
        .first()
    if highest_queue_entry:
        next_level = highest_queue_entry.level + 1

    # Ensure we didn't reach max level.
    if next_level >= len(module_data[module.type]['levels']):
        return bad_request("Max level reached.")
    module_level = module_data[module.type]['levels'][next_level]

    # Ensure we have enough resources
    requirements = module_level['resources']
    enough, missing = pod.enough_resources(requirements)
    if not enough:
        return bad_request(f'Not enough resources: {missing}')

    # Subtract the resources from the pod and create a queue entry.
    pod.subtract_resources(requirements)
    queue_entry = QueueEntry(
        pod.queue,
        next_level,
        module_level['duration'],
        module=module,
    )

    pod.queue.next_entry()
    db.session.add(queue_entry)
    db.session.add(module)
    db.session.commit()

    return ok()
コード例 #6
0
ファイル: handlers.py プロジェクト: Nukesor/spacesurvival
    def require_json_input():
        """Require JSON input.

        If the request's method is either 'POST' or 'PUT', require the
        'Content-Type' to be JSON.
        """
        if request.method in ['POST', 'PUT']:
            if request.headers['Content-Type'] != 'application/json':
                return bad_request("'Content-Type' must be 'application/json'.")
コード例 #7
0
ファイル: resources.py プロジェクト: Nukesor/spacesurvival
def pod_resources(pod_id):
    """Get the resources of a specified pod."""
    pod = db.session.query(Pod).get(pod_id)
    if pod.user_id != g.current_user.id:
        return bad_request(f"Pod doesn't belong to current user.")

    schema = ResourceSchema()

    return ok(schema.dump(pod.resources, many=True).data)
コード例 #8
0
def remove_queue_entry(pod_id, entry_id):
    """Remove a specific queue entry."""
    pod = db.session.query(Pod).get(pod_id)
    if pod.user_id != g.current_user.id:
        return bad_request("Pod doesn't belong to current user.")
    """Get the queue of the specified pod."""
    queue_entry = db.session.query(QueueEntry) \
        .join(Queue) \
        .filter(Queue.pod == pod) \
        .filter(QueueEntry.id == entry_id) \
        .one_or_none()

    if queue_entry is None:
        return bad_request("Queue entry doesn't exist")

    db.session.delete(queue_entry)
    pod.queue.next_entry()
    db.session.commit()

    return ok()
コード例 #9
0
ファイル: researches.py プロジェクト: Nukesor/spacesurvival
def get_pod_research(pod_id):
    """Send the pod meta data combined with your researches."""
    pod = db.session.query(Pod).get(pod_id)
    if pod.user_id != g.current_user.id:
        return bad_request(f"Pod doesn't belong to current user.")

    researches = db.session.query(Research) \
        .filter(Research.pod == pod) \
        .filter(Research.researched.is_(True)) \
        .all()
    schema = ResearchSchema()

    return ok(schema.dump(researches, many=True).data)
コード例 #10
0
ファイル: handlers.py プロジェクト: Nukesor/spacesurvival
 def handle_bad_request(e):
     return bad_request(e.name)