From 4870e2fa308731e2a1ce8ea22687347104e1527c Mon Sep 17 00:00:00 2001 From: Hide_D Date: Tue, 16 Aug 2022 22:47:20 +0900 Subject: [PATCH] =?UTF-8?q?feat:=20GetRecentRecord=20=EC=B6=94=EA=B0=80=20?= =?UTF-8?q?-=20=EB=A9=94=EC=9D=B8=ED=8E=98=EC=9D=B4=EC=A7=80=20=EC=B5=9C?= =?UTF-8?q?=EC=8B=A0=20=EB=A1=9C=EA=B7=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- hwe/sammo/API/Global/GetRecentRecord.php | 128 +++++++++++++++++++++++ hwe/ts/SammoAPI.ts | 5 + hwe/ts/defs/API/Global.ts | 10 ++ 3 files changed, 143 insertions(+) create mode 100644 hwe/sammo/API/Global/GetRecentRecord.php diff --git a/hwe/sammo/API/Global/GetRecentRecord.php b/hwe/sammo/API/Global/GetRecentRecord.php new file mode 100644 index 00000000..ecae8dd8 --- /dev/null +++ b/hwe/sammo/API/Global/GetRecentRecord.php @@ -0,0 +1,128 @@ +args); + $v->rule('int', 'lastGeneralRecordID') + ->rule('int', '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; + } + + public function getRequiredSessionMode(): int + { + return static::REQ_GAME_LOGIN | static::REQ_READ_ONLY; + } + + 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; + } + + public function launch(Session $session, ?DateTimeInterface $modifiedSince, ?string $reqEtag) + { + $db = DB::db(); + + $lastHistoryID = $this->args['lastWorldHistoryID']; + $lastRecordID = $this->args['lastGeneralRecordID']; + + $history = $this->getHistory($lastHistoryID); + $globalRecord = $this->getGlobalRecord($lastRecordID); + $generalRecord = $this->getGeneralRecord($session->generalID, $lastRecordID); + + $flushHistory = true; + $flushGlobalRecord = true; + $flushGeneralRecord = true; + + if($history){ + if(Util::array_last($history)[0] == $lastHistoryID){ + $flushHistory = false; + array_pop($history); + } + else if(count($history) > static::ROW_LIMIT){ + array_pop($history); + } + } + + if($globalRecord){ + if(Util::array_last($globalRecord)[0] == $lastRecordID){ + $flushGlobalRecord = false; + array_pop($globalRecord); + } + else if(count($globalRecord) > static::ROW_LIMIT){ + array_pop($globalRecord); + } + } + + if($generalRecord){ + if(Util::array_last($generalRecord)[0] == $lastRecordID){ + $flushGeneralRecord = false; + array_pop($generalRecord); + } + else if(count($generalRecord) > static::ROW_LIMIT){ + array_pop($generalRecord); + } + } + + return [ + 'result' => true, + 'history' => $history, + 'global' => $globalRecord, + 'general' => $generalRecord, + 'flushHistory' => $flushHistory?1:0, + 'flushGlobal' => $flushGlobalRecord?1:0, + 'flushGeneral' => $flushGeneralRecord?1:0, + ]; + } +} diff --git a/hwe/ts/SammoAPI.ts b/hwe/ts/SammoAPI.ts index b63d498b..2a0f2b41 100644 --- a/hwe/ts/SammoAPI.ts +++ b/hwe/ts/SammoAPI.ts @@ -26,6 +26,7 @@ import type { GetCurrentHistoryResponse, GetDiplomacyResponse, GetHistoryResponse, +GetRecentRecordResponse, } from "./defs/API/Global"; import type { CachedMapResult, GeneralListResponse, ItemTypeKey, MapResult } from "./defs"; import type { VoteDetailResult, VoteListResult } from "./defs/API/Vote"; @@ -144,6 +145,10 @@ const apiRealPath = { GetCachedMap: GET as APICallT, GetDiplomacy: GET as APICallT, ExecuteEngine: POST as APICallT, + GetRecentRecord: GET as APICallT<{ + lastGeneralRecordID: number; + lastWorldHistoryID: number; + } | undefined, GetRecentRecordResponse>, }, InheritAction: { BuyHiddenBuff: PUT as APICallT<{ diff --git a/hwe/ts/defs/API/Global.ts b/hwe/ts/defs/API/Global.ts index 681567b0..936e4ddf 100644 --- a/hwe/ts/defs/API/Global.ts +++ b/hwe/ts/defs/API/Global.ts @@ -76,4 +76,14 @@ export type GetDiplomacyResponse = { conflict: [number, Record][]; diplomacyList: Record>; myNationID: number; +} + +export type GetRecentRecordResponse = { + result: true; + history: [number, string][]; + global: [number, string][]; + general: [number, string][]; + flushHistory: 1 | 0; + flushGlobal: 1 | 0; + flushGeneral: 1 | 0; } \ No newline at end of file