forked from devsam/core
refac: 변경한 DTO에 따라 코드 수정
- Spatie/DataTransferObject 대비
- MapFrom,MapTo -> RawName
- Strict -> 항상
- 심지어 raw type도 더 까다롭게
- array에 MapConverter 적용
- NullIsUndefined 적용
- 빈 nullable arg자리에 null 강제 지정
- Vue3에 v-model.number 지정
- input type="number" 기준
This commit is contained in:
@@ -383,10 +383,12 @@ function getDummyBettingInfo(string $tnmt_type): BettingInfo
|
||||
name: $tnmt_type,
|
||||
finished: true,
|
||||
selectCnt: 1,
|
||||
isExclusive: null,
|
||||
reqInheritancePoint: false,
|
||||
openYearMonth: 0,
|
||||
closeYearMonth: 0,
|
||||
candidates: []
|
||||
candidates: [],
|
||||
winner: null,
|
||||
);
|
||||
}
|
||||
|
||||
@@ -425,6 +427,7 @@ function startBetting($type, $unit)
|
||||
$candidates[$general['no']] = new \sammo\DTO\SelectItem(
|
||||
title: $general['name'],
|
||||
info: "{$statName}: {$general[$statKey]}",
|
||||
isHtml: null,
|
||||
aux: $general
|
||||
);
|
||||
}
|
||||
@@ -436,10 +439,12 @@ function startBetting($type, $unit)
|
||||
name: $typeText,
|
||||
finished: false,
|
||||
selectCnt: 1,
|
||||
isExclusive: null,
|
||||
reqInheritancePoint: false,
|
||||
openYearMonth: $openYearMonth,
|
||||
closeYearMonth: $closeYearMonth,
|
||||
candidates: $candidates,
|
||||
winner: null,
|
||||
));
|
||||
|
||||
$betting = new Betting($bettingID);
|
||||
|
||||
+1
-1
@@ -116,7 +116,7 @@ $lastVoteID = $gameStor->lastVote;
|
||||
$lastVote = null;
|
||||
if($lastVoteID){
|
||||
$voteStor = KVStorage::getStorage($db, 'vote');
|
||||
$lastVote = new VoteInfo($voteStor->getValue("vote_{$lastVoteID}"));
|
||||
$lastVote = VoteInfo::fromArray($voteStor->getValue("vote_{$lastVoteID}"));
|
||||
if($lastVote->endDate && $lastVote->endDate < TimeUtil::now()){
|
||||
$lastVote = null;
|
||||
}
|
||||
|
||||
@@ -47,7 +47,7 @@ class GetBettingDetail extends \sammo\BaseAPI
|
||||
}
|
||||
|
||||
try{
|
||||
$bettingInfo = new BettingInfo($rawBettingInfo);
|
||||
$bettingInfo = BettingInfo::fromArray($rawBettingInfo);
|
||||
}
|
||||
catch(\Error $e){
|
||||
return $e->getMessage();
|
||||
|
||||
@@ -49,7 +49,7 @@ class GetBettingList extends \sammo\BaseAPI
|
||||
|
||||
$bettingList = [];
|
||||
foreach ($bettingStor->getAll() as $_key => $rawItem) {
|
||||
$item = new BettingInfo($rawItem);
|
||||
$item = BettingInfo::fromArray($rawItem);
|
||||
if ($reqType !== null && $item->type != $reqType) {
|
||||
continue;
|
||||
}
|
||||
|
||||
@@ -29,6 +29,7 @@ class AddComment extends \sammo\BaseAPI
|
||||
if (!$v->validate()) {
|
||||
return $v->errorStr();
|
||||
}
|
||||
$this->args['voteID'] = (int)$this->args['voteID'];
|
||||
return null;
|
||||
}
|
||||
|
||||
@@ -46,11 +47,12 @@ class AddComment extends \sammo\BaseAPI
|
||||
|
||||
|
||||
$comment = new VoteComment(
|
||||
vote_id: $voteID,
|
||||
general_id: $generalID,
|
||||
nation_id: $nationID,
|
||||
nation_name: $nationName,
|
||||
general_name: $generalName,
|
||||
id: null,
|
||||
voteID: $voteID,
|
||||
generalID: $generalID,
|
||||
nationID: $nationID,
|
||||
nationName: $nationName,
|
||||
generalName: $generalName,
|
||||
text: $text,
|
||||
date: $date
|
||||
);
|
||||
|
||||
@@ -40,7 +40,7 @@ class GetVoteDetail extends \sammo\BaseAPI
|
||||
if (!$rawVote) {
|
||||
return '설문조사가 없습니다.';
|
||||
}
|
||||
$voteInfo = new VoteInfo(...$rawVote);
|
||||
$voteInfo = VoteInfo::fromArray($rawVote);
|
||||
|
||||
|
||||
$votes = array_map(fn ($arr) => [Json::decode($arr[0]), $arr[1]], $db->queryAllLists(
|
||||
@@ -48,13 +48,13 @@ class GetVoteDetail extends \sammo\BaseAPI
|
||||
$voteID
|
||||
));
|
||||
|
||||
$comments = VoteComment::arrayOf($db->query('SELECT * FROM vote_comment WHERE vote_id = %i ORDER BY `id` ASC', $voteID));
|
||||
$comments = array_map(fn ($arr) => VoteComment::fromArray($arr), $db->query('SELECT * FROM vote_comment WHERE vote_id = %i ORDER BY `id` ASC', $voteID));
|
||||
|
||||
$myVote = null;
|
||||
if($session->isGameLoggedIn()){
|
||||
if ($session->isGameLoggedIn()) {
|
||||
$generalID = $session->generalID;
|
||||
$rawMyVote = $db->queryFirstField('SELECT selection FROM vote WHERE vote_id = %i AND general_id = %i', $voteID, $generalID);
|
||||
if($rawMyVote){
|
||||
if ($rawMyVote) {
|
||||
$myVote = Json::decode($rawMyVote);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -33,7 +33,7 @@ class GetVoteList extends \sammo\BaseAPI
|
||||
continue;
|
||||
}
|
||||
$voteID = (int)substr($voteKey, 5);
|
||||
$votes[$voteID] = new VoteInfo(...$rawVote);
|
||||
$votes[$voteID] = VoteInfo::fromArray($rawVote);
|
||||
}
|
||||
|
||||
return [
|
||||
|
||||
@@ -43,7 +43,7 @@ class NewVote extends \sammo\BaseAPI
|
||||
if (!$rawLastVoteInfo) {
|
||||
return;
|
||||
}
|
||||
$lastVoteInfo = new VoteInfo(...$rawLastVoteInfo);
|
||||
$lastVoteInfo = VoteInfo::fromArray($rawLastVoteInfo);
|
||||
if ($lastVoteInfo->endDate) {
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -58,7 +58,7 @@ class Vote extends \sammo\BaseAPI
|
||||
if (!$rawVoteInfo) {
|
||||
return '설문조사가 없습니다.';
|
||||
}
|
||||
$voteInfo = new VoteInfo(...$rawVoteInfo);
|
||||
$voteInfo = VoteInfo::fromArray($rawVoteInfo);
|
||||
|
||||
if ($voteInfo->endDate && $voteInfo->endDate < new \DateTimeImmutable()) {
|
||||
return '설문조사가 종료되었습니다.';
|
||||
|
||||
@@ -43,7 +43,7 @@ class Betting
|
||||
if ($rawBettingInfo === null) {
|
||||
throw new \RuntimeException("해당 베팅이 없습니다: {$bettingID}");
|
||||
}
|
||||
$this->info = new BettingInfo($rawBettingInfo);
|
||||
$this->info = BettingInfo::fromArray($rawBettingInfo);
|
||||
}
|
||||
|
||||
private function _convertBettingKey(array $bettingType): string
|
||||
@@ -148,13 +148,14 @@ class Betting
|
||||
}
|
||||
}
|
||||
|
||||
$bettingItem = new BettingItem([
|
||||
'betting_id' => $this->bettingID,
|
||||
'general_id' => $generalID,
|
||||
'user_id' => $userID,
|
||||
'betting_type' => $bettingTypeKey,
|
||||
'amount' => $amount
|
||||
]);
|
||||
$bettingItem = new BettingItem(
|
||||
rowID: null,
|
||||
bettingID: $this->bettingID,
|
||||
generalID: $generalID,
|
||||
userID: $userID,
|
||||
bettingType: $bettingTypeKey,
|
||||
amount: $amount
|
||||
);
|
||||
|
||||
$db->insertUpdate(
|
||||
'ng_betting',
|
||||
|
||||
@@ -2,33 +2,32 @@
|
||||
|
||||
namespace sammo\DTO;
|
||||
|
||||
use sammo\DTO\Attr\Convert;
|
||||
use sammo\DTO\Converter\MapConverter;
|
||||
use sammo\DTO\SelectItem;
|
||||
use Spatie\DataTransferObject\Attributes\CastWith;
|
||||
use Spatie\DataTransferObject\Attributes\Strict;
|
||||
use Spatie\DataTransferObject\DataTransferObject;
|
||||
use Spatie\DataTransferObject\Casters\ArrayCaster;
|
||||
|
||||
//https://json2dto.atymic.dev/
|
||||
|
||||
|
||||
|
||||
#[Strict]
|
||||
class BettingInfo extends DataTransferObject
|
||||
class BettingInfo extends DTO
|
||||
{
|
||||
public int $id;
|
||||
public string $type;
|
||||
public string $name;
|
||||
public bool $finished;
|
||||
public int $selectCnt;
|
||||
public ?bool $isExlusive;
|
||||
public bool $reqInheritancePoint;
|
||||
public int $openYearMonth;
|
||||
public int $closeYearMonth;
|
||||
public function __construct(
|
||||
public int $id,
|
||||
public string $type,
|
||||
public string $name,
|
||||
public bool $finished,
|
||||
public int $selectCnt,
|
||||
public ?bool $isExclusive,
|
||||
public bool $reqInheritancePoint,
|
||||
public int $openYearMonth,
|
||||
public int $closeYearMonth,
|
||||
|
||||
/** @var \sammo\DTO\SelectItem[] */
|
||||
#[CastWith(ArrayCaster::class, itemType: SelectItem::class)]
|
||||
public array $candidates;
|
||||
public ?array $winner;
|
||||
|
||||
/** @var \sammo\DTO\SelectItem[] */
|
||||
#[Convert(MapConverter::class, [SelectItem::class])]
|
||||
public array $candidates,
|
||||
public ?array $winner,
|
||||
) {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -2,32 +2,28 @@
|
||||
|
||||
namespace sammo\DTO;
|
||||
|
||||
use Spatie\DataTransferObject\Attributes\MapFrom;
|
||||
use Spatie\DataTransferObject\Attributes\MapTo;
|
||||
use Spatie\DataTransferObject\Attributes\Strict;
|
||||
use Spatie\DataTransferObject\DataTransferObject;
|
||||
use sammo\DTO\Attr\NullIsUndefined;
|
||||
use sammo\DTO\Attr\RawName;
|
||||
|
||||
#[Strict]
|
||||
class BettingItem extends DataTransferObject
|
||||
class BettingItem extends DTO
|
||||
{
|
||||
#[MapFrom('id')]
|
||||
#[MapTo('id')]
|
||||
public null|int $rowID = null;
|
||||
public function __construct(
|
||||
#[RawName('id')]
|
||||
#[NullIsUndefined]
|
||||
public ?int $rowID,
|
||||
|
||||
#[MapFrom('betting_id')]
|
||||
#[MapTo('betting_id')]
|
||||
public int $bettingID;
|
||||
#[RawName('betting_id')]
|
||||
public int $bettingID,
|
||||
|
||||
#[MapFrom('general_id')]
|
||||
#[MapTo('general_id')]
|
||||
public int $generalID;
|
||||
#[RawName('general_id')]
|
||||
public int $generalID,
|
||||
|
||||
#[MapFrom('user_id')]
|
||||
#[MapTo('user_id')]
|
||||
public null|int $userID;
|
||||
#[RawName('user_id')]
|
||||
public ?int $userID,
|
||||
|
||||
#[MapFrom('betting_type')]
|
||||
#[MapTo('betting_type')]
|
||||
public string $bettingType;
|
||||
public int $amount;
|
||||
#[RawName('betting_type')]
|
||||
public string $bettingType,
|
||||
public int $amount,
|
||||
) {
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,14 +2,17 @@
|
||||
|
||||
namespace sammo\DTO;
|
||||
|
||||
use Spatie\DataTransferObject\Attributes\Strict;
|
||||
use Spatie\DataTransferObject\DataTransferObject;
|
||||
use sammo\DTO\Attr\Convert;
|
||||
use sammo\DTO\Converter\MapConverter;
|
||||
|
||||
#[Strict]
|
||||
class SelectItem extends DataTransferObject
|
||||
class SelectItem extends DTO
|
||||
{
|
||||
public string $title;
|
||||
public ?string $info;
|
||||
public ?bool $isHtml;
|
||||
public ?array $aux;
|
||||
public function __construct(
|
||||
public string $title,
|
||||
public ?string $info,
|
||||
public ?bool $isHtml,
|
||||
#[Convert(MapConverter::class, ['string', 'int', 'float', 'array', 'null', 'bool'])]
|
||||
public ?array $aux,
|
||||
) {
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,37 +2,33 @@
|
||||
|
||||
namespace sammo\DTO;
|
||||
|
||||
use Spatie\DataTransferObject\Attributes\MapFrom;
|
||||
use Spatie\DataTransferObject\Attributes\MapTo;
|
||||
use Spatie\DataTransferObject\Attributes\Strict;
|
||||
use Spatie\DataTransferObject\DataTransferObject;
|
||||
use sammo\DTO\Attr\NullIsUndefined;
|
||||
use sammo\DTO\Attr\RawName;
|
||||
|
||||
#[Strict]
|
||||
class VoteComment extends DataTransferObject
|
||||
class VoteComment extends DTO
|
||||
{
|
||||
public ?int $id;
|
||||
public function __construct(
|
||||
#[NullIsUndefined]
|
||||
public ?int $id,
|
||||
|
||||
#[MapFrom('vote_id')]
|
||||
#[MapTo('vote_id')]
|
||||
public int $voteID;
|
||||
#[RawName('vote_id')]
|
||||
public int $voteID,
|
||||
|
||||
#[MapFrom('general_id')]
|
||||
#[MapTo('general_id')]
|
||||
public int $generalID;
|
||||
#[RawName('general_id')]
|
||||
public int $generalID,
|
||||
|
||||
#[MapFrom('nation_id')]
|
||||
#[MapTo('nation_id')]
|
||||
public int $nationID;
|
||||
#[RawName('nation_id')]
|
||||
public int $nationID,
|
||||
|
||||
#[MapFrom('nation_name')]
|
||||
#[MapTo('nation_name')]
|
||||
public string $nationName;
|
||||
#[RawName('nation_name')]
|
||||
public string $nationName,
|
||||
|
||||
#[MapFrom('general_name')]
|
||||
#[MapTo('general_name')]
|
||||
public string $generalName;
|
||||
#[RawName('general_name')]
|
||||
public string $generalName,
|
||||
|
||||
public string $text;
|
||||
public string $text,
|
||||
|
||||
public string $date;
|
||||
public string $date,
|
||||
) {
|
||||
}
|
||||
}
|
||||
|
||||
+11
-16
@@ -2,23 +2,18 @@
|
||||
|
||||
namespace sammo\DTO;
|
||||
|
||||
use Spatie\DataTransferObject\Attributes\CastWith;
|
||||
use Spatie\DataTransferObject\Attributes\Strict;
|
||||
use Spatie\DataTransferObject\DataTransferObject;
|
||||
use Spatie\DataTransferObject\Casters\ArrayCaster;
|
||||
|
||||
//https://json2dto.atymic.dev/
|
||||
|
||||
#[Strict]
|
||||
class VoteInfo extends DataTransferObject
|
||||
class VoteInfo extends DTO
|
||||
{
|
||||
public int $id;
|
||||
public string $title;
|
||||
public int $multipleOptions;
|
||||
public function __construct(
|
||||
public int $id,
|
||||
public string $title,
|
||||
public int $multipleOptions,
|
||||
|
||||
public string $startDate;
|
||||
public ?string $endDate;
|
||||
public string $startDate,
|
||||
public ?string $endDate,
|
||||
|
||||
/** @var string[] */
|
||||
public array $options;
|
||||
/** @var string[] */
|
||||
public array $options,
|
||||
) {
|
||||
}
|
||||
}
|
||||
|
||||
@@ -72,7 +72,7 @@ class OpenNationBetting extends \sammo\Event\Action
|
||||
aux: $nationRaw,
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
$bettingID = \sammo\Betting::genNextBettingID();
|
||||
Betting::openBetting(new BettingInfo(
|
||||
id: $bettingID,
|
||||
@@ -80,10 +80,12 @@ class OpenNationBetting extends \sammo\Event\Action
|
||||
name: "{$name} 예상",
|
||||
finished: false,
|
||||
selectCnt: $this->nationCnt,
|
||||
isExclusive: null,
|
||||
reqInheritancePoint: true,
|
||||
openYearMonth: $openYearMonth,
|
||||
closeYearMonth: $closeYearMonth,
|
||||
candidates: $candidates,
|
||||
winner: null,
|
||||
));
|
||||
|
||||
$db->insert('event', [
|
||||
|
||||
@@ -3996,7 +3996,7 @@ class GeneralAI
|
||||
if (!key_exists($chiefLevel, $this->chiefGenerals) && !key_exists($chiefLevel, $nextChiefs)) {
|
||||
$newChiefProb = 1;
|
||||
} else {
|
||||
$newChiefProb = $this->rng->nextBool(0.1);
|
||||
$newChiefProb = $this->rng->nextBool(0.1)?1:0;
|
||||
}
|
||||
|
||||
if ($newChiefProb < 1 && !$this->rng->nextBool($newChiefProb)) {
|
||||
|
||||
@@ -155,7 +155,7 @@
|
||||
<div class="col col-sm-6 f_tnum">
|
||||
<b-form-input
|
||||
:id="`buff-${buffKey}`"
|
||||
v-model="inheritBuff[buffKey]"
|
||||
v-model.number="inheritBuff[buffKey]"
|
||||
type="number"
|
||||
:min="prevInheritBuff[buffKey] ?? 0"
|
||||
:max="maxInheritBuff"
|
||||
|
||||
+6
-6
@@ -100,13 +100,13 @@
|
||||
<small class="text-muted">통/무/지</small>
|
||||
</div>
|
||||
<div class="col col-md-2 col-3 align-self-center">
|
||||
<input v-model="args.leadership" type="number" class="form-control" />
|
||||
<input v-model.number="args.leadership" type="number" class="form-control" />
|
||||
</div>
|
||||
<div class="col col-md-2 col-3 align-self-center">
|
||||
<input v-model="args.strength" type="number" class="form-control" />
|
||||
<input v-model.number="args.strength" type="number" class="form-control" />
|
||||
</div>
|
||||
<div class="col col-md-2 col-3 align-self-center">
|
||||
<input v-model="args.intel" type="number" class="form-control" />
|
||||
<input v-model.number="args.intel" type="number" class="form-control" />
|
||||
</div>
|
||||
</div>
|
||||
<div class="row" style="margin-top: 1em">
|
||||
@@ -144,10 +144,10 @@
|
||||
<div v-if="displayInherit" class="inherit-block">
|
||||
<div class="row">
|
||||
<div class="col">
|
||||
<NumberInputWithInfo v-model="inheritTotalPoint" title="보유한 유산 포인트" :readonly="true" />
|
||||
<NumberInputWithInfo v-model.number="inheritTotalPoint" title="보유한 유산 포인트" :readonly="true" />
|
||||
</div>
|
||||
<div class="col">
|
||||
<NumberInputWithInfo v-model="inheritRequiredPoint" title="필요 유산 포인트" :readonly="true" />
|
||||
<NumberInputWithInfo v-model.number="inheritRequiredPoint" title="필요 유산 포인트" :readonly="true" />
|
||||
</div>
|
||||
</div>
|
||||
<hr />
|
||||
@@ -174,7 +174,7 @@
|
||||
<div class="row">
|
||||
<div class="col col-6 a-right align-self-center">도시</div>
|
||||
<div class="col col-6 align-self-center">
|
||||
<select v-model="args.inheritCity" class="form-select form-inline" style="max-width: 20ch">
|
||||
<select v-model.number="args.inheritCity" class="form-select form-inline" style="max-width: 20ch">
|
||||
<option :value="undefined">사용안함</option>
|
||||
<option v-for="city in availableInheritCity" :key="city.id" :value="city.id">
|
||||
{{ `[${city.region}] ${city.name}` }}
|
||||
|
||||
@@ -39,7 +39,7 @@
|
||||
<div class="col-6 col-md-3 align-self-center">대상: {{ getTypeStr(pickedBetTypeKey) }}</div>
|
||||
<div class="col-4 col-md-2 d-grid">
|
||||
<!-- eslint-disable-next-line vue/max-attributes-per-line -->
|
||||
<b-form-input v-model="betPoint" class="d-grid" type="number" :min="10" :max="1000" :step="10" />
|
||||
<b-form-input v-model.number="betPoint" class="d-grid" type="number" :min="10" :max="1000" :step="10" />
|
||||
</div>
|
||||
<div class="col-2 col-md-1 d-grid">
|
||||
<b-button class="d-grid" @click="submitBet"> 베팅 </b-button>
|
||||
|
||||
@@ -34,7 +34,7 @@
|
||||
<div class="col mx-2">
|
||||
<div class="input-group my-0">
|
||||
<span class="input-group-text py-1">병력</span>
|
||||
<input v-model="amount" type="number" class="form-control py-1 f_tnum px-0 text-end" min="1" />
|
||||
<input v-model.number="amount" type="number" class="form-control py-1 f_tnum px-0 text-end" min="1" />
|
||||
<span class="input-group-text py-1 f_tnum">00명</span>
|
||||
<span
|
||||
class="input-group-text py-1 f_tnum"
|
||||
|
||||
@@ -112,7 +112,7 @@
|
||||
<div class="input-group my-0">
|
||||
<span class="input-group-text py-1">병력</span>
|
||||
<input
|
||||
v-model="amount"
|
||||
v-model.number="amount"
|
||||
type="number"
|
||||
class="form-control py-1 f_tnum px-0 text-end"
|
||||
min="1"
|
||||
|
||||
@@ -8,7 +8,7 @@
|
||||
</b-button>
|
||||
<b-button v-if="maxAmount > 200" class="btn-sm" @click="amount = Math.max(amount - 100, minAmount)"> -백 </b-button>
|
||||
<input
|
||||
v-model="amount"
|
||||
v-model.number="amount"
|
||||
type="number"
|
||||
class="form-control text-end"
|
||||
:max="maxAmount"
|
||||
|
||||
@@ -4,6 +4,7 @@ use sammo\DTO\Attr\Convert;
|
||||
use sammo\DTO\DTO;
|
||||
use sammo\DTO\Attr\JsonString;
|
||||
use sammo\DTO\Attr\NullIsUndefined;
|
||||
use sammo\DTO\Attr\RawName;
|
||||
use sammo\DTO\Converter\ArrayConverter;
|
||||
use sammo\DTO\Converter\Converter;
|
||||
use sammo\DTO\Converter\MapConverter;
|
||||
@@ -141,6 +142,19 @@ class TypeNestedMap extends DTO
|
||||
}
|
||||
}
|
||||
|
||||
class TypeRawName extends DTO
|
||||
{
|
||||
public function __construct(
|
||||
#[RawName('arg_name')]
|
||||
public int $argName,
|
||||
#[RawName('vID')]
|
||||
public int $vID,
|
||||
)
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
class DTOTest extends PHPUnit\Framework\TestCase
|
||||
{
|
||||
public function testBasic()
|
||||
@@ -326,4 +340,15 @@ class DTOTest extends PHPUnit\Framework\TestCase
|
||||
|
||||
$this->assertEquals($rawType, $testType);
|
||||
}
|
||||
|
||||
public function testRawName(){
|
||||
$rawType = [
|
||||
'arg_name' => 1,
|
||||
'vID' => 2,
|
||||
];
|
||||
$obj = TypeRawName::fromArray($rawType);
|
||||
$testType = $obj->toArray();
|
||||
|
||||
$this->assertEquals($rawType, $testType);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user