feat, refac: TurnExecutionHelper에 하드코딩된 함수를 이동

- GameConst에서 정의된 defaultEvent 사용
- runEventHandler() 작성
  - 이와 관련한 호출을 모두 통합
- 이벤트 핸들러 호출 시 gameStor 캐시 초기화하지 않도록 변경
This commit is contained in:
2023-03-12 17:02:31 +09:00
parent db66c1728a
commit 6ffe3caf4a
8 changed files with 107 additions and 530 deletions
+4 -19
View File
@@ -9,13 +9,15 @@ use \sammo\{
LastTurn,
Command,
Json,
KVStorage
KVStorage,
TurnExecutionHelper
};
use \sammo\Constraint\Constraint;
use \sammo\Constraint\ConstraintHelper;
use sammo\CityConst;
use sammo\Enums\EventTarget;
use sammo\Event\EventHandler;
use function sammo\refreshNationStaticInfo;
@@ -110,24 +112,7 @@ class che_해산 extends Command\GeneralCommand{
$general->applyDB($db);
// 이벤트 핸들러 동작
$gameStor = KVStorage::getStorage($db, 'game_env');
$e_env = null;
foreach (DB::db()->query('SELECT * FROM event WHERE target = "DESTROY_NATION" ORDER BY `priority` DESC, `id` ASC') as $rawEvent) {
if ($e_env === null) {
$e_env = $gameStor->getAll(false);
}
$eventID = $rawEvent['id'];
$cond = Json::decode($rawEvent['condition']);
$action = Json::decode($rawEvent['action']);
$event = new EventHandler($cond, $action);
$e_env['currentEventID'] = $eventID;
$event->tryRunEvent($e_env);
}
if ($e_env !== null) {
$gameStor->resetCache();
}
TurnExecutionHelper::runEventHandler($db, null, EventTarget::OccupyCity);
return true;
}
+1 -1
View File
@@ -7,6 +7,6 @@ enum EventTarget: string{
case Month = 'MONTH';
//PostMonth는 없음
case OccupyCity = 'OCCUPY_CITY';
case DestriyNation = 'DESTROY_NATION';
case DestroyNation = 'DESTROY_NATION';
case United = 'UNITED';
}
+50
View File
@@ -414,6 +414,46 @@ class GameConstBase
]
];
public static $defaultEvents = [
[
"pre_month", 9000,
true,
["UpdateCitySupply"],
["ProcessWarIncome"]
],
[
"month", 9000,
["Date", "==", null, 1],
["MergeInheritPointRank"],
["ProcessSemiAnnual", "gold"],
["ProcessIncome", "gold"],
["ResetOfficerLock"],
["RaiseDisaster"],
["RandomizeCityTradeRate"],
["NewYear"],
["AssignGeneralSpeciality"],
],
[
"month", 9000,
["Date", "==", null, 4],
["ResetOfficerLock"],
["RaiseDisaster"],
],
[
"month", 9000,
["Date", "==", null, 7],
["MergeInheritPointRank"],
["ProcessSemiAnnual", "rice"],
["ProcessIncome", "rice"],
["ResetOfficerLock"],
["RaiseDisaster"],
["RandomizeCityTradeRate"],
],
[
"month", 9000,
["Date", "==", null, 10],
["ResetOfficerLock"],
["RaiseDisaster"],
],
[
"month", 2000,
["DateRelative", "==", 1, 1],
@@ -446,5 +486,15 @@ class GameConstBase
["AddGlobalBetray", 1, 1],
["DeleteEvent"]
],
[
"month", 1000,
true,
["ProvideNPCTroopLeader"]
],
[
"united", 5000,
true,
["MergeInheritPointRank"],
]
];
}
+27 -48
View File
@@ -2,6 +2,7 @@
namespace sammo;
use sammo\Enums\EventTarget;
use sammo\Enums\InheritanceKey;
use \Symfony\Component\Lock;
@@ -352,6 +353,29 @@ class TurnExecutionHelper
return [false, $currentTurn];
}
static public function runEventHandler(\MeekroDB $db, ?KVStorage $gameStor, EventTarget $eventTarget): bool{
if($gameStor === null){
$gameStor = KVStorage::getStorage($db, 'game_env');
}
$e_env = null;
foreach ($db->query('SELECT * FROM event WHERE target = %s ORDER BY `priority` DESC, `id` ASC', $eventTarget->value) as $rawEvent) {
if ($e_env === null) {
$e_env = $gameStor->getAll(false);
}
$eventID = $rawEvent['id'];
$cond = Json::decode($rawEvent['condition']);
$action = Json::decode($rawEvent['action']);
$event = new Event\EventHandler($cond, $action);
$e_env['currentEventID'] = $eventID;
$event->tryRunEvent($e_env);
}
if ($e_env === null) {
return false;
}
return true;
}
static public function executeAllCommand(&$executed = false, &$locked = false): string
{
$db = DB::db();
@@ -431,63 +455,18 @@ class TurnExecutionHelper
)));
// 1달마다 처리하는 것들, 벌점 감소 및 건국,전턴,합병 -1, 군량 소모
static::runEventHandler($db, $gameStor, EventTarget::PreMonth);
if (!preUpdateMonthly()) {
$gameStor->resetCache();
unlock();
throw new \RuntimeException('preUpdateMonthly() 처리 에러');
}
turnDate($nextTurn);
$logger = new ActionLogger(0, 0, $gameStor->year, $gameStor->month, false);
// 분기계산. 장수들 턴보다 먼저 있다면 먼저처리
if ($gameStor->month == 1) {
processSumInheritPointRank();
processSpring();
processGoldIncome();
updateYearly();
updateQuaterly();
disaster($monthlyRng);
tradeRate($monthlyRng);
addAge($monthlyRng);
// 새해 알림
$logger->pushGlobalActionLog("<C>{$gameStor->year}</>년이 되었습니다.");
$logger->flush(); //TODO: globalAction류는 전역에서 관리하는것이 좋을 듯.
} elseif ($gameStor->month == 4) {
updateQuaterly();
disaster($monthlyRng);
} elseif ($gameStor->month == 7) {
processSumInheritPointRank();
processFall();
processRiceIncome();
updateQuaterly();
disaster($monthlyRng);
tradeRate($monthlyRng);
} elseif ($gameStor->month == 10) {
updateQuaterly();
disaster($monthlyRng);
checkStatistic();
}
// 이벤트 핸들러 동작
$e_env = null;
foreach (DB::db()->query('SELECT * FROM event WHERE target = "MONTH" ORDER BY `priority` DESC, `id` ASC') as $rawEvent) {
if ($e_env === null) {
$e_env = $gameStor->getAll(false);
}
$eventID = $rawEvent['id'];
$cond = Json::decode($rawEvent['condition']);
$action = Json::decode($rawEvent['action']);
$event = new Event\EventHandler($cond, $action);
$e_env['currentEventID'] = $eventID;
$event->tryRunEvent($e_env);
}
if ($e_env !== null) {
$gameStor->resetCache();
}
static::runEventHandler($db, $gameStor, EventTarget::Month);
postUpdateMonthly($monthlyRng);
// 다음달로 넘김