출병 커맨드 구현

This commit is contained in:
2018-10-17 17:42:47 +09:00
parent 0816d14264
commit 743c8172e8
19 changed files with 366 additions and 24 deletions
+63 -2
View File
@@ -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;
@@ -89,6 +89,8 @@ class che_NPC능동 extends Command\GeneralCommand{
}
$general->applyDB($db);
return true;
}
+11
View File
@@ -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} <G><b>{$destCityName}</b></>{$josaRo} {$commandName} 실패.";
}
public function run():bool{
if(!$this->isRunnable()){
throw new \RuntimeException('불가능한 커맨드를 강제로 실행 시도');
+1
View File
@@ -95,6 +95,7 @@ class che_귀환 extends Command\GeneralCommand{
$general->checkStatChange();
$general->applyDB($db);
return true;
}
@@ -132,6 +132,8 @@ class che_기술연구 extends che_상업투자{
$general->applyDB($db);
uniqueItemEx($general->getID(), $logger);
return true;
}
+2
View File
@@ -115,6 +115,8 @@ class che_단련 extends Command\GeneralCommand{
$general->applyDB($db);
uniqueItemEx($general->getID(), $logger);
return true;
}
@@ -122,6 +122,8 @@ class che_물자조달 extends Command\GeneralCommand{
$general->setResultTurn(new LastTurn(static::getName(), $this->arg));
$general->checkStatChange();
$general->applyDB($db);
return true;
}
@@ -97,6 +97,8 @@ class che_사기진작 extends Command\GeneralCommand{
$general->applyDB($db);
uniqueItemEx($general->getID(), $logger);
return true;
}
@@ -175,6 +175,8 @@ class che_상업투자 extends Command\GeneralCommand{
$general->applyDB($db);
uniqueItemEx($general->getID(), $logger);
return true;
}
@@ -85,7 +85,7 @@ class che_소집해제 extends Command\GeneralCommand{
$general->checkStatChange();
$general->applyDB($db);
return true;
}
+12
View File
@@ -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} <G><b>{$destCityName}</b></>{$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;
}
@@ -162,6 +162,8 @@ class che_정착장려 extends Command\GeneralCommand{
$general->applyDB($db);
uniqueItemEx($general->getID(), $logger);
return true;
}
@@ -159,6 +159,8 @@ class che_주민선정 extends Command\GeneralCommand{
$general->applyDB($db);
uniqueItemEx($general->getID(), $logger);
return true;
}
+2
View File
@@ -108,6 +108,8 @@ class che_집합 extends Command\GeneralCommand{
$general->applyDB($db);
uniqueItemEx($general->getID(), $logger);
return true;
}
+134
View File
@@ -0,0 +1,134 @@
<?php
namespace sammo\GeneralCommand;
use \sammo\{
DB, Util, JosaUtil,
General,
ActionLogger,
GameConst, GameUnitConst,
LastTurn,
Command
};
use function \sammo\{
uniqueItemEx,
processWar
};
use \sammo\Constraint\Constraint;
use sammo\CityConst;
class che_출병 extends Command\GeneralCommand{
static protected $actionName = '출병';
protected function init(){
$general = $this->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} <G><b>{$destCityName}</b></>{$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("본국입니다. <G><b>{$defenderCityName}</b></>{$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;
}
}
+2
View File
@@ -94,6 +94,8 @@ class che_훈련 extends Command\GeneralCommand{
$general->applyDB($db);
uniqueItemEx($general->getID(), $logger);
return true;
}