refac: 베팅 코드를 모듈로
This commit is contained in:
@@ -41,8 +41,6 @@ class Bet extends \sammo\BaseAPI
|
|||||||
|
|
||||||
public function launch(Session $session, ?DateTimeInterface $modifiedSince, ?string $reqEtag)
|
public function launch(Session $session, ?DateTimeInterface $modifiedSince, ?string $reqEtag)
|
||||||
{
|
{
|
||||||
$db = DB::db();
|
|
||||||
|
|
||||||
/** @var int */
|
/** @var int */
|
||||||
$bettingID = $this->args['bettingID'];
|
$bettingID = $this->args['bettingID'];
|
||||||
/** @var int[] */
|
/** @var int[] */
|
||||||
@@ -50,80 +48,12 @@ class Bet extends \sammo\BaseAPI
|
|||||||
/** @var int */
|
/** @var int */
|
||||||
$amount = $this->args['amount'];
|
$amount = $this->args['amount'];
|
||||||
|
|
||||||
$gameStor = KVStorage::getStorage($db, 'game_env');
|
|
||||||
|
|
||||||
$bettingHelper = new Betting($bettingID);
|
$bettingHelper = new Betting($bettingID);
|
||||||
$bettingInfo = $bettingHelper->getInfo();
|
try{
|
||||||
|
$bettingHelper->bet($session->generalID, $session->userID, $bettingType, $amount);
|
||||||
if ($bettingInfo->finished) {
|
|
||||||
return '이미 종료된 베팅입니다';
|
|
||||||
}
|
}
|
||||||
|
catch(\Throwable $e){
|
||||||
[$year, $month] = $gameStor->getValuesAsArray(['year', 'month']);
|
return $e->getMessage();
|
||||||
$yearMonth = Util::joinYearMonth($year, $month);
|
|
||||||
|
|
||||||
|
|
||||||
if ($bettingInfo->closeYearMonth <= $yearMonth) {
|
|
||||||
return '이미 마감된 베팅입니다';
|
|
||||||
}
|
|
||||||
|
|
||||||
if ($bettingInfo->openYearMonth > $yearMonth) {
|
|
||||||
return '아직 시작되지 않은 베팅입니다';
|
|
||||||
}
|
|
||||||
|
|
||||||
if (count($bettingType) != $bettingInfo->selectCnt) {
|
|
||||||
return '필요한 선택 수를 채우지 못했습니다.';
|
|
||||||
}
|
|
||||||
|
|
||||||
$bettingTypeKey = $bettingHelper->convertBettingKey($bettingType);
|
|
||||||
|
|
||||||
$inheritStor = KVStorage::getStorage($db, "inheritance_{$session->userID}");
|
|
||||||
|
|
||||||
$prevBetAmount = $db->queryFirstField('SELECT sum(amount) FROM ng_betting WHERE betting_id = %i AND user_id = %i', $bettingID, $session->userID) ?? 0;
|
|
||||||
|
|
||||||
if ($prevBetAmount + $amount > 1000) {
|
|
||||||
return (1000 - $prevBetAmount) . ' 포인트까지만 베팅 가능합니다.';
|
|
||||||
}
|
|
||||||
|
|
||||||
if ($bettingInfo->reqInheritancePoint) {
|
|
||||||
$remainPoint = ($inheritStor->getValue('previous') ?? [0, 0])[0];
|
|
||||||
if ($remainPoint < $amount) {
|
|
||||||
return '유산포인트가 충분하지 않습니다.';
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
$remainPoint = $db->queryFirstField('SELECT gold FROM general WHERE no = %i', $session->generalID) ?? 0;
|
|
||||||
if ($remainPoint < GameConst::$generalMinimumGold + $amount) {
|
|
||||||
return '금이 부족합니다.';
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
$userID = $session->userID;
|
|
||||||
|
|
||||||
$bettingItem = new BettingItem([
|
|
||||||
'betting_id' => $bettingID,
|
|
||||||
'general_id' => $session->generalID,
|
|
||||||
'user_id' => $userID,
|
|
||||||
'betting_type' => $bettingTypeKey,
|
|
||||||
'amount' => $amount
|
|
||||||
]);
|
|
||||||
|
|
||||||
$db->insertUpdate(
|
|
||||||
'ng_betting',
|
|
||||||
$bettingItem->toArray(),
|
|
||||||
['amount' => $db->sqleval('amount + %i', $amount)]
|
|
||||||
);
|
|
||||||
if ($bettingInfo->reqInheritancePoint) {
|
|
||||||
$inheritStor->setValue('previous', [$remainPoint - $amount, null]);
|
|
||||||
$userLogger = new UserLogger($userID);
|
|
||||||
$userLogger->push("{$amount} 포인트를 베팅에 사용", "inheritPoint");
|
|
||||||
$userLogger->flush();
|
|
||||||
} else {
|
|
||||||
$db->update('general', [
|
|
||||||
'gold' => $db->sqleval('gold - %i', $amount)
|
|
||||||
], 'no = %i', $session->generalID);
|
|
||||||
}
|
|
||||||
if (!$db->affected_rows) {
|
|
||||||
return '베팅을 실패했습니다.';
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return [
|
return [
|
||||||
|
|||||||
@@ -3,6 +3,7 @@
|
|||||||
namespace sammo;
|
namespace sammo;
|
||||||
|
|
||||||
use sammo\DTO\BettingInfo;
|
use sammo\DTO\BettingInfo;
|
||||||
|
use sammo\DTO\BettingItem;
|
||||||
|
|
||||||
class Betting
|
class Betting
|
||||||
{
|
{
|
||||||
@@ -56,6 +57,82 @@ class Betting
|
|||||||
return $this->info;
|
return $this->info;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function bet(int $generalID, ?int $userID, array $bettingType, int $amount): void{
|
||||||
|
$bettingInfo = $this->info;
|
||||||
|
|
||||||
|
if ($bettingInfo->finished) {
|
||||||
|
throw new \RuntimeException('이미 종료된 베팅입니다');
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($bettingInfo->finished) {
|
||||||
|
throw new \RuntimeException('이미 종료된 베팅입니다');
|
||||||
|
}
|
||||||
|
$db = DB::db();
|
||||||
|
$gameStor = KVStorage::getStorage($db, 'game_env');
|
||||||
|
|
||||||
|
[$year, $month] = $gameStor->getValuesAsArray(['year', 'month']);
|
||||||
|
$yearMonth = Util::joinYearMonth($year, $month);
|
||||||
|
|
||||||
|
|
||||||
|
if ($bettingInfo->closeYearMonth <= $yearMonth) {
|
||||||
|
throw new \RuntimeException('이미 마감된 베팅입니다');
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($bettingInfo->openYearMonth > $yearMonth) {
|
||||||
|
throw new \RuntimeException('아직 시작되지 않은 베팅입니다');
|
||||||
|
}
|
||||||
|
|
||||||
|
if (count($bettingType) != $bettingInfo->selectCnt) {
|
||||||
|
throw new \RuntimeException('필요한 선택 수를 채우지 못했습니다.');
|
||||||
|
}
|
||||||
|
|
||||||
|
$bettingTypeKey = $this->convertBettingKey($bettingType);
|
||||||
|
|
||||||
|
$inheritStor = KVStorage::getStorage($db, "inheritance_{$userID}");
|
||||||
|
|
||||||
|
$prevBetAmount = $db->queryFirstField('SELECT sum(amount) FROM ng_betting WHERE betting_id = %i AND user_id = %i', $this->bettingID, $userID) ?? 0;
|
||||||
|
|
||||||
|
if ($prevBetAmount + $amount > 1000) {
|
||||||
|
throw new \RuntimeException((1000 - $prevBetAmount) . ' 포인트까지만 베팅 가능합니다.');
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($bettingInfo->reqInheritancePoint) {
|
||||||
|
$remainPoint = ($inheritStor->getValue('previous') ?? [0, 0])[0];
|
||||||
|
if ($remainPoint < $amount) {
|
||||||
|
throw new \RuntimeException('유산포인트가 충분하지 않습니다.');
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
$remainPoint = $db->queryFirstField('SELECT gold FROM general WHERE no = %i', $generalID) ?? 0;
|
||||||
|
if ($remainPoint < GameConst::$generalMinimumGold + $amount) {
|
||||||
|
throw new \RuntimeException('금이 부족합니다.');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
$bettingItem = new BettingItem([
|
||||||
|
'betting_id' => $this->bettingID,
|
||||||
|
'general_id' => $generalID,
|
||||||
|
'user_id' => $userID,
|
||||||
|
'betting_type' => $bettingTypeKey,
|
||||||
|
'amount' => $amount
|
||||||
|
]);
|
||||||
|
|
||||||
|
$db->insertUpdate(
|
||||||
|
'ng_betting',
|
||||||
|
$bettingItem->toArray(),
|
||||||
|
['amount' => $db->sqleval('amount + %i', $amount)]
|
||||||
|
);
|
||||||
|
if ($bettingInfo->reqInheritancePoint) {
|
||||||
|
$inheritStor->setValue('previous', [$remainPoint - $amount, null]);
|
||||||
|
$userLogger = new UserLogger($userID);
|
||||||
|
$userLogger->push("{$amount} 포인트를 베팅에 사용", "inheritPoint");
|
||||||
|
$userLogger->flush();
|
||||||
|
} else {
|
||||||
|
$db->update('general', [
|
||||||
|
'gold' => $db->sqleval('gold - %i', $amount)
|
||||||
|
], 'no = %i', $generalID);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/** @param int[] $result */
|
/** @param int[] $result */
|
||||||
private function _calcRewardExclusive(array $bettingType): array
|
private function _calcRewardExclusive(array $bettingType): array
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in New Issue
Block a user