refac: 베팅 모듈 수정
- 강제 베팅 종료 추가 - candidate에 assoc array 지정 가능
This commit is contained in:
+30
-15
@@ -10,9 +10,10 @@ class Betting
|
|||||||
|
|
||||||
private BettingInfo $info;
|
private BettingInfo $info;
|
||||||
|
|
||||||
public CONST LAST_BETTING_ID_KEY = 'last_betting_id';
|
public const LAST_BETTING_ID_KEY = 'last_betting_id';
|
||||||
|
|
||||||
static public function genNextBettingID(): int{
|
static public function genNextBettingID(): int
|
||||||
|
{
|
||||||
$db = DB::db();
|
$db = DB::db();
|
||||||
|
|
||||||
$gameStor = KVStorage::getStorage($db, 'game_env');
|
$gameStor = KVStorage::getStorage($db, 'game_env');
|
||||||
@@ -24,7 +25,8 @@ class Betting
|
|||||||
return $bettingID;
|
return $bettingID;
|
||||||
}
|
}
|
||||||
|
|
||||||
static public function openBetting(BettingInfo $info){
|
static public function openBetting(BettingInfo $info)
|
||||||
|
{
|
||||||
$db = DB::db();
|
$db = DB::db();
|
||||||
$bettingID = $info->id;
|
$bettingID = $info->id;
|
||||||
$bettingStor = KVStorage::getStorage($db, 'betting');
|
$bettingStor = KVStorage::getStorage($db, 'betting');
|
||||||
@@ -47,7 +49,7 @@ class Betting
|
|||||||
return Json::encode($bettingType);
|
return Json::encode($bettingType);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function purifyBettingKey(array $bettingType, bool $ignoreOver = false): array
|
public function purifyBettingKey(array $bettingType, bool $noValidate = false): array
|
||||||
{
|
{
|
||||||
$selectCnt = $this->info->selectCnt;
|
$selectCnt = $this->info->selectCnt;
|
||||||
sort($bettingType, SORT_NUMERIC);
|
sort($bettingType, SORT_NUMERIC);
|
||||||
@@ -56,17 +58,30 @@ class Betting
|
|||||||
throw new \InvalidArgumentException('중복된 값이 있습니다.');
|
throw new \InvalidArgumentException('중복된 값이 있습니다.');
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($bettingType[0] < 0) {
|
if (!$noValidate) {
|
||||||
throw new \InvalidArgumentException('올바르지 않은 값이 있습니다.(0 미만)' . print_r($bettingType, true));
|
foreach($bettingType as $bettingKey){
|
||||||
}
|
if(!key_exists($bettingKey, $this->info->candidates)){
|
||||||
|
throw new \InvalidArgumentException('올바르지 않은 값이 있습니다.(후보에 없음)' . print_r($bettingType, true));
|
||||||
if (!$ignoreOver && Util::array_last($bettingType) >= count($this->info->candidates)) {
|
}
|
||||||
throw new \InvalidArgumentException('올바르지 않은 값이 있습니다.(초과)' . print_r($bettingType, true));
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return $bettingType;
|
return $bettingType;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function closeBetting(): void
|
||||||
|
{
|
||||||
|
$db = DB::db();
|
||||||
|
$bettingStor = KVStorage::getStorage($db, 'betting');
|
||||||
|
$bettingID = $this->info->id;
|
||||||
|
$gameStor = KVStorage::getStorage($db, 'game_env');
|
||||||
|
|
||||||
|
//XXX: 베팅 종료 시점을 '현재 연월'로 하여 강제로 닫는다.
|
||||||
|
[$year, $month] = $gameStor->getValuesAsArray(['year', 'month']);
|
||||||
|
$this->info->closeYearMonth = Util::joinYearMonth($year, $month);
|
||||||
|
$bettingStor->setValue("id_{$bettingID}", $this->info->toArray());
|
||||||
|
}
|
||||||
|
|
||||||
public function convertBettingKey(array $bettingType): string
|
public function convertBettingKey(array $bettingType): string
|
||||||
{
|
{
|
||||||
$bettingType = $this->purifyBettingKey($bettingType);
|
$bettingType = $this->purifyBettingKey($bettingType);
|
||||||
@@ -78,7 +93,8 @@ class Betting
|
|||||||
return $this->info;
|
return $this->info;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function bet(int $generalID, ?int $userID, array $bettingType, int $amount): void{
|
public function bet(int $generalID, ?int $userID, array $bettingType, int $amount): void
|
||||||
|
{
|
||||||
$bettingInfo = $this->info;
|
$bettingInfo = $this->info;
|
||||||
|
|
||||||
if ($bettingInfo->finished) {
|
if ($bettingInfo->finished) {
|
||||||
@@ -287,8 +303,8 @@ class Betting
|
|||||||
|
|
||||||
//남은 상금은 '당첨자'에게 몰아준다.
|
//남은 상금은 '당첨자'에게 몰아준다.
|
||||||
//당첨자가 아무도 없다면, 0개 맞춘 그룹에게 돌아간다.
|
//당첨자가 아무도 없다면, 0개 맞춘 그룹에게 돌아간다.
|
||||||
if($rewardAmount){
|
if ($rewardAmount) {
|
||||||
foreach(Util::range($selectCnt, -1, -1) as $matchPoint){
|
foreach (Util::range($selectCnt, -1, -1) as $matchPoint) {
|
||||||
if (!key_exists($matchPoint, $rewardAmount)) {
|
if (!key_exists($matchPoint, $rewardAmount)) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
@@ -338,7 +354,7 @@ class Betting
|
|||||||
$previousPoint = ($inheritStor->getValue('previous') ?? [0, 0])[0];
|
$previousPoint = ($inheritStor->getValue('previous') ?? [0, 0])[0];
|
||||||
$nextPoint = $previousPoint + $amount;
|
$nextPoint = $previousPoint + $amount;
|
||||||
$inheritStor->setValue('previous', [$nextPoint, 0]);
|
$inheritStor->setValue('previous', [$nextPoint, 0]);
|
||||||
$inheritStor->invalidateCacheValue('previous');//XXX: 실제로는 previous 값을 사용할 수 없도록 락을 걸어야 한다.
|
$inheritStor->invalidateCacheValue('previous'); //XXX: 실제로는 previous 값을 사용할 수 없도록 락을 걸어야 한다.
|
||||||
|
|
||||||
$amountText = number_format($amount);
|
$amountText = number_format($amount);
|
||||||
$previousPointText = number_format($previousPoint);
|
$previousPointText = number_format($previousPoint);
|
||||||
@@ -368,7 +384,6 @@ class Betting
|
|||||||
foreach ($loggers as $userLogger) {
|
foreach ($loggers as $userLogger) {
|
||||||
$userLogger->flush();
|
$userLogger->flush();
|
||||||
}
|
}
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
$generalList = General::createGeneralObjListFromDB(array_unique(Util::squeezeFromArray($rewardList, 'generalID')), ['gold', 'npc'], 1);
|
$generalList = General::createGeneralObjListFromDB(array_unique(Util::squeezeFromArray($rewardList, 'generalID')), ['gold', 'npc'], 1);
|
||||||
foreach ($rewardList as $rewardItem) {
|
foreach ($rewardList as $rewardItem) {
|
||||||
|
|||||||
Reference in New Issue
Block a user