using System; using System.Text; using System.Data; using System.Collections.Generic; using Woodpecker.Core; using Woodpecker.Storage; namespace Woodpecker.Game.Users.Roles { /// /// Provides various functions for user roles and their permissions, badges etc. /// public class roleManager { #region Fields #region Main roles private Dictionary> roleRights = new Dictionary>(); private Dictionary> roleBadges = new Dictionary>(); #endregion #region Club private List clubRights = new List(); #endregion #endregion #region Methods public void loadRoles() { Logging.Log("Initializing user roles..."); Database Database = new Database(true, false); DataTable dTable = new DataTable(); if (Database.Ready) { for (int roleID = 0; roleID <= 6; roleID++) { userRole Role = (userRole)roleID; //Logging.Log("Role '" + Role.ToString() + "'"); // Role rights List tmpList = new List(); dTable = Database.getTable("SELECT fuseright FROM users_roles_fuserights WHERE minrole <= '" + roleID + "'"); foreach (DataRow dRow in dTable.Rows) { string Right = (string)dRow["fuseright"]; tmpList.Add(Right); //Logging.Log(" - FUSE right: " + Right); } this.roleRights.Add(Role, tmpList); // Role badges tmpList = new List(); dTable = Database.getTable("SELECT badge FROM users_roles_badges WHERE minrole <= '" + roleID + "'"); foreach (DataRow dRow in dTable.Rows) { string Badge = (string)dRow["badge"]; tmpList.Add(Badge); //Logging.Log(" - Badge: " + Badge); } this.roleBadges.Add(Role, tmpList); //ObjectTree.Application.GUI.logSacredText(null); } //Logging.Log("FUSE rights for users with Club subscription:"); // Club rights dTable = Database.getTable("SELECT fuseright FROM users_club_fuserights"); foreach (DataRow dRow in dTable.Rows) { string Right = (string)dRow["fuseright"]; this.clubRights.Add(Right); //Logging.Log("- " + Right); } //ObjectTree.Application.GUI.logSacredText(null); Logging.Log("Initialized user roles."); } else Logging.Log("Failed to initialize user roles, database was not contactable!", Logging.logType.commonWarning); } /// /// Parses a numeric role ID (string) to a value of the userRole enum. userRole.Banned is returned if an invalid value is given. /// /// The string object containing the role ID. public userRole parseRoleFromString(string s) { int iRole = 0; if (int.TryParse(s, out iRole)) return (userRole)iRole; else return userRole.Banned; } /// /// Returns a string with every fuse right followed by char 2, for a certain user role. The fuserights for users with a club subscription are added if required. /// /// The userRole enum value of the role to get the rights of. /// If the user has club subscription, then supply true. public string getRightsForRole(userRole Role, bool hasClub) { StringBuilder ret = new StringBuilder(); try { List rolesRights = this.roleRights[Role]; foreach(string Right in rolesRights) { ret.Append(Right); ret.Append("\x02"); } if (hasClub) { foreach (string Right in this.clubRights) { ret.Append(Right); ret.Append("\x02"); } } } catch {} return ret.ToString(); } /// /// Returns a boolean that indicates if a given user role contains a certain fuse right. /// /// The userRole enum value of the role to lookup. /// Indicates if to include rights for club subscribers. /// The fuse right to check. public bool roleHasRight(userRole Role, bool hasClub, string Right) { bool Aye = this.roleRights[Role].Contains(Right); if (Aye == false && hasClub) Aye = this.clubRights.Contains(Right); return Aye; } /// /// Returns a boolean that indicates if a give user role has access to a given badge. /// /// The userRole enum value of the role to lookup. /// The badge to check. public bool roleHasBadge(userRole Role, string Badge) { return this.roleBadges[Role].Contains(Badge); } public List getDefaultBadgesForRole(userRole Role) { return this.roleBadges[Role]; } #endregion } }