Last active
October 14, 2018 00:40
-
-
Save lagonnebula/7928214 to your computer and use it in GitHub Desktop.
Get Players connected in your Starbound server. You can get Server Version, Who is online and Server status
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* a PHP class for checking server status and who is connected. | |
* | |
* @author MeuhMeuh starbound.meuhmeuh.fr | |
* @copyright (c) 2013, Jeremy Villemain | |
*/ | |
class Logs{ | |
private $etat; | |
private $players; | |
private $time; | |
private $version; | |
private $path = "/path/to/your/starbound/server/starbound_server.log"; | |
private $host = "127.0.0.1"; | |
private $port = 21025; | |
private $timeout = 30; | |
function Logs(){ | |
$this->players = null; | |
$this->etat = 0; | |
$this->time = microtime(); | |
$this->version = "unknown"; | |
$this->checkOnline(); | |
if($this->etat){ | |
$this->parseClient(); | |
} | |
} | |
/** | |
* little parser for the logs file | |
* | |
* use a preg_match to check a connected ora disconnected user | |
*/ | |
private function parseClient(){ | |
$file = fopen($this->path,"r"); | |
if($file){ | |
while(($line = fgets($file))){ | |
if(preg_match("%Info: Client '([a-z0-9\s]*)' <([0-9]*)> \(([0-9\.\:]*)\) ([a-z]*)%i", $line, $val)){ | |
if(isset($val[1]) && !empty($val[1])){ | |
if($val[4] =="disconnected"){ | |
$this->players[$val[1]] = 0; | |
}else{ | |
$this->players[$val[1]] = 1; | |
} | |
} | |
}else if(preg_match("%Info: Server version '([0-9a-zA-Z\.\s]*)' '([0-9]*)' '([0-9]*)'%i", $line, $val)){ | |
//Info: Server version 'Beta v. Indignant Koala' '622' '424' | |
if(isset($val) && count($val)>1){ | |
$this->version = ""; | |
for($i=1; $i<count($val);$i++){ | |
$this->version .= $val[$i]." "; | |
} | |
} | |
}else{ | |
//nothing to do | |
//adding some parsing line | |
} | |
} | |
fclose($file); | |
} | |
} | |
/** | |
* getter for server version | |
* thanks to https://gist.github.com/dkesberg/7926899 didn't see the server version | |
* | |
* @return string server version | |
*/ | |
public function getServerVersion(){ | |
return $this->version; | |
} | |
/** | |
* getter for the executed script time | |
* | |
* @return int the time since the status check | |
*/ | |
public function getTime(){ | |
return (microtime() - $this->time) * 1000; | |
} | |
/** | |
* getter for the max player | |
* | |
* @return int the number of player seen by the server | |
*/ | |
public function maxPlayers(){ | |
return count($this->players); | |
} | |
/** | |
* count the number of players connected | |
* | |
* @return int number of players | |
*/ | |
public function countPlayers(){ | |
$nb = 0; | |
if($this->etat && $this->players !=null){ | |
foreach($this->players as $k=>$v){ | |
if($v) | |
$nb ++; | |
} | |
} | |
return $nb; | |
} | |
/** | |
* get the player connected | |
* | |
* @return array with players connected | |
*/ | |
public function listPlayers(){ | |
$player = null; | |
if($this->etat && $this->players!=null){ | |
foreach($this->players as $k=>$v){ | |
if($v) | |
$player[] = $k; | |
} | |
return $player; | |
} | |
return $player; | |
} | |
/** | |
* getter for the server status | |
* | |
*@return int status of the server, 0 or 1 | |
*/ | |
public function getStatus(){ | |
return $this->etat; | |
} | |
/** | |
* Use fsocketopen, to see if server is up or not | |
* @param string $this->host hostname | |
* @param int $this->port port to check | |
* @param int $this->timeout timeout for the fsocketopen | |
*/ | |
private function checkOnline(){ | |
$fp = @fsockopen($this->host, $this->port, $errno, $errstr, $this->timeout); | |
if($fp){ | |
$this->etat = 1; | |
fclose($fp); | |
}else{ | |
$this->etat = 0; | |
} | |
} | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment