From 05d5039fab7e1f6fd36fa3562a89a10ca82d3ae4 Mon Sep 17 00:00:00 2001 From: Hide_D Date: Wed, 17 Oct 2018 17:42:47 +0900 Subject: [PATCH] =?UTF-8?q?=EC=B6=9C=EB=B3=91=20=EC=BB=A4=EB=A7=A8?= =?UTF-8?q?=EB=93=9C=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- hwe/sammo/Command/BaseCommand.php | 65 +++++++++- hwe/sammo/Command/General/che_NPC능동.php | 2 + hwe/sammo/Command/General/che_강행.php | 11 ++ hwe/sammo/Command/General/che_귀환.php | 1 + hwe/sammo/Command/General/che_기술연구.php | 2 + hwe/sammo/Command/General/che_단련.php | 2 + hwe/sammo/Command/General/che_물자조달.php | 2 + hwe/sammo/Command/General/che_사기진작.php | 2 + hwe/sammo/Command/General/che_상업투자.php | 2 + hwe/sammo/Command/General/che_소집해제.php | 2 +- hwe/sammo/Command/General/che_이동.php | 12 ++ hwe/sammo/Command/General/che_정착장려.php | 2 + hwe/sammo/Command/General/che_주민선정.php | 2 + hwe/sammo/Command/General/che_집합.php | 2 + hwe/sammo/Command/General/che_출병.php | 134 +++++++++++++++++++++ hwe/sammo/Command/General/che_훈련.php | 2 + hwe/sammo/Constraint/AllowWar.php | 32 +++++ hwe/sammo/Constraint/BattleGroundCity.php | 50 ++++++++ hwe/sammo/TurnExecutionHelper.php | 63 ++++++---- 19 files changed, 366 insertions(+), 24 deletions(-) create mode 100644 hwe/sammo/Command/General/che_출병.php create mode 100644 hwe/sammo/Constraint/AllowWar.php create mode 100644 hwe/sammo/Constraint/BattleGroundCity.php diff --git a/hwe/sammo/Command/BaseCommand.php b/hwe/sammo/Command/BaseCommand.php index 1267d826..039ae96b 100644 --- a/hwe/sammo/Command/BaseCommand.php +++ b/hwe/sammo/Command/BaseCommand.php @@ -39,6 +39,8 @@ abstract class BaseCommand{ protected $logger; + protected $alternative = null; + public function __construct(General $generalObj, array $env, $arg = null){ $this->generalObj = $generalObj; $this->logger = $generalObj->getLogger(); @@ -85,8 +87,31 @@ abstract class BaseCommand{ return; } + $defaultArgs = ['nation', 'name', 'color', 'type', 'level', 'capital']; + $args = array_unique(array_merge($defaultArgs, $args)); + + $defaultValues = [ + 'nation'=>0, + 'name'=>'재야', + 'color'=>'#000000', + 'type'=>GameConst::$neutralNationType, + 'level'=>0, + 'capital'=>0, + 'gold'=>0, + 'rice'=>2000, + 'tech'=>0, + 'gennum'=>1 + ]; + $db = DB::db(); - $this->nation = $db->queryFirstRow('SELECT %lb FROM nation WHERE nation=%i', $args, $this->generalObj->getVar('nation')); + $destNation = $db->queryFirstRow('SELECT %lb FROM nation WHERE nation=%i', $args, $nationNo); + if($destNation === null){ + $destNation = []; + foreach($args as $arg){ + $destNation[$arg] = $defaultValues[$arg]; + } + } + $this->destNation = $destNation; } protected function setDestGeneralFromObj(General $destGeneral){ @@ -122,8 +147,31 @@ abstract class BaseCommand{ return; } + $defaultArgs = ['nation', 'name', 'color', 'type', 'level', 'capital']; + $args = array_unique(array_merge($defaultArgs, $args)); + + $defaultValues = [ + 'nation'=>0, + 'name'=>'재야', + 'color'=>'#000000', + 'type'=>GameConst::$neutralNationType, + 'level'=>0, + 'capital'=>0, + 'gold'=>0, + 'rice'=>2000, + 'tech'=>0, + 'gennum'=>1 + ]; + $db = DB::db(); - $this->destNation = $db->queryFirstRow('SELECT %lb FROM nation WHERE nation=%i', $args, $nationNo); + $destNation = $db->queryFirstRow('SELECT %lb FROM nation WHERE nation=%i', $args, $nationNo); + if($destNation === null){ + $destNation = []; + foreach($args as $arg){ + $destNation[$arg] = $defaultValues[$arg]; + } + } + $this->destNation = $destNation; } abstract protected function init(); @@ -202,6 +250,19 @@ abstract class BaseCommand{ return $this->testRunnable() === null; } + public function getFailString():string{ + $commandName = $this->getName(); + $failReason = $this->testRunnable(); + if($failReason === null){ + throw new \RuntimeException('실행 가능한 커맨드에 대해 실패 이유를 수집'); + } + return "{$failReason} {$commandName} 실패."; + } + + public function getAlternativeCommand():?self{ + return $this->alternative; + } + abstract public function getCost():array; abstract public function getPreReqTurn():int; diff --git a/hwe/sammo/Command/General/che_NPC능동.php b/hwe/sammo/Command/General/che_NPC능동.php index d02dd8dc..cd5b5f5d 100644 --- a/hwe/sammo/Command/General/che_NPC능동.php +++ b/hwe/sammo/Command/General/che_NPC능동.php @@ -89,6 +89,8 @@ class che_NPC능동 extends Command\GeneralCommand{ } $general->applyDB($db); + + return true; } diff --git a/hwe/sammo/Command/General/che_강행.php b/hwe/sammo/Command/General/che_강행.php index 1589c9f8..4deee6e1 100644 --- a/hwe/sammo/Command/General/che_강행.php +++ b/hwe/sammo/Command/General/che_강행.php @@ -67,6 +67,17 @@ class che_강행 extends Command\GeneralCommand{ return 0; } + public function getFailString():string{ + $commandName = $this->getName(); + $failReason = $this->testRunnable(); + if($failReason === null){ + throw new \RuntimeException('실행 가능한 커맨드에 대해 실패 이유를 수집'); + } + $destCityName = CityConst::byID($this->arg['destCityID'])->name; + $josaRo = JosaUtil::pick($destCityName, '로'); + return "{$failReason} {$destCityName}{$josaRo} {$commandName} 실패."; + } + public function run():bool{ if(!$this->isRunnable()){ throw new \RuntimeException('불가능한 커맨드를 강제로 실행 시도'); diff --git a/hwe/sammo/Command/General/che_귀환.php b/hwe/sammo/Command/General/che_귀환.php index 0ef34dcc..af23cb72 100644 --- a/hwe/sammo/Command/General/che_귀환.php +++ b/hwe/sammo/Command/General/che_귀환.php @@ -95,6 +95,7 @@ class che_귀환 extends Command\GeneralCommand{ $general->checkStatChange(); $general->applyDB($db); + return true; } diff --git a/hwe/sammo/Command/General/che_기술연구.php b/hwe/sammo/Command/General/che_기술연구.php index e2f9138a..02465356 100644 --- a/hwe/sammo/Command/General/che_기술연구.php +++ b/hwe/sammo/Command/General/che_기술연구.php @@ -132,6 +132,8 @@ class che_기술연구 extends che_상업투자{ $general->applyDB($db); uniqueItemEx($general->getID(), $logger); + + return true; } diff --git a/hwe/sammo/Command/General/che_단련.php b/hwe/sammo/Command/General/che_단련.php index aa84ab05..0a1331b1 100644 --- a/hwe/sammo/Command/General/che_단련.php +++ b/hwe/sammo/Command/General/che_단련.php @@ -115,6 +115,8 @@ class che_단련 extends Command\GeneralCommand{ $general->applyDB($db); uniqueItemEx($general->getID(), $logger); + + return true; } diff --git a/hwe/sammo/Command/General/che_물자조달.php b/hwe/sammo/Command/General/che_물자조달.php index 44aa4a97..62a71005 100644 --- a/hwe/sammo/Command/General/che_물자조달.php +++ b/hwe/sammo/Command/General/che_물자조달.php @@ -122,6 +122,8 @@ class che_물자조달 extends Command\GeneralCommand{ $general->setResultTurn(new LastTurn(static::getName(), $this->arg)); $general->checkStatChange(); $general->applyDB($db); + + return true; } diff --git a/hwe/sammo/Command/General/che_사기진작.php b/hwe/sammo/Command/General/che_사기진작.php index 27d09bd8..7f599dd2 100644 --- a/hwe/sammo/Command/General/che_사기진작.php +++ b/hwe/sammo/Command/General/che_사기진작.php @@ -97,6 +97,8 @@ class che_사기진작 extends Command\GeneralCommand{ $general->applyDB($db); uniqueItemEx($general->getID(), $logger); + + return true; } diff --git a/hwe/sammo/Command/General/che_상업투자.php b/hwe/sammo/Command/General/che_상업투자.php index 05b52be1..116d499b 100644 --- a/hwe/sammo/Command/General/che_상업투자.php +++ b/hwe/sammo/Command/General/che_상업투자.php @@ -175,6 +175,8 @@ class che_상업투자 extends Command\GeneralCommand{ $general->applyDB($db); uniqueItemEx($general->getID(), $logger); + + return true; } diff --git a/hwe/sammo/Command/General/che_소집해제.php b/hwe/sammo/Command/General/che_소집해제.php index 0bdb2a75..bed7be65 100644 --- a/hwe/sammo/Command/General/che_소집해제.php +++ b/hwe/sammo/Command/General/che_소집해제.php @@ -85,7 +85,7 @@ class che_소집해제 extends Command\GeneralCommand{ $general->checkStatChange(); $general->applyDB($db); - + return true; } diff --git a/hwe/sammo/Command/General/che_이동.php b/hwe/sammo/Command/General/che_이동.php index 3c4fa6a5..9ecf7f00 100644 --- a/hwe/sammo/Command/General/che_이동.php +++ b/hwe/sammo/Command/General/che_이동.php @@ -67,6 +67,17 @@ class che_이동 extends Command\GeneralCommand{ return 0; } + public function getFailString():string{ + $commandName = $this->getName(); + $failReason = $this->testRunnable(); + if($failReason === null){ + throw new \RuntimeException('실행 가능한 커맨드에 대해 실패 이유를 수집'); + } + $destCityName = CityConst::byID($this->arg['destCityID'])->name; + $josaRo = JosaUtil::pick($destCityName, '로'); + return "{$failReason} {$destCityName}{$josaRo} {$commandName} 실패."; + } + public function run():bool{ if(!$this->isRunnable()){ throw new \RuntimeException('불가능한 커맨드를 강제로 실행 시도'); @@ -114,6 +125,7 @@ class che_이동 extends Command\GeneralCommand{ $general->checkStatChange(); $general->applyDB($db); + return true; } diff --git a/hwe/sammo/Command/General/che_정착장려.php b/hwe/sammo/Command/General/che_정착장려.php index 895ed3aa..8a8f8c06 100644 --- a/hwe/sammo/Command/General/che_정착장려.php +++ b/hwe/sammo/Command/General/che_정착장려.php @@ -162,6 +162,8 @@ class che_정착장려 extends Command\GeneralCommand{ $general->applyDB($db); uniqueItemEx($general->getID(), $logger); + + return true; } diff --git a/hwe/sammo/Command/General/che_주민선정.php b/hwe/sammo/Command/General/che_주민선정.php index 71452ebd..6499e20c 100644 --- a/hwe/sammo/Command/General/che_주민선정.php +++ b/hwe/sammo/Command/General/che_주민선정.php @@ -159,6 +159,8 @@ class che_주민선정 extends Command\GeneralCommand{ $general->applyDB($db); uniqueItemEx($general->getID(), $logger); + + return true; } diff --git a/hwe/sammo/Command/General/che_집합.php b/hwe/sammo/Command/General/che_집합.php index 145858a2..31cc4537 100644 --- a/hwe/sammo/Command/General/che_집합.php +++ b/hwe/sammo/Command/General/che_집합.php @@ -108,6 +108,8 @@ class che_집합 extends Command\GeneralCommand{ $general->applyDB($db); uniqueItemEx($general->getID(), $logger); + + return true; } diff --git a/hwe/sammo/Command/General/che_출병.php b/hwe/sammo/Command/General/che_출병.php new file mode 100644 index 00000000..ea373342 --- /dev/null +++ b/hwe/sammo/Command/General/che_출병.php @@ -0,0 +1,134 @@ +generalObj; + + $this->setCity(); + $this->setNation(['war', 'gennum', 'tech', 'gold', 'rice']); + $this->setDestCity($this->arg['destCityID'], []); + $this->setDestNation(['nation' ,'level','name','capital','gennum','tech','type','gold','rice']); + + [$reqGold, $reqRice] = $this->getCost(); + + $this->runnableConstraints=[ + ['NotOpeningPart'], + ['NearCity', 1], + ['NoNeutral'], + ['OccupiedCity'], + ['ReqGeneralCrew'], + ['ReqGeneralRice', $reqRice], + ['AllowWar'], + ['BattleGroundCity', 1], + ]; + } + + protected function argTest():bool{ + if(!key_exists('destCityID', $this->arg)){ + return false; + } + if(!key_exists($this->arg['destCityID'], CityConst::all())){ + return false; + } + $this->arg = [ + 'destCityID'=>$this->arg['destCityID'] + ]; + return true; + } + + public function getCost():array{ + return [0, Util::round($this->general->getVar('crew')/100)]; + } + + public function getPreReqTurn():int{ + return 0; + } + + public function getPostReqTurn():int{ + return 0; + } + + public function getFailString():string{ + $commandName = $this->getName(); + $failReason = $this->testRunnable(); + if($failReason === null){ + throw new \RuntimeException('실행 가능한 커맨드에 대해 실패 이유를 수집'); + } + $destCityName = CityConst::byID($this->arg['destCityID'])->name; + $josaRo = JosaUtil::pick($destCityName, '로'); + return "{$failReason} {$destCityName}{$josaRo} {$commandName} 실패."; + } + + public function run():bool{ + if(!$this->isRunnable()){ + throw new \RuntimeException('불가능한 커맨드를 강제로 실행 시도'); + } + + $db = DB::db(); + $env = $this->env; + + $general = $this->generalObj; + $attackerNationID = $general->getNationID(); + $defenderNationID = $this->destCity['nation']; + $date = substr($general->getVar('turntime'),11,5); + + $defenderCityName = $this->destCity['name']; + $defenderCityID = $this->destCity['city']; + $josaRo = JosaUtil::pick($destCityName, '로'); + + $logger = $general->getLogger(); + + if($attackerNationID == $defenderNationID){ + $logger->pushGeneralActionLog("본국입니다. {$defenderCityName}{$josaRo} 으로 이동합니다. <1>$date"); + $this->alternative = new che_이동($general, $this->env, $this->arg); + return false; + } + + $db->update('city', [ + 'state'=>43, + 'term'=>3 + ], 'city=%i', $defenderCityID); + + $this->destCity['state'] = 43; + $this->destCity['term'] = 3; + + $general->addDex($general->getCrewTypeObj(), $general->getVar('crew')/100); + + + $general->setResultTurn(new LastTurn(static::getName(), $this->arg)); + $general->applyDB($db); + + //TODO: 장기적으로 통합해야함 + processWar($general->getRaw(), $this->destCity); + + uniqueItemEx($general->getID(), $logger); + + return true; + } + + +} \ No newline at end of file diff --git a/hwe/sammo/Command/General/che_훈련.php b/hwe/sammo/Command/General/che_훈련.php index 52e47942..3ef9f87c 100644 --- a/hwe/sammo/Command/General/che_훈련.php +++ b/hwe/sammo/Command/General/che_훈련.php @@ -94,6 +94,8 @@ class che_훈련 extends Command\GeneralCommand{ $general->applyDB($db); uniqueItemEx($general->getID(), $logger); + + return true; } diff --git a/hwe/sammo/Constraint/AllowWar.php b/hwe/sammo/Constraint/AllowWar.php new file mode 100644 index 00000000..ee2b1ff6 --- /dev/null +++ b/hwe/sammo/Constraint/AllowWar.php @@ -0,0 +1,32 @@ +nation)){ + if(!$throwExeception){return false; } + throw new \InvalidArgumentException("require war in nation"); + } + + return true; + } + + public function test():bool{ + $this->checkInputValues(); + $this->tested = true; + + if($this->nation['war'] == 0){ + return true; + } + + $this->reason = "현재 전쟁 금지입니다."; + return false; + } +} \ No newline at end of file diff --git a/hwe/sammo/Constraint/BattleGroundCity.php b/hwe/sammo/Constraint/BattleGroundCity.php new file mode 100644 index 00000000..9f6c8ae1 --- /dev/null +++ b/hwe/sammo/Constraint/BattleGroundCity.php @@ -0,0 +1,50 @@ +general)){ + if(!$throwExeception){return false; } + throw new \InvalidArgumentException("require nation in general"); + } + + if(!key_exists('nation', $this->destCity)){ + if(!$throwExeception){return false; } + throw new \InvalidArgumentException("require nation in dest city"); + } + + return true; + } + + public function test():bool{ + $this->checkInputValues(); + $this->tested = true; + + $nationID = $this->general['nation']; + $destNationID = $this->city['nation']; + if($destNationID == 0){ + return true; + } + + if($this->arg && $destNationID == $nationID){ + return true; + } + + $db = DB::db(); + $diplomacy = $db->queryFirstField('SELECT state FROM diplomacy WHERE me = %i AND you = %i', $nationID, $destNationID); + if($diplomacy === 0){ + return true; + } + + $this->reason = "교전중인 국가의 도시가 아닙니다."; + return false; + } +} \ No newline at end of file diff --git a/hwe/sammo/TurnExecutionHelper.php b/hwe/sammo/TurnExecutionHelper.php index 258dea33..4403e1ac 100644 --- a/hwe/sammo/TurnExecutionHelper.php +++ b/hwe/sammo/TurnExecutionHelper.php @@ -76,15 +76,26 @@ class TurnExecutionHelper $commandObj = buildNationCommandClass($commandClassName, $general, $gameStor->getAll(true), $commandLast, $commandArg); - $failReason = $commandObj->testReservable(); - if($failReason){ - $date = substr($general->getVar('turntime'),11,5); - $commandName = $commandObj->getName(); - $text = "{$failReason} {$commandName} 실패. <1>{$date}"; - $general->getLogger()->pushGeneralActionLog($text); - } - else{ - $commandObj->run(); + while(true){ + $failReason = $commandObj->testRunnable(); + if($failReason){ + $date = substr($general->getVar('turntime'),11,5); + $failString = $commandObj->getFailString(); + $text = "{$failString} <1>{$date}"; + $general->getLogger()->pushGeneralActionLog($text); + break; + } + + $result = $commandObj->run(); + if($result){ + break; + } + $alt = $commandObj->getAlternativeCommand(); + if($alt === null){ + break; + } + $commandObj = $alt; + $commandClassName = $alt->getName(); } return $commandObj->getResultTurn(); @@ -97,20 +108,30 @@ class TurnExecutionHelper $db = DB::db(); $gameStor = KVStorage::getStorage($db, 'game_env'); - $commandClass = getGeneralCommandClass($commandClassName); - /** @var \sammo\Command\GeneralCommand $commandObj */ - $commandObj = new $commandClass($general, $gameStor->getAll(true), $commandArg, $commandLast); + $commandObj = buildGeneralCommandClass($commandClassName, $general, $gameStor->getAll(true), $commandArg); - $failReason = $commandObj->testReservable(); - if($failReason){ - $date = substr($general->getVar('turntime'),11,5); - $commandName = $commandObj->getName(); - $text = "{$failReason} {$commandName} 실패. <1>{$date}"; - $general->getLogger()->pushGeneralActionLog($text); - } - else{ - $commandObj->run(); + while(true){ + $failReason = $commandObj->testRunnable(); + if($failReason){ + $date = substr($general->getVar('turntime'),11,5); + $failString = $commandObj->getFailString(); + $text = "{$failString} <1>{$date}"; + $general->getLogger()->pushGeneralActionLog($text); + break; + } + + $result = $commandObj->run(); + if($result){ + break; + } + $alt = $commandObj->getAlternativeCommand(); + if($alt === null){ + break; + } + $commandObj = $alt; + $commandClassName = $alt->getName(); } + $general->clearActivatedSkill();