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

72 lines
2.4 KiB
PHP

<?php
namespace sammo;
use sammo\AbsGeneralPool;
use sammo\GameConst;
use sammo\Json;
use sammo\Util;
abstract class AbsFromUserPool extends AbsGeneralPool{
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 AND owner IS NOT NULL', $this->uniqueName);
return $db->affectedRows()!=0;
}
static public function pickGeneralFromPool(\MeekroDB $db, RandUtil $rng, int $owner, int $pickCnt, ?string $prefix=null):array{
$oNow = new \DateTimeImmutable();
$now = $oNow->format('Y-m-d H:i:s');
$db->update('select_pool', [
'reserved_until'=>null,
'owner'=>null,
],'reserved_until < %s AND general_id IS NULL', $now);
$pool = [];
foreach($db->query('SELECT id, unique_name, info FROM select_pool WHERE reserved_until IS NULL AND general_id IS NULL', $pickCnt) as $cand){
$cand['info'] = Json::decode($cand['info']);
$dexTotal = array_sum($cand['info']['dex']);
$pool[] = [$cand, $dexTotal];
}
if(count($pool) < $pickCnt){
throw new \RuntimeException('pool 부족');
}
$gameStor = KVStorage::getStorage($db, 'game_env');
$result = [];
$validUntil = TimeUtil::nowAddMinutes(2 * $gameStor->turnterm);
while(count($result) < $pickCnt){
$cand = $rng->choiceUsingWeightPair($pool);
$poolID = $cand['id'];
if(key_exists($poolID, $result)){
continue;
}
$candInfo = $cand['info'];
$candInfo['uniqueName'] = $cand['unique_name'];
//하나씩 한다.
$db->update('select_pool', [
'owner'=>$owner,
'reserved_until'=>$validUntil,
], 'id = %i AND reserved_until IS NULL AND owner IS NULL and general_id IS NULL', $poolID);
if($db->affectedRows()==0){
continue;
}
$result[$poolID] = new static($db, $rng, $candInfo, $validUntil);
}
return array_values($result);
}
}