feat, refac: 매달 실행되는 함수 마저 구현

- NewYear: 새해(AddAge 일부)
- AssignGeneralSpeciality: 특히 부여(AddAge 일부)
- RaiseDisaster
- RandomizeCityTradeRate
- ResetOfficeLock: 구 updateQuaterly
This commit is contained in:
2023-03-12 17:02:30 +09:00
parent 54380950eb
commit 7b1a9157e1
5 changed files with 349 additions and 0 deletions
@@ -0,0 +1,90 @@
<?php
namespace sammo\Event\Action;
use sammo\ActionLogger;
use sammo\DB;
use sammo\GameConst;
use sammo\JosaUtil;
use sammo\Json;
use sammo\LiteHashDRBG;
use sammo\RandUtil;
use sammo\SpecialityHelper;
use sammo\UniqueConst;
use sammo\Util;
use function sammo\buildGeneralSpecialDomesticClass;
use function sammo\buildGeneralSpecialWarClass;
class AssignGeneralSpeciality extends \sammo\Event\Action
{
public function run(array $env)
{
$startYear = $env['startyear'];
$year = $env['year'];
$month = $env['month'];
if ($year < $startYear + 3) {
return;
}
$db = DB::db();
$rng = new RandUtil(new LiteHashDRBG(Util::simpleSerialize(
UniqueConst::$hiddenSeed,
'assignGeneralSpeciality',
$year,
$month,
)));
foreach ($db->query('SELECT no,name,nation,leadership,strength,intel,aux from general where specage<=age and special=%s', GameConst::$defaultSpecialDomestic) as $general) {
$generalID = $general['no'];
$special = SpecialityHelper::pickSpecialDomestic(
$rng,
$general,
(Json::decode($general['aux'])['prev_types_special']) ?? []
);
$specialClass = buildGeneralSpecialDomesticClass($special);
$specialText = $specialClass->getName();
$db->update('general', [
'special' => $special
], 'no=%i', $generalID);
$logger = new ActionLogger($generalID, $general['nation'], $year, $month);
$josaUl = JosaUtil::pick($specialText, '을');
$logger->pushGeneralActionLog("특기 【<b><L>{$specialText}</></b>】{$josaUl} 익혔습니다!", ActionLogger::PLAIN);
$logger->pushGeneralHistoryLog("특기 【<b><C>{$specialText}</></b>】{$josaUl} 습득");
}
foreach ($db->query('SELECT no,name,nation,leadership,strength,intel,npc,dex1,dex2,dex3,dex4,dex5,aux from general where specage2<=age and special2=%s', GameConst::$defaultSpecialWar) as $general) {
$generalID = $general['no'];
$generalAux = Json::decode($general['aux']);
$updateVars = [];
if (key_exists('inheritSpecificSpecialWar', $generalAux)) {
$special2 = $generalAux['inheritSpecificSpecialWar'];
unset($generalAux['inheritSpecificSpecialWar']);
$updateVars['aux'] = Json::encode($generalAux);
} else {
$special2 = SpecialityHelper::pickSpecialWar(
$rng,
$general,
($generalAux['prev_types_special2']) ?? []
);
}
$specialClass = buildGeneralSpecialWarClass($special2);
$specialText = $specialClass->getName();
$updateVars['special2'] = $special2;
$db->update('general', $updateVars, 'no=%i', $general['no']);
$logger = new ActionLogger($generalID, $general['nation'], $year, $month);
$josaUl = JosaUtil::pick($specialText, '을');
$logger->pushGeneralActionLog("특기 【<b><L>{$specialText}</></b>】{$josaUl} 익혔습니다!", ActionLogger::PLAIN);
$logger->pushGeneralHistoryLog("특기 【<b><C>{$specialText}</></b>】{$josaUl} 습득");
}
}
}
+30
View File
@@ -0,0 +1,30 @@
<?php
namespace sammo\Event\Action;
use sammo\ActionLogger;
use sammo\DB;
class NewYear extends \sammo\Event\Action
{
public function run(array $env)
{
$year = $env['year'];
$month = $env['month'];
$logger = new ActionLogger(0, 0, $year, $month, false);
$logger->pushGlobalActionLog("<C>{$year}</>년이 되었습니다.");
$logger->flush(); //TODO: globalAction류는 전역에서 관리하는것이 좋을 듯.
$db = DB::db();
//나이와 호봉 증가
$db->update('general', [
'age' => $db->sqleval('age+1'),
], true);
$db->update('general', [
'belong' => $db->sqleval('belong+1')
], 'nation != 0');
}
}
+159
View File
@@ -0,0 +1,159 @@
<?php
namespace sammo\Event\Action;
use sammo\ActionLogger;
use sammo\DB;
use sammo\General;
use sammo\KVStorage;
use sammo\LiteHashDRBG;
use sammo\RandUtil;
use sammo\UniqueConst;
use sammo\Util;
use function sammo\SabotageInjury;
class RaiseDisaster extends \sammo\Event\Action
{
public function run(array $env)
{
$db = DB::db();
$startYear = $env['startyear'];
$year = $env['year'];
$month = $env['month'];
$rng = new RandUtil(new LiteHashDRBG(Util::simpleSerialize(
UniqueConst::$hiddenSeed,
'disater',
$year,
$month,
)));
//재난표시 초기화
$db->update('city', [
'state' => 0,
], 'state <= 10');
// 초반 3년은 스킵
if ($startYear + 3 > $year) return;
$boomingRate = [
1 => 0,
4 => 0.25,
7 => 0.25,
10 => 0
];
$isGood = $rng->nextBool($boomingRate[$month]);
$targetCityList = [];
foreach ($db->query('SELECT city,name,secu,secu_max FROM city') as $city) {
//호황 발생 도시 선택 ( 기본 2% )
//재해 발생 도시 선택 ( 기본 6% )
if ($isGood) {
$raiseProp = 0.02 + ($city['secu'] / $city['secu_max']) * 0.05; // 2 ~ 7%
} else {
$raiseProp = 0.06 - ($city['secu'] / $city['secu_max']) * 0.05; // 1 ~ 6%
}
if ($rng->nextBool($raiseProp)) {
$targetCityList[] = $city;
}
}
if (!$targetCityList) {
return;
}
$targetCityNames = "<G><b>" . join(' ', Util::squeezeFromArray($targetCityList, 'name')) . "</b></>";
$disasterTextList = [
1 => [
['<M><b>【재난】</b></>', 4, '역병이 발생하여 도시가 황폐해지고 있습니다.'],
['<M><b>【재난】</b></>', 5, '지진으로 피해가 속출하고 있습니다.'],
['<M><b>【재난】</b></>', 3, '추위가 풀리지 않아 얼어죽는 백성들이 늘어나고 있습니다.'],
['<M><b>【재난】</b></>', 9, '황건적이 출현해 도시를 습격하고 있습니다.'],
],
4 => [
['<M><b>【재난】</b></>', 7, '홍수로 인해 피해가 급증하고 있습니다.'],
['<M><b>【재난】</b></>', 5, '지진으로 피해가 속출하고 있습니다.'],
['<M><b>【재난】</b></>', 6, '태풍으로 인해 피해가 속출하고 있습니다.'],
],
7 => [
['<M><b>【재난】</b></>', 8, '메뚜기 떼가 발생하여 도시가 황폐해지고 있습니다.'],
['<M><b>【재난】</b></>', 5, '지진으로 피해가 속출하고 있습니다.'],
['<M><b>【재난】</b></>', 8, '흉년이 들어 굶어죽는 백성들이 늘어나고 있습니다.'],
],
10 => [
['<M><b>【재난】</b></>', 3, '혹한으로 도시가 황폐해지고 있습니다.'],
['<M><b>【재난】</b></>', 5, '지진으로 피해가 속출하고 있습니다.'],
['<M><b>【재난】</b></>', 3, '눈이 많이 쌓여 도시가 황폐해지고 있습니다.'],
['<M><b>【재난】</b></>', 9, '황건적이 출현해 도시를 습격하고 있습니다.'],
]
];
$boomingTextList = [
1 => null,
4 => [
['<C><b>【호황】</b></>', 2, '호황으로 도시가 번창하고 있습니다.'],
],
7 => [
['<C><b>【풍작】</b></>', 1, '풍작으로 도시가 번창하고 있습니다.'],
],
10 => null
];
[$logTitle, $stateCode, $logBody] = $rng->choice(($isGood ? $boomingTextList : $disasterTextList)[$month]);
$logger = new ActionLogger(0, 0, $year, $month, false);
$logger->pushGlobalHistoryLog("{$logTitle}{$targetCityNames}{$logBody}");
$logger->flush();
if (!$isGood) {
$generalListByCity = Util::arrayGroupBy($db->query('SELECT no, name, nation, city, officer_level, injury, leadership, strength, intel, horse, weapon, book, item, crew, crewtype, atmos, train, special, special2 FROM general WHERE city IN %li', Util::squeezeFromArray($targetCityList, 'city')), 'city');
//NOTE: 쿼리 1번이지만 복잡하기 vs 쿼리 여러번이지만 조금 더 깔끔하기
foreach ($targetCityList as $city) {
$affectRatio = Util::valueFit($city['secu'] / $city['secu_max'] / 0.8, 0, 1);
$affectRatio = 0.8 + $affectRatio * 0.15;
$db->update('city', [
'state' => $stateCode,
'pop' => $db->sqleval('pop * %d', $affectRatio),
'trust' => $db->sqleval('trust * %d', $affectRatio),
'agri' => $db->sqleval('agri * %d', $affectRatio),
'comm' => $db->sqleval('comm * %d', $affectRatio),
'secu' => $db->sqleval('secu * %d', $affectRatio),
'def' => $db->sqleval('def * %d', $affectRatio),
'wall' => $db->sqleval('wall * %d', $affectRatio),
], 'city = %i', $city['city']);
$generalList = array_map(
function ($rawGeneral) use ($city, $year, $month) {
return new General($rawGeneral, null, $city, null, $year, $month, false);
},
$generalListByCity[$city['city']] ?? []
);
SabotageInjury($rng, $generalList, '재난');
}
} else {
foreach ($targetCityList as $city) {
$affectRatio = Util::valueFit($city['secu'] / $city['secu_max'] / 0.8, 0, 1);
$affectRatio = 1.01 + $affectRatio * 0.04;
$db->update('city', [
'state' => $stateCode,
'pop' => $db->sqleval('least(pop * %d, pop_max)', $affectRatio),
'trust' => $db->sqleval('least(trust * %d, 100)', $affectRatio),
'agri' => $db->sqleval('least(agri * %d, agri_max)', $affectRatio),
'comm' => $db->sqleval('least(comm * %d, comm_max)', $affectRatio),
'secu' => $db->sqleval('least(secu * %d, secu_max)', $affectRatio),
'def' => $db->sqleval('least(def * %d, def_max)', $affectRatio),
'wall' => $db->sqleval('least(wall * %d, wall_max)', $affectRatio),
], 'city = %i', $city['city']);
}
}
}
}
@@ -0,0 +1,48 @@
<?php
namespace sammo\Event\Action;
use DB;
use sammo\LiteHashDRBG;
use sammo\RandUtil;
use sammo\UniqueConst;
use sammo\Util;
class RandomizeCityTradeRate extends \sammo\Event\Action
{
public function run(array $env)
{
$year = $env['year'];
$month = $env['month'];
$rng = new RandUtil(new LiteHashDRBG(Util::simpleSerialize(
UniqueConst::$hiddenSeed,
'randomizeCityTradeRate',
$year,
$month,
)));
$db = DB::db();
foreach ($db->query('SELECT city,level FROM city') as $city) {
//시세
$prob = [
1 => 0,
2 => 0,
3 => 0,
4 => 0.2,
5 => 0.4,
6 => 0.6,
7 => 0.8,
8 => 1
][$city['level']];
if ($prob > 0 && $rng->nextBool($prob)) {
$trade = $rng->nextRangeInt(95, 105);
} else {
$trade = null;
}
$db->update('city', [
'trade' => $trade
], 'city=%i', $city['city']);
}
}
}
@@ -0,0 +1,22 @@
<?php
namespace sammo\Event\Action;
use sammo\DB;
class ResetOfficerLock extends \sammo\Event\Action
{
public function run(array $env)
{
$db = DB::db();
//천도 제한 해제, 관직 변경 제한 해제
$db->update('nation', [
'chief_set' => 0,
], true);
//관직 변경 제한 해제
$db->update('city', [
'officer_set' => 0,
], true);
}
}