이동 구현. BaseCommand에 GeneralSource 연결.

This commit is contained in:
2018-10-16 02:29:33 +09:00
parent d16c86a4d7
commit 9846d7f8e4
10 changed files with 250 additions and 65 deletions
+1 -1
View File
@@ -286,7 +286,7 @@ for($j=0; $j < $gencount; $j++) {
if($ourGeneral && !$isNPC){
$turnText = [];
$generalObj = new General($general, null, 0, 0, false);
$generalObj = new General($general, null, null, null, false);
$turnBrief = getGeneralTurnBrief($genralObj, $generalTurnList[$generalObj->getID()]);
foreach($turnBrief as $turnRawIdx=>$turn){
$turnIdx = $turnRawIdx+1;
+1 -1
View File
@@ -174,7 +174,7 @@ foreach ($generals as &$general) {
if ($general['npc'] < 2) {
$generalObj = new General($general, null, 0, 0, false);
$generalObj = new General($general, null, null, null, false);
$turnBrief = getGeneralTurnBrief($genralObj, $generalTurnList[$generalObj->getID()]);
$turntext = [];
+1 -1
View File
@@ -83,7 +83,7 @@ function myCommandList() {
echo "로그인 되어있지 않습니다.";
return;
}
$generalObj = new General($me, null, 0, 0, false);
$generalObj = new General($me, null, null, null, false);
$turnList = $db->query('SELECT turn_idx, action, arg FROM general_turn WHERE general_id = %i ORDER BY turn_idx ASC', $generalObj->getID());
$turnBrief = getGeneralTurnBrief($generalObj, $turnList);
-40
View File
@@ -551,46 +551,6 @@ function process_16(&$general) {
pushGenLog($general, $log);
}
function process_17(&$general) {
$db = DB::db();
$gameStor = KVStorage::getStorage($db, 'game_env');
$connect=$db->get();
$log = [];
$alllog = [];
$history = [];
$date = substr($general['turntime'],11,5);
$admin = $gameStor->getValues(['year', 'month']);
if($general['crew'] == 0) {
$log[] = "<C>●</>{$admin['month']}월:병사가 없습니다. 소집해제 실패. <1>$date</>";
} else {
// 주민으로 돌아감
$query = "update city set pop=pop+'{$general['crew']}' where city='{$general['city']}'";
MYDB_query($query, $connect) or Error(__LINE__.MYDB_error($connect),"");
$query = "update general set crew='0' where no='{$general['no']}'";
MYDB_query($query, $connect) or Error(__LINE__.MYDB_error($connect),"");
$log[0] = "<C>●</>{$admin['month']}월:병사들을 <R>소집해제</>하였습니다. <1>$date</>";
// 경험, 공헌 상승
$exp = 70;
$ded = 100;
// 성격 보정
$exp = CharExperience($exp, $general['personal']);
$ded = CharDedication($ded, $general['personal']);
$query = "update general set resturn='SUCCESS',dedication=dedication+'$ded',experience=experience+'$exp' where no='{$general['no']}'";
MYDB_query($query, $connect) or Error(__LINE__.MYDB_error($connect),"");
$log = checkAbility($general, $log);
}
pushGenLog($general, $log);
}
function process_21(&$general) {
$db = DB::db();
$gameStor = KVStorage::getStorage($db, 'game_env');
+19 -3
View File
@@ -37,7 +37,7 @@ abstract class BaseCommand{
protected $runnableConstraints = null;
protected $reservableConstraints = null;
protected $generalSource = null;
protected $logger;
@@ -54,6 +54,17 @@ abstract class BaseCommand{
}
public function setGeneralSource(?array $generalSource){
$this->generalSource = $generalSource;
}
protected function getGeneralObjFromGeneralSource(int $generalNo):?General{
if($this->generalSource === null){
return null;
}
return $this->generalSource[$generalNo]??null;
}
protected function resetTestCache():void{
$this->runnable = null;
$this->reservable = null;
@@ -105,7 +116,12 @@ abstract class BaseCommand{
protected function setDestCity(int $cityNo, ?array $args){
$this->resetTestCache();
$db = DB::db();
if($args == null){
if($args === []){
$cityObj = \sammo\CityConst::byID($cityNo);
$this->destCity = ['city'=>$cityNo, 'name'=>$cityObj->name];
return;
}
if($args === null){
$this->destCity = $db->queryFirstRow('SELECT * FROM city WHERE city=%i', $cityNo);
return;
}
@@ -114,7 +130,7 @@ abstract class BaseCommand{
protected function setDestNation(int $nationNo, ?array $args = null){
$this->resetTestCache();
if($args == null){
if($args === null || $args === []){
$this->destNation = getNationStaticInfo($nationNo);
return;
}
+119
View File
@@ -0,0 +1,119 @@
<?php
namespace sammo\GeneralCommand;
use \sammo\{
DB, Util, JosaUtil,
General,
ActionLogger,
GameConst, GameUnitConst,
LastTurn,
Command
};
use function \sammo\{
uniqueItemEx
};
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();
$this->setDestCity($this->arg['destCityID'], []);
[$reqGold, $reqRice] = $this->getCost();
$this->runnableConstraints=[
['NotSameCity'],
['NearCity', 1],
['ReqGeneralGold', $reqGold],
['ReqGeneralRice', $reqRice],
];
}
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{
$env = $this->env;
return [$env['develcost'], 0];
}
public function getPreReqTurn():int{
return 0;
}
public function getPostReqTurn():int{
return 0;
}
public function run():bool{
if(!$this->isRunnable()){
throw new \RuntimeException('불가능한 커맨드를 강제로 실행 시도');
}
$db = DB::db();
$env = $this->env;
$general = $this->generalObj;
$date = substr($general->getVar('turntime'),11,5);
$destCityName = $this->destCity['name'];
$destCityID = $this->destCity['city'];
$josaRo = JosaUtil::pick($destCityName, '로');
$logger = $general->getLogger();
$logger->pushGeneralActionLog("<G><b>{$destCityName}</b></>{$josaRo} 이동했습니다. <1>$date</>");
$exp = 50;
$exp = $general->onPreGeneralStatUpdate($general, 'experience', $exp);
$general->setVar('city', $destCityID);
if($general->getVar('level') == 12 && $this->nation['level'] == 0){
$generalList = $db->queryFirstColumn('SELECT no, name, city, nation, level FROM general WHERE nation=%i AND no!=%i', $general->getNationID(), $general->getID());
foreach($generalList as $targetRawGeneral){
$targetGeneralID = $targetRawGeneral['no'];
$targetGeneral = $this->getGeneralObjFromGeneralSource($targetGeneralID);
if($targetGeneral === null){
$targetGeneral = new General($targetRawGeneral, null, $env['year'], $env['month'], false);
}
$targetGeneral->setVar('city', $destCityID);
$targetGeneral->getLogger()->pushGeneralActionLog("방랑군 세력이 <G><b>{$destCityName}</b></>{$josaRo} 이동했습니다.", ActionLogger::PLAIN);
$targetGeneral = null;
}
}
[$reqGold, $reqRice] = $this->getCost();
$general->increaseVarWithLimit('gold', -$reqGold, 0);
$general->increaseVar('experience', $exp);
$general->increaseVar('leader2', 1);
$general->setResultTurn(new LastTurn(static::getName(), $this->arg));
$general->checkStatChange();
$general->applyDB($db);
}
}
+43
View File
@@ -0,0 +1,43 @@
<?php
namespace sammo\Constraint;
class NearCity extends Constraint{
const REQ_VALUES = Constraint::REQ_GENERAL|Constraint::REQ_DEST_CITY|Constraint::REQ_NUMERIC_ARG;
public function checkInputValues(bool $throwExeception=true){
if(!parent::checkInputValues($throwExeception) && !$throwException){
return false;
}
if(!key_exists('city', $this->general)){
if(!$throwExeception){return false; }
throw new \InvalidArgumentException("require city in general");
}
if($this->arg < 1 || !is_integer($this->arg)){
if(!$throwExeception){return false; }
throw new \InvalidArgumentException("arg should be >= 1 integer");
}
return true;
}
public function test():bool{
$this->checkInputValues();
$this->tested = true;
$dist = \sammo\searchDistance($this->general['city'], $this->arg, false);
if(key_exist($this->destCity['city'], $dist)){
return true;
}
if($this->arg == 1){
$this->reason = "인접도시가 아닙니다.";
}
else{
$this->reason = "거리가 너무 멉니다.";
}
return false;
}
}
+32
View File
@@ -0,0 +1,32 @@
<?php
namespace sammo\Constraint;
class NearCity extends Constraint{
const REQ_VALUES = Constraint::REQ_GENERAL|Constraint::REQ_DEST_CITY;
public function checkInputValues(bool $throwExeception=true){
if(!parent::checkInputValues($throwExeception) && !$throwException){
return false;
}
if(!key_exists('city', $this->general)){
if(!$throwExeception){return false; }
throw new \InvalidArgumentException("require city in general");
}
return true;
}
public function test():bool{
$this->checkInputValues();
$this->tested = true;
if($this->destCity['city'] != $this->general['city']){
return true;
}
$this->reason = "같은 도시입니다.";
return false;
}
}
+12 -8
View File
@@ -38,7 +38,7 @@ class General implements iAction{
* @param int $month 게임 월
* @param bool $fullConstruct iAction, 및 ActionLogger 초기화 여부, false인 경우 no, name, city, nation, level 정도로 초기화 가능
*/
public function __construct(array $raw, ?array $city, int $year, int $month, bool $fullConstruct=true){
public function __construct(array $raw, ?array $city, ?int $year, ?int $month, bool $fullConstruct=true){
//TODO: 밖에서 가져오도록 하면 버그 확률이 높아짐. 필요한 raw 값을 직접 구해야함.
$staticNation = getNationStaticInfo($raw['nation']);
@@ -51,17 +51,21 @@ class General implements iAction{
}
$this->resultTurn = new LastTurn();
if($year !== null || $month !== null){
$this->logger = new ActionLogger(
$this->getVar('no'),
$this->getVar('nation'),
$year,
$month,
false
);
}
if(!$fullConstruct){
return;
}
$this->logger = new ActionLogger(
$this->getVar('no'),
$this->getVar('nation'),
$year,
$month,
false
);
$nationTypeClass = getNationTypeClass($staticNation['type']);
$this->nationType = new $nationTypeClass;
+22 -11
View File
@@ -8,11 +8,12 @@ class TurnExecutionHelper
* @var General $generalObj;
*/
protected $generalObj;
protected $generalSource = null;
public function __construct(array $rawGeneral, int $year, int $month)
public function __construct(General $general, ?array $generalSource = null)
{
$this->generalObj = new General($rawGeneral, null, $year, $month);
$this->nationTurn = $nationTurn;
$this->generalObj = $general;
$this->generalSource = $generalSource;
}
public function __destruct()
@@ -76,6 +77,7 @@ class TurnExecutionHelper
$gameStor = KVStorage::getStorage($db, 'game_env');
$commandObj = buildNationCommandClass($commandClassName, $general, $gameStor->getAll(true), $commandLast, $commandArg);
$commandObj->setGeneralSource($this->generalSource);
$failReason = $commandObj->testReservable();
if($failReason){
@@ -101,6 +103,7 @@ class TurnExecutionHelper
$commandClass = getGeneralCommandClass($commandClassName);
/** @var \sammo\Command\GeneralCommand $commandObj */
$commandObj = new $commandClass($general, $gameStor->getAll(true), $commandArg, $commandLast);
$commandObj->setGeneralSource($this->generalSource);
$failReason = $commandObj->testReservable();
if($failReason){
@@ -185,7 +188,7 @@ class TurnExecutionHelper
static public function executeGeneralCommandUntil(string $date, \DateTimeInterface $limitActionTime, int $year, int $month){
$db = DB::db();
$generalsTodo = $db->queryFirstRow(
$rawGeneralsTodo = $db->queryFirstRow(
'SELECT no,name,name2,picture,imgsvr,nation,nations,city,troop,injury,affinity,
leader,leader2,power,power2,intel,intel2,weap,book,horse,item,
experience,dedication,level,gold,rice,crew,crewtype,train,atmos,
@@ -200,20 +203,28 @@ WHERE turntime < %s ORDER BY turntime ASC, `no` ASC',
$date
);
$generalsTodo = [];
$generalSource = [];
foreach($rawGeneralsTodo as $rawGeneral){
$generalCommand = $rawGeneral['action'];
$generalArg = Json::decode($rawGeneral['arg'])??[];
unset($rawGeneral['action']);
unset($rawGeneral['arg']);
$general = new General($rawGeneral, $year, $month);
$generalsTodo[] = [$general, $generalCommand, $generalArg];
$generalSource[$general->getID()] = $general;
}
$currentTurn = null;
foreach($generalsTodo as $generalWork){
foreach($generalsTodo as [$general, $generalCommand, $generalArg]){
$currActionTime = new \DateTimeImmutable();
if($currActionTime > $limitActionTime){
return [true, $currentTurn];
}
$generalCommand = $generalWork['action'];
$generalArg = Json::decode($generalWork['arg'])??[];
unset($generalWork['action']);
unset($generalWork['arg']);
$turnObj = new static($generalWork, $year, $month);
$turnObj = new static($general, $generalSource);
$hasNationTurn = false;
if($generalWork['nation'] != 0 && $generalWork['level'] >= 5){