Unsupported PHP app for analysing and displaying stats for Team Fortress 2
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

parser.php 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. <?PHP
  2. function sqr($x) {
  3. return $x * $x;
  4. }
  5. require_once(dirname(dirname(__FILE__)) . '/player.php');
  6. class Parser {
  7. private static $handlers = array();
  8. private static $daemon = null;
  9. public static function setDaemon(&$daemon) {
  10. self::$daemon =& $daemon;
  11. }
  12. public static function hasDaemon() {
  13. return self::$daemon != null;
  14. }
  15. public static function getDaemon() {
  16. return self::$daemon;
  17. }
  18. public static function parseDate(&$line) {
  19. $index = strpos($line, ': ');
  20. $date = substr($line, 0, $index);
  21. $line = substr($line, $index + 2);
  22. return strtotime(str_replace(' - ', ' ', $date));
  23. }
  24. public static function parsePlayerString(&$line) {
  25. if (preg_match('/^"(.*?<[0-9]+><(BOT|Console|STEAM_.*?)><.*?>)"/', $line, $m)) {
  26. $line = substr($line, strlen($m[0]) + 1);
  27. return $m[1];
  28. }
  29. if (!preg_match('/^".*?" = .*$/', $line)) {
  30. echo "Unable to parse player string: $line";
  31. }
  32. return self::parseString($line);
  33. }
  34. public static function parseString(&$line) {
  35. $index = strpos($line, '"', 1);
  36. $data = substr($line, 1, $index - 1);
  37. $line = substr($line, $index + 2);
  38. return $data;
  39. }
  40. public static function parsePlayer(&$line, $fromLine = true) {
  41. if ($fromLine) {
  42. $player = self::parsePlayerString($line);
  43. } else {
  44. $player = $line;
  45. }
  46. if (preg_match('((.*?)<([0-9]+)><([A-Za-z_:0-9]+)><([A-Za-z]*)>)', $player, $m)) {
  47. return array('alias' => $m[1], 'uid' => $m[2], 'steamid' => $m[3], 'team' => $m[4]);
  48. } else {
  49. // Unable to parse player
  50. return null;
  51. }
  52. }
  53. public static function parseProps(&$line) {
  54. preg_match_all('(\((.*?) "(.*?)"\))', $line, $matches, PREG_SET_ORDER);
  55. $res = array();
  56. foreach ($matches as $match) {
  57. $res[$match[1]] = $match[2];
  58. }
  59. if (isset($res['attacker_position']) && isset($res['victim_position'])) {
  60. // Calculate distance
  61. $a = explode(" ", $res['attacker_position']);
  62. $v = explode(" ", $res['victim_position']);
  63. $res['distance'] = sqrt(sqr($a[0] - $v[0]) + sqr($a[1] - $v[1]) + sqr($a[2] - $v[2]));
  64. }
  65. return $res;
  66. }
  67. public static function parseLine($line) {
  68. if ($line[0] == 'L') {
  69. $line = substr($line, 2);
  70. } else if (substr($line, 4, 2) == 'RL') {
  71. $line = substr($line, 7);
  72. } else {
  73. // TODO: log me
  74. return;
  75. }
  76. $date = self::parseDate($line);
  77. $player = null;
  78. // Broken admin mod
  79. if (substr($line, 0, 2) == 'L ') {
  80. $line = substr($line, 2);
  81. $date = self::parseDate($line);
  82. }
  83. if ($line[0] == '"') {
  84. // Player event
  85. if (($player = self::parsePlayer($line)) == null) {
  86. // Or not?
  87. return;
  88. }
  89. }
  90. $parts = explode(' ', $line);
  91. $handler = strtolower(str_replace(',', '', $parts[0]));
  92. if (isset(self::$handlers[$handler])) {
  93. self::$handlers[$handler]->parseLine($date, Player::getBySteamID($player['steamid']), $player, implode(' ', array_slice($parts, 1)));
  94. } else {
  95. // TODO: log me
  96. return;
  97. }
  98. }
  99. public static function init() {
  100. foreach (glob(dirname(__FILE__) . '/handlers/*.php') as $file) {
  101. require($file);
  102. $handler = basename($file, '.php');
  103. $name = str_replace(' ', '', ucwords(str_replace('_', ' ', $handler))) . 'Handler';
  104. self::$handlers[$handler] = new $name();
  105. }
  106. }
  107. }
  108. Parser::init();
  109. ?>