/roles.php
PHP | 169 lines | 95 code | 26 blank | 48 comment | 10 complexity | 4a13d7a7146c5e3c73426cbc388aed85 MD5 | raw file
- <?php
- //This is dependent on MediaWiki to read a page from it's api.php. It reads the raw page. Everything works without this, and I personally ignored its existance until now.
- /* Define the errors */
- define('ROLE_NOT_FOUND', 'The role you requested for could not be found');
- define('ROLE_RETRIEVAL_ERROR', 'There was an error while retrieving the roles');
- /* Define the configuration */
- // Role update interval in seconds
- define('ROLE_UPDATE_INTERVAL', 3600); // 1 Hour
- define('ROLE_SITE', 'http://cyberalan.com/mafia/wiki/');
- define('ROLE_PAGE', 'List_of_Roles');
- // Alignments
- define('TOWN_ALIGNED', 'Town-Aligned');
- define('MAFIA_ALIGNED', 'Mafia-Aligned');
- define('INDEPENDENT', 'Independent/Multiple Alignment');
- // The class
- class Roles {
- // This gets the roles
- private static function get_roles() {
- // The roles
- static $roles = false;
- // See if we need to update
- if (self::_check_interval('roles', ROLE_UPDATE_INTERVAL)) {
- // We need to update
- if (($contents = file_get_contents(ROLE_SITE .'api.php?action=query&prop=revisions&rvprop=content&format=php&titles='. ROLE_PAGE)) === false) {
- // See if we got the roles before
- if ($roles !== false) {
- // Nice save!
- return $roles;
- } else {
- // :(
- throw new Exception(ROLE_RETRIEVAL_ERROR);
- return false;
- }
- }
- // Unserialize it
- $contents = unserialize($contents);
- // This is the string we want to search from
- $contents = $contents['query']['pages'][4]['revisions'][0]['*'];
- // Match for the roles
- preg_match_all('/===? ?Town-Aligned Roles ?===?(.+?)===? ?Mafia-Aligned Roles ?===?/is', $contents, $townAligneds, PREG_SET_ORDER);
- preg_match_all('/===? ?Mafia-Aligned Roles ?===?(.+?)===? ?Independent\/Multiple Alignment Roles ?===?/is', $contents, $mafiaAligneds, PREG_SET_ORDER);
- preg_match_all('/===? ?Independent\/Multiple Alignment Roles ?===?(.+?)===? ?(New Roles|Variant Game Roles) ?===?/is', $contents, $independents, PREG_SET_ORDER);
- // Create the empty strings
- $townAligned = '';
- $mafiaAligned = '';
- $independent = '';
-
- // Now get the roles for each
- foreach ($townAligneds as $void => $details) {
- $townAligned .= $details[1];
- }
- foreach ($mafiaAligneds as $void => $details) {
- $mafiaAligned .= $details[1];
- }
- foreach ($independents as $void => $details) {
- $independent .= $details[1];
- }
- preg_match_all('/\[\[(.+?)\]\] ?-? ?([^\r\n]+)/i', $townAligned, $townAligned, PREG_SET_ORDER);
- preg_match_all('/\[\[(.+?)\]\] ?-? ?([^\r\n]+)/i', $mafiaAligned, $mafiaAligned, PREG_SET_ORDER);
- preg_match_all('/\[\[(.+?)\]\] ?-? ?([^\r\n]+)/i', $independent, $independent, PREG_SET_ORDER);
- // Create the empty roles array
- $roles = array();
- // Loop through each role and create an object
- foreach ($townAligned as $role => $details) {
- // Add it
- $roles[trim(strtolower($details[1]))] = new Role(trim($details[1]), trim($details[2]), TOWN_ALIGNED);
- }
- // Loop through each role and create an object
- foreach ($mafiaAligned as $role => $details) {
- // Add it
- $roles[trim(strtolower($details[1]))] = new Role(trim($details[1]), trim($details[2]), MAFIA_ALIGNED);
- }
- // Loop through each role and create an object
- foreach ($independent as $role => $details) {
- // Add it
- $roles[trim(strtolower($details[1]))] = new Role(trim($details[1]), trim($details[2]), INDEPENDENT);
- }
- }
- // Return the roles
- return $roles;
- }
- // This sees if an update is needed
- private static function _check_interval($id, $interval) {
- // Define the static variable
- static $updates = array();
- // See if this function has been called before
- if (isset($updates[$id])) {
- // Check to see if an update is needed
- if (time() - $updates[$id] > $interval) {
- // An update is needed, but first update the time
- $updates[$id] = time();
- // Let them know that an update is needed
- return true;
- } else {
- // An update is not yet needed
- return false;
- }
- } else {
- // Create the id set to this time
- $updates[$id] = time();
- // An update is needed
- return true;
- }
- }
- // This searches for a role
- public static function search_role($role) {
- // Try to get the roles first
- try {
- $roles = self::get_roles();
- } catch (Exception $e) {
- // Pass it on and leave
- throw $e;
- return;
- }
- // Make the role lowercase for searching
- $role = strtolower($role);
- // See if it exists
- if (! isset($roles[$role])) {
- // Leave
- throw new Exception(ROLE_NOT_FOUND);
- return false;
- } else {
- // Return the role
- return $roles[$role];
- }
- }
- }
- // This is a role
- class Role {
- // The role name
- public $name;
- // The description
- public $description;
- // The alignment
- public $alignment;
- // Supply the role information
- public function __construct($name, $description, $alignment) {
- // Set the variables
- $this->name = $name;
- $this->description = $description;
- $this->alignment = $alignment;
- }
- }
- ?>