Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/beheh/sulphur
🔎 Parse and extract data from Clonk game references.
https://github.com/beheh/sulphur
clonk masterserver
Last synced: about 1 month ago
JSON representation
🔎 Parse and extract data from Clonk game references.
- Host: GitHub
- URL: https://github.com/beheh/sulphur
- Owner: beheh
- License: isc
- Created: 2014-06-04T15:19:50.000Z (over 10 years ago)
- Default Branch: master
- Last Pushed: 2016-10-14T10:28:16.000Z (about 8 years ago)
- Last Synced: 2024-07-24T14:45:42.069Z (4 months ago)
- Topics: clonk, masterserver
- Language: PHP
- Homepage: https://packagist.org/packages/beheh/sulphur
- Size: 42 KB
- Stars: 2
- Watchers: 2
- Forks: 1
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# Sulphur
[![Packagist](https://img.shields.io/packagist/v/beheh/sulphur.svg?style=flat-square)](https://packagist.org/packages/beheh/sulphur)
[![License](https://img.shields.io/packagist/l/beheh/sulphur.svg?style=flat-square)](https://packagist.org/packages/beheh/sulphur)
[![Travis](https://img.shields.io/travis/beheh/sulphur/master.svg?style=flat-square)](https://travis-ci.org/beheh/sulphur)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).
This library was created by Benedict Etzel ([email protected]) and is licensed under the ISC license.
## Installing
Install Sulphur by using [composer](https://getcomposer.org/).
```ShellSession
$ composer require beheh/sulphur
```## Examples
```php
// 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.
```php
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.
## Game references
Game sessions are tracked as game references. They can have a variety of fields which describe something about the game.
### Access
To access references, simply call the corresponding functions in the reference object:
```php
$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.
### Filtering
Responses can be filtered in multiple ways:
```php
$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:
```php
$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):
```php
$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`:
```php
$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:
```php
echo $reference->Title;
echo $reference->Game;
```#### Subsections
To access fields in a specific section you can use the `all` and `first` methods:
```php
echo $reference->first('PlayerInfos')->first('Client')->first('Player')->Name;
foreach($reference->all('Resource') as $resource) {
echo $resource->Filename;
}
```