feat: 접경 귀환 기능 추가

This commit is contained in:
2023-08-05 08:57:15 +00:00
parent 57b12e8c04
commit 905669755f
5 changed files with 214 additions and 0 deletions
+81
View File
@@ -0,0 +1,81 @@
<?php
namespace sammo\API\General;
use sammo\DB;
use sammo\DummyGeneral;
use sammo\Enums\APIRecoveryType;
use sammo\Enums\GeneralAccessLogColumn;
use sammo\GameConst;
use sammo\Session;
use sammo\General;
use sammo\JosaUtil;
use sammo\KVStorage;
use sammo\LiteHashDRBG;
use sammo\RandUtil;
use sammo\TimeUtil;
use sammo\UniqueConst;
use sammo\Util;
use function sammo\addTurn;
use function sammo\buildGeneralCommandClass;
use function sammo\increaseRefresh;
class InstantRetreat extends \sammo\BaseAPI
{
public function validateArgs(): ?string
{
return null;
}
public function getRequiredSessionMode(): int
{
return static::REQ_GAME_LOGIN;
}
public function launch(Session $session, ?\DateTimeInterface $modifiedSince, ?string $reqEtag): null | string | array | APIRecoveryType
{
if(!(GameConst::$availableInstantAction['instantRetreat'] ?? false)){
return '접경귀환을 사용할 수 없는 시나리오입니다.';
}
$db = DB::db();
$gameStor = KVStorage::getStorage($db, 'game_env');
$gameStor->cacheAll();
$general = General::createObjFromDB($session->generalID);
if (!$general) {
return '장수가 없습니다';
}
increaseRefresh("접경귀환", 1);
$commandObj = buildGeneralCommandClass('che_접경귀환', $general, $gameStor->getAll(true));
$logger = $general->getLogger();
if (!$commandObj->hasFullConditionMet()) {
$logger->pushGeneralActionLog($commandObj->getFailString());
$reason = $commandObj->getFailString();
return $reason;
}
$result = $commandObj->run(new RandUtil(
new LiteHashDRBG(Util::simpleSerialize(
UniqueConst::$hiddenSeed,
'InstantRetreat',
$general->getID(),
$gameStor->year,
$gameStor->month,
$general->getCityID(),
))
));
if (!$result) {
return '가까운 아국 도시가 없습니다.';
}
return null;
}
}
@@ -0,0 +1,107 @@
<?php
namespace sammo\Command\General;
use \sammo\{
DB, Util, JosaUtil,
General,
ActionLogger,
GameConst, GameUnitConst,
LastTurn,
Command
};
use \sammo\Constraint\ConstraintHelper;
use sammo\CityConst;
use function sammo\searchDistance;
class che_접경귀환 extends Command\GeneralCommand{
static protected $actionName = '접경귀환';
protected function argTest():bool{
$this->arg = null;
return true;
}
protected function init()
{
$general = $this->generalObj;
$this->setCity();
$this->setNation();
[$reqGold, $reqRice] = $this->getCost();
$this->fullConditionConstraints=[
ConstraintHelper::NotBeNeutral(),
ConstraintHelper::NotWanderingNation(),
ConstraintHelper::NotOccupiedCity()
];
}
public function getCost():array{
return [0, 0];
}
public function getPreReqTurn():int{
return 0;
}
public function getPostReqTurn():int{
return 0;
}
public function run(\Sammo\RandUtil $rng):bool{
if(!$this->hasFullConditionMet()){
throw new \RuntimeException('불가능한 커맨드를 강제로 실행 시도');
}
$db = DB::db();
$general = $this->generalObj;
$cityID = $general->getCityID();
$logger = $general->getLogger();
$distanceList = searchDistance($cityID, 3, true);
$occupiedCityList = new \Ds\Set($db->queryFirstColumn(
'SELECT city FROM city WHERE nation = %i AND city IN %li AND supply = 1',
$general->getNationID(),
array_merge(...$distanceList)
));
$nearestCityList = [];
foreach($distanceList as $cityList){
foreach($cityList as $cityID){
if($occupiedCityList->contains($cityID)){
$nearestCityList[] = $cityID;
}
}
if($nearestCityList){
break;
}
}
if(!$nearestCityList){
$logger->pushGeneralActionLog("3칸 이내에 아국 도시가 없습니다.");
return false;
}
$destCityID = $rng->choice($nearestCityList);
$destCityName = CityConst::byID($destCityID)->name;
$josaRo = JosaUtil::pick($destCityName, '로');
$logger->pushGeneralActionLog("<G><b>{$destCityName}</b></>{$josaRo} 접경귀환했습니다.");
$general->setVar('city', $destCityID);
//TODO: InstantAction일때에만 설정하지 않는게 나은데..
//$this->setResultTurn(new LastTurn(static::getName(), $this->arg));
$general->applyDB($db);
return true;
}
}