From 9b1a57bbd23dae2ab8a48ef19d3dac175ff81469 Mon Sep 17 00:00:00 2001 From: Hide_D Date: Sat, 4 Jun 2022 21:27:09 +0900 Subject: [PATCH] =?UTF-8?q?feat,wip:=20=EA=B2=BD=EB=A7=A4=EC=9E=A5?= =?UTF-8?q?=EC=9D=84=20db=20table=EB=A1=9C=20=EB=B6=84=EB=A6=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- hwe/sammo/Auction.php | 65 +++++++++-------------------- hwe/sammo/AuctionUniqueItem.php | 41 +++++++++--------- hwe/sammo/DTO/AuctionInfo.php | 19 ++++----- hwe/sammo/DTO/AuctionInfoDetail.php | 26 ++++++++++++ hwe/sql/schema.sql | 28 +++++++------ 5 files changed, 90 insertions(+), 89 deletions(-) create mode 100644 hwe/sammo/DTO/AuctionInfoDetail.php diff --git a/hwe/sammo/Auction.php b/hwe/sammo/Auction.php index c3ca625e..7284b779 100644 --- a/hwe/sammo/Auction.php +++ b/hwe/sammo/Auction.php @@ -5,21 +5,17 @@ namespace sammo; use DateTime; use DateTimeImmutable; use DateTimeInterface; -use Ds\Map; use sammo\DTO\AuctionBidItem; use sammo\DTO\AuctionBidItemData; use sammo\DTO\AuctionInfo; use sammo\Enums\InheritanceKey; use sammo\Enums\RankColumn; use sammo\Enums\ResourceType; -use sammo\VO\InheritancePointType; class Auction { protected AuctionInfo $info; - public const LAST_AUCTION_ID_KEY = 'last_auction_id'; - public const COEFF_AUCTION_CLOSE_MINUTES = 24; public const COEFF_EXTENSION_MINUTES_PER_BID = 1 / 6; @@ -30,39 +26,24 @@ class Auction public const MIN_EXTENSION_MINUTES_LIMIT_BY_BID = 5; public const MIN_EXTENSION_MINUTES_BY_EXTENSION_QUERY = 5; - static public function genNextAuctionID(): int + static public function openAuction(AuctionInfo $info, General $general): int|string { $db = DB::db(); - - $gameStor = KVStorage::getStorage($db, 'game_env'); - $gameStor->invalidateCacheValue(self::LAST_AUCTION_ID_KEY); - $auctionID = ($gameStor->getValue(self::LAST_AUCTION_ID_KEY) ?? 0) + 1; - - $gameStor->setValue(self::LAST_AUCTION_ID_KEY, $auctionID); - - return $auctionID; - } - - - static public function openAuction(AuctionInfo $info, General $general): ?string - { - $db = DB::db(); - $auctionID = $info->id; - $auctionStor = KVStorage::getStorage($db, 'auction'); - - $openedAuction = $general->getAuxVar('openedAuction') ?? []; - $prevAuctionID = $openedAuction[$info->type->value] ?? null; - if ($prevAuctionID !== null) { - //XXX: 이 구조보다는 차라리 DB에 insert로 넣는 게 나을 수도. - $prevAuction = new Auction($prevAuctionID, $general); - if (!$prevAuction->info->finished) { - return '아직 경매가 끝나지 않았습니다.'; - } - unset($openedAuction[$info->type->value]); - $general->setAuxVar('openedAuction', $openedAuction); + if ($info->id !== null) { + return 'id가 지정되어 있습니다.'; } - $auctionStor->setValue("id_{$auctionID}", $info->toArray()); + $prevAuctionID = $db->queryFirstField( + 'SELECT id FROM ng_auction WHERE opener_general_id = %i AND finished = 0 AND `type` = %s', + $general->getID(), + $info->type + ); + if ($prevAuctionID !== null) { + return '아직 경매가 끝나지 않았습니다.'; + } + + $db->insert('ng_auction', $info->toArray()); + return $db->insertId(); } public function getHighestBid(): ?AuctionBidItem @@ -96,8 +77,7 @@ class Auction public function __construct(protected readonly int $auctionID, protected General $general) { $db = DB::db(); - $auctionStor = KVStorage::getStorage($db, 'auction'); - $rawAuctionInfo = $auctionStor->getValue("id_{$auctionID}"); + $rawAuctionInfo = $db->query('SELECT * FROM ng_auction WHERE id = %i', $auctionID); if ($rawAuctionInfo === null) { throw new \RuntimeException("해당 경매가 없습니다: {$auctionID}"); } @@ -111,7 +91,7 @@ class Auction public function extendCloseDate(DateTimeInterface $date, bool $force = false): ?string { - if(!$force){ + if (!$force) { if ($this->info->remainCloseExtensionCnt === null) { return '연장할 수 없는 경매입니다.'; } @@ -124,7 +104,6 @@ class Auction } $db = DB::db(); - $auctionStor = KVStorage::getStorage($db, 'auction'); $gameStor = KVStorage::getStorage($db, 'game_env'); $turnTerm = $gameStor->getValue('turnterm'); $closeDate = DateTimeImmutable::createFromInterface($date); @@ -132,7 +111,7 @@ class Auction $this->info->availableLatestBidCloseDate = $closeDate->add(TimeUtil::secondsToDateInterval( max(static::MIN_EXTENSION_MINUTES_PER_BID, $turnTerm * static::COEFF_EXTENSION_MINUTES_PER_BID) * 60 )); - $auctionStor->setValue("id_{$this->info->id}", $this->info->toArray()); + $db->update('ng_auction', $this->info->toArrayExcept('id'), 'id = %i', $this->info->id); return null; } @@ -184,23 +163,17 @@ class Auction public function closeAuction(bool $isRollback = false): void { $db = DB::db(); - $auctionStor = KVStorage::getStorage($db, 'auction'); $this->info->finished = true; - $openedAuction = $this->general->getAuxVar('openedAuction') ?? []; - if (key_exists($this->info->type->value, $openedAuction)) { - unset($openedAuction[$this->info->type->value]); - $this->general->setAuxVar('openedAuction', $openedAuction); - } if ($isRollback) { $highestBid = $this->getHighestBid(); if ($highestBid !== null) { $this->refundBid($highestBid, "{$this->info->title} 경매가 취소되었습니다."); } } - $auctionID = $this->info->id; - $auctionStor->setValue("id_{$auctionID}", $this->info->toArray()); + + $db->update('ng_auction', $this->info->toArrayExcept('id'), 'id = %i', $this->info->id); } private function bidInheritPoint(int $amount, \DateTimeImmutable $now, bool $tryExtendCloseDate): ?string diff --git a/hwe/sammo/AuctionUniqueItem.php b/hwe/sammo/AuctionUniqueItem.php index 6a6e87df..21af7aae 100644 --- a/hwe/sammo/AuctionUniqueItem.php +++ b/hwe/sammo/AuctionUniqueItem.php @@ -5,6 +5,7 @@ namespace sammo; use DateTimeImmutable; use sammo\DTO\AuctionBidItem; use sammo\DTO\AuctionInfo; +use sammo\DTO\AuctionInfoDetail; use sammo\Enums\AuctionType; use sammo\Enums\InheritanceKey; use sammo\Enums\ResourceType; @@ -44,7 +45,7 @@ class AuctionUniqueItem extends Auction return "{$namePool[$subIdx]}{$dupIdx}"; } - static public function openItemAuction(BaseItem $item, General $general, int $startAmount): ?string + static public function openItemAuction(BaseItem $item, General $general, int $startAmount): self|string { if ($startAmount < GameConst::$inheritItemUniqueMinPoint) { return '최소 경매 금액은 ' . GameConst::$inheritItemUniqueMinPoint . '입니다.'; @@ -58,9 +59,6 @@ class AuctionUniqueItem extends Auction return '구매할 수 있는 아이템입니다.'; } - if (GameConst::$allItems) - - $auctionID = static::genNextAuctionID(); $now = new DateTimeImmutable(); $db = DB::db(); @@ -75,25 +73,28 @@ class AuctionUniqueItem extends Auction )); $info = new AuctionInfo( - $auctionID, - static::genObfuscatedName($general->getID()), - $general->getID(), - AuctionType::UniqueItem, - $item->getRawClassName(), - "{$item->getName()} 경매", - false, - ResourceType::inheritancePoint, - $startAmount, null, - 1, + AuctionType::UniqueItem, + false, + $item->getRawClassName(), + $general->getID(), + ResourceType::inheritancePoint, $now, $closeDate, - $availableLatestBidCloseDate, + new AuctionInfoDetail( + "{$item->getName()} 경매", + static::genObfuscatedName($general->getID()), + + $startAmount, + null, + 1, + $availableLatestBidCloseDate, + ) ); - $result = static::openAuction($info, $general); - if ($result !== null) { - return $result; + $auctionID = static::openAuction($info, $general); + if (!is_int($auctionID)) { + return $auctionID; } $auction = new static($auctionID, $general); try { @@ -105,7 +106,7 @@ class AuctionUniqueItem extends Auction return "경매를 시작했지만, 첫 입찰에 실패했습니다: {$msg}"; } - return null; + return $auction; } private function giveUniqueItem(AuctionBidItem $highestBid, General $bidder): ?string @@ -137,7 +138,7 @@ class AuctionUniqueItem extends Auction } } - if($availableEquipUniqueCnt <= 0){ + if ($availableEquipUniqueCnt <= 0) { return '유니크 아이템 소유 제한 상태입니다. 종료 시간이 연장됩니다.'; } diff --git a/hwe/sammo/DTO/AuctionInfo.php b/hwe/sammo/DTO/AuctionInfo.php index 10087714..3116680b 100644 --- a/hwe/sammo/DTO/AuctionInfo.php +++ b/hwe/sammo/DTO/AuctionInfo.php @@ -3,6 +3,8 @@ namespace sammo\DTO; use sammo\DTO\Attr\Convert; +use sammo\DTO\Attr\JsonString; +use sammo\DTO\Attr\NullIsUndefined; use sammo\DTO\Converter\DateTimeConverter; use sammo\Enums\AuctionType; use sammo\Enums\ResourceType; @@ -10,24 +12,21 @@ use sammo\Enums\ResourceType; class AuctionInfo extends DTO { public function __construct( - public int $id, - public string $openerName, - public int $openerGeneralID, + #[NullIsUndefined] + public ?int $id, public AuctionType $type, - public ?string $target, - public string $title, public bool $finished, + public ?string $target, + public int $openerGeneralID, public ResourceType $reqResource, - public int $minAmount, - public ?int $buyImmediatelyAmount, - public ?int $remainCloseDateExtensionCnt, #[Convert(DateTimeConverter::class)] public \DateTimeImmutable $openDate, #[Convert(DateTimeConverter::class)] public \DateTimeImmutable $closeDate, - #[Convert(DateTimeConverter::class)] - public ?\DateTimeImmutable $availableLatestBidCloseDate, + + #[JsonString] + public AuctionInfoDetail $detail, ) { } } diff --git a/hwe/sammo/DTO/AuctionInfoDetail.php b/hwe/sammo/DTO/AuctionInfoDetail.php new file mode 100644 index 00000000..b73dfc11 --- /dev/null +++ b/hwe/sammo/DTO/AuctionInfoDetail.php @@ -0,0 +1,26 @@ +