harrk/gamejolt-api

用于Game Jolt API的PHP库。

2.1.0 2023-11-25 19:57 UTC

This package is auto-updated.

Last update: 2024-09-25 21:54:07 UTC


README

CircleCI

https://gamejolt.com

此库提供了Game Jolt API的全面覆盖(除批处理外),包括奖杯、分数、会话等。目前此库是为Game Jolt API的v1.2版本编写和测试的。

安装

$ composer require "harrk/gamejolt-api"

用法

创建一个GamejoltApi实例,并提供一个GamejoltConfig以开始对Game Jolt进行API调用。

$gameId = 0; // Your game's ID
$gamePrivaykey = "Your game's private key";

$api = new GamejoltApi(new GamejoltConfig(
    $gameId,
    $gamePrivaykey
));

端点

以下是与每个端点交互的示例。请注意,这些仅是示例,可能不使用所有参数选项。

有关更多选项,请参阅Game Jolt API文档或源代码。

数据存储

可以使用数据存储API在云中存储/检索数据。

// Fetch the value for my-key
$api->dataStore()->fetch('my-key');

// Set the value for my-key to my-value
$api->dataStore()->set('my-key', 'my-value');

// Update player-logins by 1
$api->dataStore()->update('player-logins', DataStore::OPERATION_ADD, 1);

// Remove my-key
$api->dataStore()->remove('my-key');

// Fetch all keys
$api->dataStore()->getKeys();

朋友

朋友API只能用于查找当前用户的朋友ID。

// Fetch all friend ids for the user
$api->friends()->fetch('username', 'user-token');

分数

//Fetch scores
$api->scores()->fetch();

//Fetch all score tables
$api->scores()->tables();

//Add a gamejolt user's score to the table
$api->scores()->addUserScore('username', 'user_token', '100 Jumps', 100);

//Add a guest's score to the table
$api->scores()->addGuestScore('Mr Guest', '50 jumps', 50);

//Get score rank
$api->scores()->getRank(60);

会话

//Open a session for the given user
$api->sessions()->open('username', 'user_token');

//Let GJ know the user session is still active
$api->sessions()->ping('username', 'user_token', Sessions::STATUS_ACTIVE);

//Check if the session is still open for user
$api->sessions()->check('username', 'user_token');

//Close the session for user
$api->sessions()->close('username', 'user_token');

时间

//Get GJ's server time
$api->time()->fetch();

奖杯

//Get all trophies
$api->trophies()->fetch('username', 'user_token');

//User has achieved a trophy with ID 1
$api->trophies()->addAchieved('username', 'user_token', 1);

//User unachieved trophy with ID 1
$api->trophies()->removeAchieved('username', 'user_token', 1);

用户

//Fetch data on user
$api->users()->fetch('username', 'user_token');

//Verify if the user's credentials are correct
$api->users()->auth('username', 'user_token');

捕获异常

以下是如何捕获异常的示例

try {
    $auth = $api->users()->auth('username', 'user_token');
} catch (TimeOutException $e) {
    return redirect()->route('home')->with('error', $e->getMessage());
}

测试

已提供两个命令以简化测试的运行。

运行单元测试

$ composer test

运行静态代码分析

$ composer analyse

待办事项

  • 在端点上允许POST方法
  • 增加单元测试覆盖率

许可

MIT许可