mafiab0t /roles.php

Language PHP Lines 169
MD5 Hash 4a13d7a7146c5e3c73426cbc388aed85 Estimated Cost $2,231 (why?)
Repository https://bitbucket.org/trident/mafiab0t View Raw File View Project SPDX
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
<?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;
		}
	}
?>
Back to Top