feat: UserAction 실행기
This commit is contained in:
@@ -4,6 +4,7 @@ namespace sammo\API\Command;
|
|||||||
|
|
||||||
use sammo\Session;
|
use sammo\Session;
|
||||||
use DateTimeInterface;
|
use DateTimeInterface;
|
||||||
|
use sammo\Command\UserActionCommand;
|
||||||
use sammo\DB;
|
use sammo\DB;
|
||||||
use sammo\Enums\APIRecoveryType;
|
use sammo\Enums\APIRecoveryType;
|
||||||
use sammo\Json;
|
use sammo\Json;
|
||||||
@@ -30,7 +31,7 @@ class GetReservedUserAction extends \sammo\BaseAPI
|
|||||||
$db = DB::db();
|
$db = DB::db();
|
||||||
$generalID = $session->generalID;
|
$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);
|
$rawUserActions = $db->queryFirstField('SELECT JSON_QUERY(`aux`, %s) FROM general WHERE no=%i', "$.{$userActionKey}", $generalID);
|
||||||
if($rawUserActions === null){
|
if($rawUserActions === null){
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ namespace sammo\API\Command;
|
|||||||
|
|
||||||
use sammo\Session;
|
use sammo\Session;
|
||||||
use DateTimeInterface;
|
use DateTimeInterface;
|
||||||
|
use sammo\Command\UserActionCommand;
|
||||||
use sammo\DB;
|
use sammo\DB;
|
||||||
use sammo\DTO\UserAction;
|
use sammo\DTO\UserAction;
|
||||||
use sammo\DTO\UserActionItem;
|
use sammo\DTO\UserActionItem;
|
||||||
@@ -47,7 +48,7 @@ class ReserveUserAction extends \sammo\BaseAPI
|
|||||||
$turnIdx = $this->args['turnIdx'];
|
$turnIdx = $this->args['turnIdx'];
|
||||||
$action = $this->args['action'];
|
$action = $this->args['action'];
|
||||||
|
|
||||||
$userActionKey = 'user_action';
|
$userActionKey = UserActionCommand::USER_ACTION_KEY;
|
||||||
|
|
||||||
if($turnIdx < 0 || $turnIdx >= GameConst::$maxTurn){
|
if($turnIdx < 0 || $turnIdx >= GameConst::$maxTurn){
|
||||||
return '올바르지 않은 턴입니다.';
|
return '올바르지 않은 턴입니다.';
|
||||||
|
|||||||
@@ -32,7 +32,6 @@ class 휴식 extends Command\UserActionCommand{
|
|||||||
}
|
}
|
||||||
|
|
||||||
public function run(\Sammo\RandUtil $rng):bool{
|
public function run(\Sammo\RandUtil $rng):bool{
|
||||||
$this->setResultTurn(new LastTurn(static::getName(), $this->arg));
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -3,11 +3,14 @@ namespace sammo\Command;
|
|||||||
use \sammo\Util;
|
use \sammo\Util;
|
||||||
|
|
||||||
abstract class UserActionCommand extends BaseCommand{
|
abstract class UserActionCommand extends BaseCommand{
|
||||||
|
const USER_ACTION_KEY = 'user_action';
|
||||||
|
|
||||||
public function getNextExecuteKey():string{
|
public function getNextExecuteKey():string{
|
||||||
|
//NOTE: 일반 턴 체계와 다르므로 쓸 일은 없음
|
||||||
$turnKey = static::$actionName;
|
$turnKey = static::$actionName;
|
||||||
|
$userActionKey = static::USER_ACTION_KEY;
|
||||||
$generalID = $this->getGeneral()->getID();
|
$generalID = $this->getGeneral()->getID();
|
||||||
$executeKey = "next_execute_{$generalID}_user_action_{$turnKey}";
|
$executeKey = "next_execute_{$generalID}_{$userActionKey}_{$turnKey}";
|
||||||
return $executeKey;
|
return $executeKey;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -15,23 +18,34 @@ abstract class UserActionCommand extends BaseCommand{
|
|||||||
if($this->isArgValid && !$this->getPostReqTurn()){
|
if($this->isArgValid && !$this->getPostReqTurn()){
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
$db = \sammo\DB::db();
|
$rawUserAction = $this->generalObj->getAuxVar(static::USER_ACTION_KEY);
|
||||||
$lastExecuteStor = \sammo\KVStorage::getStorage($db, 'next_execute');
|
if($rawUserAction === null){
|
||||||
return $lastExecuteStor->getValue($this->getNextExecuteKey());
|
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){
|
public function setNextAvailable(?int $yearMonth=null){
|
||||||
if(!$this->getPostReqTurn()){
|
if(!$this->getPostReqTurn()){
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if($yearMonth === null){
|
$rawUserAction = $this->generalObj->getAuxVar(static::USER_ACTION_KEY);
|
||||||
$yearMonth = Util::joinYearMonth($this->env['year'], $this->env['month'])
|
if($rawUserAction === null){
|
||||||
+ $this->getPostReqTurn() - $this->getPreReqTurn();
|
$rawUserAction = [];
|
||||||
|
}
|
||||||
|
$userAction = \sammo\DTO\UserAction::fromArray($rawUserAction);
|
||||||
|
|
||||||
|
if($userAction->nextAvailableTurn === null){
|
||||||
|
$userAction->nextAvailableTurn = [];
|
||||||
}
|
}
|
||||||
|
|
||||||
$db = \sammo\DB::db();
|
$userAction->nextAvailableTurn[static::$actionName] = $yearMonth;
|
||||||
$lastExecuteStor = \sammo\KVStorage::getStorage($db, 'next_execute');
|
$this->generalObj->setAuxVar(static::USER_ACTION_KEY, $userAction->toArray());
|
||||||
$lastExecuteStor->setValue($this->getNextExecuteKey(), $yearMonth);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
};
|
};
|
||||||
@@ -13,6 +13,7 @@ class UserAction extends \LDTO\DTO
|
|||||||
/**
|
/**
|
||||||
* @param UserActionItem[] $active
|
* @param UserActionItem[] $active
|
||||||
* @param Array<int,UserActionItem> $reserved
|
* @param Array<int,UserActionItem> $reserved
|
||||||
|
* @param Array<string,int> $nextAvailableTurn
|
||||||
* */
|
* */
|
||||||
public function __construct(
|
public function __construct(
|
||||||
|
|
||||||
@@ -20,6 +21,8 @@ class UserAction extends \LDTO\DTO
|
|||||||
public ?array $reserved,
|
public ?array $reserved,
|
||||||
#[Convert(ArrayConverter::class, [UserActionItem::class])]
|
#[Convert(ArrayConverter::class, [UserActionItem::class])]
|
||||||
public ?array $active,
|
public ?array $active,
|
||||||
|
#[Convert(MapConverter::class, ['int'])]
|
||||||
|
public ?array $nextAvailableTurn,
|
||||||
) {
|
) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ namespace sammo;
|
|||||||
|
|
||||||
use Ds\Map;
|
use Ds\Map;
|
||||||
use sammo\Command\GeneralCommand;
|
use sammo\Command\GeneralCommand;
|
||||||
|
use sammo\Command\UserActionCommand;
|
||||||
use sammo\Enums\GeneralAccessLogColumn;
|
use sammo\Enums\GeneralAccessLogColumn;
|
||||||
use sammo\Enums\GeneralQueryMode;
|
use sammo\Enums\GeneralQueryMode;
|
||||||
use sammo\Enums\InheritanceKey;
|
use sammo\Enums\InheritanceKey;
|
||||||
@@ -269,6 +270,46 @@ class General extends GeneralBase implements iAction
|
|||||||
return $result;
|
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
|
function getReservedTurn(int $turnIdx, array $env): GeneralCommand
|
||||||
{
|
{
|
||||||
$db = DB::db();
|
$db = DB::db();
|
||||||
|
|||||||
@@ -69,6 +69,42 @@ class TurnExecutionHelper
|
|||||||
return true;
|
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
|
public function processNationCommand(RandUtil $rng, Command\NationCommand $commandObj): LastTurn
|
||||||
{
|
{
|
||||||
$general = $this->getGeneral();
|
$general = $this->getGeneral();
|
||||||
@@ -323,6 +359,12 @@ class TurnExecutionHelper
|
|||||||
$general->setRawCity(null);
|
$general->setRawCity(null);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$userActionObj = $general->getReservedUserAction($env);
|
||||||
|
if($userActionObj){
|
||||||
|
$turnObj->processUserAction($rng, $userActionObj);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
$generalCommandObj = $general->getReservedTurn(0, $env);
|
$generalCommandObj = $general->getReservedTurn(0, $env);
|
||||||
if (!($generalCommandObj instanceof Command\General\휴식)) {
|
if (!($generalCommandObj instanceof Command\General\휴식)) {
|
||||||
$hasReservedTurn = true;
|
$hasReservedTurn = true;
|
||||||
@@ -348,6 +390,7 @@ class TurnExecutionHelper
|
|||||||
$turnObj->processCommand($rng, $generalCommandObj, $autorunMode);
|
$turnObj->processCommand($rng, $generalCommandObj, $autorunMode);
|
||||||
}
|
}
|
||||||
pullNationCommand($general->getVar('nation'), $general->getVar('officer_level'));
|
pullNationCommand($general->getVar('nation'), $general->getVar('officer_level'));
|
||||||
|
$general->pullUserAction();
|
||||||
pullGeneralCommand($general->getID());
|
pullGeneralCommand($general->getID());
|
||||||
|
|
||||||
$currentTurn = $general->getTurnTime();
|
$currentTurn = $general->getTurnTime();
|
||||||
|
|||||||
Reference in New Issue
Block a user