Files
core/hwe/sammo/GeneralPool/RandomNameGeneral.php
T
Hide_D c5ceff6fb5 feat,refac: 게임 로직 내 '랜덤'을 RandUtil을 활용하여 재설정
- 기존의 랜덤 코드는 deprecated 처리
- 서버 시작 시 랜덤하게 정해진 hiddenSeed를 활용
- 랜덤 생성 단위
  - 월 실행
  - 사령턴 실행 결과
  - 커맨드 실행 결과
  - 작위 보상
  - 부대장 생성
  - 유니크 획득 시도
  - 설문 조사
  - 장수 생성
  - NPC국 생성, NPC 생성, NPC의 토너먼트 베팅
  - 전투
  - 도시 점령
  - 자율 행동 선택
  - 랜덤 턴 변경
- 거래장, 토너먼트 등 구 랜덤 코드를 이용하는 잔여 코드 있음
2022-05-17 03:46:59 +09:00

102 lines
3.0 KiB
PHP

<?php
namespace sammo\GeneralPool;
use MeekroDB;
use sammo\AbsGeneralPool;
use sammo\GameConst;
use sammo\RandUtil;
use sammo\Util;
class RandomNameGeneral extends AbsGeneralPool{
public static function getPoolName():string{return "임의 장수명";}
public function occupyGeneralName(): bool
{
$generalID = $this->getGeneralBuilder()->getGeneralID();
if($generalID === null){
throw new \RuntimeException('build되지 않음');
}
$db= $this->db;
$db->update('select_pool', [
'general_id'=>$generalID,
'owner'=>null,
'reserved_until'=>null,
], 'unique_name = %s', $this->uniqueName);
return $db->affectedRows()!=0;
}
static protected function pickGeneral1FromPool(\MeekroDB $db, RandUtil $rng, int $owner, ?string $prefix=null):self{
$loopCnt = 0;
while(true){
$firstname = $rng->choice(GameConst::$randGenFirstName);
$middlename = $rng->choice(GameConst::$randGenMiddleName);
$lastname = $rng->choice(GameConst::$randGenLastName);
$generalName = "{$firstname}{$middlename}{$lastname}";
if($prefix){
$generalName = $prefix.$generalName;
}
$dupCnt = static::checkDuplicatedCnt($db, $generalName);
if($dupCnt == 0){
break;
}
if($loopCnt >= 99 || $dupCnt < 2){
$generalName .= $dupCnt+1;
break;
}
$loopCnt += 1;
}
$uniqueName = $generalName;
return new static($db, $rng, [
'uniqueName'=>$uniqueName,
'generalName'=>$generalName,
'imgsvr'=>0,
'picture'=>null
], '9999-12-31 12:00:00');
}
static public function pickGeneralFromPool(MeekroDB $db, RandUtil $rng, int $owner, int $pickCnt, ?string $prefix = null): array
{
/** @var RandomNameGeneral[] */
$result = [];
$dbInsert = [];
$oNow = new \DateTimeImmutable();
for($i=0;$i<$pickCnt;$i++){
$result[] = static::pickGeneral1FromPool($db, $rng, $owner, $prefix);
}
if($owner){
$now = $oNow->format('Y-m-d H:i:s');
$db->delete('select_pool', [
'reserved_until'=>null,
'owner'=>null,
],'(reserved_until < %s OR reserved_until IS NULL) AND general_id IS null', $now);
$validUntil = $oNow->add(new \DateInterval(sprintf('PT%dS', 30)));
foreach($result as $pickedGeneral){
$dbInsert[] = [
'owner'=>$owner,
'uniqueName'=>$pickedGeneral->getUniqueName(),
'info'=>$pickedGeneral->getInfo(),
'reserved_until'=>$validUntil->format(('Y-m-d H:i:s'))
];
}
$db->insert('select_pool', $dbInsert);
}
return $result;
}
public static function initPool(\MeekroDB $db){
//do Nothing
}
}