feat,wip: 경매장을 db table로 분리

This commit is contained in:
2022-06-09 01:21:21 +09:00
parent bb04632a7f
commit 9b1a57bbd2
5 changed files with 90 additions and 89 deletions
+19 -46
View File
@@ -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