gs2/gs2-php-sdk

1.4.245 2024-09-26 08:33 UTC

This package is auto-updated.

Last update: 2024-09-26 08:34:14 UTC


README

⇒英文 README

gs2-php-sdk

Game Server Services(https://gs2.io) 是用于 PHP 的 SDK。

什么是 Game Server Services

Game Server Services(GS2) 是针对游戏开发的专业后端服务器服务(BaaS)。

GS2 是一个通用游戏服务器的解决方案,旨在提高游戏开发者的效率,并支持 Games as a Service(GaaS) 和 Live Gaming 等功能。

此服务允许灵活管理玩家数据和分析数据,可以适当地分析游戏内资源的流通和消耗量,从而维护健康的环境。此外,它还提供了故事进度管理、物品管理等功能,有助于游戏收益化和提高玩家参与度。GS2 支持在线功能,通过使开发者能够轻松进行数据分析和经济管理,从而支持游戏的成功。

入门指南

使用 SDK 需要GS2的凭证。请按照 GS2的设置 的说明步骤获取凭证。

运行条件

  • PHP7.1+

⇒开始使用GS2 - SDK - 各种编程语言

示例

GS2-Account 示例

初始化处理

use Gs2\Account\Gs2AccountRestClient;
use Gs2\Core\Model\BasicGs2Credential;
use Gs2\Core\Model\Region;
use Gs2\Core\Net\Gs2RestSession;

$session = new Gs2RestSession(
    new BasicGs2Credential(
        "your client id",
        "your client secret"
    ),
    Region::AP_NORTHEAST_1
);

$session->open();

$client = new Gs2AccountRestClient(
    $session
);

同步处理

use Gs2\Account\Model\ScriptSetting;
use Gs2\Account\Request\CreateNamespaceRequest;
use Gs2\Core\Exception\Gs2Exception;
use PHPUnit\Framework\Assert;

try {
    $result = $client->createNamespace(
        (new CreateNamespaceRequest())
            ->withName('namespace-0001')
            ->withAuthenticationScript(
                (new ScriptSetting())
                    ->withTriggerScriptId('script-0001')
            )
    );
    
    Assert::assertNotNull($result->getItem());
    Assert::assertEquals('namespace-0001', $result->getItem()->getName());
    Assert::assertEquals('script-0001', $result->getItem()->getAuthenticationScript()->getTriggerScriptId());
} catch (Gs2Exception $e) {
    Assert::fail($e->getMessage());
}

异步处理

use Gs2\Account\Model\ScriptSetting;
use Gs2\Account\Request\CreateNamespaceRequest;
use Gs2\Account\Result\CreateNamespaceResult;
use Gs2\Core\Exception\Gs2Exception;
use PHPUnit\Framework\Assert;

// 非同期処理をハンドリングするための Promise が返る
$promise = $client->createNamespaceAsync(
    (new CreateNamespaceRequest())
        ->withName('namespace-0001')
        ->withAuthenticationScript(
            (new ScriptSetting())
                ->withTriggerScriptId('script-0001')
        )
)->then(
    function (CreateNamespaceResult $result) {
        // コールバック形式でハンドリングしたい場合は成功時のハンドラーをここに記述
        Assert::assertNotNull($result->getItem());
        Assert::assertEquals('namespace-0001', $result->getItem()->getName());
        Assert::assertEquals('script-0001', $result->getItem()->getAuthenticationScript()->getTriggerScriptId());
    },
    function (Gs2Exception $e) {
        // コールバック形式でハンドリングしたい場合は失敗時のハンドラーをここに記述
        Assert::fail($e->getMessage());
    }
);

try {
    // Promise を wait することで処理が実行される。戻り値には成功時の結果が返り、失敗時には例外が発生する。
    $result = $promise->wait();
    Assert::assertNotNull($result->getItem());
    Assert::assertEquals('namespace-0001', $result->getItem()->getName());
    Assert::assertEquals('script-0001', $result->getItem()->getAuthenticationScript()->getTriggerScriptId());
} catch (Gs2Exception $e) {
    Assert::fail($e->getMessage());
}

关于 Promise 的文档请参阅 https://github.com/guzzle/promises

SDK 详细规格

有关各种服务和通信方式的详细信息,请参阅

⇒API 参考文档

⇒API 参考文档 - 初始化处理

由于本项目代码除 gs2-php-sdk-core 以外都是自动生成的,因此无法单独处理 Pull-Request。