2017 © Pedro Peláez
 

library sulphur

Parse and filter game references from Clonk masterservers

image

beheh/sulphur

Parse and filter game references from Clonk masterservers

  • Friday, October 14, 2016
  • by BehEh
  • Repository
  • 1 Watchers
  • 2 Stars
  • 34 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 2 Open issues
  • 11 Versions
  • 0 % Grown

The README.md

Sulphur

Packagist License Travis, (*1)

A full library to parse and filter data from the Clonk masterserver protocol, as used in the games Clonk Rage (http://clonk.de) and OpenClonk (http://openclonk.org)., (*2)

This library was created by Benedict Etzel (developer@beheh.de) and is licensed under the ISC license., (*3)

Installing

Install Sulphur by using composer., (*4)

$ composer require beheh/sulphur

Examples

// fetch masterserver response
$parser = new BehEh\Sulphur\Parser();
$response = $parser->parse(file_get_contents('example.com:80'));

// count all running games
echo count($response->where('State')->is('Running')).' game(s) are currently running';

// iterate through all games currently in lobby
foreach($response->where('State')->is('Lobby') as $reference) {
    echo $reference->Title.' is now open!';
}

// show comment of first game containing "CMC" (case insensitive)
$references = $response->where('Title')->contains('cmc', true);
echo $references[0]->Comment;

// show title of first running league game
$references = $response->where('State')->is('Running')
                       ->where('League')->doesNotExist();
echo $references[0]->Title;

// count games for Clonk Rage or OpenClonk
$references = $response->where('Game')->passes(function($field, $value) { return $value === 'Clonk Rage' || $value === 'OpenClonk'; });
echo count($references).' Clonk Rage and OpenClonk games open';

// print all player names in a reference
foreach($reference->first('PlayerInfos')->all('Client') as $client) {
    foreach($client->all('Player') as $player) {
        echo $player->Name;
    }
}

Basic usage

You can access the master data by using the parser and passing masterserver data., (*5)

use BehEh\Sulphur\Parser;

$parser = new Parser();
$response = $parser->parse($data);

It's recommended to cache this data so the masterserver doesn't blacklist your server., (*6)

Game references

Game sessions are tracked as game references. They can have a variety of fields which describe something about the game., (*7)

Access

To access references, simply call the corresponding functions in the reference object:, (*8)

$references = $response->all();
$references = $response->where('Title')->is('Clepal');
$reference = $response->first('Reference'); // or $response->first()

The calls return an object which should handle like an array., (*9)

Filtering

Responses can be filtered in multiple ways:, (*10)

$response->where('State')->is('Lobby');
$response->where('League')->exists();
$response->where('Comment')->contains('friendly');
$response->where('Comment')->contains('friendly', true); // case insensitive
$response->where('Version')->matches('/4(,[0-9]+){3}/');

Inverse filtering is also available:, (*11)

$response->where('State')->isNot('Running');
$response->where('League')->doesNotExist();
$response->where('Comment')->doesNotContain('bad');
$response->where('Comment')->doesNotContains('bad', true); // case insensitive
$response->where('Version')->doesNotMatch('/5(,[0-9]+){3}/');

You can also use custom callbacks (anything accepted by call_user_func):, (*12)

$response->where('Title')->passes(function($field, $value) { return strlen($value) > 5; });
$response->where('Title')->doesNotPass(function($field, $value) { return strlen($value) <= 3; });

Chain filtering

You can filter multiple fields by repeating calls to where:, (*13)

$response->where('State')->is('Running')->where('League')->exists();
$response->where('State')->is('Lobby')->where('Password')->doesNotExist();

Fields

Fields are key-value pairs and can be read simply by accessing the corresponding (case-sensitive) local variables:, (*14)

echo $reference->Title;
echo $reference->Game;

Subsections

To access fields in a specific section you can use the all and first methods:, (*15)

echo $reference->first('PlayerInfos')->first('Client')->first('Player')->Name;
foreach($reference->all('Resource') as $resource) {
    echo $resource->Filename;
}

The Versions

14/10 2016

dev-master

9999999-dev

Parse and filter game references from Clonk masterservers

  Sources   Download

ISC

The Requires

  • php >=5.3.0

 

clonk openclonk masterserver

14/10 2016

3.0.0

3.0.0.0

Parse and filter game references from Clonk masterservers

  Sources   Download

ISC

The Requires

  • php >=5.3.0

 

clonk openclonk masterserver

25/01 2015

dev-stable

dev-stable

Parse and filter game references from Clonk masterservers

  Sources   Download

ISC

The Requires

  • php >=5.3.0

 

clonk openclonk masterserver

25/01 2015

v2.0.1

2.0.1.0

Parse and filter game references from Clonk masterservers

  Sources   Download

ISC

The Requires

  • php >=5.3.0

 

clonk openclonk masterserver

21/01 2015

v2.0.0

2.0.0.0

Parse and filter game references from Clonk masterservers

  Sources   Download

ISC

The Requires

  • php >=5.3.0

 

clonk openclonk masterserver

11/12 2014

v1.1.2

1.1.2.0

Parse and filter game references from Clonk masterservers

  Sources   Download

MIT

The Requires

  • php >=5.3.0
  • ext-curl *

 

clonk

11/12 2014

v1.1.1

1.1.1.0

Parse and filter game references from Clonk masterservers

  Sources   Download

MIT

The Requires

  • php >=5.3.0
  • ext-curl *

 

clonk

10/12 2014

v1.1.0

1.1.0.0

Parse and filter game references from Clonk masterservers

  Sources   Download

MIT

The Requires

  • php >=5.3.0
  • ext-curl *

 

clonk

18/11 2014

v1.0.0

1.0.0.0

Parse and filter game references from Clonk masterservers

  Sources   Download

MIT

The Requires

  • php >=5.3.0
  • ext-curl *

 

clonk

05/06 2014

v0.1.5

0.1.5.0

Parse and filter game references from Clonk masterservers

  Sources   Download

MIT

The Requires

  • php >=5.3.0
  • ext-curl *

 

clonk

05/06 2014

v0.1.0

0.1.0.0

Parse and filter game references from Clonk masterservers

  Sources   Download

MIT

The Requires

  • php >=5.3.0
  • ext-curl *

 

clonk