PageRenderTime 51ms CodeModel.GetById 28ms RepoModel.GetById 0ms app.codeStats 0ms

/roles.php

https://bitbucket.org/trident/mafiab0t
PHP | 169 lines | 95 code | 26 blank | 48 comment | 10 complexity | 4a13d7a7146c5e3c73426cbc388aed85 MD5 | raw file
  1. <?php
  2. //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.
  3. /* Define the errors */
  4. define('ROLE_NOT_FOUND', 'The role you requested for could not be found');
  5. define('ROLE_RETRIEVAL_ERROR', 'There was an error while retrieving the roles');
  6. /* Define the configuration */
  7. // Role update interval in seconds
  8. define('ROLE_UPDATE_INTERVAL', 3600); // 1 Hour
  9. define('ROLE_SITE', 'http://cyberalan.com/mafia/wiki/');
  10. define('ROLE_PAGE', 'List_of_Roles');
  11. // Alignments
  12. define('TOWN_ALIGNED', 'Town-Aligned');
  13. define('MAFIA_ALIGNED', 'Mafia-Aligned');
  14. define('INDEPENDENT', 'Independent/Multiple Alignment');
  15. // The class
  16. class Roles {
  17. // This gets the roles
  18. private static function get_roles() {
  19. // The roles
  20. static $roles = false;
  21. // See if we need to update
  22. if (self::_check_interval('roles', ROLE_UPDATE_INTERVAL)) {
  23. // We need to update
  24. if (($contents = file_get_contents(ROLE_SITE .'api.php?action=query&prop=revisions&rvprop=content&format=php&titles='. ROLE_PAGE)) === false) {
  25. // See if we got the roles before
  26. if ($roles !== false) {
  27. // Nice save!
  28. return $roles;
  29. } else {
  30. // :(
  31. throw new Exception(ROLE_RETRIEVAL_ERROR);
  32. return false;
  33. }
  34. }
  35. // Unserialize it
  36. $contents = unserialize($contents);
  37. // This is the string we want to search from
  38. $contents = $contents['query']['pages'][4]['revisions'][0]['*'];
  39. // Match for the roles
  40. preg_match_all('/===? ?Town-Aligned Roles ?===?(.+?)===? ?Mafia-Aligned Roles ?===?/is', $contents, $townAligneds, PREG_SET_ORDER);
  41. preg_match_all('/===? ?Mafia-Aligned Roles ?===?(.+?)===? ?Independent\/Multiple Alignment Roles ?===?/is', $contents, $mafiaAligneds, PREG_SET_ORDER);
  42. preg_match_all('/===? ?Independent\/Multiple Alignment Roles ?===?(.+?)===? ?(New Roles|Variant Game Roles) ?===?/is', $contents, $independents, PREG_SET_ORDER);
  43. // Create the empty strings
  44. $townAligned = '';
  45. $mafiaAligned = '';
  46. $independent = '';
  47. // Now get the roles for each
  48. foreach ($townAligneds as $void => $details) {
  49. $townAligned .= $details[1];
  50. }
  51. foreach ($mafiaAligneds as $void => $details) {
  52. $mafiaAligned .= $details[1];
  53. }
  54. foreach ($independents as $void => $details) {
  55. $independent .= $details[1];
  56. }
  57. preg_match_all('/\[\[(.+?)\]\] ?-? ?([^\r\n]+)/i', $townAligned, $townAligned, PREG_SET_ORDER);
  58. preg_match_all('/\[\[(.+?)\]\] ?-? ?([^\r\n]+)/i', $mafiaAligned, $mafiaAligned, PREG_SET_ORDER);
  59. preg_match_all('/\[\[(.+?)\]\] ?-? ?([^\r\n]+)/i', $independent, $independent, PREG_SET_ORDER);
  60. // Create the empty roles array
  61. $roles = array();
  62. // Loop through each role and create an object
  63. foreach ($townAligned as $role => $details) {
  64. // Add it
  65. $roles[trim(strtolower($details[1]))] = new Role(trim($details[1]), trim($details[2]), TOWN_ALIGNED);
  66. }
  67. // Loop through each role and create an object
  68. foreach ($mafiaAligned as $role => $details) {
  69. // Add it
  70. $roles[trim(strtolower($details[1]))] = new Role(trim($details[1]), trim($details[2]), MAFIA_ALIGNED);
  71. }
  72. // Loop through each role and create an object
  73. foreach ($independent as $role => $details) {
  74. // Add it
  75. $roles[trim(strtolower($details[1]))] = new Role(trim($details[1]), trim($details[2]), INDEPENDENT);
  76. }
  77. }
  78. // Return the roles
  79. return $roles;
  80. }
  81. // This sees if an update is needed
  82. private static function _check_interval($id, $interval) {
  83. // Define the static variable
  84. static $updates = array();
  85. // See if this function has been called before
  86. if (isset($updates[$id])) {
  87. // Check to see if an update is needed
  88. if (time() - $updates[$id] > $interval) {
  89. // An update is needed, but first update the time
  90. $updates[$id] = time();
  91. // Let them know that an update is needed
  92. return true;
  93. } else {
  94. // An update is not yet needed
  95. return false;
  96. }
  97. } else {
  98. // Create the id set to this time
  99. $updates[$id] = time();
  100. // An update is needed
  101. return true;
  102. }
  103. }
  104. // This searches for a role
  105. public static function search_role($role) {
  106. // Try to get the roles first
  107. try {
  108. $roles = self::get_roles();
  109. } catch (Exception $e) {
  110. // Pass it on and leave
  111. throw $e;
  112. return;
  113. }
  114. // Make the role lowercase for searching
  115. $role = strtolower($role);
  116. // See if it exists
  117. if (! isset($roles[$role])) {
  118. // Leave
  119. throw new Exception(ROLE_NOT_FOUND);
  120. return false;
  121. } else {
  122. // Return the role
  123. return $roles[$role];
  124. }
  125. }
  126. }
  127. // This is a role
  128. class Role {
  129. // The role name
  130. public $name;
  131. // The description
  132. public $description;
  133. // The alignment
  134. public $alignment;
  135. // Supply the role information
  136. public function __construct($name, $description, $alignment) {
  137. // Set the variables
  138. $this->name = $name;
  139. $this->description = $description;
  140. $this->alignment = $alignment;
  141. }
  142. }
  143. ?>