Skip to main content

Roles

A static class in TavernLib, TavernLib.ModdingApi.

Players' roles from users.json, kept in memory and re-read about a second after the file changes. Server only: on a client every list is empty.

Fields​

MemberWhat it is
static readonly TavernEvents.TavernEvent<RolesChange> RolesChangedRaised on the main thread for each user whose roles change, after their new roles are sent to them. Not raised at start.
7 private and internal fields
MemberWhat it is
private static DateTime _loadedStampWrite time of users.json when the roles were last loaded.
private static float _nextPollEarliest Time.realtimeSinceStartup for the next poll.
private static Dictionary<string, IReadOnlyList<string>> _rolesRoles by username (case-insensitive), as last read from users.json.
private static DateTime _seenStampWrite time of users.json at the last poll.
private static DateTime _warnedStampWrite time of the users.json that last failed to read, so the warning isn't repeated.
private static readonly IReadOnlyList<string> NoRolesEmpty role list returned for users with no roles.
private static readonly string UsersFilePath of users.json, which the roles are read from.

Methods​

MemberWhat it is
static IReadOnlyList<string> GetRoles(Connection connection)A player's roles, by connection or by username, without case. An empty list (never null) for anyone with none.
static IReadOnlyList<string> GetRoles(string username)A player's roles, by connection or by username, without case. An empty list (never null) for anyone with none.
static bool HasRole(Connection connection, string role)True if the connection's player has the role. Role names compare without case.
5 private and internal methods
MemberWhat it is
private static void Announce(RolesChange change)Sends the new roles to the user if online and raises RolesChanged.
private static void Poll()Once a second, reloads the roles if users.json changed and has stopped changing.
private static Dictionary<string, IReadOnlyList<string>> Read()Reads users.json and returns each user's non-blank roles.
private static void Reload(DateTime stamp)Rereads the roles and announces each user whose roles changed; keeps the old roles if the file can't be read.
internal static void Start()Internal, server: reads users.json and starts the once-a-second poll from MelonEvents.OnUpdate.