Ticks per Second info

This commit is contained in:
Shoghi Cervantes Pueyo 2012-12-17 19:50:55 +01:00
parent 745c300840
commit f42f2d5e82
4 changed files with 24 additions and 7 deletions

3
TODO
View File

@ -1,4 +1,5 @@
- Save placed blocks and relay them to other players.
- Fix spawn position resetting to 0,128,0
- Mob spawning, item pick up
- Fix metadata orientation
- Fix metadata orientation
- Proper session checks

View File

@ -31,6 +31,7 @@ class ConsoleAPI{
$this->help = array();
$this->server = $server;
$this->input = fopen(FILE_PATH."console.in", "w+b");
$this->last = microtime(true);
}
public function init(){
@ -44,6 +45,11 @@ class ConsoleAPI{
public function defaultCommands($cmd, $params){
switch($cmd){
case "status":
case "lag":
$info = $this->server->debugInfo();
console("[INFO] TPS: ".$info["tps"].", Memory usage: ".$info["memory_usage"]." (Peak ".$info["memory_peak_usage"].")");
break;
case "update-done":
$this->server->api->setProperty("last-update", time());
break;
@ -175,6 +181,7 @@ class ConsoleAPI{
case "help":
case "?":
console("[INFO] /help: Show available commands");
console("[INFO] /status: Show server TPS and memory usage");
console("[INFO] /gamemode: Changes default gamemode");
console("[INFO] /difficulty: Changes difficulty");
console("[INFO] /say: Broadcasts mesages");
@ -197,7 +204,7 @@ class ConsoleAPI{
$this->help[strtolower(trim($cmd))] = array($help, $callback);
}
public function handle(){
public function handle($time){
while(($line = fgets($this->input)) !== false){
$line = trim($line);
if($line === ""){

View File

@ -66,7 +66,7 @@ class ServerAPI extends stdClass{ //Yay! I can add anything to this class in run
console("[NOTICE] Pocket-PHP-Minecraft has been updated at ".date("Y-m-d H:i:s", $last));
console("[NOTICE] If you want to update, get the latest version at https://github.com/shoghicp/Pocket-Minecraft-PHP/archive/master.zip");
console("[NOTICE] This message will dissapear when you issue the command \"/update-done\"");
sleep(5);
sleep(3);
}else{
$last = time();
$this->setProperty("last-update", $last);

View File

@ -28,7 +28,7 @@ the Free Software Foundation, either version 3 of the License, or
require_once("classes/Session.class.php");
class PocketMinecraftServer extends stdClass{
var $preparedSQL, $seed, $protocol, $gamemode, $name, $maxClients, $clients, $eidCnt, $custom, $description, $motd, $timePerSecond, $responses, $spawn, $entities, $mapDir, $mapName, $map, $level, $tileEntities;
var $tickMeasure, $preparedSQL, $seed, $protocol, $gamemode, $name, $maxClients, $clients, $eidCnt, $custom, $description, $motd, $timePerSecond, $responses, $spawn, $entities, $mapDir, $mapName, $map, $level, $tileEntities;
private $database, $interface, $evCnt, $handCnt, $events, $handlers, $version, $serverType, $lastTick;
function __construct($name, $gamemode = 1, $seed = false, $protocol = CURRENT_PROTOCOL, $port = 19132, $serverID = false, $version = CURRENT_VERSION){
$this->port = (int) $port; //19132 - 19135
@ -67,6 +67,7 @@ class PocketMinecraftServer extends stdClass{
$this->spawn = array("x" => 128.5,"y" => 100,"z" => 128.5);
$this->time = 0;
$this->timePerSecond = 10;
$this->tickMeasure = array_fill(0, 40, 0);
$this->setType("normal");
$this->interface = new MinecraftInterface("255.255.255.255", $this->protocol, $this->port, true, false);
$this->reloadConfig();
@ -77,13 +78,19 @@ class PocketMinecraftServer extends stdClass{
$this->stop = false;
}
public function getTPS(){
$v = array_values($this->tickMeasure);
$tps = 40/($v[39] - $v[0]);
return round($tps, 4);
}
public function loadEvents(){
$this->event("onChat", "eventHandler", true);
$this->event("onPlayerDeath", "eventHandler", true);
$this->event("onPlayerAdd", "eventHandler", true);
$this->event("onHealthChange", "eventHandler", true);
$this->action(500000, '$this->time += ceil($this->timePerSecond / 2);$this->trigger("onTimeChange", $this->time);');
$this->action(500000, '$this->time += (int) ($this->timePerSecond / 2);$this->trigger("onTimeChange", $this->time);');
$this->action(5000000, '$this->trigger("onHealthRegeneration", 1);');
$this->action(1000000 * 60, '$this->reloadConfig();');
$this->action(1000000 * 60 * 10, '$this->custom = array();');
@ -130,6 +137,7 @@ class PocketMinecraftServer extends stdClass{
public function debugInfo($console = false){
$info = array();
$info["tps"] = $this->getTPS();
$info["memory_usage"] = round((memory_get_usage(true) / 1024) / 1024, 2)."MB";
$info["memory_peak_usage"] = round((memory_get_peak_usage(true) / 1024) / 1024, 2)."MB";
$info["entities"] = $this->query("SELECT count(EID) as count FROM entities;", true);
@ -140,7 +148,7 @@ class PocketMinecraftServer extends stdClass{
$info["actions"] = $info["actions"]["count"];
$info["garbage"] = gc_collect_cycles();
if($console === true){
console("[DEBUG] Memory usage: ".$info["memory_usage"]." (Peak ".$info["memory_peak_usage"]."), Entities: ".$info["entities"].", Events: ".$info["events"].", Actions: ".$info["actions"].", Garbage: ".$info["garbage"], true, true, 2);
console("[DEBUG] TPS: ".$info["tps"].", Memory usage: ".$info["memory_usage"]." (Peak ".$info["memory_peak_usage"]."), Entities: ".$info["entities"].", Events: ".$info["events"].", Actions: ".$info["actions"].", Garbage: ".$info["garbage"], true, true, 2);
}
return $info;
}
@ -310,7 +318,8 @@ class PocketMinecraftServer extends stdClass{
public function tick(){
$time = microtime(true);
if($this->lastTick <= ($time - 0.05)){
$this->lastTick = $time;
$this->tickMeasure[] = $this->lastTick = $time;
array_shift($this->tickMeasure);
$this->trigger("onTick", $time);
}
}