diff --git a/hwe/sammo/API/Command/GetReservedUserAction.php b/hwe/sammo/API/Command/GetReservedUserAction.php index 9b31837c..85d1c3b1 100644 --- a/hwe/sammo/API/Command/GetReservedUserAction.php +++ b/hwe/sammo/API/Command/GetReservedUserAction.php @@ -4,6 +4,7 @@ namespace sammo\API\Command; use sammo\Session; use DateTimeInterface; +use sammo\Command\UserActionCommand; use sammo\DB; use sammo\Enums\APIRecoveryType; use sammo\Json; @@ -30,7 +31,7 @@ class GetReservedUserAction extends \sammo\BaseAPI $db = DB::db(); $generalID = $session->generalID; - $userActionKey = 'user_action'; + $userActionKey = UserActionCommand::USER_ACTION_KEY; $rawUserActions = $db->queryFirstField('SELECT JSON_QUERY(`aux`, %s) FROM general WHERE no=%i', "$.{$userActionKey}", $generalID); if($rawUserActions === null){ diff --git a/hwe/sammo/API/Command/ReserveUserAction.php b/hwe/sammo/API/Command/ReserveUserAction.php index 20824ceb..81299564 100644 --- a/hwe/sammo/API/Command/ReserveUserAction.php +++ b/hwe/sammo/API/Command/ReserveUserAction.php @@ -4,6 +4,7 @@ namespace sammo\API\Command; use sammo\Session; use DateTimeInterface; +use sammo\Command\UserActionCommand; use sammo\DB; use sammo\DTO\UserAction; use sammo\DTO\UserActionItem; @@ -47,7 +48,7 @@ class ReserveUserAction extends \sammo\BaseAPI $turnIdx = $this->args['turnIdx']; $action = $this->args['action']; - $userActionKey = 'user_action'; + $userActionKey = UserActionCommand::USER_ACTION_KEY; if($turnIdx < 0 || $turnIdx >= GameConst::$maxTurn){ return '올바르지 않은 턴입니다.'; diff --git a/hwe/sammo/Command/UserAction/휴식.php b/hwe/sammo/Command/UserAction/휴식.php index ae425434..fe80f8f5 100644 --- a/hwe/sammo/Command/UserAction/휴식.php +++ b/hwe/sammo/Command/UserAction/휴식.php @@ -32,7 +32,6 @@ class 휴식 extends Command\UserActionCommand{ } public function run(\Sammo\RandUtil $rng):bool{ - $this->setResultTurn(new LastTurn(static::getName(), $this->arg)); return true; } } \ No newline at end of file diff --git a/hwe/sammo/Command/UserActionCommand.php b/hwe/sammo/Command/UserActionCommand.php index 7bcb8c6b..25f45a99 100644 --- a/hwe/sammo/Command/UserActionCommand.php +++ b/hwe/sammo/Command/UserActionCommand.php @@ -3,11 +3,14 @@ namespace sammo\Command; use \sammo\Util; abstract class UserActionCommand extends BaseCommand{ + const USER_ACTION_KEY = 'user_action'; public function getNextExecuteKey():string{ + //NOTE: 일반 턴 체계와 다르므로 쓸 일은 없음 $turnKey = static::$actionName; + $userActionKey = static::USER_ACTION_KEY; $generalID = $this->getGeneral()->getID(); - $executeKey = "next_execute_{$generalID}_user_action_{$turnKey}"; + $executeKey = "next_execute_{$generalID}_{$userActionKey}_{$turnKey}"; return $executeKey; } @@ -15,23 +18,34 @@ abstract class UserActionCommand extends BaseCommand{ if($this->isArgValid && !$this->getPostReqTurn()){ return null; } - $db = \sammo\DB::db(); - $lastExecuteStor = \sammo\KVStorage::getStorage($db, 'next_execute'); - return $lastExecuteStor->getValue($this->getNextExecuteKey()); + $rawUserAction = $this->generalObj->getAuxVar(static::USER_ACTION_KEY); + if($rawUserAction === null){ + return null; + } + $userAction = \sammo\DTO\UserAction::fromArray($rawUserAction); + $nextAvailableTurn = $userAction->nextAvailableTurn; + if($nextAvailableTurn === null){ + return null; + } + $nextAvailableTurn = $nextAvailableTurn[static::$actionName] ?? null; } public function setNextAvailable(?int $yearMonth=null){ if(!$this->getPostReqTurn()){ return; } - if($yearMonth === null){ - $yearMonth = Util::joinYearMonth($this->env['year'], $this->env['month']) - + $this->getPostReqTurn() - $this->getPreReqTurn(); + $rawUserAction = $this->generalObj->getAuxVar(static::USER_ACTION_KEY); + if($rawUserAction === null){ + $rawUserAction = []; + } + $userAction = \sammo\DTO\UserAction::fromArray($rawUserAction); + + if($userAction->nextAvailableTurn === null){ + $userAction->nextAvailableTurn = []; } - $db = \sammo\DB::db(); - $lastExecuteStor = \sammo\KVStorage::getStorage($db, 'next_execute'); - $lastExecuteStor->setValue($this->getNextExecuteKey(), $yearMonth); + $userAction->nextAvailableTurn[static::$actionName] = $yearMonth; + $this->generalObj->setAuxVar(static::USER_ACTION_KEY, $userAction->toArray()); } }; \ No newline at end of file diff --git a/hwe/sammo/DTO/UserAction.php b/hwe/sammo/DTO/UserAction.php index 833105ca..a2d3f84c 100644 --- a/hwe/sammo/DTO/UserAction.php +++ b/hwe/sammo/DTO/UserAction.php @@ -13,6 +13,7 @@ class UserAction extends \LDTO\DTO /** * @param UserActionItem[] $active * @param Array $reserved + * @param Array $nextAvailableTurn * */ public function __construct( @@ -20,6 +21,8 @@ class UserAction extends \LDTO\DTO public ?array $reserved, #[Convert(ArrayConverter::class, [UserActionItem::class])] public ?array $active, + #[Convert(MapConverter::class, ['int'])] + public ?array $nextAvailableTurn, ) { } } diff --git a/hwe/sammo/General.php b/hwe/sammo/General.php index 3610c49a..cb6d115e 100644 --- a/hwe/sammo/General.php +++ b/hwe/sammo/General.php @@ -4,6 +4,7 @@ namespace sammo; use Ds\Map; use sammo\Command\GeneralCommand; +use sammo\Command\UserActionCommand; use sammo\Enums\GeneralAccessLogColumn; use sammo\Enums\GeneralQueryMode; use sammo\Enums\InheritanceKey; @@ -269,6 +270,46 @@ class General extends GeneralBase implements iAction return $result; } + function getReservedUserAction(array $env): ?UserActionCommand + { + $rawUserAction = $this->getAuxVar(UserActionCommand::USER_ACTION_KEY); + if(!$rawUserAction){ + return null; + } + $userAction = \sammo\DTO\UserAction::fromArray($rawUserAction); + if(!$userAction->reserved){ + return null; + } + $reservedAction = $userAction->reserved; + if(!key_exists(0, $reservedAction)){ + return null; + } + $action = $reservedAction[0]; + return buildUserActionCommandClass($action->command, $this, $env); + } + + function pullUserAction(){ + $rawUserAction = $this->getAuxVar(UserActionCommand::USER_ACTION_KEY); + if(!$rawUserAction){ + return null; + } + $userAction = \sammo\DTO\UserAction::fromArray($rawUserAction); + if(!$userAction->reserved){ + return null; + } + $reservedAction = $userAction->reserved; + + $newReservedAction = []; + foreach($reservedAction as $idx => $action){ + if($idx <= 0){ + continue; + } + $newReservedAction[$idx - 1] = $action; + } + $userAction->reserved = $newReservedAction; + $this->setAuxVar(UserActionCommand::USER_ACTION_KEY, $userAction->toArray()); + } + function getReservedTurn(int $turnIdx, array $env): GeneralCommand { $db = DB::db(); diff --git a/hwe/sammo/TurnExecutionHelper.php b/hwe/sammo/TurnExecutionHelper.php index 7a5c347b..f75b8c2c 100644 --- a/hwe/sammo/TurnExecutionHelper.php +++ b/hwe/sammo/TurnExecutionHelper.php @@ -69,6 +69,42 @@ class TurnExecutionHelper return true; } + public function processUserAction(RandUtil $rng, Command\UserActionCommand $commandObj): LastTurn{ + $general = $this->getGeneral(); + + while (true) { + if (!$commandObj->hasFullConditionMet()) { + $date = $general->getTurnTime($general::TURNTIME_HM); + $failString = $commandObj->getFailString(); + $text = "{$failString} <1>{$date}"; + $general->getLogger()->pushGeneralActionLog($text); + break; + } + + if (!$commandObj->addTermStack()) { + $date = $general->getTurnTime($general::TURNTIME_HM); + $termString = $commandObj->getTermString(); + $text = "{$termString} <1>{$date}"; + $general->getLogger()->pushGeneralActionLog($text); + break; + } + + $result = $commandObj->run($rng); + if ($result) { + $commandObj->setNextAvailable(); + break; + } + + $alt = $commandObj->getAlternativeCommand(); + if ($alt === null) { + break; + } + $commandObj = $alt; + } + + return $commandObj->getResultTurn(); + } + public function processNationCommand(RandUtil $rng, Command\NationCommand $commandObj): LastTurn { $general = $this->getGeneral(); @@ -323,6 +359,12 @@ class TurnExecutionHelper $general->setRawCity(null); } + $userActionObj = $general->getReservedUserAction($env); + if($userActionObj){ + $turnObj->processUserAction($rng, $userActionObj); + } + + $generalCommandObj = $general->getReservedTurn(0, $env); if (!($generalCommandObj instanceof Command\General\휴식)) { $hasReservedTurn = true; @@ -348,6 +390,7 @@ class TurnExecutionHelper $turnObj->processCommand($rng, $generalCommandObj, $autorunMode); } pullNationCommand($general->getVar('nation'), $general->getVar('officer_level')); + $general->pullUserAction(); pullGeneralCommand($general->getID()); $currentTurn = $general->getTurnTime();