Skip to content

Examples v3

Austin Bischoff edited this page Mar 2, 2015 · 13 revisions

GameQ v3 Examples

Simple

Here is a simple example to get you off the ground. Note that the port used is the client connect port not the server query port. A port is always required as part of the host key.

// Define the servers you wish you query
$servers = [
    [
    'type'    => 'css',
    'host'    => '127.0.0.1:27015',
    ]
];

$GameQ = new \GameQ\GameQ(); // or $GameQ = \GameQ\GameQ::factory();
$GameQ->addServers($servers);
$GameQ->setOption('timeout', 5); // seconds

$results = $GameQ->process();
print_r($results);

What do these settings mean? See configuration for help.

Different client and query ports

Many games such as Source use the same port for both the client and the server query. For games that do not GameQ tries to determine the query port based on known offsets for the game. In some cases the server can define the specific query port in which case GameQ has no idea how to find it. For cases like these you will need to specify the query_port within the server information.

$GameQ = new \GameQ\GameQ();
$GameQ->addServer([
    'type'    => 'ut3',
    'host'    => '127.0.0.1:6500',
    'options' => [
        'query_port' => 6502,
    ],
]);

...

Loading servers from a file or set of files

If your list of servers does not change often you can use a JSON based file to define your servers and have them loaded in to GameQ. This only supports JSON currently. Any file paths passed that are invalid, unreadable or are improperly formatted are ignored.

$GameQ = new \GameQ\GameQ();
$GameQ->addServersFromFiles('/path/to/your/servers.json');
'''

You can also pass an array of files to load if you have multiple lists you maintain.

// List of JSON files
$files = [
    '/path/to/your/server_list1.json',
    '/path/to/your/server_list2.json',
    ...
];

$GameQ = new \GameQ\GameQ();
$GameQ->addServersFromFiles($files);
...

The format for the JSON file is the same as a the addServers() method.

[
  {
    "type": "cs16",
    "host": "127.0.0.1:27015"
  },
  {
    "type": "css",
    "host": "127.0.0.1:27016"
  }
]

If you need help making a JSON file you can use PHP's json_encode() function on an array of servers and then save that to a file.

Teamspeak 2 & 3

For Teamspeak versions 2 and 3 the query_port server option is required due to how Teamspeak operates. Note the ports used are the defaults and your server may have different ports.

$GameQ = new \GameQ\GameQ();
$GameQ->addServer([
    'type'    => 'teamspeak3',
    'host'    => '127.0.0.1:9987',
    'options' => [
        'query_port' => 10011,
    ],
]);

...