Advertisement
Guest User

Untitled

a guest
Dec 25th, 2014
181
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.36 KB | None | 0 0
  1. <?php
  2. namespace Tibia;
  3. /**
  4. * @author Mads Dahlen Aune
  5. * @version 0.1
  6. * @package Tibia
  7. */
  8. class Player {
  9. public $name;
  10. public $sex;
  11. public $vocation;
  12. public $level;
  13. public $points;
  14. public $world;
  15. public $formerWorld;
  16. public $city;
  17. public $house;
  18. public $guild;
  19. public $lastLogin;
  20. public $comment;
  21. public $status;
  22. public $created;
  23. private $_otherChars = array();
  24. private $_deaths = array();
  25.  
  26. /**
  27. * @param string $playerName The name of player to lookup
  28. */
  29. public function __construct($playerName) {
  30. $html = file_get_contents("http://www.tibia.com/community/?subtopic=characters&name=" . urlencode($playerName));
  31. $this->name = Parser::doesExist(trim(Parser::parse($html, '#Name:</td><td>(.+?)<div#')), $playerName);
  32. $this->sex = Parser::parse($html, '#Sex:</td><td>(.+?)</td>#');
  33. $this->vocation = Parser::parse($html, '#Vocation:</td><td>(.+?)</td>#');
  34. $this->level = Parser::parse($html, '#Level:</td><td>(\d+?)</td>#');
  35. $this->points = Parser::parse($html, '#Points:</nobr></td><td>(\d+?)</td>#');
  36. $this->world = Parser::parse($html, '#World:</td><td>(.+?)</td>#');
  37. $this->formerWorld = Parser::parse($html, '#Former World:</td><td>(.+?)</td>#');
  38. $this->city = Parser::parse($html, '#Residence:</td><td>(.+?)</td>#');
  39. $this->house = Parser::parse($html, '#House:</td><td>(.+?)</td>#');
  40. $this->guild = Parser::parse($html, '#membership:</td><td>(.+?)</td>#');
  41. $this->lastLogin = Parser::parse($html, '#login:</td><td>(.+?)</td>#');
  42. $this->comment = Parser::parse($html, '#Comment:</td><td>(.+?)</td>#s');
  43. $this->status = Parser::parse($html, '#Status:</td><td>(.+?)</td>#');
  44. $this->created = Parser::parse($html, '#Created:</td><td>(.+?)</td>#');
  45. $this->_otherChars = Parser::parse($html, '#<NOBR>[0-9].&\#160;(.+?)</NOBR></TD><TD WIDTH=[0-9]+%><NOBR>(.+?)</NOBR>#', true);
  46. $this->_deaths = Parser::parse($html, '#<td width="[0-9]+%" valign="top" >(.+?)</td><td>(.+?)</td>#', true);
  47. }
  48. /**
  49. * @return mixed|bool
  50. */
  51. public function getOtherChars() {
  52. if($this->_otherChars) {
  53. $tmp = array();
  54. for($i = 0; $i < count($this->_otherChars); $i++) {
  55. if($this->_otherChars[$i][1] === $this->name) { continue; }
  56. $tmp[$i]['name'] = $this->_otherChars[$i][1];
  57. $tmp[$i]['world'] = $this->_otherChars[$i][2];
  58. }
  59. return $tmp;
  60. }
  61. return false;
  62. }
  63. /**
  64. * @return mixed|bool
  65. */
  66. public function getDeaths() {
  67. if($this->_deaths) {
  68. $tmp = array();
  69. for($i = 0; $i < count($this->_deaths); $i++) {
  70. $tmp[] = $this->_deaths[$i][1] . " " . $this->_deaths[$i][2];
  71. }
  72. return $tmp;
  73. }
  74. return false;
  75. }
  76.  
  77. /**
  78. * @param mixed $alt Option to set custom output.
  79. * @return string
  80. */
  81. public function isOnline($alt = array('Online', 'Offline')) {
  82. $html = file_get_contents("http://www.tibia.com/community/?subtopic=worlds&world=" . urlencode($this->world));
  83. return (preg_match('#>('.str_replace(' ', '&nbsp;', $this->name).'?)</a>#', $html)) ? $alt[0] : $alt[1];
  84. }
  85. }
  86. class Guild {
  87. public $name;
  88. public $description;
  89. public $founded;
  90. public $guildHall;
  91. private $_members;
  92. public function __construct($guildName) {
  93. $html = file_get_contents("http://www.tibia.com/community/?subtopic=guilds&page=view&GuildName=" . urlencode($guildName));
  94. $this->name = Parser::doesExist(Parser::parse($html, '#<H1>(.+?)</H1>#'), $guildName);
  95. $this->description = Parser::parse($html, '#</TABLE><BR>(.+?)<BR>#s');
  96. $this->founded = Parser::parse($html, '#(The guild was founded on .+?)<BR>#');
  97. $this->guildHall = Parser::parse($html, '#(Their home .+?)<BR>#');
  98. $this->_members = Parser::parse($html, '#<TD><A HREF="(.+)">(.+?)</A>#', true);
  99. }
  100. public function getMembers() {
  101. $tmp = array();
  102. for($i = 0; $i < count($this->_members); $i++) {
  103. $tmp[$i]['name'] = $this->_members[$i][2];
  104. $tmp[$i]['link'] = $this->_members[$i][1];
  105. }
  106. return $tmp;
  107. }
  108. }
  109. class Parser {
  110. public static function parse($source, $pattern, $all = false) {
  111. if($all) {
  112. if(preg_match_all($pattern, $source, $matches, PREG_SET_ORDER))
  113. return $matches;
  114. } else {
  115. if(preg_match($pattern, $source, $matches))
  116. return $matches[1];
  117. }
  118. return false;
  119. }
  120. public static function doesExist($value, $expected, $failMsg = "Could not find.") {
  121. // echo "DEBUG: Value: $value Expected: $expected";
  122. return ($value === $expected) ? $value : exit($failMsg);
  123. }
  124. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement