Ejemplo n.º 1
0
def run():
    ctl = Rcon(
        SERVER_INFO
    )

    config = AutoBroadcasts()

    while True: 
        msgs = config.get_messages()

        if not config.get_enabled() or not msgs:
            logger.debug("Auto broadcasts are disabled. Sleeping %s seconds", CHECK_INTERVAL)
            time.sleep(CHECK_INTERVAL)
            continue
     
        if config.get_randomize():
            logger.debug("Auto broadcasts. Radomizing")
            random.shuffle(msgs)

        for time_sec, msg in msgs:
            subs = {
                'nextmap': safe(ctl.get_next_map, "")(),
                'maprotation': ' -> '.join(safe(ctl.get_map_rotation, [])()),
                'servername': safe(ctl.get_name, "")(),
                'onlineadmins': ', '.join(safe(ctl.get_online_admins, [])())
            }
            formatted = msg.format(**subs)
            logger.debug("Broadcasting for %s seconds: %s", time_sec, formatted)
            ctl.set_broadcast(formatted) 
            time.sleep(int(time_sec)) 
        # Clear state in case next next iteration disables 
        ctl.set_broadcast('') 
Ejemplo n.º 2
0
def run():
    # avoid circular import
    from rcon.extended_commands import Rcon

    ctl = Rcon(SERVER_INFO)

    config = AutoBroadcasts()

    while True:
        msgs = config.get_messages()

        if not config.get_enabled() or not msgs:
            logger.debug("Auto broadcasts are disabled. Sleeping %s seconds",
                         CHECK_INTERVAL)
            time.sleep(CHECK_INTERVAL)
            continue

        if config.get_randomize():
            logger.debug("Auto broadcasts. Radomizing")
            random.shuffle(msgs)

        for time_sec, msg in msgs:
            formatted = format_message(ctl, msg)
            logger.debug("Broadcasting for %s seconds: %s", time_sec,
                         formatted)
            ctl.set_broadcast(formatted)
            time.sleep(int(time_sec))
Ejemplo n.º 3
0
def set_auto_broadcasts_config(request):
    failed = False
    res = None
    data = _get_data(request)
    broadcasts = AutoBroadcasts()
    config_keys = {
        "messages": broadcasts.set_messages,
        "randomized": broadcasts.set_randomize,
        "enabled": broadcasts.set_enabled,
    }
    try:
        for k, v in data.items():
            if k in config_keys:
                config_keys[k](v)
                audit(set_auto_broadcasts_config.__name__, request, {k: v})
    except InvalidConfigurationError as e:
        failed = True
        res = str(e)

    return JsonResponse({
        "result": res,
        "command": "set_auto_broadcasts_config",
        "arguments": data,
        "failed": failed,
    })
Ejemplo n.º 4
0
def get_auto_broadcasts_config(request):
    failed = False
    config = None
    
    try:
        broadcasts = AutoBroadcasts()
        config = {
            'messages': ["{} {}".format(m[0], m[1]) for m in broadcasts.get_messages()],
            'randomized': broadcasts.get_randomize(),
            'enabled': broadcasts.get_enabled()
        }
    except:
        logger.exception("Error fetch broadcasts config")
        failed = True

    return JsonResponse({
        "result": config,
        "command": "get_auto_broadcasts_config",
        "arguments": None,
        "failed": failed
    })
Ejemplo n.º 5
0
def safe(func, default=None):
    @wraps(func)
    def wrapper(*args, **kwargs):
        try:
            return func(*args, **kwargs)
        except:
            logger.exception("Unable to get data for broacasts")
            return default

    return wrapper


if __name__ == '__main__':
    ctl = Rcon(SERVER_INFO)

    config = AutoBroadcasts()

    while True:
        msgs = config.get_messages()

        if not config.get_enabled() or not msgs:
            logger.debug("Auto broadcasts are disabled. Sleeping %s seconds",
                         CHECK_INTERVAL)
            time.sleep(CHECK_INTERVAL)
            continue

        if config.get_randomize():
            logger.debug("Auto broadcasts. Radomizing")
            random.shuffle(msgs)

        for time_sec, msg in msgs: