laspi94/csgo-server-api-laravel

与CS 2服务器通信的一种简单方式。

1.0.0 2023-07-12 02:31 UTC

This package is auto-updated.

Last update: 2024-09-14 22:10:19 UTC


README

Laravel包,用于与CS:GO Server API接口。

如何工作

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

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

/sendAll 可以向API控制的全部服务器发送命令,并延迟发送;

要求

安装

使用Composer运行(Laravel/Framework ^10.0)

composer require laspi94/csgo-server-api-laravel:1.0.0

发布配置

php artisan vendor:publish --provider="laspi94\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 是服务器端口。
示例
/** 
 * Uso de IP y puerto
 */
$server1 = new Server('177.54.150.15', 27001);

/** 
 * Usando la dirección completa
 */  
$server2 = new Server('177.54.150.15:27002');

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

/** 
 * Puede reemplazar `direct` con` to ', son los mismos  
 */ 
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();

/** 
 * Respuesta esperada:
 * [
 *     "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控制的所有服务器广播命令列表

/** 
 * Envía `say` y `quit` a todos los servidores
 * 
 * Puedes reemplazar `broadcast` con ``, son lo mismo
 */ 
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();

添加命令或服务器的方式

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

服务器
/**
 * Creación de un remitente directo
 */ 
$sender = CsgoApi::direct();

/**
 * Usando el objeto de servidor completo
 * 
 * Los 4 métodos son idénticos, use lo que se sienta bien
 * 
 */
$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'));

/**
 * Uso de la lista de objetos del servidor 
 */
$sender->addServer([
    new Server('177.54.150.15:27001'),
    new Server('177.54.150.15:27002'),
]);

/**
 * Usando dirección de cadena
 */
$sender->addServer('177.54.150.15:27002');

/**
 * Uso de la lista de direcciones de cadena
 */
$sender->addServer([
    '177.54.150.15:27001',
    '177.54.150.15:27002',
]);

/**
 * Uso de IP y puerto por separado
 */ 
$sender->addServer('177.54.150.15', 27002);

/**
 * Usando la lista de IP y puertos
 */
$sender->addServer([
    ['177.54.150.15', 27001],
    ['177.54.150.15', 27002],
]);
命令
/**
 * Creación de un remitente directo
 */ 
$sender = CsgoApi::direct();

/**
 * Usando el objeto de comando completo
 *  
 * Los 4 métodos son idénticos, use lo que se sienta bien
 * 
 */
$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));

/**
 * Uso de la lista de objetos de comando
 */ 
$sender->addCommandItem([
    new Command('stats', 1500, false),
    new Command('status', 1500, false),
]);

/**
 * Uso de parámetros de comando directamente
 */
$sender->addCommand('stats', 1500, false);

/**
 * Uso de la lista de parámetros de comando
 */ 
$sender->addCommand([
    ['stats', '1500', false],
    ['status', '1500', false],
]);

更改摘要类

通过传递新的摘要类来更改分组响应的方式

ByCommandSummary::class

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

/** 
 * Ejemplo de respuesta:
 * [
 *    "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

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

/** 
 * Ejemplo de respuesta:
 *  [
 *      "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);
$directByCommandSender = CsGoApi::direct(ByCommandSummary::class);
$directByServerSender = CsGoApi::direct(ByServerCummary::class);

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

原作者