66 lines
2.4 KiB
PHP
66 lines
2.4 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace sammo;
|
|
|
|
if (PHP_SAPI !== 'cli' || getenv('REF_DETERMINISTIC_INSTALL_ENABLED') !== '1') {
|
|
http_response_code(404);
|
|
exit(1);
|
|
}
|
|
|
|
chdir(dirname(__DIR__));
|
|
require_once 'lib.php';
|
|
require_once 'func.php';
|
|
|
|
$request = json_decode(stream_get_contents(STDIN), true, flags: JSON_THROW_ON_ERROR);
|
|
$generalIds = $request['generalIds'] ?? null;
|
|
if (!is_array($generalIds) || $generalIds === []) {
|
|
throw new \InvalidArgumentException('generalIds must be a non-empty array');
|
|
}
|
|
|
|
$db = DB::db();
|
|
$gameStorage = KVStorage::getStorage($db, 'game_env');
|
|
$env = $gameStorage->getAll(true);
|
|
$result = [];
|
|
$includeNation = ($request['includeNation'] ?? false) === true;
|
|
foreach ($generalIds as $generalId) {
|
|
if (!is_int($generalId) || $generalId < 1) {
|
|
throw new \InvalidArgumentException('generalIds must contain positive integers');
|
|
}
|
|
$general = General::createObjFromDB($generalId);
|
|
if ($general->getNPCType() < 2) {
|
|
throw new \InvalidArgumentException("general {$generalId} is not an NPC");
|
|
}
|
|
$ai = new GeneralAI($general);
|
|
$nationDecision = null;
|
|
if ($includeNation && $general->getNationID() !== 0 && $general->getVar('officer_level') >= 5) {
|
|
$nationStorage = KVStorage::getStorage($db, $general->getNationID(), 'nation_env');
|
|
$lastNationTurn = LastTurn::fromRaw($nationStorage->getValue("turn_last_{$general->getVar('officer_level')}"));
|
|
$reservedNation = buildNationCommandClass(null, $general, $env, $lastNationTurn);
|
|
$selectedNation = $ai->chooseNationTurn($reservedNation);
|
|
$nationDecision = [
|
|
'action' => $selectedNation->getRawClassName(),
|
|
'args' => $selectedNation->getArg(),
|
|
'reason' => $selectedNation->reason,
|
|
];
|
|
}
|
|
$reserved = $general->getReservedTurn(0, $env);
|
|
$selected = $ai->chooseGeneralTurn($reserved);
|
|
$result[] = [
|
|
'generalId' => $generalId,
|
|
'name' => $general->getName(),
|
|
'action' => $selected->getRawClassName(),
|
|
'brief' => $selected->getBrief(),
|
|
'args' => $selected->getArg(),
|
|
'reason' => $selected->reason,
|
|
'nationDecision' => $nationDecision,
|
|
];
|
|
}
|
|
|
|
echo json_encode([
|
|
'year' => (int) $env['year'],
|
|
'month' => (int) $env['month'],
|
|
'decisions' => $result,
|
|
], JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES | JSON_THROW_ON_ERROR);
|