277 lines
8.0 KiB
PHP
277 lines
8.0 KiB
PHP
<?php
|
|
|
|
namespace models;
|
|
|
|
require_once('mt/Item.php');
|
|
require_once('mt/ChipQuality.php');
|
|
require_once('mt/AttrHelper.php');
|
|
require_once('services/NftService.php');
|
|
|
|
use mt;
|
|
use phpcommon\SqlHelper;
|
|
use services\NftService;
|
|
|
|
class Bag extends BaseModel {
|
|
|
|
public static function find($itemId)
|
|
{
|
|
$row = SqlHelper::ormSelectOne(
|
|
myself()->_getSelfMysql(),
|
|
't_bag',
|
|
array(
|
|
'account_id' => myself()->_getAccountId(),
|
|
'item_id' => $itemId,
|
|
)
|
|
);
|
|
if ($row) {
|
|
$row['item_uniid'] = $row['idx'];
|
|
if ($row['account_id'] != myself()->_getAccountId()) {
|
|
if (!NftService::isEquipOwner(myself()->_getOpenId(), $row['token_id'])) {
|
|
$row = null;
|
|
}
|
|
}
|
|
}
|
|
return $row;
|
|
}
|
|
|
|
public static function findByUniId($itemUniId)
|
|
{
|
|
$row = SqlHelper::ormSelectOne(
|
|
myself()->_getSelfMysql(),
|
|
't_bag',
|
|
array(
|
|
'account_id' => myself()->_getAccountId(),
|
|
'idx' => $itemUniId,
|
|
)
|
|
);
|
|
if ($row) {
|
|
$row['item_uniid'] = $row['idx'];
|
|
}
|
|
return $row;
|
|
}
|
|
|
|
public static function findByType($type, $subType = null)
|
|
{
|
|
foreach (self::all() as $itemDto) {
|
|
if ($itemDto['item_num'] > 0) {
|
|
$itemMeta = mt\Item::get($itemDto['item_id']);
|
|
if ($itemMeta['type'] == $type) {
|
|
if (is_null($subType)) {
|
|
return $itemDto;
|
|
} else {
|
|
if ($itemMeta['sub_type'] == $subType) {
|
|
return $itemDto;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
return null;
|
|
}
|
|
|
|
public static function toDto($row)
|
|
{
|
|
$todayGetGold = $row['today_get_gold'];
|
|
$lastGetGoldTime = $row['last_get_gold_time'];
|
|
if (myself()->_getDaySeconds($lastGetGoldTime) > myself()->_getNowDaySeconds()) {
|
|
$todayGetGold = 0;
|
|
}
|
|
return array(
|
|
'item_uniid' => $row['idx'],
|
|
'token_id' => $row['token_id'],
|
|
'item_id' => $row['item_id'],
|
|
'item_num' => $row['item_num'],
|
|
'attr' => emptyReplace(json_decode($row['rand_attr'], true), array()),
|
|
'today_get_gold' => $todayGetGold,
|
|
'last_get_gold_time' => $lastGetGoldTime,
|
|
);
|
|
}
|
|
|
|
public static function all()
|
|
{
|
|
$itemList = array();
|
|
self::getItemList(function ($row) use(&$itemList) {
|
|
if ($row['item_num'] > 0) {
|
|
array_push($itemList, $row);
|
|
}
|
|
});
|
|
return $itemList;
|
|
}
|
|
|
|
public static function getItemList($cb)
|
|
{
|
|
SqlHelper::ormSelect(
|
|
myself()->_getSelfMysql(),
|
|
't_bag',
|
|
array(
|
|
'account_id' => myself()->_getAccountId()
|
|
),
|
|
function ($row) use($cb) {
|
|
$cb($row);
|
|
}
|
|
);
|
|
foreach (NftService::getChips(myself()->_getOpenId()) as $nftDb) {
|
|
$row = SqlHelper::ormSelectOne(
|
|
myself()->_getSelfMysql(),
|
|
't_bag',
|
|
array(
|
|
'token_id' => $nftDb['token_id'],
|
|
)
|
|
);
|
|
if (!$row) {
|
|
$itemMeta = mt\Item::get($nftDb['item_id']);
|
|
if ($itemMeta) {
|
|
self::addNftItem($nftDb['item_id'], $nftDb['token_id']);
|
|
$row = SqlHelper::ormSelectOne(
|
|
myself()->_getSelfMysql(),
|
|
't_bag',
|
|
array(
|
|
'token_id' => $nftDb['token_id'],
|
|
)
|
|
);
|
|
}
|
|
}
|
|
if ($row) {
|
|
$cb($row);
|
|
}
|
|
}
|
|
}
|
|
|
|
public static function getAttrs()
|
|
{
|
|
$items = self::all();
|
|
$attrs = array();
|
|
foreach ($items as $item) {
|
|
$dbAttrs = emptyReplace(json_decode($item['rand_attr'], true), array());
|
|
mt\AttrHelper::mergeAttr($attrs, $dbAttrs);
|
|
}
|
|
return $attrs;
|
|
}
|
|
|
|
public static function getItemCount($itemId)
|
|
{
|
|
$itemDb = self::find($itemId);
|
|
return $itemDb ? $itemDb['item_num'] : 0;
|
|
}
|
|
|
|
public static function addItem($itemId, $itemNum)
|
|
{
|
|
return self::internalAddItem(
|
|
myself()->_getSelfMysql(),
|
|
$itemId,
|
|
$itemNum,
|
|
myself()->_getAccountId(),
|
|
null);
|
|
}
|
|
|
|
public static function addNftItem($itemId, $tokenId)
|
|
{
|
|
return self::internalAddItem(
|
|
myself()->_getMysql($tokenId),
|
|
$itemId,
|
|
1,
|
|
null,
|
|
$tokenId);
|
|
}
|
|
|
|
public static function internalAddItem($conn, $itemId, $itemNum, $accountId, $tokenId)
|
|
{
|
|
if (myself()->_isVirtualItem($itemId)) {
|
|
return;
|
|
}
|
|
if ($itemNum <= 0) {
|
|
return;
|
|
}
|
|
$itemMeta = mt\Item::get($itemId);
|
|
if (!$itemMeta) {
|
|
return;
|
|
}
|
|
if ($itemMeta['cannot_stack']) {
|
|
$randAttr = array();
|
|
if (mt\Item::isRandAttrItem($itemMeta)) {
|
|
$qualityMeta = mt\ChipQuality::getByQuality($itemMeta['quality']);
|
|
if ($qualityMeta) {
|
|
$randAttr = mt\ChipQuality::getRandAttr($qualityMeta);
|
|
}
|
|
}
|
|
$fieldsKv = array(
|
|
'item_id' => $itemId,
|
|
'item_num' => 1,
|
|
'rand_attr' => json_encode($randAttr),
|
|
'createtime' => myself()->_getNowTime(),
|
|
'modifytime' => myself()->_getNowTime()
|
|
);
|
|
if ($accountId) {
|
|
$fieldsKv['account_id'] = $accountId;
|
|
}
|
|
if ($tokenId) {
|
|
$fieldsKv['token_id'] = $tokenId;
|
|
}
|
|
|
|
SqlHelper::insert
|
|
($conn,
|
|
't_bag',
|
|
$fieldsKv
|
|
);
|
|
} else if ($accountId){
|
|
SqlHelper::upsert
|
|
($conn,
|
|
't_bag',
|
|
array(
|
|
'account_id' => $accountId,
|
|
'item_id' => $itemId
|
|
),
|
|
array(
|
|
'item_num' => function () use($itemNum) { return "item_num + {$itemNum}";},
|
|
'modifytime' => myself()->_getNowTime(),
|
|
),
|
|
array(
|
|
'account_id' => $accountId,
|
|
'item_id' => $itemId,
|
|
'item_num' => $itemNum,
|
|
'createtime' => myself()->_getNowTime(),
|
|
'modifytime' => myself()->_getNowTime()
|
|
)
|
|
);
|
|
}
|
|
}
|
|
|
|
public static function decItem($itemId, $itemNum)
|
|
{
|
|
SqlHelper::update
|
|
(myself()->_getSelfMysql(),
|
|
't_bag',
|
|
array(
|
|
'account_id' => myself()->_getAccountId(),
|
|
'item_id' => $itemId,
|
|
),
|
|
array(
|
|
'item_num' => function () use($itemNum) {
|
|
return "GREATEST(0, item_num - ${itemNum})";
|
|
},
|
|
'modifytime' => myself()->_getNowTime(),
|
|
)
|
|
);
|
|
}
|
|
|
|
public static function decItemByUnIid($itemUniId, $itemNum)
|
|
{
|
|
SqlHelper::update
|
|
(myself()->_getSelfMysql(),
|
|
't_bag',
|
|
array(
|
|
'account_id' => myself()->_getAccountId(),
|
|
'idx' => $itemUniId,
|
|
),
|
|
array(
|
|
'item_num' => function () use($itemNum) {
|
|
return "GREATEST(0, item_num - ${itemNum})";
|
|
},
|
|
'modifytime' => myself()->_getNowTime(),
|
|
)
|
|
);
|
|
}
|
|
|
|
|
|
}
|