Esempio n. 1
0
async def browse_missions(request):
    pretty = 'pretty' in request.query
    root_dir = request.app['dedicated_server'].missions_dir

    try:
        relative_dir = request.match_info.get('dir_path', '')
        absolute_dir = (root_dir / relative_dir).resolve()
    except Exception:
        LOG.exception("HTTP failed to browse missions: incorrect input data")
        return RESTBadRequest(
            detail="incorrect input data",
            pretty=pretty,
        )

    try:
        dirs = []
        files = []

        result = {'dirs': dirs, 'files': files}

        for node in absolute_dir.iterdir():
            st_mode = node.stat().st_mode

            if S_ISDIR(st_mode):
                dirs.append(node.name)
            elif (S_ISREG(st_mode)
                  and node.suffix.lower() in {'.mis', '.properties'}):
                files.append(node.name)

        dirs.sort()
        files.sort()

    except Exception:
        LOG.exception("HTTP failed to browse missions")
        return RESTInternalServerError(
            detail="failed to browse missions",
            pretty=pretty,
        )
    else:
        return RESTSuccess(payload=result, pretty=pretty)
Esempio n. 2
0
async def kick_all_humans(request):
    pretty = 'pretty' in request.query
    timeout = request.query.get('timeout')

    try:
        if timeout is not None:
            timeout = float(timeout)
    except Exception:
        LOG.exception("HTTP failed to kick all humans: incorrect input data")
        return RESTBadRequest(
            detail="incorrect input data",
            pretty=pretty,
        )

    try:
        await request.app['console_client'].kick_all_humans(timeout)
    except Exception:
        LOG.exception("HTTP failed to kick all humans")
        return RESTInternalServerError(
            detail="failed to kick all humans",
            pretty=pretty,
        )
    else:
        return RESTSuccess(pretty=pretty)
Esempio n. 3
0
async def get_humans_count(request):
    pretty = 'pretty' in request.query
    timeout = request.query.get('timeout')

    try:
        if timeout is not None:
            timeout = float(timeout)
    except Exception:
        LOG.exception("HTTP failed to get humans count: incorrect input data")
        return RESTBadRequest(
            detail="incorrect input data",
            pretty=pretty,
        )

    try:
        result = await request.app['console_client'].get_humans_count(timeout)
    except Exception:
        LOG.exception("HTTP failed to get humans count")
        return RESTInternalServerError(
            detail="failed to get humans count",
            pretty=pretty,
        )
    else:
        return RESTSuccess(payload=result, pretty=pretty)
Esempio n. 4
0
async def chat_to_belligerent(request):
    pretty = 'pretty' in request.query
    timeout = request.query.get('timeout')

    try:
        if timeout is not None:
            timeout = float(timeout)

        addressee = request.match_info['addressee']
        addressee = int(addressee)
        addressee = Belligerents.get_by_value(addressee)

        body = await request.json(loads=json.loads)
        message = body['message']
    except Exception:
        LOG.exception(
            "HTTP failed to chat to belligerent: incorrect input data")
        return RESTBadRequest(
            detail="incorrect input data",
            pretty=pretty,
        )

    try:
        await request.app['console_client'].chat_to_belligerent(
            message=message,
            addressee=addressee,
            timeout=timeout,
        )
    except Exception:
        LOG.exception("HTTP failed to chat to belligerent")
        return RESTInternalServerError(
            detail="failed to chat to belligerent",
            pretty=pretty,
        )
    else:
        return RESTSuccess(pretty=pretty)
Esempio n. 5
0
async def get_health(request):
    pretty = 'pretty' in request.query
    payload = {'status': 'alive'}

    return RESTSuccess(payload=payload, pretty=pretty)