diff --git a/hwe/func_command.php b/hwe/func_command.php index c9444d45..492c32f0 100644 --- a/hwe/func_command.php +++ b/hwe/func_command.php @@ -128,7 +128,7 @@ function pullNationCommand(int $nationID, int $level, int $turnCnt=1){ ], 'nation_id=%i AND level=%i', $nationID, $level); } -function setGeneralCommand(int $generalID, array $turnList, string $command, ?array $arg = null) { +function _setGeneralCommand(int $generalID, array $turnList, string $command, ?array $arg = null) { if(!$turnList){ return; } @@ -141,7 +141,7 @@ function setGeneralCommand(int $generalID, array $turnList, string $command, ?ar ], 'general_id = %i AND turn_idx IN %li', $generalID, $turnList); } -function setNationCommand(int $nationID, int $level, array $turnList, string $command, ?array $arg = null) { +function _setNationCommand(int $nationID, int $level, array $turnList, string $command, ?array $arg = null) { if(!$turnList){ return; } @@ -153,3 +153,180 @@ function setNationCommand(int $nationID, int $level, array $turnList, string $co 'arg'=>Json::encode($arg, JSON::EMPTY_ARRAY_IS_DICT) ], 'nation_id = %i AND level = %i AND turn_idx IN %li', $generalID, $level, $turnList); } + +function checkCommandArg(?array $arg):?string{ + if($arg === null){ + return null; + } + $defaultCheck = [ + 'string'=>[ + 'nationName', 'optionText', 'itemType', 'nationType' + ], + 'integer'=>[ + 'crewType', 'destGeneralID', 'destCityID', 'destNationID', + 'amount', 'colorType', 'itemCode' + ], + 'boolean'=>[ + 'isGold', 'buyRice', + ], + 'between'=>[ + ['month', [1, 12]] + ], + 'min'=>[ + ['year', 0], + ['itemCode', 0], + ['destGeneralID', 1], + ['destCityID', 1], + ['destNationID', 1], + ['amount', 1], + ['crewType', 0] + ], + 'integerArray'=>[ + 'destNationIDList', 'destGeneralIDList' + ], + 'stringWidthBetween'=>[ + ['nationName', 1, 18] + ] + ]; + $v = new Validator($arg); + $v->rules($defaultCheck); + if (!$v->validate()){ + return $v->errorStr(); + } + return null; +} + +function setGeneralCommand(int $generalID, array $turnList, string $command, ?array $arg = null):array{ + $turnList = array_unique($turnList); + foreach($turnList as $turnIdx){ + if(!is_int($turnIdx) || $turnIdx < 0 || $turnIdx >= GameConst::$maxTurn){ + return [ + 'result'=>false, + 'reason'=>'올바른 턴이 아닙니다. : '.$turnIdx + ]; + } + } + + $argBasicTestResult = checkCommandArg($arg); + if($argBasicTestResult !== null){ + return [ + 'result'=>false, + 'reason'=>'턴이 입력되지 않았습니다.' + ]; + } + + $db = DB::db(); + $gameStor = KVStorage::getStorage($db, 'game_env'); + $env = $gameStor->getAll(); + $general = General::createGeneralObjFromDB($generalID); + + try{ + $commandObj = buildGeneralCommandClass($action, $general, $env, $arg); + } + catch (\InvalidArgumentException $e){ + return [ + 'result'=>false, + 'reason'=>$e->getMessage(), + ]; + } + catch (\Exception $e){ + return [ + 'result'=>false, + 'reason'=>$e->getCode().$e->getMessage() + ]; + } + + if(!$commandObj->isArgValid()){ + return [ + 'result'=>false, + 'arg_test'=>false, + 'reason'=>'올바르지 않은 argument' + ]; + } + + if(!$commandObj->isReservable()){ + return [ + 'result'=>false, + 'arg_test'=>true, + 'reason'=>'예약 불가능한 커맨드 :'.$commandObj->testReservable() + ]; + } + + _setGeneralCommand($generalID, $turnList, $command, $arg); + return [ + 'result'=>true, + 'arg_test'=>true, + 'reason'=>'success' + ]; +} + +function setNationCommand(int $generalID, array $turnList, string $command, ?array $arg = null):array{ + $turnList = array_unique($turnList); + foreach($turnList as $turnIdx){ + if(!is_int($turnIdx) || $turnIdx < 0 || $turnIdx >= GameConst::$maxChiefTurn){ + return [ + 'result'=>false, + 'reason'=>'올바른 턴이 아닙니다. : '.$turnIdx + ]; + } + } + + $argBasicTestResult = checkCommandArg($arg); + if($argBasicTestResult !== null){ + return [ + 'result'=>false, + 'reason'=>'턴이 입력되지 않았습니다.' + ]; + } + + $db = DB::db(); + $gameStor = KVStorage::getStorage($db, 'game_env'); + $env = $gameStor->getAll(); + $general = General::createGeneralObjFromDB($generalID); + + if($general->getVar('level') < 5){ + return [ + 'result'=>false, + 'reason'=>'수뇌가 아닙니다' + ]; + } + + try{ + $commandObj = buildNationCommandClass($action, $general, $env, $arg); + } + catch (\InvalidArgumentException $e){ + return [ + 'result'=>false, + 'reason'=>$e->getMessage(), + ]; + } + catch (\Exception $e){ + return [ + 'result'=>false, + 'reason'=>$e->getCode().$e->getMessage() + ]; + } + + if(!$commandObj->isArgValid()){ + return [ + 'result'=>false, + 'arg_test'=>false, + 'reason'=>'올바르지 않은 argument' + ]; + } + + if(!$commandObj->isReservable()){ + return [ + 'result'=>false, + 'arg_test'=>true, + 'reason'=>'예약 불가능한 커맨드 :'.$commandObj->testReservable() + ]; + } + + _setNationCommand($general->getNationID(), $general->getVar('level'), $turnList, $command, $arg); + return [ + 'result'=>true, + 'arg_test'=>true, + 'reason'=>'success' + ]; +} \ No newline at end of file diff --git a/hwe/j_set_general_command.php b/hwe/j_set_general_command.php index 8758b8cb..630d2d58 100644 --- a/hwe/j_set_general_command.php +++ b/hwe/j_set_general_command.php @@ -8,8 +8,17 @@ $session = Session::requireGameLogin([])->setReadOnly(); $generalID = $session->generalID; + $action = Util::getReq('action', 'string'); $arg = Json::decode(Util::getReq('arg', 'string')); +$turnList = Json::decode(Util::getReq('turnList', 'string')); + +if(!is_array($turnList) || !$turnList){ + Json::die([ + 'result'=>false, + 'reason'=>'턴이 입력되지 않았습니다.' + ]); +} if(!$action){ Json::die([ @@ -18,29 +27,21 @@ if(!$action){ ]); } -$defaultCheck = [ - 'string'=>[ - 'nationName', 'optionText', 'itemType', 'nationType' - ], - 'integer'=>[ - 'crewType', 'destGeneralID', 'destCityID', 'destNationID', - 'amount', 'colorType', 'itemCode' - ], - 'boolean'=>[ - 'isGold', 'buyRice', - ], - 'between'=>[ - ['month', [1, 12]] - ], - 'min'=>[ - ['year', 0] - ], - 'integerArray'=>[ - 'destNationIDList', 'destGeneralIDList' - ], - 'stringWidthBetween'=>[ - ['nationName', 1, 18] - ] -]; +if($arg === null || !is_array($arg)){ + Json::die([ + 'result'=>false, + 'reason'=>'올바른 arg 형태가 아닙니다.' + ]); +} -$v = new Validator($query); \ No newline at end of file +$result = setGeneralCommand($generalID, $turnList, $action, $arg); +if(!key_exists('result', $result)){ + $result['result'] = false; +} +if(!key_exists('arg_test', $result)){ + $result['arg_test'] = false; +} +if(!key_exists('reason', $result)){ + throw new MustNotBeReachedException('reason이 왜 없어?'); +} +Json::die($result); \ No newline at end of file diff --git a/hwe/j_set_nation_command.php b/hwe/j_set_nation_command.php new file mode 100644 index 00000000..56175f3e --- /dev/null +++ b/hwe/j_set_nation_command.php @@ -0,0 +1,47 @@ +setReadOnly(); + +$generalID = $session->generalID; + + +$action = Util::getReq('action', 'string'); +$arg = Json::decode(Util::getReq('arg', 'string')); +$turnList = Json::decode(Util::getReq('turnList', 'string')); + +if(!is_array($turnList) || !$turnList){ + Json::die([ + 'result'=>false, + 'reason'=>'턴이 입력되지 않았습니다.' + ]); +} + +if(!$action){ + Json::die([ + 'result'=>false, + 'reason'=>'action이 입력되지 않았습니다.' + ]); +} + +if($arg === null || !is_array($arg)){ + Json::die([ + 'result'=>false, + 'reason'=>'올바른 arg 형태가 아닙니다.' + ]); +} + +$result = setNationCommand($generalID, $turnList, $action, $arg); +if(!key_exists('result', $result)){ + $result['result'] = false; +} +if(!key_exists('arg_test', $result)){ + $result['arg_test'] = false; +} +if(!key_exists('reason', $result)){ + throw new MustNotBeReachedException('reason이 왜 없어?'); +} +Json::die($result); \ No newline at end of file diff --git a/hwe/processing.php b/hwe/processing.php index 4268ca9b..b39619dc 100644 --- a/hwe/processing.php +++ b/hwe/processing.php @@ -188,7 +188,7 @@ function command_99($turn) { $me = MYDB_fetch_array($result); if($me['level'] >= 5) { - setNationCommand($me['nation'], $me['level'], range(0, $turn - 1), 'che_휴식'); + _setNationCommand($me['nation'], $me['level'], range(0, $turn - 1), 'che_휴식'); } header('location:b_chiefcenter.php'); diff --git a/src/sammo/Util.php b/src/sammo/Util.php index 66bea4a2..96f83fb5 100644 --- a/src/sammo/Util.php +++ b/src/sammo/Util.php @@ -582,4 +582,25 @@ class Util extends \utilphp\util $reflect = new ReflectionClass($object); return $reflect->getShortName(); } + + /** + * 배열의 원소에 대해서 테스트를 수행하고 모두 true인지 확인 + */ + public static function testArrayValues(array $array, ?callable $callback):bool{ + if($callback === null){ + foreach($array as $value){ + if(!$value){ + return false; + } + } + return true; + } + + foreach($array as $value){ + if(!($callback)($value)){ + return false; + } + } + return true; + } };