feat,wip: 입찰 기능 동작

This commit is contained in:
2022-06-09 01:21:21 +09:00
parent 30401382fd
commit e0c209bcbb
19 changed files with 390 additions and 64 deletions
@@ -37,18 +37,32 @@ class GetActiveResourceAuctionList extends \sammo\BaseAPI
$sellRiceList = [];
/** @var AuctionInfo[] */
$auctions = array_map(fn ($raw) => AuctionInfo::fromArray($raw), $db->query(
'SELECT * FROM `ng_auction` WHERE `type` IN %ls AND `finished` != 0 ORDER BY `close_date` DESC',
'SELECT * FROM `ng_auction` WHERE `type` IN %ls AND `finished` = 0 ORDER BY `close_date` DESC',
[
AuctionType::BuyRice->value,
AuctionType::SellRice->value,
]
));
$recentLogs = getAuctionLogRecent(20);
if (!$auctions) {
return [
'result' => true,
'buyRice' => $buyRiceList,
'sellRice' => $sellRiceList,
'recentLogs' => $recentLogs,
'generalID' => $session->generalID,
];
}
$auctionIDList = [];
foreach ($auctions as $auction) {
$auctionIDList[] = $auction->id;
}
$rawHighestBids = Util::convertArrayToDict($db->query(
'SELECT bid.* FROM `ng_auction_bid` bid INNER JOIN (
SELECT `auction_id`, MAX(`amount`) as `max_amount`
@@ -87,7 +101,7 @@ class GetActiveResourceAuctionList extends \sammo\BaseAPI
'amount' => $highestBid->amount,
'date' => TimeUtil::format($highestBid->date, false),
'generalID' => $highestBid->generalID,
'generalName' => $highestBid->data->generalName,
'generalName' => $highestBid->aux->generalName,
];
}
@@ -98,8 +112,6 @@ class GetActiveResourceAuctionList extends \sammo\BaseAPI
}
}
$recentLogs = getAuctionLogRecent(20);
return [
'result' => true,
'buyRice' => $buyRiceList,
@@ -60,7 +60,7 @@ class GetUniqueItemAuctionDetail extends \sammo\BaseAPI
$responseBid = [];
foreach ($bidList as $bid) {
$responseBid[] = [
'generalName' => $bid->data->generalName,
'generalName' => $bid->aux->generalName,
'amount' => $bid->amount,
'isCallerHighestBidder' => $bid->generalID === $generalID,
'date' => $bid->date,
@@ -74,7 +74,7 @@ class GetUniqueItemAuctionDetail extends \sammo\BaseAPI
'finished' => $auction->finished,
'title' => $auction->detail->title,
'target' => $auction->target,
'isCallerHost' => $auction->openerGeneralID === $generalID,
'isCallerHost' => $auction->hostGeneralID === $generalID,
'hostName' => $auction->detail->hostName,
'remainCloseDateExtensionCnt' => $auction->detail->remainCloseDateExtensionCnt,
'availableLatestBidCloseDate' => $auction->detail->availableLatestBidCloseDate,
@@ -69,12 +69,12 @@ class GetUniqueItemAuctionList extends \sammo\BaseAPI
'finished' => $auction->finished,
'title' => $auction->detail->title,
'target' => $auction->target,
'isCallerHost' => $auction->openerGeneralID === $generalID,
'isCallerHost' => $auction->hostGeneralID === $generalID,
'hostName' => $auction->detail->hostName,
'remainCloseDateExtensionCnt' => $auction->detail->remainCloseDateExtensionCnt,
'availableLatestBidCloseDate' => $auction->detail->availableLatestBidCloseDate,
'highestBid' => [
'generalName' => $highestBid->data->generalName,
'generalName' => $highestBid->aux->generalName,
'amount' => $highestBid->amount,
'isCallerHighestBidder' => $highestBid->generalID === $generalID,
'date' => $highestBid->date,
+35 -26
View File
@@ -46,7 +46,7 @@ abstract class Auction
{
$db = DB::db();
if($this->_highestBid !== false){
if ($this->_highestBid !== false) {
return $this->_highestBid;
}
@@ -75,11 +75,19 @@ abstract class Auction
public function getMyPrevBid(): ?AuctionBidItem
{
$db = DB::db();
$rawMyPrevBid = $db->queryFirstRow(
'SELECT * FROM ng_auction_bid WHERE general_id = %i AND auction_id = %i ORDER BY `amount` DESC LIMIT 1',
$this->general->getID(),
$this->info->id
);
if (!$this->info->detail->isReverse) {
$rawMyPrevBid = $db->queryFirstRow(
'SELECT * FROM ng_auction_bid WHERE general_id = %i AND auction_id = %i ORDER BY `amount` DESC LIMIT 1',
$this->general->getID(),
$this->info->id
);
} else {
$rawMyPrevBid = $db->queryFirstRow(
'SELECT * FROM ng_auction_bid WHERE general_id = %i AND auction_id = %i ORDER BY `amount` ASC LIMIT 1',
$this->general->getID(),
$this->info->id
);
}
if (!$rawMyPrevBid) {
return null;
}
@@ -89,13 +97,14 @@ abstract class Auction
public function __construct(protected readonly int $auctionID, protected General $general)
{
$db = DB::db();
$rawAuctionInfo = $db->query('SELECT * FROM ng_auction WHERE id = %i', $auctionID);
if ($rawAuctionInfo === null) {
$rawAuctionInfo = $db->queryFirstRow('SELECT * FROM `ng_auction` WHERE id = %i', $auctionID);
if (!$rawAuctionInfo) {
throw new \RuntimeException("해당 경매가 없습니다: {$auctionID}");
}
$this->info = AuctionInfo::fromArray($rawAuctionInfo);
if($this->info->type !== $this->auctionType){
throw new \RuntimeException("잘못된 경매 타입입니다: {$this->info->type} != {$this->auctionType}");
$thisAuctionType = static::$auctionType;
if ($this->info->type !== $thisAuctionType) {
throw new \RuntimeException("잘못된 경매 타입입니다: {$this->info->type->value} != {$thisAuctionType->value}");
}
}
@@ -104,8 +113,9 @@ abstract class Auction
return $this->info;
}
public function shrinkCloseDate(?DateTimeInterface $date): ?string{
if($date === null){
public function shrinkCloseDate(?DateTimeInterface $date): ?string
{
if ($date === null) {
$date = new DateTimeImmutable();
}
@@ -196,7 +206,7 @@ abstract class Auction
if ($isRollback) {
$highestBid = $this->getHighestBid();
if ($highestBid !== null) {
$this->refundBid($highestBid, "{$this->info->title} 경매가 취소되었습니다.");
$this->refundBid($highestBid, "{$this->info->id}{$this->info->detail->title} 경매가 취소되었습니다.");
}
$this->rollbackAuction();
}
@@ -216,15 +226,10 @@ abstract class Auction
return '현재입찰가보다 높게 입찰해야 합니다.';
}
$rawMyPrevBid = $db->queryFirstRow(
'SELECT * FROM ng_auction_bid WHERE general_id = %i AND auction_id = %i ORDER BY `amount` DESC LIMIT 1',
$general->getID(),
$auctionInfo->id
);
if ($rawMyPrevBid === null) {
$myPrevBid = $this->getMyPrevBid();
if($myPrevBid !== null && $highestBid->no !== $myPrevBid->no){
//이미 환불 받았으니 무효.
$myPrevBid = null;
} else {
$myPrevBid = AuctionBidItem::fromArray($rawMyPrevBid);
}
$morePoint = $amount - ($myPrevBid ? $myPrevBid->amount : 0);
@@ -269,8 +274,8 @@ abstract class Auction
$general->increaseInheritancePoint(InheritanceKey::previous, -$morePoint);
$general->increaseRankVar(RankColumn::inherit_point_spent_dynamic, $morePoint);
if ($myPrevBid === null || ($highestBid !== null && $myPrevBid->id !== $highestBid->id)) {
$this->refundBid($highestBid, "{$auctionInfo->title} 경매에 상회입찰자가 나타났습니다.");
if ($highestBid !== null && $myPrevBid === null) {
$this->refundBid($highestBid, "{$auctionInfo->id}$auctionInfo->detail->title}에 상회입찰자가 나타났습니다.");
}
$general->applyDB($db);
return null;
@@ -326,6 +331,10 @@ abstract class Auction
$myPrevBid = $this->getMyPrevBid();
if($myPrevBid !== null && $highestBid->no !== $myPrevBid->no){
//이미 환불 받았으니 무효.
$myPrevBid = null;
}
$morePoint = $amount - ($myPrevBid ? $myPrevBid->amount : 0);
$resType = $auctionInfo->reqResource;
@@ -361,8 +370,8 @@ abstract class Auction
$general->increaseVar($resType->value, -$morePoint);
if ($myPrevBid === null || ($highestBid !== null && $myPrevBid->id !== $highestBid->id)) {
$this->refundBid($highestBid, "{$auctionInfo->title} 경매에 상회입찰자가 나타났습니다.");
if ($highestBid !== null && $myPrevBid === null) {
$this->refundBid($highestBid, "{$auctionInfo->id}{$auctionInfo->detail->title}에 상회입찰자가 나타났습니다.");
}
$general->applyDB($db);
return null;
@@ -382,7 +391,7 @@ abstract class Auction
return true;
}
if ($highestBid->data->tryExtendCloseDate) {
if ($highestBid->aux->tryExtendCloseDate) {
//연장 요청이 있었다.
$extendedCloseDate = $this->info->closeDate->add(TimeUtil::secondsToDateInterval(
max(static::MIN_EXTENSION_MINUTES_BY_EXTENSION_QUERY, $this->info->turnTerm * static::COEFF_EXTENSION_MINUTES_BY_EXTENSION_QUERY) * 60
+11 -9
View File
@@ -45,7 +45,7 @@ abstract class AuctionBasicResource extends Auction
$db = DB::db();
if (!($general instanceof DummyGeneral)) {
$prevAuctionID = $db->queryFirstField(
'SELECT id FROM ng_auction WHERE opener_general_id = %i AND finished = 0 AND `type` IN %ls',
'SELECT id FROM ng_auction WHERE host_general_id = %i AND finished = 0 AND `type` IN %ls',
$general->getID(),
[AuctionType::BuyRice->value, AuctionType::SellRice->value],
);
@@ -88,7 +88,7 @@ abstract class AuctionBasicResource extends Auction
$general->increaseVarWithLimit($hostRes->value, -$amount, 0);
$general->applyDB($db);
return new self($openResult, $general);
return new static($openResult, $general);
}
static public function genDummy(): DummyGeneral
@@ -102,12 +102,12 @@ abstract class AuctionBasicResource extends Auction
protected function rollbackAuction(): void
{
if ($this->general->getID() === $this->info->openerGeneralID) {
if ($this->general->getID() === $this->info->hostGeneralID) {
$auctionHost = $this->general;
} else if ($this->info->openerGeneralID == 0) {
} else if ($this->info->hostGeneralID == 0) {
$auctionHost = $this->genDummy();
} else {
$auctionHost = General::createGeneralObjFromDB($this->info->openerGeneralID);
$auctionHost = General::createGeneralObjFromDB($this->info->hostGeneralID);
}
$hostRes = static::$hostRes;
@@ -143,12 +143,12 @@ abstract class AuctionBasicResource extends Auction
protected function finishAuction(AuctionBidItem $highestBid, General $bidder): ?string
{
if ($this->general->getID() === $this->info->openerGeneralID) {
if ($this->general->getID() === $this->info->hostGeneralID) {
$auctionHost = $this->general;
} else if ($this->info->openerGeneralID == 0) {
} else if ($this->info->hostGeneralID == 0) {
$auctionHost = $this->genDummy();
} else {
$auctionHost = General::createGeneralObjFromDB($this->info->openerGeneralID);
$auctionHost = General::createGeneralObjFromDB($this->info->hostGeneralID);
}
$highestBid = $this->getHighestBid();
@@ -215,7 +215,7 @@ abstract class AuctionBasicResource extends Auction
public function bid(int $amount, bool $tryExtendCloseDate = false): ?string
{
if ($this->info->openerGeneralID === $this->general->getID()) {
if ($this->info->hostGeneralID === $this->general->getID()) {
return '자신이 연 경매에 입찰할 수 없습니다.';
}
$result = $this->_bid($amount, $tryExtendCloseDate);
@@ -232,5 +232,7 @@ abstract class AuctionBasicResource extends Auction
$date = (new DateTimeImmutable())->add(TimeUtil::secondsToDateInterval($turnTerm * 60));
$this->shrinkCloseDate($date);
}
return null;
}
}
+1 -1
View File
@@ -74,7 +74,7 @@ class AuctionUniqueItem extends Auction
}
$prevAuctionID = $db->queryFirstField(
'SELECT id FROM ng_auction WHERE opener_general_id = %i AND finished = 0 AND `type` = %s',
'SELECT id FROM ng_auction WHERE host_general_id = %i AND finished = 0 AND `type` = %s',
$general->getID(),
AuctionType::UniqueItem->value,
);
+2 -2
View File
@@ -12,7 +12,7 @@ class AuctionBidItem extends DTO
{
public function __construct(
#[NullIsUndefined]
public ?int $id,
public ?int $no,
#[RawName('auction_id')]
public int $auctionID,
public ?int $owner,
@@ -25,7 +25,7 @@ class AuctionBidItem extends DTO
#[Convert(DateTimeConverter::class)]
public \DateTimeImmutable $date,
#[JsonString]
public AuctionBidItemData $data,
public AuctionBidItemData $aux,
) {
}
}
+1 -1
View File
@@ -20,7 +20,7 @@ class AuctionInfo extends DTO
public ?string $target,
#[RawName('host_general_id')]
public int $hostGeneralID,
#[RawName('reqResource')]
#[RawName('req_resource')]
public ResourceType $reqResource,
#[RawName('open_date')]