From 058a6e63f221e3d9d48bd667452c07610494b0bf Mon Sep 17 00:00:00 2001 From: Hide_D Date: Thu, 21 Apr 2022 01:24:07 +0900 Subject: [PATCH] =?UTF-8?q?feat:=20Global/GetMap,=20Global/GetCachedMap=20?= =?UTF-8?q?=EC=B6=94=EA=B0=80=20-=20=20=EC=A7=80=EB=8F=84=EB=A5=BC=20?= =?UTF-8?q?=EA=B0=80=EC=A0=B8=EC=98=A4=EB=8A=94=20=EC=BD=94=EB=93=9C=20-?= =?UTF-8?q?=20GetCahcedMap=EC=97=90=EC=84=9C=EB=8A=94=20600=EC=B4=88=20?= =?UTF-8?q?=EB=8B=A8=EC=9C=84=20=EC=BA=90=EC=8B=B1=20=20=20-=20history=20?= =?UTF-8?q?=ED=95=84=EB=93=9C=EC=97=90=20=EC=B5=9C=EA=B7=BC=2010=EA=B1=B4?= =?UTF-8?q?=20=ED=91=9C=EC=8B=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- hwe/sammo/API/Global/GetCachedMap.php | 101 ++++++++++++++++++++++++++ hwe/sammo/API/Global/GetMap.php | 37 ++++++++++ hwe/ts/SammoAPI.ts | 6 +- hwe/ts/defs/index.ts | 3 +- 4 files changed, 144 insertions(+), 3 deletions(-) create mode 100644 hwe/sammo/API/Global/GetCachedMap.php create mode 100644 hwe/sammo/API/Global/GetMap.php diff --git a/hwe/sammo/API/Global/GetCachedMap.php b/hwe/sammo/API/Global/GetCachedMap.php new file mode 100644 index 00000000..a41aa452 --- /dev/null +++ b/hwe/sammo/API/Global/GetCachedMap.php @@ -0,0 +1,101 @@ +cachedTime === null) { + return null; + } + + $now = TimeUtil::nowDateTimeImmutable(); + $diff = TimeUtil::DateIntervalToSeconds($this->cachedTime->diff($now)); + $nextCacheTime = self::CACHE_SECONDS - $diff; + return new APICacheResult($this->cachedTime, null, Util::toInt($nextCacheTime), true); + } + + public function getRequiredSessionMode(): int + { + return static::NO_SESSION; + } + + public function launch(Session $session, ?DateTimeInterface $modifiedSince, ?string $reqEtag) + { + if (!class_exists('\\sammo\\UniqueConst')) { + return '서버 초기화되지 않음'; + } + + if (!prepareDir('data/file_cache')) { + return 'cache 불가'; + } + + $storage = new \Nette\Caching\Storages\FileStorage('data/file_cache'); + $cache = new Cache($storage); + + $now = TimeUtil::nowDateTimeImmutable(); + if ($modifiedSince) { + $diff = TimeUtil::DateIntervalToSeconds($modifiedSince->diff($now)); + if (0 <= $diff && $diff < self::CACHE_SECONDS) { + $this->cachedTime = $modifiedSince; + return null; + } + } + + $mapInfo = $cache->load("recent_map"); + if ($mapInfo) { + $cachedTime = TimeUtil::secondsToDateTime($mapInfo['timestamp'], true, true); + $diff = $cachedTime->diff($now); + if (0 <= $diff && $diff < self::CACHE_SECONDS) { + $this->cachedTime = $cachedTime; + return $mapInfo['data']; + } + } + + $history = getGlobalHistoryLogRecent(10); + $cachedMap = getWorldMap([ + 'year' => null, + 'month' => null, + 'aux' => null, + 'neutralView' => true, + 'showMe' => false, + ]); + + $cachedMap['history'] = $history; + $cachedMap['theme'] = GameConst::$mapName; + $timestamp = $now->getTimestamp(); + $this->cachedTime = $now; + + $map = [ + 'timestamp' => $timestamp, + 'data' => $cachedMap, + ]; + $cache->save("recent_map", $map); + + return $cachedMap; + } +} diff --git a/hwe/sammo/API/Global/GetMap.php b/hwe/sammo/API/Global/GetMap.php new file mode 100644 index 00000000..57fc3ae9 --- /dev/null +++ b/hwe/sammo/API/Global/GetMap.php @@ -0,0 +1,37 @@ +args); + $v->rule('boolean', 'neutralView') + ->rule('boolean', 'showMe'); + if (!$v->validate()) { + return $v->errorStr(); + } + return null; + } + + public function getRequiredSessionMode(): int + { + return static::REQ_LOGIN | static::REQ_READ_ONLY; + } + + public function launch(Session $session, ?DateTimeInterface $modifiedSince, ?string $reqEtag) + { + return getWorldMap(new MapRequest([ + 'neutralView' => !!($this->args['neutralView'] ?? false), + 'showMe' => !!($this->args['showMe'] ?? false), + ])); + } +} diff --git a/hwe/ts/SammoAPI.ts b/hwe/ts/SammoAPI.ts index ff7dfaea..44a99a1b 100644 --- a/hwe/ts/SammoAPI.ts +++ b/hwe/ts/SammoAPI.ts @@ -13,7 +13,7 @@ import type { SetBlockWarResponse, GeneralListResponse as NationGeneralListRespo import type { UploadImageResponse } from "./defs/API/Misc"; import type { JoinArgs } from "./defs/API/General"; import type { GetConstResponse, GetCurrentHistoryResponse, GetHistoryResponse } from "./defs/API/Global"; -import type { GeneralListResponse } from "./defs"; +import type { CachedMapResult, GeneralListResponse, MapResult } from "./defs"; const apiRealPath = { Betting: { @@ -57,7 +57,9 @@ const apiRealPath = { NumVar('year', NumVar('month', GET as APICallT ))), - GetCurrentHistory: GET as APICallT + GetCurrentHistory: GET as APICallT, + GetMap: GET as APICallT, + GetCachedMap: GET as APICallT, }, InheritAction: { BuyHiddenBuff: PUT as APICallT<{ diff --git a/hwe/ts/defs/index.ts b/hwe/ts/defs/index.ts index d1cdd8b8..f74a8ec9 100644 --- a/hwe/ts/defs/index.ts +++ b/hwe/ts/defs/index.ts @@ -244,12 +244,13 @@ export type MapResult = { shownByGeneralList: number[], myCity?: number, myNation?: number, +} +export type CachedMapResult = MapResult & { theme?: string, history?: string[], } - export type SimpleNationObj = { capital: number, cities: string[],