| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217 |
- <?php
- /**
- * This file is part of GameQ.
- *
- * GameQ is free software; you can redistribute it and/or modify
- * it under the terms of the GNU Lesser General Public License as published by
- * the Free Software Foundation; either version 3 of the License, or
- * (at your option) any later version.
- *
- * GameQ is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public License
- * along with this program. If not, see <http://www.gnu.org/licenses/>.
- */
- namespace GameQ\Protocols;
- use GameQ\Protocol;
- use GameQ\Buffer;
- use GameQ\Result;
- /**
- * All-Seeing Eye Protocol class
- *
- * @author Marcel Bößendörfer <[email protected]>
- * @author Austin Bischoff <[email protected]>
- */
- class Ase extends Protocol
- {
- /**
- * Array of packets we want to look up.
- * Each key should correspond to a defined method in this or a parent class
- *
- * @type array
- */
- protected $packets = [
- self::PACKET_ALL => "s",
- ];
- /**
- * The query protocol used to make the call
- *
- * @type string
- */
- protected $protocol = 'ase';
- /**
- * String name of this protocol class
- *
- * @type string
- */
- protected $name = 'ase';
- /**
- * Longer string name of this protocol class
- *
- * @type string
- */
- protected $name_long = "All-Seeing Eye";
- /**
- * The client join link
- *
- * @type string
- */
- protected $join_link = null;
- /**
- * Normalize settings for this protocol
- *
- * @type array
- */
- protected $normalize = [
- // General
- 'general' => [
- // target => source
- 'dedicated' => 'dedicated',
- 'gametype' => 'gametype',
- 'hostname' => 'servername',
- 'mapname' => 'map',
- 'maxplayers' => 'max_players',
- 'mod' => 'game_dir',
- 'numplayers' => 'num_players',
- 'password' => 'password',
- ],
- // Individual
- 'player' => [
- 'name' => 'name',
- 'score' => 'score',
- 'team' => 'team',
- 'ping' => 'ping',
- 'time' => 'time',
- ],
- ];
- /**
- * Process the response
- *
- * @return array
- * @throws \GameQ\Exception\Protocol
- */
- public function processResponse()
- {
- // Create a new buffer
- $buffer = new Buffer(implode('', $this->packets_response));
- // Check for valid response
- if ($buffer->getLength() < 4) {
- throw new \GameQ\Exception\Protocol(sprintf('%s The response from the server was empty.', __METHOD__));
- }
- // Read the header
- $header = $buffer->read(4);
- // Verify header
- if ($header !== 'EYE1') {
- throw new \GameQ\Exception\Protocol(sprintf('%s The response header "%s" does not match expected "EYE1"', __METHOD__, $header));
- }
- // Create a new result
- $result = new Result();
- // Variables
- $result->add('gamename', $buffer->readPascalString(1, true));
- $result->add('port', $buffer->readPascalString(1, true));
- $result->add('servername', $buffer->readPascalString(1, true));
- $result->add('gametype', $buffer->readPascalString(1, true));
- $result->add('map', $buffer->readPascalString(1, true));
- $result->add('version', $buffer->readPascalString(1, true));
- $result->add('password', $buffer->readPascalString(1, true));
- $result->add('num_players', $buffer->readPascalString(1, true));
- $result->add('max_players', $buffer->readPascalString(1, true));
- $result->add('dedicated', 1);
- // Offload the key/value pair processing
- $this->processKeyValuePairs($buffer, $result);
- // Offload processing player and team info
- $this->processPlayersAndTeams($buffer, $result);
- unset($buffer);
- return $result->fetch();
- }
- /*
- * Internal methods
- */
- /**
- * Handles processing the extra key/value pairs for server settings
- *
- * @param \GameQ\Buffer $buffer
- * @param \GameQ\Result $result
- */
- protected function processKeyValuePairs(Buffer &$buffer, Result &$result)
- {
- // Key / value pairs
- while ($buffer->getLength()) {
- $key = $buffer->readPascalString(1, true);
- // If we have an empty key, we've reached the end
- if (empty($key)) {
- break;
- }
- // Otherwise, add the pair
- $result->add(
- $key,
- $buffer->readPascalString(1, true)
- );
- }
- unset($key);
- }
- /**
- * Handles processing the player and team data into a usable format
- *
- * @param \GameQ\Buffer $buffer
- * @param \GameQ\Result $result
- */
- protected function processPlayersAndTeams(Buffer &$buffer, Result &$result)
- {
- // Players and team info
- while ($buffer->getLength()) {
- // Get the flags
- $flags = $buffer->readInt8();
- // Get data according to the flags
- if ($flags & 1) {
- $result->addPlayer('name', $buffer->readPascalString(1, true));
- }
- if ($flags & 2) {
- $result->addPlayer('team', $buffer->readPascalString(1, true));
- }
- if ($flags & 4) {
- $result->addPlayer('skin', $buffer->readPascalString(1, true));
- }
- if ($flags & 8) {
- $result->addPlayer('score', $buffer->readPascalString(1, true));
- }
- if ($flags & 16) {
- $result->addPlayer('ping', $buffer->readPascalString(1, true));
- }
- if ($flags & 32) {
- $result->addPlayer('time', $buffer->readPascalString(1, true));
- }
- }
- }
- }
|