refac: General/GetFrontInfo 정보 추가

- Global/GetRecentRecord 항목 포함
This commit is contained in:
2023-03-09 02:20:23 +09:00
parent 53569e3f12
commit 6395f73f6a
4 changed files with 148 additions and 62 deletions
+103 -1
View File
@@ -32,12 +32,14 @@ use function sammo\getDed;
use function sammo\getHonor;
use function sammo\getNationStaticInfo;
use function sammo\getOfficerLevelText;
use function sammo\increaseRefresh;
/**
* 통째로 메인페이지의 주요 정보를 반환하는 날로먹는 API
*/
class GetFrontInfo extends \sammo\BaseAPI
{
const ROW_LIMIT = 15;
public function getRequiredSessionMode(): int
{
@@ -47,13 +49,110 @@ class GetFrontInfo extends \sammo\BaseAPI
public function validateArgs(): ?string
{
$v = new Validator($this->args);
$v->rule('lengthMin', 'lastNationNoticeDate', 19);
$v->rule('lengthMin', 'lastNationNoticeDate', 19)
->rule('integer', 'lastGeneralRecordID')
->rule('integer', 'lastWorldHistoryID');
if (!$v->validate()) {
return $v->errorStr();
}
$this->args['lastGeneralRecordID'] = (int)($this->args['lastGeneralRecordID'] ?? 0);
$this->args['lastWorldHistoryID'] = (int)($this->args['lastWorldHistoryID'] ?? 0);
return null;
}
private function getGlobalRecord(int $lastRecordID): array
{
$db = DB::db();
$globalRecord = $db->queryAllLists(
'SELECT id, `text` FROM general_record WHERE `general_id` = 0 AND log_type = %s AND id >= %i ORDER BY `id` DESC LIMIT %i',
'history',
$lastRecordID,
static::ROW_LIMIT + 1,
);
return $globalRecord;
}
private function getGeneralRecord(int $generalID, int $lastRecordID): array
{
$db = DB::db();
$generalRecord = $db->queryAllLists(
'SELECT id, `text` FROM general_record WHERE `general_id` = %i AND log_type = %s AND id >= %i ORDER BY `id` DESC LIMIT %i',
$generalID,
'action',
$lastRecordID,
static::ROW_LIMIT + 1,
);
return $generalRecord;
}
private function getHistory(int $lastHistoryID): array
{
$db = DB::db();
$history = $db->queryAllLists(
'SELECT id, `text` FROM world_history WHERE nation_id = 0 AND id >= %i ORDER BY `id` DESC LIMIT %i',
$lastHistoryID,
static::ROW_LIMIT + 1,
);
return $history;
}
private function generateRecentRecord(int $generalID){
$db = DB::db();
$gameStor = KVStorage::getStorage($db, 'game_env');
$gameStor->cacheValues(['isunited', 'opentime', 'refresh']);
$lastHistoryID = $this->args['lastWorldHistoryID'];
$lastRecordID = $this->args['lastGeneralRecordID'];
$history = $this->getHistory($lastHistoryID);
$globalRecord = $this->getGlobalRecord($lastRecordID);
$generalRecord = $this->getGeneralRecord($generalID, $lastRecordID);
$flushHistory = false;
$flushGlobalRecord = false;
$flushGeneralRecord = false;
if (!$history) {
$flushHistory = false;
} else if (Util::array_last($history)[0] <= $lastHistoryID) {
$flushHistory = false;
array_pop($history);
} else if (count($history) > static::ROW_LIMIT) {
array_pop($history);
}
if (!$globalRecord) {
$flushGlobalRecord = false;
} else if (Util::array_last($globalRecord)[0] == $lastRecordID) {
$flushGlobalRecord = false;
array_pop($globalRecord);
} else if (count($globalRecord) > static::ROW_LIMIT) {
array_pop($globalRecord);
}
if (!$generalRecord) {
$flushGeneralRecord = false;
} else if (Util::array_last($generalRecord)[0] == $lastRecordID) {
$flushGeneralRecord = false;
array_pop($generalRecord);
} else if (count($generalRecord) > static::ROW_LIMIT) {
array_pop($generalRecord);
}
return [
'history' => $history,
'global' => $globalRecord,
'general' => $generalRecord,
'flushHistory' => $flushHistory ? 1 : 0,
'flushGlobal' => $flushGlobalRecord ? 1 : 0,
'flushGeneral' => $flushGeneralRecord ? 1 : 0,
];
}
private function generateGlobalInfo(MeekroDB $db): array
{
$gameStor = KVStorage::getStorage($db, 'game_env');
@@ -442,6 +541,8 @@ class GetFrontInfo extends \sammo\BaseAPI
$gameStor = KVStorage::getStorage($db, 'game_env');
$gameStor->cacheAll(true);
increaseRefresh("General/GetFrontInfo", 1);
$rawCity = $db->queryFirstRow('SELECT * FROM city WHERE city = %i', $cityID);
$general->setRawCity($rawCity);
@@ -463,6 +564,7 @@ class GetFrontInfo extends \sammo\BaseAPI
return [
'result' => true,
'recentRecord' => $this->generateRecentRecord($generalID),
'global' => $globalInfo,
'nation' => $nationInfo,
'general' => $generalInfo,