feat: UserAction 실행기
This commit is contained in:
@@ -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){
|
||||
|
||||
@@ -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 '올바르지 않은 턴입니다.';
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -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());
|
||||
}
|
||||
|
||||
};
|
||||
@@ -13,6 +13,7 @@ class UserAction extends \LDTO\DTO
|
||||
/**
|
||||
* @param UserActionItem[] $active
|
||||
* @param Array<int,UserActionItem> $reserved
|
||||
* @param Array<string,int> $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,
|
||||
) {
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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();
|
||||
|
||||
@@ -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();
|
||||
|
||||
Reference in New Issue
Block a user