feat: GetRecentRecord 추가
- 메인페이지 최신 로그
This commit is contained in:
@@ -0,0 +1,128 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace sammo\API\Global;
|
||||||
|
|
||||||
|
use sammo\Session;
|
||||||
|
use DateTimeInterface;
|
||||||
|
use sammo\DB;
|
||||||
|
use sammo\Util;
|
||||||
|
use sammo\Validator;
|
||||||
|
|
||||||
|
class GetRecentRecord extends \sammo\BaseAPI
|
||||||
|
{
|
||||||
|
|
||||||
|
const ROW_LIMIT = 15;
|
||||||
|
|
||||||
|
public function validateArgs(): ?string
|
||||||
|
{
|
||||||
|
$v = new Validator($this->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,
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -26,6 +26,7 @@ import type {
|
|||||||
GetCurrentHistoryResponse,
|
GetCurrentHistoryResponse,
|
||||||
GetDiplomacyResponse,
|
GetDiplomacyResponse,
|
||||||
GetHistoryResponse,
|
GetHistoryResponse,
|
||||||
|
GetRecentRecordResponse,
|
||||||
} from "./defs/API/Global";
|
} from "./defs/API/Global";
|
||||||
import type { CachedMapResult, GeneralListResponse, ItemTypeKey, MapResult } from "./defs";
|
import type { CachedMapResult, GeneralListResponse, ItemTypeKey, MapResult } from "./defs";
|
||||||
import type { VoteDetailResult, VoteListResult } from "./defs/API/Vote";
|
import type { VoteDetailResult, VoteListResult } from "./defs/API/Vote";
|
||||||
@@ -144,6 +145,10 @@ const apiRealPath = {
|
|||||||
GetCachedMap: GET as APICallT<undefined, CachedMapResult>,
|
GetCachedMap: GET as APICallT<undefined, CachedMapResult>,
|
||||||
GetDiplomacy: GET as APICallT<undefined, GetDiplomacyResponse>,
|
GetDiplomacy: GET as APICallT<undefined, GetDiplomacyResponse>,
|
||||||
ExecuteEngine: POST as APICallT<undefined, ValidResponse & { updated: boolean }>,
|
ExecuteEngine: POST as APICallT<undefined, ValidResponse & { updated: boolean }>,
|
||||||
|
GetRecentRecord: GET as APICallT<{
|
||||||
|
lastGeneralRecordID: number;
|
||||||
|
lastWorldHistoryID: number;
|
||||||
|
} | undefined, GetRecentRecordResponse>,
|
||||||
},
|
},
|
||||||
InheritAction: {
|
InheritAction: {
|
||||||
BuyHiddenBuff: PUT as APICallT<{
|
BuyHiddenBuff: PUT as APICallT<{
|
||||||
|
|||||||
@@ -76,4 +76,14 @@ export type GetDiplomacyResponse = {
|
|||||||
conflict: [number, Record<number, number>][];
|
conflict: [number, Record<number, number>][];
|
||||||
diplomacyList: Record<number, Record<number, diplomacyState>>;
|
diplomacyList: Record<number, Record<number, diplomacyState>>;
|
||||||
myNationID: number;
|
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;
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user