feat: Global/GetDiplomacy 중원 정보 API

This commit is contained in:
2022-04-21 02:28:17 +09:00
parent 058a6e63f2
commit 6938c74014
3 changed files with 119 additions and 2 deletions
+107
View File
@@ -0,0 +1,107 @@
<?php
namespace sammo\API\Global;
use sammo\DB;
use sammo\Json;
use sammo\KVStorage;
use sammo\Session;
use function sammo\getAllNationStaticInfo;
use function sammo\increaseRefresh;
class GetDiplomacy extends \sammo\BaseAPI
{
public function validateArgs(): ?string
{
return null;
}
public function getRequiredSessionMode(): int
{
return static::REQ_GAME_LOGIN | static::REQ_READ_ONLY;
}
public function launch(Session $session, ?\DateTimeInterface $modifiedSince, ?string $reqEtag)
{
$userID = $session->userID;
increaseRefresh("중원정보", 1);
$db = DB::db();
$gameStor = KVStorage::getStorage($db, 'game_env');
$myNationID = $db->queryFirstField('SELECT nation FROM general WHERE owner=%i', $userID);
$nations = array_filter(getAllNationStaticInfo(), function (array $nation) {
return $nation['level'];
});
uasort($nations, function (array $lhs, array $rhs) {
return - ($lhs['power'] <=> $rhs['power']);
});
foreach(array_keys($nations) as $nationID){
$nations[$nationID]['cities'] = [];
}
$realConflict = [];
foreach ($db->queryAllLists('SELECT nation, city, conflict FROM city') as [
$nationID,
$cityID,
$rawConflict
]) {
if($nationID != 0){
$nations[$nationID]['cities'][] = $cityID;
}
if($rawConflict == '{}'){
continue;
}
$rawConflict = Json::decode($rawConflict);
if (count($rawConflict) < 2) {
continue;
}
$sum = array_sum($rawConflict);
$conflict = [];
foreach ($rawConflict as $nationID => $killnum) {
$conflict[$nationID] = [
'killnum' => $killnum,
'percent' => round(100 * $killnum / $sum, 1),
];
}
$realConflict[] = [$cityID, $conflict];
};
$neutralDiplomacyMap = [
3 => 2,
4 => 2,
5 => 2,
6 => 2,
7 => 2,
];
$diplomacyList = [];
foreach ($db->queryAllLists('SELECT me, you, state FROM diplomacy') as [$me, $you, $state]) {
if (!key_exists($me, $diplomacyList)) {
$diplomacyList[$me] = [];
}
if ($me != $myNationID && $you != $myNationID) {
$diplomacyList[$me][$you] = $neutralDiplomacyMap[$state] ?? $state;
} else {
$diplomacyList[$me][$you] = $state;
}
}
return [
'result' => true,
'nations' => $nations,
'conflict' => $realConflict,
'diplomacyList' => $diplomacyList,
'myNationID' => $myNationID,
];
}
}
+2 -1
View File
@@ -12,7 +12,7 @@ import type { inheritBuffType } from "./defs/API/InheritAction";
import type { SetBlockWarResponse, GeneralListResponse as NationGeneralListResponse } from "./defs/API/Nation";
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 { GetConstResponse, GetCurrentHistoryResponse, GetDiplomacyResponse, GetHistoryResponse } from "./defs/API/Global";
import type { CachedMapResult, GeneralListResponse, MapResult } from "./defs";
const apiRealPath = {
@@ -60,6 +60,7 @@ const apiRealPath = {
GetCurrentHistory: GET as APICallT<undefined, GetCurrentHistoryResponse>,
GetMap: GET as APICallT<undefined, MapResult>,
GetCachedMap: GET as APICallT<undefined, CachedMapResult>,
GetDiplomacy: GET as APICallT<undefined, GetDiplomacyResponse>,
},
InheritAction: {
BuyHiddenBuff: PUT as APICallT<{
+10 -1
View File
@@ -1,5 +1,5 @@
import type { CityID, CrewTypeID, GameCityDefault, GameConstType, GameUnitType, GameIActionKey, GameIActionCategory, GameIActionInfo } from "@/defs/GameObj";
import type { MapResult } from "@/defs";
import type { diplomacyState, MapResult, SimpleNationObj } from "@/defs";
export interface GetConstResponse {
result: true;
@@ -67,4 +67,13 @@ export type GetHistoryResponse = {
export type GetCurrentHistoryResponse = {
result: true;
data: HistoryObj;
}
export type GetDiplomacyResponse = {
result: true;
nations: SimpleNationObj[];
conflict: [number, Record<number, number>][];
diplomacyList: Record<number, Record<number, diplomacyState>>;
myNationID: number;
}