Exporting online player list to web via FLHook
-
I’m interested in having a list of current players on my Freelancer server listed on my web server. I would like it to be relatively real time (maybe polling the server every 5 minutes) versus being a tool like FLStats that scans the account files and provides data about all accounts.
Are there any current tools or routines out there to do this?
I could envision a script that use the remote port for flhook to use “getplayers”, and parse that out to an online list of users which could be formatted and then uploaded using something like NcFTP to automatically update the web server.
If anyone aware of any existing tools or scripts that do this, I would appreciate you pointing me towards them.
Thanks!
-
array(0,1)); function connect($ip, $port){ $this->con = fsockopen($ip,$port,$this->errn,$this->errs,$this->timeout); } function quit(){ fclose($this->con); } function sendCmd($cmd, $param=""){ fwrite($this->con, $cmd . " " .$param."\r\n"); while(1){ $output .= fgetc($this->con); //\n and space used incase a player is named OK or ERR if((trim(substr($output,-3,3) == "\nOK")) || (trim(substr($output,-4,4) == "ERR "))){ break; } else{ continue; } } return trim($this->cleanOutput($output)); } function processCmd($cmd, $params=""){ switch($this->cmds[$cmd][0]){ case 0: $params = str_replace(" ","",$params); $output = $this->cleanOutput($this->sendCmd($cmd, $params)); break; case 1: $params = str_replace(",","",$params); $params = preg_replace("/[a-zA-Z]/","",$params); $output = $this->cleanOutput($this->sendCmd($cmd, $params)); break; case 2: $output = $this->cleanOutput($this->sendCmd($cmd, $params)); break; } switch($this->cmds[$cmd][1]){ //No return needed, other than OK case 0: if(trim(substr($output,-2,2) == "OK")){ return true; } else{ $this->error = trim($output); return false; } break; //Data needs to be returned case 1: if(trim(substr($output,-2,2) == "OK")){ return substr($output,0,(strlen($output) - 2)); } else{ $this->error = trim($output); return false; } break; } } function login($pass){ $check = $this->sendCmd("pass", $pass); if($check == $this->ok){ $this->loggedIn = true; } else{ $this->loggedIn = false; $this->error = $check; } } function cleanOutput($output){ global $flhookHeader; //Removes flhook welcome message, so we only have the data returned by the server after sending a command. $remove = array("/".$flhookHeader."/"); $output = preg_replace($remove,"",$output); return trim($output); } } $flhook = new admin(); $flhook->connect($ip, $port); if($flhook->con){ $flhook->login($pass); if($flhook->loggedIn){ $players = explode("\n",$flhook->processCmd("getplayers")); foreach($players as $playerline){ if(preg_match("/charname=(.+?) /",$playerline,$matches)){ echo $matches[1]." \r\n"; } } } else{ echo "Error: " . $flhook->error; } } ?>
-
Yeah, i use this class too, it was initially posted on HHC forums i think.
Then parse the output and store it in DB or something.
edit: regarding the end of previous script, i’m not too familiar with regexps but i think strpos() would be better resource-wise there.
-
Perfect. Thanks Alex!
I’m extremely new to php, but I would like to simply sort the output alphabetically. Would you happen to already have code to do that?
In the mean time, I’m going to try to figure it out on my own as well. I assume I would load the output into an array, then sort it, then echo out the array.
If I get it figured out, I’ll post my results back up for comment.
Edit: Ok… wow… duh. The data is already in the array $players. Found the sort command and added
sort($players);
right before I echo them out.