beheh/sulphur

解析并过滤来自 Clonk 服务器主机的游戏引用

3.0.0 2016-10-14 10:00 UTC

This package is auto-updated.

Last update: 2024-09-07 00:02:27 UTC


README

Packagist License Travis

一个完整的库,用于解析和过滤 Clonk 服务器协议中的数据,该协议用于游戏 Clonk Rage (http://clonk.de) 和 OpenClonk (http://openclonk.org)。

此库由 Benedict Etzel (developer@beheh.de) 创建,并使用 ISC 许可证授权。

安装

使用 composer 安装 Sulphur。

$ composer require beheh/sulphur

示例

// 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;
	}
}

基本用法

您可以通过使用解析器并传递服务器数据来访问主数据。

use BehEh\Sulphur\Parser;

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

建议缓存此数据,以免主服务器将您的服务器列入黑名单。

游戏引用

游戏会话作为游戏引用进行跟踪。它们可以有多种字段,用于描述有关游戏的信息。

访问

要访问引用,只需调用引用对象中相应的函数

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

这些调用返回一个对象,其处理方式应类似于数组。

过滤

响应可以通过多种方式过滤

$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}/');

也提供逆过滤

$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}/');

您还可以使用自定义回调(任何由 call_user_func 接受的内容)

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

链式过滤

您可以通过重复调用 where 来过滤多个字段

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

字段

字段是键值对,可以通过访问相应的(区分大小写的)局部变量简单地读取

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

子部分

要访问特定部分的字段,您可以使用 allfirst 方法

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