• This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn more.

PageCallback versus my function

#1
So I'll be displaying my blissful ignorance here in OOP

I've got some code that looks like this (that works a treat in a single php file):
PHP:
<?php
/* SOURCE ENGINE QUERY FUNCTION, requires the server ip:port */

function source_query($ip){
    $cut = explode(":", $ip);
    $HL2_address = $cut[0];
    $HL2_port = $cut[1];

    $HL2_command = "\377\377\377\377TSource Engine Query\0";

    $HL2_socket = fsockopen("udp://".$HL2_address, $HL2_port, $errno, $errstr,3);
    fwrite($HL2_socket, $HL2_command);
    $JunkHead = fread($HL2_socket,4);
    $CheckStatus = socket_get_status($HL2_socket);

    if($CheckStatus["unread_bytes"] == 0)return 0;

    $do = 1;
    while($do){
        $str = fread($HL2_socket,1);
        $HL2_stats.= $str;
        $status = socket_get_status($HL2_socket);
        if($status["unread_bytes"] == 0){
               $do = 0;
        }
    }
    fclose($HL2_socket);

    $x = 0;
    while ($x <= strlen($HL2_stats)){
        $x++;
        $result.= substr($HL2_stats, $x, 1);
    }

    // ord ( string $string );
    $result = str_split($result);
    $info['network'] = ord($result[0]);$char = 1;
    while(ord($result[$char]) != "%00"){$info['name'] .= $result[$char];$char++;}$char++;
    while(ord($result[$char]) != "%00"){$info['map'] .= $result[$char];$char++;}$char++;
    while(ord($result[$char]) != "%00"){$info['dir'] .= $result[$char];$char++;}$char++;
    while(ord($result[$char]) != "%00"){$info['description'] .= $result[$char];$char++;}$char++;
    $info['appid'] = ord($result[$char].$result[($char+1)]);$char += 2;
    $info['players'] = ord($result[$char]);$char++;
    $info['max'] = ord($result[$char]);$char++;
    $info['bots'] = ord($result[$char]);$char++;
    $info['dedicated'] = ord($result[$char]);$char++;
    $info['os'] = chr(ord($result[$char]));$char++;
    $info['password'] = ord($result[$char]);$char++;
    $info['secure'] = ord($result[$char]);$char++;
    while(ord($result[$char]) != "%00"){$info['version'] .= $result[$char];$char++;}

    return $info;
}

$q = source_query('131.203.119.140:27015');

echo "network: ".$q['network']."<br/>\n";
echo "name: ".$q['name']."<br/>\n";
echo "map: ".$q['map']."<br/>\n";
?>
Now I want to put this in an integrated page using pages and PageCallback
so here goes:
PHP:
<?php

class FPS_HomePage_PageCallback_Servers
{
        public static function response(XenForo_ControllerPublic_Abstract $controller, XenForo_ControllerResponse_Abstract $response)
        {

/* SOURCE ENGINE QUERY FUNCTION, requires the server ip:port */

        $fps_server = source_query('131.203.119.140:27015');

                // send the response to the template
                $response->templateName = 'FPS_servers';
                // I'll have more than one server soon
                $response->params += array(
                        'fps_nz' => $fps_server
                );
        }

}
        function source_query($ip){
                $result = "";
                $cut = explode(":", $ip);
                $HL2_address = $cut[0];
                $HL2_port = ....
... (skip the boring bits)
         }
it's as if no data is passed to the function source_query() so nothing seems to work
... see .. I don't really understand classes etc :)

Can somebody help please?
Thanks
-TA
 

Shadab

Well-known member
#2
What's the exact output when you dump the value right after the fn' returns it, like:
PHP:
$fps_server = source_query('131.203.119.140:27015');
Zend_Debug::dump($fps_server, 'FPS Server Query');
Also, If you are comfortable using a debugger, have you tried stepping through the code yet?
 
#3
actually you put me in the right direction (perhaps again? :) )
http://www.fps.net.nz/community/pages/servers/

xF requires me to be more vigilant creating variables .. not just willy nilly add stuff like I often do
example:
$x = 0;
$y.=$x;
echo $y; // returns undefined variable in xenforo

so the fix was basically to build a blank array before populating it
$info = array("network"=>"","name"=>"","map"=>"","dir"=>"","description"=>"","appid"=>"","players"=>"","max"=>"","bots"=>"","dedicated"=>"","os"=>"","password"=>"","secure"=>"","version"=>"");

plus a couple of other variables needed initialising ($results, and $HL2_stats)

thanks for your help
now to make my page look pretty :confused: