Skip to content

Commit

Permalink
Improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
jacklul committed May 5, 2019
1 parent 412449c commit 9217b7d
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 27 deletions.
8 changes: 6 additions & 2 deletions src/MagicGetterTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,13 @@ public function __call($method, $args)
if ($action === 'get') {
if (isset($this->$property) && $this->$property !== null) {
return $this->$property;
} elseif (isset($this->status[$property])) {
}

if (isset($this->status[$property])) {
return $this->status[$property];
} elseif (isset($this->info[$property])) {
}

if (isset($this->info[$property])) {
return $this->info[$property];
}
}
Expand Down
24 changes: 13 additions & 11 deletions src/MasterServer.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

namespace jacklul\q3serverlist;

use InvalidArgumentException;

/**
* This class represents single master server
*
Expand Down Expand Up @@ -49,15 +51,15 @@ class MasterServer
public function __construct($address, $port, $protocol)
{
if (!is_string($address)) {
throw new \InvalidArgumentException('Address must be a STRING!');
throw new InvalidArgumentException('Address must be a STRING!');
}

if (!is_int($port)) {
throw new \InvalidArgumentException('Port must be a NUMBER!');
throw new InvalidArgumentException('Port must be a NUMBER!');
}

if (!is_int($protocol)) {
throw new \InvalidArgumentException('Protocol must be a NUMBER!');
throw new InvalidArgumentException('Protocol must be a NUMBER!');
}

$this->address = $address;
Expand All @@ -71,18 +73,18 @@ public function __construct($address, $port, $protocol)
*
* @return array|bool
*/
public function getServers($keywords = "empty full", $timeout = 1)
public function getServers($keywords = 'empty full', $timeout = 1)
{
if (!empty($this->servers)) {
return $this->servers;
}

if (!is_string($keywords)) {
throw new \InvalidArgumentException('Keywords must be a STRING!');
if (!is_string($keywords) && $keywords !== null) {
throw new InvalidArgumentException('Keywords must be a STRING!');
}

if (!is_int($timeout)) {
throw new \InvalidArgumentException('Timeout must be a NUMBER!');
throw new InvalidArgumentException('Timeout must be a NUMBER!');
}

if ($socket = fsockopen('udp://' . $this->address, $this->port)) {
Expand All @@ -92,18 +94,18 @@ public function getServers($keywords = "empty full", $timeout = 1)
fwrite($socket, str_repeat(chr(255), 4) . 'getservers ' . $this->protocol . ' ' . $keywords . "\n");

$time = time() + $timeout;
$returned = "";
$returned = '';
while ($time > time()) {
$returned .= fgets($socket);
}

$servers = array();
for ($i = 0; $i < (strlen($returned) - 10); $i++) {
if ($returned[$i] == "\\" && $returned[$i+7] == "\\") {
$ip = ord($returned[$i+1]) . "." . ord($returned[$i+2]) . "." . ord($returned[$i+3]) . "." . ord($returned[$i+4]);
if ($returned[$i] === "\\" && $returned[$i+7] === "\\") {
$ip = ord($returned[$i+1]) . '.' . ord($returned[$i+2]) . '.' . ord($returned[$i+3]) . '.' . ord($returned[$i+4]);
$port = (ord($returned[$i+5])<<8) + ord($returned[$i+6]);

array_push($servers, new Server($ip, $port));
$servers[] = new Server($ip, $port);
}
}

Expand Down
30 changes: 16 additions & 14 deletions src/Server.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

namespace jacklul\q3serverlist;

use InvalidArgumentException;

/**
* This class represents single server
*
Expand Down Expand Up @@ -49,11 +51,11 @@ class Server
public function __construct($address, $port)
{
if (!is_string($address)) {
throw new \InvalidArgumentException('Address must be a STRING!');
throw new InvalidArgumentException('Address must be a STRING!');
}

if (!is_int($port)) {
throw new \InvalidArgumentException('Port must be a NUMBER!');
throw new InvalidArgumentException('Port must be a NUMBER!');
}

$this->address = $address;
Expand All @@ -72,11 +74,11 @@ public function getInfo($timeout = 1)
}

if (!is_int($timeout)) {
throw new \InvalidArgumentException('Timeout must be a NUMBER!');
throw new InvalidArgumentException('Timeout must be a NUMBER!');
}

if ($socket = fsockopen('udp://' . $this->address, $this->port)) {
socket_set_timeout($socket, $timeout);
stream_set_timeout($socket, $timeout);
fwrite($socket, str_repeat(chr(255), 4) . 'getinfo' . "\n");
$data = fread($socket, 10000);
fclose($socket);
Expand All @@ -87,7 +89,7 @@ public function getInfo($timeout = 1)
if (isset($vars[1])) {
$ret = explode("\\", substr($vars[1], 1, strlen($vars[1])));

for ($i = 0; $i <= count($ret); $i = $i + 2) {
for ($i = 0, $iMax = count($ret); $i <= $iMax; $i += 2) {
$list[strtolower(@$ret[$i])] = @$ret[$i + 1];
}
array_pop($list);
Expand Down Expand Up @@ -115,11 +117,11 @@ public function getStatus($timeout = 1)
}

if (!is_int($timeout)) {
throw new \InvalidArgumentException('Timeout must be a NUMBER!');
throw new InvalidArgumentException('Timeout must be a NUMBER!');
}

if ($socket = fsockopen('udp://' . $this->address, $this->port)) {
socket_set_timeout($socket, $timeout);
stream_set_timeout($socket, $timeout);
fwrite($socket, str_repeat(chr(255), 4) . 'getstatus' . "\n");
$data = fread($socket, 10000);
fclose($socket);
Expand All @@ -130,7 +132,7 @@ public function getStatus($timeout = 1)
if (isset($vars[1])) {
$ret = explode("\\", substr($vars[1], 1, strlen($vars[1])));

for ($i = 0; $i <= count($ret); $i = $i + 2) {
for ($i = 0, $iMax = count($ret); $i <= $iMax; $i += 2) {
$list[strtolower(@$ret[$i])] = @$ret[$i + 1];
}
array_pop($list);
Expand All @@ -139,10 +141,10 @@ public function getStatus($timeout = 1)
$list['port'] = $this->port;

$players = array();
for ($i = 2; $i < sizeof($vars); $i++) {
for ($i = 2, $iMax = sizeof($vars); $i < $iMax; $i++) {
$infos = explode(' ', $vars[$i], 3);

$name = "";
$name = '';
if (isset($infos[2])) {
$name = explode('"', $infos[2]);

Expand All @@ -160,8 +162,8 @@ public function getStatus($timeout = 1)
if (isset($infos[1])) {
$ping = $infos[1];
}
array_push($players, array('score' => $score, 'ping' => $ping, 'name' => $name));

$players[] = ['score' => $score, 'ping' => $ping, 'name' => $name];
}

array_pop($players);
Expand All @@ -173,8 +175,8 @@ public function getStatus($timeout = 1)
}

$list['numbots'] = 0;
for ($i = 0; $i < sizeof($players); $i++) {
if ($players[$i]['ping'] == 0) {
for ($i = 0, $iMax = sizeof($players); $i < $iMax; $i++) {
if ($players[$i]['ping'] === 0) {
$list['numbots']++;
}
}
Expand Down

0 comments on commit 9217b7d

Please sign in to comment.