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[],