내부 코드 사용례에서 General 테이블의 column을 일부만 가져오는 경우가 다수 있습니다. 다만 General 클래스에서 각종 iAction에 해당하는 트리거 함수 호출을 위해서는 모든 column을 다 가져와야 정상 동작이 가능한 만큼, iAction interface를 구현한 General 클래스와, 구현하지 않은 GeneralLite 클래스 둘로 나눕니다. 또한 General 클래스에서는 필요한 column을 다 가져오도록 변경합니다. Reviewed-on: https://storage.hided.net/gitea/devsam/core/pulls/236
101 lines
3.7 KiB
PHP
101 lines
3.7 KiB
PHP
<?php
|
|
|
|
namespace sammo\API\InheritAction;
|
|
|
|
use DateTimeImmutable;
|
|
use sammo\Session;
|
|
use DateTimeInterface;
|
|
use sammo\DB;
|
|
use sammo\Enums\APIRecoveryType;
|
|
use sammo\Enums\RankColumn;
|
|
use sammo\GameConst;
|
|
use sammo\General;
|
|
use sammo\KVStorage;
|
|
use sammo\LiteHashDRBG;
|
|
use sammo\RandUtil;
|
|
use sammo\TimeUtil;
|
|
use sammo\UniqueConst;
|
|
use sammo\UserLogger;
|
|
use sammo\Util;
|
|
|
|
class ResetTurnTime extends \sammo\BaseAPI
|
|
{
|
|
public function validateArgs(): ?string
|
|
{
|
|
return null;
|
|
}
|
|
|
|
public function getRequiredSessionMode(): int
|
|
{
|
|
//KVStrorage, General.aux 모두 쓰므로 lock;
|
|
return static::REQ_GAME_LOGIN;
|
|
}
|
|
|
|
public function launch(Session $session, ?DateTimeInterface $modifiedSince, ?string $reqEtag): null | string | array | APIRecoveryType
|
|
{
|
|
$userID = $session->userID;
|
|
$generalID = $session->generalID;
|
|
|
|
$general = General::createObjFromDB($generalID);
|
|
if ($userID != $general->getVar('owner')) {
|
|
return '로그인 상태가 이상합니다. 다시 로그인해 주세요.';
|
|
}
|
|
|
|
$currentLevel = $general->getAuxVar('inheritResetTurnTime') ?? -1;
|
|
$nextLevel = $currentLevel + 1;
|
|
while (count(GameConst::$inheritResetAttrPointBase) <= $nextLevel) {
|
|
$baseLen = count(GameConst::$inheritResetAttrPointBase);
|
|
GameConst::$inheritResetAttrPointBase[] = GameConst::$inheritResetAttrPointBase[$baseLen - 1] + GameConst::$inheritResetAttrPointBase[$baseLen - 2];
|
|
}
|
|
|
|
$reqPoint = GameConst::$inheritResetAttrPointBase[$nextLevel];
|
|
|
|
$db = DB::db();
|
|
$gameStor = KVStorage::getStorage($db, 'game_env');
|
|
if($gameStor->isunited){
|
|
return '이미 천하가 통일되었습니다.';
|
|
}
|
|
$inheritStor = KVStorage::getStorage($db, "inheritance_{$userID}");
|
|
$previousPoint = ($inheritStor->getValue('previous') ?? [0, 0])[0];
|
|
if ($previousPoint < $reqPoint) {
|
|
return '충분한 유산 포인트를 가지고 있지 않습니다.';
|
|
}
|
|
|
|
$gameStor = KVStorage::getStorage($db, 'game_env');
|
|
[$turnTerm, $serverTurnTime] = $gameStor->getValuesAsArray(['turnterm', 'turntime']);
|
|
|
|
$currTurnTime = new DateTimeImmutable($general->getTurnTime());
|
|
$serverTurnTimeObj = new DateTimeImmutable($serverTurnTime);
|
|
|
|
$rng = new RandUtil(new LiteHashDRBG(Util::simpleSerialize(
|
|
UniqueConst::$hiddenSeed,
|
|
'ResetTurnTime',
|
|
$userID,
|
|
$general->getTurnTime()
|
|
)));
|
|
|
|
$afterTurn = $rng->nextRange($turnTerm * -60 / 2, $turnTerm * 60 / 2);
|
|
|
|
$userLogger = new UserLogger($userID);
|
|
if($afterTurn >= 0){
|
|
$userLogger->push(sprintf("{$reqPoint} 포인트로 턴 시간을 바꾼 결과 %02d:%02d 뒤로 밀림", intdiv(Util::toInt($afterTurn), 60), $afterTurn%60), "inheritPoint");
|
|
}
|
|
else{
|
|
$userLogger->push(sprintf("{$reqPoint} 포인트로 턴 시간을 바꾼 결과 %02d:%02d 앞으로 당김", intdiv(Util::toInt(-$afterTurn), 60), (-$afterTurn)%60), "inheritPoint");
|
|
}
|
|
$userLogger->flush();
|
|
|
|
$turnTime = $currTurnTime->add(TimeUtil::secondsToDateInterval($afterTurn));
|
|
if ($turnTime <= $serverTurnTimeObj && $serverTurnTimeObj <= $currTurnTime) {
|
|
$turnTime = $turnTime->add(TimeUtil::secondsToDateInterval($turnTerm * 60));
|
|
}
|
|
|
|
$general->setVar('turntime', TimeUtil::format($turnTime, true));
|
|
$general->setAuxVar('inheritResetTurnTime', $nextLevel);
|
|
$inheritStor->setValue('previous', [$previousPoint - $reqPoint, null]);
|
|
$general->increaseRankVar(RankColumn::inherit_point_spent_dynamic, $reqPoint);
|
|
$general->applyDB($db);
|
|
return null;
|
|
}
|
|
}
|