hugojf/csgo-server-api-laravel

与CS:GO服务器通信的简单方法。

v2.0 2019-11-04 06:55 UTC

This package is auto-updated.

Last update: 2024-09-04 17:54:31 UTC


README

Laravel包,用于接口CS:GO服务器API

Build Status Coverage Status

工作原理

此包接口我的CS:GO API主要端点

/send,您可以通过延迟向单个服务器发送命令;

/sendAll,您可以通过延迟向由API管理的所有服务器发送命令;

要求

安装

使用Composer,运行

composer require hugojf/csgo-server-api-laravel

发布配置

php artisan vendor:publish --provider="hugojf\CsgoServerApi\Providers\PackageServiceProvider"

配置

任何配置都可以在configs/csgo-api.php内修改

CSGO_API_URL=http://my-csgo-server-api.com/

API服务器端点URL。

CSGO_API_KEY=abcdef123456

API服务器认证密钥。

用法

创建一个命令

$myCommand = new Command($command, $delay = 0, $wait = false);
参数
  • $command是要执行的命令;
  • $delay告诉API在发送命令前应等待多长时间;
  • $wait告诉API在响应请求前等待服务器响应。
示例
// Get server stats
$statsCommand = new Command('stats', 0,  true);

// Kick bots
$botsCommand = new Command('bot_kick');

// Schedule say message
$sayCommand = new Command('say Hi!', 30000)

创建一个服务器

$myServer = new Server($address, $port);
参数
  • $address是完整地址或仅IP;
  • $port是服务器端口。
示例
// Using IP and Port
$server1 = new Server('177.54.150.15', 27001);

// Using full address
$server2 = new Server('177.54.150.15:27002');

向一组服务器发送命令列表

// Sends `stats` and `status` to both servers
//
// You can replace `direct` with `to`, they are the same
CsgoApi::direct(ByCommandSummary::class)->addCommand([
    new Command('stats', 1500, true),
    new Command('status', 1500, true),
])->addServer(
    new Server('177.54.150.15:27001'),
    new Server('177.54.150.15:27002'),
)->send();

// Expected response:
//  [
//      "stats"  => [
//          "177.54.150.15:27001" => "response-1",
//          "177.54.150.15:27001" => "response-2",
//      ],
//      "status" => [
//          "177.54.150.15:27001" => "response-3",
//          "177.54.150.15:27001" => "response-4",
//      ],
//   ]

向API管理的所有服务器广播命令列表

// Sends `say` and `quit` to all servers
//
// You can replace `broadcast` with ``, they are the same
CsgoApi::broadcast(ByCommandSummary::class)->addCommand([
    new Command('say "Closing server for maintenance in 30 seconds', 0),
    new Command('say "Closing server for maintenance in 15 seconds', 15000),
    new Command('say "Closing server for maintenance in 5 seconds', 25000),
    new Command('quit', 30000),
])->send();

添加命令或服务器的方法

为了尝试避免实例化每个服务器或命令,您可以使用以下格式

服务器
// Creating a DirectSender
$sender = CsgoApi::direct();

// Using full Server object
//
// All 4 methods are identical, use what feels right
$sender->addServer(new Server('177.54.150.15:27001'));
$sender->addServers(new Server('177.54.150.15:27001'));
$sender->server(new Server('177.54.150.15:27001'));
$sender->servers(new Server('177.54.150.15:27001'));

// Using list of Server objects
$sender->addServer([
    new Server('177.54.150.15:27001'),
    new Server('177.54.150.15:27002'),
]);

// Using string address
$sender->addServer('177.54.150.15:27002');

// Using list of string addresses
$sender->addServer([
    '177.54.150.15:27001',
    '177.54.150.15:27002',
]);

// Using IP and Port separately
$sender->addServer('177.54.150.15', 27002);

// Using list of IP and Ports
$sender->addServer([
    ['177.54.150.15', 27001],
    ['177.54.150.15', 27002],
]);
命令
// Creating a DirectSender
$sender = CsgoApi::direct();

// Using full Command object
//
// All 4 methods are identical, use what feels right
$sender->addCommand(new Command('stats', 1000, false));
$sender->addCommands(new Command('stats', 1000, false));
$sender->command(new Command('stats', 1000, false));
$sender->commands(new Command('stats', 1000, false));

// Using list of Command objects
$sender->addCommandItem([
    new Command('stats', 1500, false),
    new Command('status', 1500, false),
]);

// Using command parameters directly
$sender->addCommand('stats', 1500, false);

// Using list of command parameters
$sender->addCommand([
    ['stats', '1500', false],
    ['status', '1500', false],
]);

更改摘要类

您可以通过传递新的Summary类来更改响应分组的方式

ByCommandSummary::class

首先按命令分组,然后按服务器分组。

// Example response 
//  [
//      "stats"  => [
//          "177.54.150.15:27001" => "response-1",
//          "177.54.150.15:27002" => "response-2",
//      ],
//      "status" => [
//          "177.54.150.15:27001" => "response-3",
//          "177.54.150.15:27002" => "response-4",
//      ],
//   ]
ByServerSummary::class

首先按服务器分组,然后按命令分组。

// Example response
//  [
//      "177.54.150.15:27001"  => [
//          "stats" => "response-1",
//          "status" => "response-3",
//      ],
//      "177.54.150.15:27002" => [
//          "stats" => "response-2",
//          "status" => "response-4",
//      ],
//   ]
示例
$directByCommandSender = CsGoApi::direct(ByCommandSummary::class);
$directByServerSender = CsGoApi::direct(ByServerCummary::class);

$broadcastByCommandSender = CsGoApi::broadcast(ByCommandSummary::class);
$broadcastByServerSender = CsGoApi::broadcast(ByServerCummary::class);