From 6395f73f6a211553c30776d02144b7892bcb4e61 Mon Sep 17 00:00:00 2001 From: Hide_D Date: Tue, 7 Mar 2023 02:40:51 +0900 Subject: [PATCH] =?UTF-8?q?refac:=20General/GetFrontInfo=20=EC=A0=95?= =?UTF-8?q?=EB=B3=B4=20=EC=B6=94=EA=B0=80=20-=20Global/GetRecentRecord=20?= =?UTF-8?q?=ED=95=AD=EB=AA=A9=20=ED=8F=AC=ED=95=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- hwe/sammo/API/General/GetFrontInfo.php | 104 ++++++++++++++++++++++++- hwe/ts/PageFront.vue | 99 ++++++++++------------- hwe/ts/SammoAPI.ts | 6 +- hwe/ts/defs/API/Global.ts | 1 + 4 files changed, 148 insertions(+), 62 deletions(-) diff --git a/hwe/sammo/API/General/GetFrontInfo.php b/hwe/sammo/API/General/GetFrontInfo.php index b0cba471..96877b0b 100644 --- a/hwe/sammo/API/General/GetFrontInfo.php +++ b/hwe/sammo/API/General/GetFrontInfo.php @@ -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, diff --git a/hwe/ts/PageFront.vue b/hwe/ts/PageFront.vue index 1c4a1ba3..e30c644c 100644 --- a/hwe/ts/PageFront.vue +++ b/hwe/ts/PageFront.vue @@ -306,65 +306,6 @@ void Promise.all([storeP, tryRefresh()]).then(() => { asyncReady.value = true; }); -const lastGeneralRecordID = ref(0); -const lastWorldHistoryID = ref(0); - -const generalRecords = ref(new Denque<[number, string]>()); -const globalRecords = ref(new Denque<[number, string]>()); -const worldHistory = ref(new Denque<[number, string]>()); - -let recordLock = false; - -watch(refreshCounter, async () => { - if (recordLock) { - return; - } - try { - recordLock = true; - - - const response = await SammoAPI.Global.GetRecentRecord({ - lastGeneralRecordID: lastGeneralRecordID.value, - lastWorldHistoryID: lastWorldHistoryID.value, - }); - recordLock = false; - - - const recordIter = [ - ['flushGeneral', generalRecords, 'general'], - ['flushGlobal', globalRecords, 'global'], - ['flushHistory', worldHistory, 'history'], - ] as const; - - for(const [flushKey, recordRef, recordKey] of recordIter) { - if (response[flushKey]) { - recordRef.value = new Denque<[number, string]>(); - } - if (response[recordKey].length) { - lastGeneralRecordID.value = Math.max(lastGeneralRecordID.value, response[recordKey][0][0]); - } - const subRecord = response[recordKey]; - while(subRecord.length){ - const [id, record] = unwrap(subRecord.pop()); - if(!recordRef.value.isEmpty() && id <= unwrap(recordRef.value.get(0))[0]){ - continue; - } - if(recordRef.value.length >= 15){ - recordRef.value.pop(); - } - recordRef.value.unshift([id, formatLog(record)]); - } - } - } catch (e) { - recordLock = false; - console.error(e); - toasts.danger({ - title: "최근 기록 갱신 실패", - body: `${e}`, - }); - } -}); - const globalMenu = ref(); onMounted(async () => { try { @@ -387,6 +328,14 @@ const generalInfo = ref(); const nationStaticInfo = ref(); const nationInfo = ref(); +const lastGeneralRecordID = ref(0); +const lastWorldHistoryID = ref(0); + +const generalRecords = ref(new Denque<[number, string]>()); +const globalRecords = ref(new Denque<[number, string]>()); +const worldHistory = ref(new Denque<[number, string]>()); + + let generalInfoLock = false; watch(refreshCounter, async () => { @@ -395,7 +344,10 @@ watch(refreshCounter, async () => { } try { generalInfoLock = true; - const response = await SammoAPI.General.GetFrontInfo(); + const response = await SammoAPI.General.GetFrontInfo({ + lastGeneralRecordID: lastGeneralRecordID.value, + lastWorldHistoryID: lastWorldHistoryID.value, + }); generalInfoLock = false; frontInfo.value = response; @@ -419,6 +371,33 @@ watch(refreshCounter, async () => { gennum: rawNation.gennum, power: rawNation.power, }; + + const recentRecord = response.recentRecord; + const recordIter = [ + ['flushGeneral', generalRecords, 'general'], + ['flushGlobal', globalRecords, 'global'], + ['flushHistory', worldHistory, 'history'], + ] as const; + + for(const [flushKey, recordRef, recordKey] of recordIter) { + if (recentRecord[flushKey]) { + recordRef.value = new Denque<[number, string]>(); + } + if (recentRecord[recordKey].length) { + lastGeneralRecordID.value = Math.max(lastGeneralRecordID.value, recentRecord[recordKey][0][0]); + } + const subRecord = recentRecord[recordKey]; + while(subRecord.length){ + const [id, record] = unwrap(subRecord.pop()); + if(!recordRef.value.isEmpty() && id <= unwrap(recordRef.value.get(0))[0]){ + continue; + } + if(recordRef.value.length >= 15){ + recordRef.value.pop(); + } + recordRef.value.unshift([id, formatLog(record)]); + } + } } catch (e) { generalInfoLock = false; console.error(e); diff --git a/hwe/ts/SammoAPI.ts b/hwe/ts/SammoAPI.ts index fa46b9fb..437a71f4 100644 --- a/hwe/ts/SammoAPI.ts +++ b/hwe/ts/SammoAPI.ts @@ -133,7 +133,11 @@ const apiRealPath = { DieOnPrestart: POST as APICallT, BuildNationCandidate: POST as APICallT, GetCommandTable: GET as APICallT, - GetFrontInfo: GET as APICallT, + GetFrontInfo: GET as APICallT<{ + lastNationNoticeDate?: string, + lastGeneralRecordID?: number, + lastWorldHistoryID?: number, + }, GetFrontInfoResponse>, }, Global: { GeneralList: GET as APICallT, diff --git a/hwe/ts/defs/API/Global.ts b/hwe/ts/defs/API/Global.ts index 458a5402..4353b11d 100644 --- a/hwe/ts/defs/API/Global.ts +++ b/hwe/ts/defs/API/Global.ts @@ -103,6 +103,7 @@ export type AutorunUserMode = "develop" | "warp" | "recruit" | "recruit_high" | export type GetFrontInfoResponse = { result: true; + recentRecord: Omit; global: { scenarioText: string; extendedGeneral: 1 | 0;