PhP script to send stats to console (top 5 players) ?

Locked
Dangerdanger
Posts: 4
Joined: Tue Jul 29, 2008 7:49 pm

PhP script to send stats to console (top 5 players) ?

Post by Dangerdanger »

Hello. I was wondering if someone out there could help me with this script.

The run down..

I have a quake 3 server and a website.

I have setup VSP stats so players can see their rank when they visit my site.

What I'm trying to do is set it up so I can run a .php file that sends the top 5 players of the vsp stats to the game console.

I have a script and when I direct my browser to the location it reads out the info on the browser. So everything is connected to the database where vsp is running, but the info never reaches the console.

Here is the script if someone could check it out and give me some feed back.

Code: Select all

<?php
/*-------------------------------------------------------------------------*/
// Set up the variables
/*-------------------------------------------------------------------------*/

// ----------------------------------------
// Set number of players to list
// ----------------------------------------
$limit = "5";


// ----------------------------------------
// Variables to connect to game server
// ----------------------------------------

$rconpassword = "*****";
$port = "27960"; // default quake port


// ----------------------------------------
// MYSQL variables:
// ----------------------------------------

$db_host = "localhost";      //Address of database server (localhost or IP)
$db_db   = "****";        //Database to use
$db_user = "****";        //Username of database (read rights)
$db_pwd  = "****";       //Password
$table_prefix = "vsp_"; //Table prefix you use in vsp


/*-------------------------------------------------------------------------*/
// Connect to database and tables
/*-------------------------------------------------------------------------*/

mysql_connect($db_host,$db_user,$db_pwd);

// ----------------------------------------
// Select which database to use
// ----------------------------------------

mysql_select_db($db_db);

// ----------------------------------------
// Connect or die if fails
// ----------------------------------------

$db_link = mysql_connect($db_host, $db_user, $db_pwd)
or die("Connection with ".$db_host." failed");


// ----------------------------------------
// mysql query
// ----------------------------------------


$table = $table_prefix."playerprofile";

$query = "SELECT  playerID, playerName, MAX(skill) AS max_skill FROM $table GROUP by skill ORDER BY skill DESC LIMIT $limit";


/*-------------------------------------------------------------------------*/
// Query database and conect to game server.
// If query failes -> no records stop else fetch rows and send msg.
/*-------------------------------------------------------------------------*/



if(!($result = mysql_query($query, $db_link))) {
	$error_msg .= "No records found with query ".$query;


} else {



$msg = " ";

	// ----------------------------------------
	// fetch mysql result into an array
	// ----------------------------------------
	$rank = "1";
	while ($result_array = mysql_fetch_array($result)) {
  $playerID    = $result_array['playerID'];
  $playerName  = $result_array['playerName'];
  $playerSkill = $result_array['max_skill'];

  $msg = $msg."^5Current Player ranked number ".$rank." is ^7".$playerID." Skill: ".$playerSkill."\n";  // Change text to your liking
  $rank++;

	}
	// ----------------------------------------
	//for debug, run file on webserver
	// ----------------------------------------

  print($msg."<br>");

	// ----------------------------------------
	// Script to send the message to the CoD console
	// ----------------------------------------

	$start = "\xff\xff\xff\xffrcon " . $rconpassword . " ";
	// ----------------------------------------
	// Open socket
	// ----------------------------------------

	$fp = fsockopen("udp://8.2.122.62", $port, $errno, $errstr, 10);


	// ----------------------------------------
	// Error checking
	// ----------------------------------------

	if (!$fp) {
   echo "$errstr ($errno)<br />\n";
	} else {
   stream_set_timeout($fp, 2);   // Set a low timeout
  // echo $msg."<br>";


   // Send commands
  fputs($fp, $start . "say " . $msg . "\n");
  fclose($fp);


  } //end if
	}

exit;
?>
It shows the stats on the browser but doesn't send the info to the game server.

Ps. I didn't write this script.

Thanks. :q3:
^misantropia^
Posts: 4022
Joined: Sat Mar 12, 2005 6:24 pm

Re: PhP script to send stats to console (top 5 players) ?

Post by ^misantropia^ »

Code: Select all

$fp = fsockopen("udp://8.2.122.62", $port, $errno, $errstr, 10);
Is 8.2.122.62 the address of your server? A quick port scan reveals no Q3 server running there.
Dangerdanger
Posts: 4
Joined: Tue Jul 29, 2008 7:49 pm

Re: PhP script to send stats to console (top 5 players) ?

Post by Dangerdanger »

YEah .That's the address. It's through a game server provider. I think that is where the issue is though.. In that code you qouted. My webhosting company must not allow fsockopen or something.
^misantropia^
Posts: 4022
Joined: Sat Mar 12, 2005 6:24 pm

Re: PhP script to send stats to console (top 5 players) ?

Post by ^misantropia^ »

Replace the call to fputs() with this:

Code: Select all

$msg = "{$start}say {$msg}\n";
fputs($fp, $msg) == strlen($msg) || echo 'Write error.';
Does it complain about a write error now?
Dangerdanger
Posts: 4
Joined: Tue Jul 29, 2008 7:49 pm

Re: PhP script to send stats to console (top 5 players) ?

Post by Dangerdanger »

Yeah. That helped a lot. I guess I had it set to writable by group. I fixed it.

The script seems to work now but doesn't send the info to the to the server.
Dangerdanger
Posts: 4
Joined: Tue Jul 29, 2008 7:49 pm

Re: PhP script to send stats to console (top 5 players) ?

Post by Dangerdanger »

k so. I figured it out.

The main issue was the my website host needed to open port 27960 (or whatever port you have quake setup on)

IF you are running vsp stats and want to send the top 5 players to the console, here is the script. 100% workin for php 5

Code: Select all

<?php

// - edit - this is  Open Source of course....  

/*-------------------------------------------------------------------------*/
// Set up the variables
/*-------------------------------------------------------------------------*/

// ----------------------------------------
// Set number of players to list (top 5)
// ----------------------------------------
$limit = "5";

// ----------------------------------------
// MYSQL variables:
// ----------------------------------------

$db_host = "localhost"; //Address of database server (localhost)
$db_db = "******"; //Database to use
$db_user = "*******"; //Username of database
$db_pwd = "**********"; //Password
$table_prefix = "vsp_"; //Table prefix you use in vsp change if needed

/*-------------------------------------------------------------------------*/
// Connect to database and tables
/*-------------------------------------------------------------------------*/

mysql_connect($db_host,$db_user,$db_pwd);

// ----------------------------------------
// Select which database to use
// ----------------------------------------

mysql_select_db($db_db);

// ----------------------------------------
// Connect or die if fails
// ----------------------------------------

$db_link = mysql_connect($db_host, $db_user, $db_pwd)
or die("Connection with ".$db_host." failed");


// ----------------------------------------
// mysql query
// ----------------------------------------


$table = $table_prefix."playerprofile";
$query = " SELECT  playerID, playerName, MAX(skill) AS max_skill".
         " FROM $table ".
         " GROUP by skill ".
         " ORDER BY skill DESC ".
         " LIMIT $limit";

/*-------------------------------------------------------------------------*/
// Query database and conect to game server.
// If query failes -> no records stop else fetch rows and send msg.
/*-------------------------------------------------------------------------*/

if(!($result = mysql_query($query, $db_link))) {
  $msg = "No data on best player available yet";
  //print($msg)
} else {
  $msg = " ";

// ----------------------------------------
// fetch mysql result into an array
// ----------------------------------------
  $rank = "1";

  while ($result_array = mysql_fetch_array($result)) {
    $playerID    = $result_array['playerID'];
    $playerName  = $result_array['playerName'];
    $playerSkill = $result_array['max_skill'];

    $msg = "^5Current Player ranked number ".$rank." is ^7".$playerID." ^5Skill: ^7".$playerSkill.""; 
    $rank++;
    print($msg);
    send_msg($msg);
  }
}


// ----------------------------------------
//for debug, run file on webserver
// ----------------------------------------



// ----------------------------------------
// function to connect to game server and
// send the message to the q3a console
// ----------------------------------------

function send_msg($msg){
  $rconpassword = "your rcon password here";
  $port = "27960"; // default quake port. Change if needed
  $start = "\xff\xff\xff\xffrcon " . $rconpassword . " ";

// ----------------------------------------
// Open socket
// ----------------------------------------

  $fp = fsockopen("udp://change to your ip of gameserver. Just ip, no port", $port, $errno, $errstr, 10);

// ----------------------------------------
// Error checking
// ----------------------------------------

  if (!$fp) {
    echo "$errstr ($errno)<br />\n";
  } else {
    stream_set_timeout($fp, 2);   // Set a low timeout

  // Send commands
   fputs($fp, $start . "say " . $msg . "\n");
   fclose($fp);


  } // end if

// ----------------------------------------
// Change to your liking    sleep(seconds)
// ----------------------------------------
sleep(4);
}  // end function send_msg

?>
Copy all the text in the code brackets and paste into a note pad file then rename to top5.php.
You can run it by directing your web browser to the file location. ie http://www.yoursite.com/top5.php

THe best way to run it is through cron jobs. Here is the cron job.

/usr/bin/php -q /home/your user name/public_html/top5.php

PZ ggz. :q3:

Oh yeah. Set the top5.php file to chmod 755
Last edited by Dangerdanger on Wed Aug 06, 2008 8:03 pm, edited 1 time in total.
^misantropia^
Posts: 4022
Joined: Sat Mar 12, 2005 6:24 pm

Re: PhP script to send stats to console (top 5 players) ?

Post by ^misantropia^ »

Nice that you got it to work. Cheers!
Locked