Some function and event updates

This commit is contained in:
Vgr E. Barry 2017-08-23 22:06:31 -04:00
parent fa82be59f3
commit 370b2c6ed2
5 changed files with 25 additions and 27 deletions

View File

@ -3,7 +3,7 @@ from src.events import Event
from src import settings as var from src import settings as var
from src import users from src import users
__all__ = ["get_players", "get_participants", "get_target", "get_role"] __all__ = ["get_players", "get_participants", "get_target", "get_main_role", "get_all_roles"]
def get_players(roles=None, *, mainroles=None): def get_players(roles=None, *, mainroles=None):
if mainroles is None: if mainroles is None:
@ -48,7 +48,7 @@ def get_target(var, wrapper, message, *, allow_self=False, allow_bot=False):
return match return match
def get_role(user): def get_main_role(user):
role = var.MAIN_ROLES.get(user) role = var.MAIN_ROLES.get(user)
if role is not None: if role is not None:
return role return role
@ -60,3 +60,6 @@ def get_role(user):
if role is None: if role is None:
raise ValueError("User {0} isn't playing and has no defined participant role".format(user)) raise ValueError("User {0} isn't playing and has no defined participant role".format(user))
return role return role
def get_all_roles(user):
return {role for role, nicks in var.ROLES.items() if user.nick in nicks}

View File

@ -12,7 +12,8 @@ import botconfig
import src.settings as var import src.settings as var
from src import decorators, wolfgame, events, channels, hooks, users, errlog as log, stream_handler as alog from src import decorators, wolfgame, events, channels, hooks, users, errlog as log, stream_handler as alog
from src.messages import messages from src.messages import messages
from src.utilities import reply, list_participants, get_role, get_templates from src.utilities import reply, get_role, get_templates
from src.functions import get_participants
from src.dispatcher import MessageDispatcher from src.dispatcher import MessageDispatcher
from src.decorators import handle_error from src.decorators import handle_error
@ -67,7 +68,7 @@ def on_privmsg(cli, rawnick, chan, msg, *, notice=False, force_role=None):
# as we don't want to insert bogus command keys into the dict. # as we don't want to insert bogus command keys into the dict.
cmds = [] cmds = []
phase = var.PHASE phase = var.PHASE
if user.nick in list_participants(): if user in get_participants():
roles = {get_role(user.nick)} | set(get_templates(user.nick)) roles = {get_role(user.nick)} | set(get_templates(user.nick))
if force_role is not None: if force_role is not None:
roles &= {force_role} # only fire off role commands for the forced role roles &= {force_role} # only fire off role commands for the forced role

View File

@ -60,10 +60,10 @@ def vg_retract(var, wrapper, message):
del KILLS[wrapper.source.nick] del KILLS[wrapper.source.nick]
wrapper.pm(messages["retracted_kill"]) wrapper.pm(messages["retracted_kill"])
@event_listener("list_participants") @event_listener("get_participants")
def on_list_participants(evt, var): def on_get_participants(evt, var):
evt.data["pl"].extend([p.nick for p in GHOSTS if GHOSTS[p][0] != "!"]) evt.data["pl"].extend([p for p in GHOSTS if GHOSTS[p][0] != "!"])
evt.data["pl"].extend([p.nick for p in drivenoff]) evt.data["pl"].extend(drivenoff)
@event_listener("player_win", priority=1) @event_listener("player_win", priority=1)
def on_player_win(evt, var, user, role, winner, survived): def on_player_win(evt, var, user, role, winner, survived):

View File

@ -13,7 +13,7 @@ __all__ = ["pm", "is_fake_nick", "mass_mode", "mass_privmsg", "reply",
"relay_wolfchat_command", "chk_nightdone", "chk_decision", "relay_wolfchat_command", "chk_nightdone", "chk_decision",
"chk_win", "irc_lower", "irc_equals", "is_role", "match_hostmask", "chk_win", "irc_lower", "irc_equals", "is_role", "match_hostmask",
"is_owner", "is_admin", "plural", "singular", "list_players", "is_owner", "is_admin", "plural", "singular", "list_players",
"list_players_and_roles", "list_participants", "get_role", "get_roles", "list_players_and_roles", "get_role", "get_roles",
"get_reveal_role", "get_templates", "change_role", "role_order", "break_long_message", "get_reveal_role", "get_templates", "change_role", "role_order", "break_long_message",
"complete_match","complete_one_match", "get_victim", "get_nick", "InvalidModeException"] "complete_match","complete_one_match", "get_victim", "get_nick", "InvalidModeException"]
# message either privmsg or notice, depending on user settings # message either privmsg or notice, depending on user settings
@ -318,22 +318,16 @@ def list_players_and_roles():
# (and working with user objects instead of nicks) # (and working with user objects instead of nicks)
return {u.nick: r for u, r in var.MAIN_ROLES.items()} return {u.nick: r for u, r in var.MAIN_ROLES.items()}
def list_participants():
"""List all people who are still able to participate in the game in some fashion."""
pl = list_players()
evt = Event("list_participants", {"pl": pl})
evt.dispatch(var)
return evt.data["pl"][:]
def get_role(p): def get_role(p):
# FIXME: make the arg a user instead of a nick # FIXME: make the arg a user instead of a nick
from src import users from src import users
from src.functions import get_participants
user = users._get(p) user = users._get(p)
role = var.MAIN_ROLES.get(user, None) role = var.MAIN_ROLES.get(user, None)
if role is not None: if role is not None:
return role return role
# not found in player list, see if they're a special participant # not found in player list, see if they're a special participant
if p in list_participants(): if user in get_participants():
evt = Event("get_participant_role", {"role": None}) evt = Event("get_participant_role", {"role": None})
evt.dispatch(var, user) evt.dispatch(var, user)
role = evt.data["role"] role = evt.data["role"]

View File

@ -49,7 +49,7 @@ import src.settings as var
from src.utilities import * from src.utilities import *
from src import db, events, dispatcher, channels, users, hooks, logger, proxy, debuglog, errlog, plog from src import db, events, dispatcher, channels, users, hooks, logger, proxy, debuglog, errlog, plog
from src.decorators import command, cmd, hook, handle_error, event_listener, COMMANDS from src.decorators import command, cmd, hook, handle_error, event_listener, COMMANDS
from src.functions import get_players from src.functions import get_players, get_participants
from src.messages import messages from src.messages import messages
from src.warnings import * from src.warnings import *
from src.context import IRCContext from src.context import IRCContext
@ -552,7 +552,7 @@ def replace(var, wrapper, message):
for user in var.ALL_PLAYERS: for user in var.ALL_PLAYERS:
if users.equals(user.account, wrapper.source.account): if users.equals(user.account, wrapper.source.account):
if user is wrapper.source or user.nick not in list_participants(): # FIXME: Need to fix once list_participants() holds User instances if user is wrapper.source or user not in get_participants():
continue continue
elif target is None: elif target is None:
target = user target = user
@ -565,7 +565,7 @@ def replace(var, wrapper, message):
return return
else: else:
pl = [users._get(p) for p in list_participants()] # FIXME: Need to fix once list_participants() holds User instances pl = get_participants()
target, _ = users.complete_match(rest[0], pl) target, _ = users.complete_match(rest[0], pl)
@ -729,10 +729,10 @@ def join_deadchat(var, *all_users):
return return
to_join = [] to_join = []
pl = list_participants() pl = get_participants()
for user in all_users: for user in all_users:
if user.stasis_count() or user.nick in pl or user in var.DEADCHAT_PLAYERS: # FIXME: Fix this when list_participants() returns User instances if user.stasis_count() or user in pl or user in var.DEADCHAT_PLAYERS:
continue continue
to_join.append(user) to_join.append(user)
@ -6464,8 +6464,8 @@ def listroles(cli, nick, chan, rest):
def myrole(cli, nick, chan, rest): # FIXME: Need to fix !swap once this gets converted def myrole(cli, nick, chan, rest): # FIXME: Need to fix !swap once this gets converted
"""Reminds you of your current role.""" """Reminds you of your current role."""
ps = list_participants() ps = get_participants()
if nick not in ps: if users._get(nick) not in ps:
return return
role = get_role(nick) role = get_role(nick)
@ -6517,7 +6517,7 @@ def myrole(cli, nick, chan, rest): # FIXME: Need to fix !swap once this gets con
pm(cli, nick, messages["prophet_simple"]) pm(cli, nick, messages["prophet_simple"])
# Remind lovers of each other # Remind lovers of each other
if nick in ps and nick in var.LOVERS: if users._get(nick) in ps and nick in var.LOVERS:
message = messages["matched_info"] message = messages["matched_info"]
lovers = sorted(list(set(var.LOVERS[nick]))) lovers = sorted(list(set(var.LOVERS[nick])))
if len(lovers) == 1: if len(lovers) == 1:
@ -6899,9 +6899,9 @@ def can_run_restricted_cmd(user):
if botconfig.DEBUG_MODE: if botconfig.DEBUG_MODE:
return True return True
pl = list_participants() pl = get_participants()
if user.nick in pl: # FIXME: Need to update this once list_participants() holds User instances if user in pl:
return False return False
if not var.DISABLE_ACCOUNTS and user.account in [users._get(player).account for player in pl]: # FIXME if not var.DISABLE_ACCOUNTS and user.account in [users._get(player).account for player in pl]: # FIXME