feat,wip: 경매장 API 작성

This commit is contained in:
2022-06-09 01:21:21 +09:00
parent f607350442
commit 4b436b1499
13 changed files with 372 additions and 53 deletions
+29 -16
View File
@@ -13,11 +13,10 @@ abstract class AuctionBasicResource extends Auction
{
const MIN_AUCTION_AMOUNT = 100;
const MAX_AUCTION_AMOUNT = 10000;
static AuctionType $auctionType;
static ResourceType $hostRes;
static ResourceType $bidderRes;
static public function openBuyRiceAuction(General $general, int $amount, int $closeTurnCnt, int $startBidAmount, int $finishBidAmount): self|string
static public function openResourceAuction(General $general, int $amount, int $closeTurnCnt, int $startBidAmount, int $finishBidAmount): self|string
{
if ($closeTurnCnt < 1 || $closeTurnCnt > 24) {
return '종료기한은 1 ~ 24 턴 이어야 합니다.';
@@ -44,15 +43,18 @@ abstract class AuctionBasicResource extends Auction
}
$db = DB::db();
$prevAuctionID = $db->queryFirstField(
'SELECT id FROM ng_auction WHERE opener_general_id = %i AND finished = 0 AND `type` IN %ls',
$general->getID(),
[AuctionType::BuyRice->value, AuctionType::SellRice->value],
);
if ($prevAuctionID !== null) {
return '아직 경매가 끝나지 않았습니다.';
if (!($general instanceof DummyGeneral)) {
$prevAuctionID = $db->queryFirstField(
'SELECT id FROM ng_auction WHERE opener_general_id = %i AND finished = 0 AND `type` IN %ls',
$general->getID(),
[AuctionType::BuyRice->value, AuctionType::SellRice->value],
);
if ($prevAuctionID !== null) {
return '아직 경매가 끝나지 않았습니다.';
}
}
$now = new \DateTimeImmutable();
$gameStor = KVStorage::getStorage($db, 'game_env');
$turnTerm = $gameStor->getValue('turnterm');
@@ -89,10 +91,21 @@ abstract class AuctionBasicResource extends Auction
return new self($openResult, $general);
}
static public function genDummy(): DummyGeneral
{
$dummyGeneral = new DummyGeneral(true);
$dummyGeneral->setVar('name', '상인');
$dummyGeneral->setVar('gold', static::MAX_AUCTION_AMOUNT * 10);
$dummyGeneral->setVar('rice', static::MAX_AUCTION_AMOUNT * 10);
return $dummyGeneral;
}
protected function rollbackAuction(): void
{
if ($this->general->getID() === $this->info->openerGeneralID) {
$auctionHost = $this->general;
} else if ($this->info->openerGeneralID == 0) {
$auctionHost = $this->genDummy();
} else {
$auctionHost = General::createGeneralObjFromDB($this->info->openerGeneralID);
}
@@ -132,6 +145,8 @@ abstract class AuctionBasicResource extends Auction
{
if ($this->general->getID() === $this->info->openerGeneralID) {
$auctionHost = $this->general;
} else if ($this->info->openerGeneralID == 0) {
$auctionHost = $this->genDummy();
} else {
$auctionHost = General::createGeneralObjFromDB($this->info->openerGeneralID);
}
@@ -174,7 +189,6 @@ abstract class AuctionBasicResource extends Auction
$josaYiHost = JosaUtil::pick($auctionHost->getName(), '이');
$josaYiBidder = JosaUtil::pick($bidder->getName(), '이');
$josaRo = JosaUtil::pick($bidAmount, '로');
$auctionLog = [];
$auctionLog[] = "{$auctionID}{$hostResName} 경매 <C>성사</> : <Y>{$auctionHost->getName()}</>{$josaYiHost} {$hostResName} <C>{$auctionAmount}</> 판매, <Y>{$bidder->getName()}</>{$josaYiBidder} <C>{$bidAmount}</> 구매";
@@ -188,7 +202,7 @@ abstract class AuctionBasicResource extends Auction
pushAuctionLog(array_map(
fn ($log) =>
$auctionHost->getLogger()->formatText($log, ActionLogger::EVENT_PLAIN),
$bidder->getLogger()->formatText($log, ActionLogger::EVENT_PLAIN),
$auctionLog
));
@@ -199,18 +213,18 @@ abstract class AuctionBasicResource extends Auction
return null;
}
public function bid(int $amount, bool $_tryExtendCloseDate = true): ?string
public function bid(int $amount, bool $tryExtendCloseDate = false): ?string
{
if ($this->info->openerGeneralID === $this->general->getID()) {
return '자신이 연 경매에 입찰할 수 없습니다.';
}
$result = $this->_bid($amount, true);
$result = $this->_bid($amount, $tryExtendCloseDate);
if(is_string($result)){
if (is_string($result)) {
return $result;
}
if($amount === $this->info->detail->finishBidAmount){
if ($amount === $this->info->detail->finishBidAmount) {
//즉구, 1턴 후 지급
$db = DB::db();
$gameStor = KVStorage::getStorage($db, 'game_env');
@@ -218,6 +232,5 @@ abstract class AuctionBasicResource extends Auction
$date = (new DateTimeImmutable())->add(TimeUtil::secondsToDateInterval($turnTerm * 60));
$this->shrinkCloseDate($date);
}
}
}