Exemple #1
0
def change_nick(msg):
    log.info("subscriber nick change requested")
    # log.info(request.event)

    old_nick = subscribers[request.sid].nick
    color = subscribers[request.sid].color
    try:
        new_nick = msg['new']
    except KeyError:
        emit("log_message", {"data": "obey the API! (missing key 'new')"})
        return
    if old_nick == new_nick:
        emit("log_message",
             {"data": f'Your nick is already "{new_nick}"'})
        return
    else:
        other_nicks = {z.nick for z in subscribers.values()}.union(
            {z.nick for z in publishers.values()})
        if new_nick in other_nicks:
            emit("log_message",
                 {"data": f"Nick {new_nick} already exists"})
            return

    subscribers[request.sid].nick = new_nick

    emit('nick change', {'new': new_nick, 'old': old_nick, "color": color,
                         'complete': clean_subscribers()}, broadcast=False)
    emit("update subscriptions",
         {'complete': clean_subscribers(), 'new': new_nick,
          'old': old_nick}, broadcast=True, include_self=False)
Exemple #2
0
def update_nick(msg):
    log.info("publisher nick change requested")

    old_nick = publishers[request.sid].nick
    try:
        new_nick = msg['new']
    except KeyError:
        emit("log_message", {"data": "obey the API! (missing key 'new')"})
        return
    if old_nick == new_nick:
        emit("log_message", {"data": f"Your nick is already {new_nick}"})
        return
    else:
        subscribers_nicks = {z.nick for z in subscribers.values()}
        other_nicks = subscribers_nicks.union(
            {z.nick
             for z in publishers.values()})
        if new_nick in other_nicks:
            emit("log_message", {"data": f"Nick {new_nick} already exists"})
            return

    publishers[request.sid].nick = new_nick

    emit('log_message', {'data': f"nick updated to {new_nick}"},
         broadcast=False)
    emit('update publishers', {
        'data': clean_publishers(),
        'new': new_nick,
        'old': old_nick
    },
         namespace='/subscribe',
         broadcast=True)
Exemple #3
0
def connect_publisher():
    log.info(f"Connecting publisher {request.sid}")
    other_publisher_nicks = {z.nick for z in publishers.values()}
    other_nicks = other_publisher_nicks.union(
        {z.nick
         for z in subscribers.values()})

    if request.sid in publishers:
        raise RuntimeError(f"{request.sid} (publisher) Connected twice.")
    for i in range(10):
        x = str(random.randint(1, 10000))
        if x not in other_nicks:
            publishers[request.sid] = Publisher(sid=request.sid, nick=x)
            other_publisher_nicks.add(x)
            other_nicks.add(x)
            break
    else:
        log.info("Couldn't assign a nick, disconnecting the publisher...")
        emit("log_message", {
            "data": "Failed to assign you a nick",
            "fatal": True
        })
        return

    log.info(f"A publisher just connected (id={request.sid}, nick={x})"
             f" - total publishers: {len(publishers)}")
    emit('update publishers', {
        'data': clean_publishers(),
        'new': x,
        'old': None
    },
         namespace='/subscribe',
         broadcast=True)
    return True
Exemple #4
0
def connect_publisher():
    log.info("Connecting publisher {}".format(request.sid))
    other_publisher_nicks = {z.nick for z in publishers.values()}
    other_nicks = other_publisher_nicks.union(
        {z.nick for z in subscribers.values()})

    if request.sid in publishers:
        raise RuntimeError(
            "{} (publisher) Connected twice.".format(request.sid))
    for i in range(10):
        x = str(random.randint(1, 10000))
        if x not in other_nicks:
            publishers[request.sid] = Publisher(sid=request.sid, nick=x)
            other_publisher_nicks.add(x)
            other_nicks.add(x)
            break
    else:
        log.info("Couldn't assign a nick, disconnecting the publisher...")
        emit("log message",
             {"data": "Failed to assign you a nick", "fatal": True})
        return

    log.info(
        "A publisher just connected (id={}, nick={})".format(request.sid, x) +
        " - total publishers: {})".format(len(publishers)))
    emit('update publishers',
         {'data': clean_publishers(), 'new': x, 'old': None},
         namespace='/subscribe', broadcast=True)
    return True
Exemple #5
0
def update_nick(msg):
    log.info("publisher nick change requested")

    old_nick = publishers[request.sid].nick
    try:
        new_nick = msg['new']
    except KeyError:
        emit("log message", {"data": "obey the API! (missing key 'new')"})
        return
    if old_nick == new_nick:
        emit("log message",
             {"data": "Your nick is already {}".format(new_nick)})
        return
    else:
        subscribers_nicks = {z.nick for z in subscribers.values()}
        other_nicks = subscribers_nicks.union(
            {z.nick for z in publishers.values()})
        if new_nick in other_nicks:
            emit("log message",
                 {"data": "Nick {} already exists".format(new_nick)})
            return

    publishers[request.sid].nick = new_nick

    emit('log message', {'data': "nick updated to {}".format(new_nick)},
         broadcast=False)
    emit('update publishers',
         {'data': clean_publishers(), 'new': new_nick, 'old': old_nick},
         namespace='/subscribe', broadcast=True)
Exemple #6
0
def connect_subscriber():
    log.info(f"Connecting subscriber {request.sid}")
    other_nicks = {z.nick for z in subscribers.values()}.union(
        {z.nick for z in publishers.values()})

    if request.sid in subscribers:
        raise RuntimeError(f"{request.sid} (subscriber) Connected twice.")

    nicks_pool = subscribers_nick_presets
    for used_nick in other_nicks:
        try:
            nicks_pool.remove(used_nick)
        except ValueError:  # user is using a non-preset nick
            pass

    colors_pool = subscribers_color_presets
    for used_color in {z.color for z in subscribers.values()}:
        try:
            colors_pool.remove(used_color)
        except ValueError:  # user is using a non-preset color
            pass

    if nicks_pool and colors_pool:
        assigned_nick = random.choice(nicks_pool)
        assigned_color = random.choice(colors_pool)
        subscribers[request.sid] = Subscriber(
            nick=assigned_nick, color=assigned_color)
    else:
        log.info("Couldn't assign a nick, disconnecting the subscriber...")
        emit("log_message",
             {"data": "Failed to assign you a nick", "fatal": True})
        return

    log.info("Someone (id={}, nick={}) just subscribed! - total: {}".format(
        request.sid, assigned_nick, len(subscribers)))
    emit(
        'nick change', {
            'new': assigned_nick, 'old': None,
            "color": assigned_color, 'complete': clean_subscribers(),
        }, broadcast=False)
    emit('update subscriptions',
         {'complete': clean_subscribers(), 'new': assigned_nick, 'old': None},
         broadcast=True, include_self=False)

    emit('update publishers',
         {'data': clean_publishers(), 'state': current_state.value})
    return True
Exemple #7
0
def clean_subscribers():
    # index by nick instead of request.sid (which is private info)
    return {s.nick: s.dict_repr() for s in subscribers.values()}