feat: Global/GetMap, Global/GetCachedMap 추가

-  지도를 가져오는 코드
- GetCahcedMap에서는 600초 단위 캐싱
  - history 필드에 최근 10건 표시
This commit is contained in:
2022-04-21 01:24:07 +09:00
parent 1be6b7a509
commit 058a6e63f2
4 changed files with 144 additions and 3 deletions
+101
View File
@@ -0,0 +1,101 @@
<?php
namespace sammo\API\Global;
use sammo\Session;
use DateTimeInterface;
use Nette\Caching\Cache;
use sammo\APICacheResult;
use sammo\GameConst;
use sammo\MapRequest;
use sammo\TimeUtil;
use sammo\UniqueConst;
use sammo\Util;
use sammo\Validator;
use function sammo\getGlobalHistoryLogRecent;
use function sammo\getWorldMap;
use function sammo\prepareDir;
class GetCachedMap extends \sammo\BaseAPI
{
const CACHE_SECONDS = 600;
private ?\DateTimeInterface $cachedTime = null;
public function validateArgs(): ?string
{
return null;
}
public function tryCache(): ?APICacheResult
{
if ($this->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;
}
}
+37
View File
@@ -0,0 +1,37 @@
<?php
namespace sammo\API\Global;
use sammo\Session;
use DateTimeInterface;
use sammo\MapRequest;
use sammo\Validator;
use function sammo\getWorldMap;
class GetMap extends \sammo\BaseAPI
{
public function validateArgs(): ?string
{
$v = new Validator($this->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),
]));
}
}
+4 -2
View File
@@ -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<undefined, GetHistoryResponse>
))),
GetCurrentHistory: GET as APICallT<undefined, GetCurrentHistoryResponse>
GetCurrentHistory: GET as APICallT<undefined, GetCurrentHistoryResponse>,
GetMap: GET as APICallT<undefined, MapResult>,
GetCachedMap: GET as APICallT<undefined, CachedMapResult>,
},
InheritAction: {
BuyHiddenBuff: PUT as APICallT<{
+2 -1
View File
@@ -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[],