gs2/gs2-php-sdk

1.4.205 2024-04-21 04:55 UTC

README

⇒README in English

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は、オンライン機能をサポートし、ゲーム開発者がデータの分析や経済管理を容易に行えるようにすることで、ゲームの成功を支援します。

Getting Started

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 を頂いても対応できません。