From 1e963ee1d8b16ef587637bc39d1d71fc807f2885 Mon Sep 17 00:00:00 2001 From: hide_d Date: Wed, 13 May 2020 01:56:08 +0900 Subject: [PATCH] =?UTF-8?q?=EC=A0=95=EC=B1=85=20=EB=B0=98=EC=98=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- hwe/j_set_npc_control.php | 192 +++++++++++++++++++++++++++++ hwe/js/npc_control.js | 8 +- hwe/sammo/AutorunGeneralPolicy.php | 8 +- hwe/sammo/AutorunNationPolicy.php | 6 +- hwe/sammo/GeneralAI.php | 14 ++- i_entrance/entrance.php | 2 +- i_entrance/user_info.php | 2 +- index.php | 2 +- 8 files changed, 221 insertions(+), 13 deletions(-) create mode 100644 hwe/j_set_npc_control.php diff --git a/hwe/j_set_npc_control.php b/hwe/j_set_npc_control.php new file mode 100644 index 00000000..794c09b2 --- /dev/null +++ b/hwe/j_set_npc_control.php @@ -0,0 +1,192 @@ +setReadOnly(); +$userID = Session::getUserID(); +$generalID = $session->generalID; + + +$type = Util::getPost('type', 'string'); +$data = Util::getPost('data', 'string'); + +$availableTypes = [ + 'generalPriority', + 'nationPriority', + 'nationPolicy', +]; + +if(!in_array($type, $availableTypes)){ + Json::die([ + 'result'=>false, + 'reason'=>'올바른 타입이 아닙니다.', + ]); +} + +$data = JSON::decode($data); +if(!$data || !is_array($data)){ + Json::die([ + 'result'=>false, + 'reason'=>'올바른 입력이 아닙니다.', + ]); +} + +$db = DB::db(); + +$me = $db->queryFirstRow('SELECT no, name, npc, nation, city, officer_level, con, turntime, belong, permission, penalty FROM general WHERE owner=%i', $userID); + + +$nationID = $me['nation']; +if (!$nationID) { + Json::die([ + 'result'=>false, + 'reason'=>'국가에 소속되어있지 않습니다.', + ]); +} +if ($me['officer_level'] < 5) { + Json::die([ + 'result'=>false, + 'reason'=>'수뇌가 아닙니다.', + ]); +} + + +function applyNationPolicy($policy, $nationID, $generalName):?string{ + $db = DB::db(); + $nationStor = KVStorage::getStorage($db, $nationID, 'nation_env'); + + $defaultPolicy = AutorunNationPolicy::$defaultPolicy; + $troopCache = null; + foreach($db->queryFirstColumn('SELECT troop_leader FROM troop WHERE nation=%i',$nationID) as $troopID){ + $troopCache[$troopID] = 'Neutral'; + } + $cityList = CityConst::all(); + $nationPolicyRoot = $nationStor->npc_nation_policy; + $nationPolicy = $nationPolicyRoot['values']??[]; + foreach($policy as $key=>$val){ + + if($key === 'CombatForce'){ + if(!is_array($val)){ + return "{$key}는 올바른 정책값이 아닙니다."; + } + foreach($val as $troopID=>$troopTarget){ + if(!key_exists($troopID, $troopCache)){ + return "{$troopID}는 국가의 부대가 아닙니다."; + } + if($troopCache[$troopID] != 'Neutral'){ + return "부대({$troopID}는 하나의 역할만 지정할 수 있습니다."; + } + if(!is_array($troopTarget) || count($troopTarget)!=2){ + return "{$troopID}의 입력양식이 올바르지 않습니다."; + } + [$fromCity, $toCity] = $troopCache; + if(!key_exists($fromCity, $cityList) || !key_exists($toCity, $cityList)){ + return "{$troopID}의 도시 {$fromCity}, {$toCity}가 올바른 도시 번호가 아닙니다."; + } + $troopCache[$troopID]=$key; + } + $nationPolicy[$key]=$val; + continue; + } + if(in_array($key, ['SupportForce', 'DevelopForce'])){ + if(!is_array($val)){ + return "{$key}는 올바른 정책값이 아닙니다."; + } + foreach($val as $troopID){ + if(!key_exists($troopID, $troopCache)){ + return "{$troopID}는 국가의 부대가 아닙니다."; + } + if($troopCache[$troopID] != 'Neutral'){ + return "부대({$troopID}는 하나의 역할만 지정할 수 있습니다."; + } + $troopCache[$troopID]=$key; + } + + $nationPolicy[$key]=array_values($val); + continue; + } + + + if(!key_exists($key, $defaultPolicy)){ + return "{$key}는 올바른 정책값이 아닙니다."; + } + $defaultValue = $defaultPolicy[$key]; + if(is_numeric($defaultValue) != is_numeric($val)){ + return "{$key}는 올바른 값이 아닙니다."; + } + if(is_integer($defaultValue) != is_integer($val)){ + return "{$key}는 올바른 값이 아닙니다."; + } + if(is_array($defaultValue) != is_array($val)){ + return "{$key}는 올바른 값이 아닙니다."; + } + $nationPolicy[$key] = $val; + } + + $nationPolicyRoot['values'] = $nationPolicy; + $nationPolicyRoot['valueSetter'] = $generalName; + $nationStor->npc_nation_policy = $nationPolicyRoot; + return null; +} + +function applyNationPriority($priority, $nationID, $generalName):?string{ + $db = DB::db(); + $nationStor = KVStorage::getStorage($db, $nationID, 'nation_env'); + $nationPolicyRoot = $nationStor->npc_nation_policy; + + $defaultPriority = AutorunNationPolicy::$defaultPriority; + foreach($priority as $item){ + if(!in_array($item, $defaultPriority)){ + return "{$item}은 올바른 명령이 아닙니다."; + } + } + $nationPolicyRoot['priority'] = $priority; + $nationPolicyRoot['prioritySetter'] = $generalName; + $nationStor->npc_nation_policy = $nationPolicyRoot; + return null; +} + +function applyGeneralPriority($priority, $nationID, $generalName):?string{ + $db = DB::db(); + $nationStor = KVStorage::getStorage($db, $nationID, 'nation_env'); + $generalPolicyRoot = $nationStor->npc_general_policy; + + $defaultPriority = AutorunGeneralPolicy::$default_priority; + foreach($priority as $item){ + if(!in_array($item, $defaultPriority)){ + return "{$item}은 올바른 명령이 아닙니다."; + } + } + $generalPolicyRoot['priority'] = $priority; + $generalPolicyRoot['prioritySetter'] = $generalName; + $nationStor->npc_nation_policy = $generalPolicyRoot; + return null; +} + +if($type == 'nationPolicy'){ + $result = applyNationPolicy($data, $nationID, $me['name']); +} +else if($type == 'generalPriority'){ + $result = applyGeneralPriority($data, $nationID, $me['name']); +} +else if($type == 'nationPriority'){ + $result = applyNationPriority($data, $nationID, $me['name']); +} +else{ + throw new MustNotBeReachedException(); +} + +if($result!==null){ + Json::die([ + 'result'=>false, + 'reason'=>$result, + ]); +} +Json::die([ + 'result'=>true, + 'reason'=>'success', +]); \ No newline at end of file diff --git a/hwe/js/npc_control.js b/hwe/js/npc_control.js index 9f4d03d3..4a145c4e 100644 --- a/hwe/js/npc_control.js +++ b/hwe/js/npc_control.js @@ -127,7 +127,7 @@ jQuery(function ($) { $.toast({ title: '되돌리기 완료', content: '이전 설정으로 되돌렸습니다.', - type: 'success', + type: 'info', delay: 5000 }); }); @@ -201,6 +201,12 @@ jQuery(function ($) { if (!data.result) { return quickReject('설정하지 못했습니다. : ' + data.reason); } + $.toast({ + title: '적용 완료', + content: 'NPC 정책이 반영되었습니다.', + type: 'success', + delay: 5000 + }); }, errUnknownToast) .fail(function (reason) { $.toast({ diff --git a/hwe/sammo/AutorunGeneralPolicy.php b/hwe/sammo/AutorunGeneralPolicy.php index 75faf145..0a23b150 100644 --- a/hwe/sammo/AutorunGeneralPolicy.php +++ b/hwe/sammo/AutorunGeneralPolicy.php @@ -124,7 +124,9 @@ class AutorunGeneralPolicy{ } $priority[] = $priorityItem; } - $this->priority = $priority; + if($priority){ + $this->priority = $priority; + } } if($nationPolicy && key_exists('priority', $nationPolicy)){ @@ -135,7 +137,9 @@ class AutorunGeneralPolicy{ } $priority[] = $priorityItem; } - $this->priority = $priority; + if($priority){ + $this->priority = $priority; + } } if($general->getNPCType() >= 2){ diff --git a/hwe/sammo/AutorunNationPolicy.php b/hwe/sammo/AutorunNationPolicy.php index 880d7f25..93011ec4 100644 --- a/hwe/sammo/AutorunNationPolicy.php +++ b/hwe/sammo/AutorunNationPolicy.php @@ -174,13 +174,13 @@ class AutorunNationPolicy { foreach(static::$defaultPolicy as $policy=>$value){ $this->{$policy} = $value; } - + if($serverPolicy){ foreach($serverPolicy['values']??[] as $policy=>$value){ if(!property_exists($this, $policy)){ continue; } - $this->$policy = $value; + $this->{$policy} = $value; } if(key_exists('priority', $serverPolicy)){ @@ -194,7 +194,7 @@ class AutorunNationPolicy { if(!property_exists($this, $policy)){ continue; } - $this->$policy = $value; + $this->{$policy} = $value; } if(key_exists('priority', $nationPolicy)){ diff --git a/hwe/sammo/GeneralAI.php b/hwe/sammo/GeneralAI.php index acc577bb..bbcdd399 100644 --- a/hwe/sammo/GeneralAI.php +++ b/hwe/sammo/GeneralAI.php @@ -116,8 +116,8 @@ class GeneralAI $gameStor = KVStorage::getStorage($db, 'game_env'); $nationStor = KVStorage::getStorage($db, $this->nation['nation'], 'nation_env'); - $this->nationPolicy = new AutorunNationPolicy($general, $this->env['autorun_user']['options'], $nationStor->getValue('npc_general_policy'), $gameStor->getValue('npc_general_policy'), $this->nation, $this->env); - $this->generalPolicy = new AutorunGeneralPolicy($general, $this->env['autorun_user']['options'], $nationStor->getValue('npc_nation_policy'), $gameStor->getValue('npc_nation_policy'), $this->nation, $this->env); + $this->nationPolicy = new AutorunNationPolicy($general, $this->env['autorun_user']['options'], $gameStor->getValue('npc_nation_policy'), $nationStor->getValue('npc_nation_policy'), $this->nation, $this->env); + $this->generalPolicy = new AutorunGeneralPolicy($general, $this->env['autorun_user']['options'], $gameStor->getValue('npc_general_policy'), $nationStor->getValue('npc_general_policy'), $this->nation, $this->env); $this->nation['aux'] = Json::decode($this->nation['aux']??'{}'); @@ -3126,7 +3126,10 @@ class GeneralAI foreach($this->nationPolicy->priority as $actionName){ - if(property_exists($this->nationPolicy, 'can'.$actionName) && !$this->nationPolicy->{'can'.$actionName}){ + if(!property_exists($this->nationPolicy, 'can'.$actionName)){ + continue; + } + if(!$this->nationPolicy->{'can'.$actionName}){ continue; } if($npcType < 2 && !($this->nationPolicy::$availableInstantTurn[$actionName]??false)){ @@ -3276,7 +3279,10 @@ class GeneralAI } foreach($this->generalPolicy->priority as $actionName){ - if(!$this->generalPolicy->{'can'.$actionName}){ + if(!property_exists($this->generalPolicy, 'can'.$actionName)){ + continue; + } + if(!($this->generalPolicy->{'can'.$actionName})){ continue; } /** @var ?GeneralCommand */ diff --git a/i_entrance/entrance.php b/i_entrance/entrance.php index 7b5499bc..da14dce4 100644 --- a/i_entrance/entrance.php +++ b/i_entrance/entrance.php @@ -46,7 +46,7 @@ $acl = $session->acl; -