feat: 통합 세력 장수/암행부 페이지 (#213)

- ag-grid 기반
- 컬럼 사용자 정의 기능 제공
  - 컬럼 재정렬, 고정, 정렬 기능
  - 컬럼 상태 저장, 불러오기
  - 마지막 컬럼 재 사용
- 아이콘, 장수명 클릭 시 특수 기능 제공
  - 기본 세력 장수/암행부에서는 '감찰부'
- 추가 컬럼
  - 최근 전투
  - 전투 수
  - 승리 수
  - 살상률
- API/Nation/GeneralList 추가

Co-authored-by: hide_d <hided62@gmail.com>
Reviewed-on: https://storage.hided.net/gitea/devsam/core/pulls/213
This commit is contained in:
2022-04-08 02:28:58 +09:00
parent c96f6d9381
commit a95b26551c
32 changed files with 3137 additions and 381 deletions
+165
View File
@@ -0,0 +1,165 @@
<?php
namespace sammo\API\List\Nation;
use sammo\DB;
use sammo\General;
use sammo\Json;
use sammo\KVStorage;
use sammo\RootDB;
use sammo\Session;
use sammo\Util;
use sammo\Validator;
use function sammo\calcLeadershipBonus;
use function sammo\checkLimit;
use function sammo\checkSecretPermission;
use function sammo\getDed;
use function sammo\getDedLevelText;
use function sammo\getExpLevel;
use function sammo\getGenChar;
use function sammo\getGeneralSpecialDomesticName;
use function sammo\getGeneralSpecialWarName;
use function sammo\getHonor;
use function sammo\getNationStaticInfo;
use function sammo\getOfficerLevelText;
use function sammo\increaseRefresh;
class GeneralList extends \sammo\BaseAPI
{
public function validateArgs(): ?string
{
$v = new Validator($this->args);
$v
->rule('boolean', 'with_token');
if (!$v->validate()) {
return $v->errorStr();
}
return null;
}
public function getRequiredSessionMode(): int
{
return static::REQ_LOGIN;
}
public function launch(Session $session, ?\DateTimeInterface $modifiedSince, ?string $reqEtag)
{
$db = DB::db();
$withToken = $this->args['with_token']??false;
$gameStor = KVStorage::getStorage($db, 'game_env');
$userID = $session->userID;
if ($session->isGameLoggedIn()) {
increaseRefresh("장수일람", 2);
$me = $db->queryFirstRow('SELECT con, turntime FROM general WHERE owner=%i', $userID);
$con = checkLimit($me['con']);
if ($con >= 2) {
return '접속 제한중입니다. 1턴 이내에 너무 많은 갱신을 하셨습니다.';
}
} else {
$availableNextCall = $session->availableNextCallGetGeneralList ?? '2000-01-01 00:00:00';
$now = new \DateTimeImmutable();
if ($now <= new \DateTimeImmutable($availableNextCall) && $session->userGrade < 5) {
return "장수 리스트는 10초에 한번 갱신 가능합니다.\n다음 시간 : " . $availableNextCall;
}
$availableNextCall = $now->add(new \DateInterval('PT10S'))->format('Y-m-d H:i:s');
$session->availableNextCallGetGeneralList = $availableNextCall;
}
$session->setReadOnly();
$rawGeneralList = $db->queryAllLists('SELECT owner,no,picture,imgsvr,npc,age,nation,special,special2,personal,name,owner_name as ownerName,injury,leadership,strength,intel,experience,dedication,officer_level,killturn,connect from general');
$ownerNameList = [];
if ($gameStor->isunited) {
foreach (RootDB::db()->queryAllLists('SELECT no, name FROM member') as [$ownerID, $ownerName]) {
$ownerNameList[$ownerID] = $ownerName;
}
}
$generalList = [];
foreach ($rawGeneralList as $rawGeneral) {
[$owner, $no, $picture, $imgsvr, $npc, $age, $nation, $special, $special2, $personal, $name, $ownerName, $injury, $leadership, $strength, $intel, $experience, $dedication, $officerLevel, $killturn, $connectCnt] = $rawGeneral;
if (key_exists($owner, $ownerNameList)) {
$ownerName = $ownerNameList[$owner];
}
$nationArr = getNationStaticInfo($nation);
$lbonus = calcLeadershipBonus($officerLevel, $nationArr['level']);
$generalList[] = [
$no,
$picture,
$imgsvr,
$npc,
$age,
$nationArr['name'],
getGeneralSpecialDomesticName($special),
getGeneralSpecialWarName($special2),
getGenChar($personal),
$name,
$npc == 1 ? $ownerName : null,
$injury,
$leadership,
$lbonus,
$strength,
$intel,
getExpLevel($experience),
getHonor($experience),
getDed($dedication),
getOfficerLevelText($officerLevel, $nationArr['level']),
$killturn,
$connectCnt
];
}
$resultColumns = [
'no',
'picture',
'imgsvr',
'npc',
'age',
'nationName',
'special',
'special2',
'personal',
'name',
'ownerName',
'injury',
'leadership',
'lbonus',
'strength',
'intel',
'explevel',
'honorText',
'dedLevelText',
'officerLevelText',
];
$result = [
'result' => 'true',
'column' => $resultColumns,
'list' => $generalList,
];
if ($withToken) {
$now = (new \DateTimeImmutable())->format('Y-m-d H:i:s');
$tokens = [];
foreach ($db->query('SELECT * FROM select_npc_token WHERE `valid_until`>=%s', $now) as $token) {
$validUntil = $token['valid_until'];
foreach (Json::decode($token['pick_result']) as $pickResult) {
$tokens[$pickResult['no']] = $pickResult['keepCnt'];
}
}
$result['token'] = $tokens;
}
return $result;
}
}
+14 -2
View File
@@ -20,7 +20,7 @@ use function sammo\prepareDir;
class GetConst extends \sammo\BaseAPI
{
/** 반환하는 StaticValues 타입이 달라지면 +1 */
const CONST_API_VERSION = 1;
const CONST_API_VERSION = 2;
const CACHE_KEY = 'JSConst';
private ?string $cacheKey = null;
@@ -109,7 +109,7 @@ class GetConst extends \sammo\BaseAPI
public function tryCache(): ?APICacheResult
{
if (is_subclass_of('\\sammo\\VersionGit', '\\sammo\VersionGitDynamic')) {
return new APICacheResult(TimeUtil::secondsToDateTime($this->findLastModified(), true, true));
return new APICacheResult(TimeUtil::secondsToDateTime($this->findLastModified()??\time(), true, true));
}
return new APICacheResult(null, $this->getCacheKey());
@@ -258,6 +258,18 @@ class GetConst extends \sammo\BaseAPI
$iActionInfo[$mappedKey] = $actionInfo;
}
$crewtypeMap = [];
foreach(GameUnitConst::all() as $crewtypeObj){
$crewtypeMap[$crewtypeObj->id] = [
'value'=>(string)$crewtypeObj->id,
'name'=>$crewtypeObj->name,
'info'=>$crewtypeObj->getInfo(),
];
}
$iActionInfo['crewtype'] = $crewtypeMap;
return [
'gameConst' => get_class_vars('\sammo\GameConst'),
'gameUnitConst' => GameUnitConst::all(),
+285
View File
@@ -0,0 +1,285 @@
<?php
namespace sammo\API\Nation;
use sammo\DB;
use sammo\General;
use sammo\Session;
use sammo\Util;
use sammo\Validator;
use function sammo\calcLeadershipBonus;
use function sammo\checkLimit;
use function sammo\checkSecretPermission;
use function sammo\getBillByLevel;
use function sammo\getDedLevelText;
use function sammo\getGenChar;
use function sammo\getGeneralSpecialDomesticName;
use function sammo\getGeneralSpecialWarName;
use function sammo\getHonor;
use function sammo\getNationStaticInfo;
use function sammo\getOfficerLevelText;
use function sammo\increaseRefresh;
class GeneralList extends \sammo\BaseAPI
{
private int $permission;
static $viewColumns = [
'no' => 0,
'name' => 0,
'nation' => 0,
'npc' => 0,
'injury' => 0,
'leadership' => 0,
'strength' => 0,
'intel' => 0,
'explevel' => 0,
'dedlevel' => 0,
'gold' => 0,
'rice' => 0,
'killturn' => 0,
'picture' => 0,
'imgsvr' => 0,
'age' => 0,
'special' => 0,
'special2' => 0,
'personal' => 0,
'belong' => 0,
'connect' => 0,
'city' => 1,
'experience' => 1,
'dedication' => 1,
'officer_level' => 2,
'officer_city' => 2,
'defence_train' => 2,
'troop' => 2,
'crewtype' => 2,
'crew' => 2,
'train' => 2,
'atmos' => 2,
'turntime' => 2,
'horse' => 2,
'weapon' => 2,
'book' => 2,
'item' => 2,
'recent_war' => 2,
'aux' => 2,
'owner_name' => 9,//안씀.
//RANK
'warnum' => 2,
'killnum' => 2,
'deathnum' => 2,
'killcrew' => 2,
'deathcrew' => 2,
'firenum' => 2,
];
static $columnRemap = [
'special' => 'specialDomestic',
'special2' => 'specialWar',
'aux' => null,
];
static $customViewColumns = [
'officerLevel' => 0,
'officerLevelText' => 0,
'lbonus' => 0,
'ownerName' => 0,
'honorText' => 0,
'dedLevelText' => 0,
'bill' => 0,
'reservedCommand' => 2,
'autorun_limit' => 2,
];
public function validateArgs(): ?string
{
//TODO: 장기적으로는 요청에 따라 반환해야...
return null;
}
public function getRequiredSessionMode(): int
{
return static::REQ_GAME_LOGIN | static::REQ_READ_ONLY;
}
private function getOfficerLevel($rawGeneral)
{
$level = $rawGeneral['officer_level'];
if ($level >= 5) {
return $level;
}
if ($this->permission > 1) {
return $level;
}
return 1;
}
public function launch(Session $session, ?\DateTimeInterface $modifiedSince, ?string $reqEtag)
{
increaseRefresh("세력장수", 1);
$db = DB::db();
$gameStor = \sammo\KVStorage::getStorage($db, 'game_env');
$env = $gameStor->getValues(['year', 'month', 'turntime', 'turnterm', 'autorun_user', 'killturn']);
$me = $db->queryFirstRow('SELECT con, turntime, belong, nation, officer_level, permission, penalty FROM general WHERE owner=%i', $session->getUserID());
$con = checkLimit($me['con']);
if ($con >= 2) {
return '접속 제한중입니다. 1턴 이내에 너무 많은 갱신을 하셨습니다.';
}
$nationID = $me['nation'];
$this->permission = checkSecretPermission($me, true);
$nationArr = getNationStaticInfo($nationID);
[$queryColumns, $rankColumns] = General::mergeQueryColumn(array_keys(static::$viewColumns), 1);
$rawGeneralList = Util::convertArrayToDict($db->query('SELECT %l from general WHERE nation = %i ORDER BY turntime ASC', Util::formatListOfBackticks($queryColumns), $nationID), 'no');
$reservedCommand = [];
if ($this->permission >= 2) {
$nonNPCGeneralIDList = [];
foreach ($rawGeneralList as $rawGeneral) {
if ($rawGeneral['npc'] < 2) {
$nonNPCGeneralIDList[] = $rawGeneral['no'];
}
}
$rawTurnList = $db->query(
'SELECT general_id, turn_idx, action, arg, brief FROM general_turn WHERE general_id IN %li AND turn_idx < 5 ORDER BY general_id asc, turn_idx asc',
$nonNPCGeneralIDList
);
foreach ($rawTurnList as $rawTurn) {
[
'general_id' => $generalID,
'action' => $action,
'arg' => $arg,
'brief' => $brief,
] = $rawTurn;
if (!key_exists($generalID, $reservedCommand)) {
$reservedCommand[$generalID] = [];
}
$reservedCommand[$generalID][] = [
'action' => $action,
'arg' => $arg,
'brief' => $brief
];
}
}
$rankList = [];
if ($rankColumns) {
$rawRankList = $db->query('SELECT general_id, `type`, `value` FROM rank_data WHERE nation_id = %i AND `type` IN %ls', $nationID, $rankColumns);
foreach ($rawRankList as $rawRank) {
[
'general_id' => $generalID,
'type' => $type,
'value' => $value
] = $rawRank;
if (!key_exists($generalID, $rankList)) {
$rankList[$generalID] = [];
}
$rankList[$generalID][$type] = $value;
}
}
$getRankVar = fn ($key) => (fn ($rawGeneral) => (($rankList[$rawGeneral['no']] ?? [])[$key] ?? 0));
$specialViewFilter = [
'officerLevel' => fn ($rawGeneral) => $this->getOfficerLevel($rawGeneral),
'officerLevelText' => fn ($rawGeneral) => getOfficerLevelText($this->getOfficerLevel($rawGeneral), $nationArr['level']),
'lbonus' => fn ($rawGeneral) => calcLeadershipBonus($rawGeneral['officer_level'], $nationArr['level']),
'ownerName' => fn ($rawGeneral) => ($rawGeneral['npc'] != 1) ? null : $rawGeneral['owner_name'],
'honorText' => fn ($rawGeneral) => getHonor($rawGeneral['experience']),
'dedLevelText' => fn ($rawGeneral) => getDedLevelText($rawGeneral['dedlevel']),
//'0000-00-00 11:23';
'turntime' => fn ($rawGeneral) => substr($rawGeneral['turntime'], 0, 19),
'recent_war' => fn ($rawGeneral) => substr($rawGeneral['recent_war'], 0, 19),
'bill' => fn ($rawGeneral) => getBillByLevel($rawGeneral['dedlevel']),
'reservedCommand' => fn ($rawGeneral) => $reservedCommand[$rawGeneral['no']] ?? null,
'autorun_limit' => fn ($rawGeneral) => ($rawGeneral['aux'] ?? [])['autorun_limit'] ?? 0,
];
foreach ($rankColumns as $rankKey) {
$specialViewFilter[$rankKey] = $getRankVar($rankKey);
}
$resultColumns = [];
foreach (static::$viewColumns as $column => $reqPermission) {
if ($reqPermission > $this->permission) {
continue;
}
if (key_exists($column, static::$columnRemap)) {
$newColumn = static::$columnRemap[$column];
if($newColumn !== null){
$resultColumns[$newColumn] = $column;
}
} else {
$resultColumns[$column] = $column;
}
}
foreach (static::$customViewColumns as $column => $reqPermission) {
if ($reqPermission > $this->permission) {
continue;
}
$resultColumns[$column] = $column;
}
$generalList = [];
foreach ($rawGeneralList as $rawGeneral) {
//General 생성?
if (key_exists('aux', $rawGeneral)) {
$rawGeneral['aux'] = \sammo\JSON::decode($rawGeneral['aux']);
}
$item = [];
foreach ($resultColumns as $column) {
if (key_exists($column, $specialViewFilter)) {
$value = $specialViewFilter[$column]($rawGeneral);
} else {
$value = $rawGeneral[$column];
}
$item[] = $value;
}
$generalList[] = $item;
}
if($this->permission >= 2){
$troops = $db->queryAllLists('SELECT troop_leader,name FROM troop WHERE nation = %i', $nationID);
}
else{
$troops = null;
}
$result = [
'result' => true,
'permission' => $this->permission,
'column' => array_keys($resultColumns),
'list' => $generalList,
'troops' => $troops,
'env' => $env,
];
return $result;
}
}