countFreeBuyTimes($goods); $goods['free_num'] = $goods['free_num'] - $count; } } $this->_rspData( array( 'goods_list' => $goodsList, ) ); } public function getShopNames() { $shopList = mt\Shop::all(); $this->_rspData( array( 'shop_name_list' => $shopList ? $shopList : array(), ) ); } public function buyGoodsNew() { $id = getReqVal('id', 0); $token_type = getReqVal('token_type', ''); $goods_num = getReqVal('goods_num', 0); $row = mt\ShopGoods::get($id); $desired_token_type = $row['token_type']; $check_token_type = splitStr1($desired_token_type); $token_pos = array_search($token_type, $check_token_type, true); if (!in_array($token_type, $check_token_type)) { $this->_rspErr(1, "token_type parameter error, desired_token_type: {$desired_token_type}"); return; } if ($goods_num > $row['max_amount']) { $this->_rspErr(1, "goods_num parameter error, max_amount: {$row['max_amount']}"); return; } // 这里命名混乱了, 购买个数,一捆个数命名冲突 $goods_count = $row['goods_num']; $buyRecordHash = ShopBuyRecord::allToHash(); $boughtTimes = 1; switch ($row['limit_type']) { case ShopController::DAILY_BUY_LIMIT: { $buyRecord = getXVal($buyRecordHash, $id); $boughtTimes = $buyRecord ? $buyRecord['this_day_buy_times'] + 1 : 1; if ($buyRecord && getXVal($buyRecord, 'this_day_buy_times', 0) >= $row['limit_num']) { $this->_rspErr(2, 'Has reached the maximum number of purchase restrictions today'); return; } if ($row['limit_num'] <= 0) { $this->_rspErr(2, 'The maximum number of purchase restrictions has been reached'); return; } } break; case ShopController::WEEKLY_BUY_LIMIT: { $buyRecord = getXVal($buyRecordHash, $id); $boughtTimes = $buyRecord ? $buyRecord['this_week_buy_times'] + 1 : 1; if ($buyRecord && getXVal($buyRecord, 'this_week_buy_times', 0) >= $row['limit_num']) { $this->_rspErr(2, 'The maximum number of purchase restrictions this week has been reached'); return; } if ($row['limit_num'] <= 0) { $this->_rspErr(2, 'The maximum number of purchase restrictions has been reached'); return; } } break; case ShopController::TOTAL_BUY_LIMIT: { $buyRecord = getXVal($buyRecordHash, $id); $boughtTimes = $buyRecord ? $buyRecord['total_buy_times'] + 1 : 1; if ($buyRecord && getXVal($buyRecord, 'total_buy_times', 0) >= $row['limit_num']) { $this->_rspErr(2, 'The maximum number of purchase restrictions has been reached'); return; } if ($row['limit_num'] <= 0) { $this->_rspErr(2, 'he maximum number of purchase restrictions has been reached'); return; } } break; default: { } break; } $price_array = splitStr1($row['price']); $discount_array = splitStr1($row['discount']); $need_price = $price_array[$token_pos]; $discount = $discount_array[$token_pos]; $discount_begin = strtotime($row['discount_begin'] . ' UTC'); $discount_end = strtotime($row['discount_end'] . ' UTC'); $nowTime = $this->_getNowTime(); if ($nowTime >= $discount_begin && $nowTime < $discount_end) { $need_price = ceil($need_price * ($discount / 100.0)); } $costItemId = $this->getCostItemIdByTokenType($token_type); switch ($token_type) { case ShopController::TOKEN_TYPE_CEG: case ShopController::TOKEN_TYPE_CEC: $costItems = $this->makeCostItems($costItemId, $goods_num * $need_price); $lackItem = null; if (!$this->_hasEnoughItems($costItems, $lackItem)) { $this->_rspErr(2, $this->_getLackItemErrMsg($lackItem)); return; } $itemMeta = mt\Item::get($row['goods_id']); $propertyChgService = new services\PropertyChgService(); for ($i = 0; $i < $goods_num; $i++) { $this->internalAddItem($propertyChgService, $itemMeta, $goods_count); } $awardService = new services\AwardService(); $awardService->addItem($row['goods_id'], $goods_num); ShopBuyRecord::add($id, $goods_num); $this->_decItems($costItems); $goodsDto = array( 'goods_id' => $id, 'item_id' => $row['goods_id'], 'price_info' => array( 'item_id' => $row['goods_id'], 'cost_list' => array(), 'discount_begin_time' => phpcommon\datetimeToTimestamp($row['discount_begin']), 'discount_end_time' => phpcommon\datetimeToTimestamp($row['discount_end']) ), 'flag_icon' => $row['tag'], 'limit_type' => $row['limit_type'], 'bought_times' => $boughtTimes, 'total_buy_times' => $row['limit_num'], ); { $priceInfo = mt\Item::getPriceInfo($itemMeta); if (!empty($priceInfo)) { $goodsDto['price_info'] = $priceInfo['price_info']; } } $propertyChgService->addUserChg(); $this->_rspData( array( 'award' => $awardService->toDto(), 'property_chg' => $propertyChgService->toDto(), 'goods_chg' => $goodsDto ) ); break; case ShopController::TOKEN_TYPE_BCEG: break; case ShopController::TOKEN_TYPE_USDT: case ShopController::TOKEN_TYPE_USDC: case ShopController::TOKEN_TYPE_BUSD: case ShopController::TOKEN_TYPE_MATIC: case ShopController::TOKEN_TYPE_BNB: default: $this->_rspErr(1, "token_type is unsupport, {$token_type}"); } } public function buyGoodsDirect() { // let repdata = { // account_id: string // order_id: string // status: string // id: string // txhash: string // } // 我返回给你这些数据和一个sign字段, // sign使用上面 repdata 按key 顺序排后, 组成key1=val1&key2=val2后, 使用hmac_sha256 hash, key是 iG4Rpsa)6U31$H#^T85$^^3 // PENDING = 0, // 初始状态 // TRANSFERING = 1, //只有国库模式才会有该状态 // TRANSFERED = 2, //只有国库模式才会有该状态 // SUCCESS = 9, // 成功的最终状态 // TRANSFER_FAIL = 98, // 转账错误 // FAIL = 99, // 也是错误 // $account_id = getReqVal('account_id', ''); $order_id = getReqVal('order_id', ''); $status = getReqVal('status', ''); $id = getReqVal('id', ''); $txhash = getReqVal('txhash', ''); $sign = getReqVal('sign', ''); $data = array( 'account_id' => $account_id, 'id' => $id, 'order_id' => $order_id, 'status' => $status, 'txhash' => $txhash, ); $hash_data = http_build_query($data); $signature = hash_hmac('sha256', $hash_data, 'iG4Rpsa)6U31$H#^T85$^^3'); if ($signature != $sign) { $this->_rspErr(1, "signature error, signature: {$signature}, sign: {$sign}"); return; } error_log("buyGoodsDirect-------" . $order_id . "---" . $status); $conn = myself()->_getMysql(''); $order = SqlHelper::selectOne($conn, 't_shop_buy_order', array('account_id', 'item_id', 'goods_num', 'status'), array('idx' => $order_id)); $id = $order['item_id']; $goods_num = $order['goods_num']; $o_status = $order['status']; if ($o_status != 0) { $this->_rspErr(1, "order status error, status: {$o_status}"); return; } $buyStatus = 0; // 1: 成功, 2: 失败 switch ($status) { case "9": $buyStatus = 1; break; case "99": case "98": $buyStatus = 2; break; } SqlHelper::update($conn, 't_shop_buy_order', array('idx' => $order_id), array('status' => $buyStatus)); $row = mt\ShopGoods::get($id); if ($row) { $desired_token_type = $row['token_type']; $check_token_type = splitStr1($desired_token_type); // $token_pos = array_search($token_type, $check_token_type, true); // if (!in_array($token_type, $check_token_type)) { // $this->_rspErr(1, "token_type parameter error, desired_token_type: {$desired_token_type}"); // return; // } if ($goods_num > $row['max_amount']) { $this->_rspErr(1, "goods_num parameter error, max_amount: {$row['max_amount']}"); return; } // 这里命名混乱了, 购买个数,一捆个数命名冲突 $goods_count = $row['goods_num']; $itemMeta = mt\Item::get($row['goods_id']); $propertyChgService = new services\PropertyChgService(); for ($i = 0; $i < $goods_num; $i++) { $this->internalAddItem($propertyChgService, $itemMeta, $goods_count); } $awardService = new services\AwardService(); $awardService->addItem($row['goods_id'], $goods_num); ShopBuyRecord::add($id, $goods_num); } $this->_rspOk(); } public function startGoodsDirect() { $id = getReqVal('id', 0); $token_type = getReqVal('token_type', ''); $goods_num = getReqVal('goods_num', 0); $conn = myself()->_getMysql(''); $chk = SqlHelper::insert( $conn, 't_shop_buy_order', array( 'address' => myself()->_getAddress(), 'createtime' => myself()->_getNowTime(), 'item_id' => $id, 'goods_num' => $goods_num, 'status' => 0, // 0-客户端申请了订单 1-订单完成 2-订单失败 ) ); if ($chk) { $lastId = $this->lastInsertId($conn); $this->_rspData( array( 'order_id' => $lastId, ) ); } else { $this->_rspErr(1, "insert error, id: {$id}, token_type: {$token_type}, goods_num: {$goods_num}"); } } public function statusGoodsDirect() { $order_id = getReqVal('order_id', ''); $conn = myself()->_getMysql(''); $row = SqlHelper::selectOne( $conn, 't_shop_buy_order', array('status'), array( 'idx' => $order_id, ) ); if ($row) { $this->_rspData( array( 'status' => $row['status'], ) ); } else { $this->_rspErr(1, "order_id not found, order_id: {$order_id}"); } } private function getCostItemIdByTokenType($token_type) { switch ($token_type) { case ShopController::TOKEN_TYPE_CEG: return V_ITEM_GOLD; break; case ShopController::TOKEN_TYPE_CEC: return V_ITEM_DIAMOND; break; case ShopController::TOKEN_TYPE_BCEG: case ShopController::TOKEN_TYPE_USDT: case ShopController::TOKEN_TYPE_USDC: case ShopController::TOKEN_TYPE_BUSD: case ShopController::TOKEN_TYPE_MATIC: case ShopController::TOKEN_TYPE_BNB: default: return -1; } } private function makeCostItems($item_id, $num) { $costItems = array( array( 'item_id' => $item_id, 'item_num' => $num ) ); return $costItems; } public function info() { $shopId = getReqVal('shop_id', 0); if ($shopId == mt\Shop::OUTSIDE_SHOP) { $this->getOutsideShopInfo(); return; } $goodsList = mt\ShopGoods::getGoodsList($shopId); if (!$goodsList) { $this->_rspData( array( 'info' => array( 'shop_id' => $shopId, 'goods_list1' => array(), 'goods_list2' => array(), ) ) ); return; } $buyRecordHash = ShopBuyRecord::allToHash(); $goodsDtoList1 = array(); $goodsDtoList2 = array(); foreach ($goodsList as $goods) { $itemMeta = mt\Item::get($goods['goods_id']); if ($itemMeta) { $goodsDto = array( 'goods_id' => $goods['goods_id'], 'item_id' => $itemMeta['id'], 'price_info' => null, 'flag_icon' => $goods['tag'], 'limit_type' => $itemMeta['limit_type'], 'bought_times' => 0, 'total_buy_times' => $itemMeta['limit_num'], ); switch ($itemMeta['limit_type']) { case mt\Item::DAILY_BUY_LIMIT: { $buyRecord = getXVal($buyRecordHash, $itemMeta['id']); $goodsDto['bought_times'] = $buyRecord ? $buyRecord['this_day_buy_times'] : 0; } break; case mt\Item::WEEKLY_BUY_LIMIT: { $buyRecord = getXVal($buyRecordHash, $itemMeta['id']); $goodsDto['bought_times'] = $buyRecord ? $buyRecord['this_week_buy_times'] : 0; } break; case mt\Item::TOTAL_BUY_LIMIT: { $buyRecord = getXVal($buyRecordHash, $itemMeta['id']); $goodsDto['bought_times'] = $buyRecord ? $buyRecord['total_buy_times'] : 0; } break; default: { } break; } $priceInfo = mt\Item::getPriceInfo($itemMeta); if (!empty($priceInfo)) { $goodsDto['price_info'] = $priceInfo['price_info']; array_push($goodsDtoList1, $goodsDto); } } } $this->_rspData( array( 'info' => array( 'shop_id' => $shopId, 'goods_list1' => $goodsDtoList1, 'goods_list2' => $goodsDtoList2, ) ) ); } public function buyGoods() { $shopId = getReqVal('shop_id', 0); $goodsId = getReqVal('goods_id', 0); $itemNum = getReqVal('goods_num', 0); $costItemId = getReqVal('cost_item_id', 0); $itemId = $goodsId; if ($shopId == mt\Shop::OUTSIDE_SHOP) { $this->outsideBuy($shopId, $itemId, $itemNum, $costItemId); return; } $propertyChgService = new services\PropertyChgService(); $itemMeta = mt\Item::get($itemId); if (!$itemMeta) { $this->_rspErr(1, 'goods_id parameter error'); return; } $goodsMeta = mt\ShopGoods::getGoodsInfo($shopId, $goodsId); if (!$goodsMeta) { $this->_rspErr(1, 'goods_id parameter error'); return; } $buyRecordHash = ShopBuyRecord::allToHash(); $boughtTimes = 1; switch ($itemMeta['limit_type']) { case mt\Item::DAILY_BUY_LIMIT: { $buyRecord = getXVal($buyRecordHash, $itemMeta['id']); $boughtTimes = $buyRecord ? $buyRecord['this_day_buy_times'] + 1 : 1; if ($buyRecord && getXVal($buyRecord, 'this_day_buy_times', 0) >= $itemMeta['limit_num']) { $this->_rspErr(2, 'Has reached the maximum number of purchase restrictions today'); return; } if ($itemMeta['limit_num'] <= 0) { $this->_rspErr(2, 'The maximum number of purchase restrictions has been reached'); return; } } break; case mt\Item::WEEKLY_BUY_LIMIT: { $buyRecord = getXVal($buyRecordHash, $itemMeta['id']); $boughtTimes = $buyRecord ? $buyRecord['this_week_buy_times'] + 1 : 1; if ($buyRecord && getXVal($buyRecord, 'this_week_buy_times', 0) >= $itemMeta['limit_num']) { $this->_rspErr(2, 'The maximum number of purchase restrictions this week has been reached'); return; } if ($itemMeta['limit_num'] <= 0) { $this->_rspErr(2, 'The maximum number of purchase restrictions has been reached'); return; } } break; case mt\Item::TOTAL_BUY_LIMIT: { $buyRecord = getXVal($buyRecordHash, $itemMeta['id']); $boughtTimes = $buyRecord ? $buyRecord['total_buy_times'] + 1 : 1; if ($buyRecord && getXVal($buyRecord, 'total_buy_times', 0) >= $itemMeta['limit_num']) { $this->_rspErr(2, 'The maximum number of purchase restrictions has been reached'); return; } if ($itemMeta['limit_num'] <= 0) { $this->_rspErr(2, 'he maximum number of purchase restrictions has been reached'); return; } } break; default: { } break; } { $errCode = 0; $errMsg = ''; if (!$this->canBuy($itemMeta, $errCode, $errMsg)) { $this->_rspErr($errCode, $errMsg); return; } } $priceInfo = mt\Item::getPriceInfo($itemMeta); if (empty($priceInfo)) { $this->_rspErr(2, 'config error'); return; } $costItems = $this->getCostItems($priceInfo, $costItemId); if (empty($costItems)) { $this->_rspErr(2, 'config error2'); return; } $lackItem = null; if (!$this->_hasEnoughItems($costItems, $lackItem)) { $this->_rspErr(3, $this->_getLackItemErrMsg($lackItem)); return; } $this->_decItems($costItems); $this->internalAddItem($propertyChgService, $itemMeta, 1); $awardService = new services\AwardService(); $awardService->addItem($itemId, $itemNum); ShopBuyRecord::add($itemId, $itemNum); $goodsDto = array( 'goods_id' => $itemMeta['id'], 'item_id' => $itemMeta['id'], 'price_info' => array( 'item_id' => $itemMeta['id'], 'cost_list' => array(), 'discount_begin_time' => phpcommon\datetimeToTimestamp($itemMeta['discount_begin']), 'discount_end_time' => phpcommon\datetimeToTimestamp($itemMeta['discount_end']) ), 'flag_icon' => $goodsMeta['tag'], 'limit_type' => $itemMeta['limit_type'], 'bought_times' => $boughtTimes, 'total_buy_times' => $itemMeta['limit_num'], ); { $priceInfo = mt\Item::getPriceInfo($itemMeta); if (!empty($priceInfo)) { $goodsDto['price_info'] = $priceInfo['price_info']; } } $propertyChgService->addUserChg(); $this->_rspData( array( 'award' => $awardService->toDto(), 'property_chg' => $propertyChgService->toDto(), 'goods_chg' => $goodsDto ) ); } public function getDiscountList() { $items = array(); { $types = array( mt\Item::HERO_TYPE, mt\Item::HERO_SKIN_TYPE, mt\Item::GUN_SKIN_TYPE ); mt\Item::filter(function ($meta) use (&$items, &$types) { if (mt\Item::inTypes($meta, $types)) { array_push($items, $meta); } return true; }); } $goodsDtoList = array(); array_walk($items, function ($val) use (&$priceList, &$goodsDtoList) { $goodsDto = array( 'item_id' => $val['id'], 'gold_discount' => 0, 'diamond_discount' => 0, ); $priceInfo = mt\Item::getPriceInfo($val); if (!empty($priceInfo)) { foreach ($priceInfo['price_info']['cost_list'] as $costGroup) { foreach ($costGroup as $cost) { if ($cost['discount'] > 0) { switch ($cost['item_id']) { case V_ITEM_GOLD: { $goodsDto['gold_discount'] = $cost['discount']; } break; case V_ITEM_DIAMOND: { $goodsDto['diamond_discount'] = $cost['discount']; } break; } } } } if ($goodsDto['gold_discount'] > 0 || $goodsDto['diamond_discount'] > 0) { array_push($goodsDtoList, $goodsDto); } } }); $this->_rspData( array( 'goods_list' => $goodsDtoList, ) ); } public function getPayMethods() { $token_type = getReqVal('token_type', 99); $payMethods = mt\PayMethod::getPayMethods($token_type); $this->_rspData( array( 'pay_methods' => $payMethods, ) ); } public function refreshDailySelection() { $address = $this->_getAccountId(); if (empty($address)) { $this->_rspErr(2, 'address is empty'); return; } $count = $this->countTodayRefreshTimes($address); if ($count >= 6) { $this->_rspErr(2, 'The maximum number of refreshes has been reached'); return; } $chk = $this->refreshDailySelectionWithMode($address, 1); if ($chk) { $this->_rspOk(); } } public function getDailySelectionList() { $address = $this->_getAccountId(); if (empty($address)) { $this->_rspErr(2, 'address is empty'); return; } // 不清除过期的每日精选可以避免跨日操作错误 // $chk = $this->clearBeforeTodayDailySelections(); $chk = $this->getTodayLastDailySelection($address); if (!$chk) { $chk = $this->refreshDailySelectionWithMode($address, 0); $chk = $this->getTodayLastDailySelection($address); } $selection = $chk[0]; $goodsList = array(); for ($i = 1; $i <= 6; $i++) { $goodsList[$i] = mt\Dailyselection::get($selection['grid_' . $i]); $goodsList[$i]['count'] = $selection['count_' . $i]; } $count = $this->countTodayRefreshTimes($address); $costs = mt\Parameter::getByName('daily_selection_refresh_cost'); $arrCosts = explode('|', $costs['param_value']); $cost = $arrCosts[$count]; $this->_rspData( array( 'idx' => $selection['idx'], 'cost' => $cost, 'goods_list' => $goodsList, ) ); } public function buyGoodsNormal() { $address = $this->_getAddress(); if (empty($address)) { $this->_rspErr(2, 'address is empty'); return; } $id = getReqVal('id', 0); $token_type = getReqVal('token_type', ''); $goods_num = getReqVal('goods_num', 0); $row = mt\ShopGoods::get($id); $goods_id = $row['goods_id']; $desired_token_type = $row['token_type']; $check_token_type = splitStr1($desired_token_type); $token_pos = array_search($token_type, $check_token_type, true); $isFreeBuy = false; if (!empty($row['free_type'])) { $count = $this->countFreeBuyTimes($row); if ($count < $row['free_num']) { $isFreeBuy = true; } else { $this->_rspErr(1, "free_num parameter error, free_num: {$row['free_num']}"); return; } } else if (!in_array($token_type, $check_token_type)) { $this->_rspErr(1, "token_type parameter error, desired_token_type: {$desired_token_type}"); return; } if ($goods_num > $row['max_amount']) { $this->_rspErr(1, "goods_num parameter error, max_amount: {$row['max_amount']}"); return; } // 这里命名混乱了, 购买个数,一捆个数命名冲突 $goods_count = $row['goods_num']; $buyRecordHash = ShopBuyRecord::allToHash(); $boughtTimes = 1; switch ($row['limit_type']) { case ShopController::DAILY_BUY_LIMIT: { $buyRecord = getXVal($buyRecordHash, $id); $boughtTimes = $buyRecord ? $buyRecord['this_day_buy_times'] + 1 : 1; if ($buyRecord && getXVal($buyRecord, 'this_day_buy_times', 0) >= $row['limit_num']) { $this->_rspErr(2, 'Has reached the maximum number of purchase restrictions today'); return; } if ($row['limit_num'] <= 0) { $this->_rspErr(2, 'The maximum number of purchase restrictions has been reached'); return; } } break; case ShopController::WEEKLY_BUY_LIMIT: { $buyRecord = getXVal($buyRecordHash, $id); $boughtTimes = $buyRecord ? $buyRecord['this_week_buy_times'] + 1 : 1; if ($buyRecord && getXVal($buyRecord, 'this_week_buy_times', 0) >= $row['limit_num']) { $this->_rspErr(2, 'The maximum number of purchase restrictions this week has been reached'); return; } if ($row['limit_num'] <= 0) { $this->_rspErr(2, 'The maximum number of purchase restrictions has been reached'); return; } } break; case ShopController::TOTAL_BUY_LIMIT: { $buyRecord = getXVal($buyRecordHash, $id); $boughtTimes = $buyRecord ? $buyRecord['total_buy_times'] + 1 : 1; if ($buyRecord && getXVal($buyRecord, 'total_buy_times', 0) >= $row['limit_num']) { $this->_rspErr(2, 'The maximum number of purchase restrictions has been reached'); return; } if ($row['limit_num'] <= 0) { $this->_rspErr(2, 'he maximum number of purchase restrictions has been reached'); return; } $sendingTimes = $this->countBuyGoodsRequestTimesByGoodsId($address, $row['goods_id']); if ($sendingTimes >= $row['limit_num']) { $this->_rspErr(2, 'The maximum number of purchase restrictions has been reached'); return; } } break; default: { } break; } $price_array = splitStr1($row['price']); $discount_array = splitStr1($row['discount']); $need_price = $price_array[$token_pos]; $discount = $discount_array[$token_pos]; $discount_begin = strtotime($row['discount_begin'] . ' UTC'); $discount_end = strtotime($row['discount_end'] . ' UTC'); $nowTime = $this->_getNowTime(); if ($nowTime >= $discount_begin && $nowTime < $discount_end) { $need_price = ceil($need_price * ($discount / 100.0)); } $costItemId = $this->getCostItemIdByTokenType($token_type); switch ($token_type) { case ShopController::TOKEN_TYPE_GOLD: $costItems = $this->makeCostItems($costItemId, $goods_num * $need_price); $lackItem = null; if (!$this->_hasEnoughItems($costItems, $lackItem)) { $this->_rspErr(2, $this->_getLackItemErrMsg($lackItem)); return; } $itemMeta = mt\Item::get($row['goods_id']); $propertyChgService = new services\PropertyChgService(); for ($i = 0; $i < $goods_num; $i++) { $this->internalAddItem($propertyChgService, $itemMeta, $goods_count); } $awardService = new services\AwardService(); $awardService->addItem($row['goods_id'], $goods_num); ShopBuyRecord::add($id, $goods_num); $this->_decItems($costItems); $goodsDto = array( 'goods_id' => $id, 'item_id' => $row['goods_id'], 'price_info' => array( 'item_id' => $row['goods_id'], 'cost_list' => array(), 'discount_begin_time' => phpcommon\datetimeToTimestamp($row['discount_begin']), 'discount_end_time' => phpcommon\datetimeToTimestamp($row['discount_end']) ), 'flag_icon' => $row['tag'], 'limit_type' => $row['limit_type'], 'bought_times' => $boughtTimes, 'total_buy_times' => $row['limit_num'], ); { $priceInfo = mt\Item::getPriceInfo($itemMeta); if (!empty($priceInfo)) { $goodsDto['price_info'] = $priceInfo['price_info']; } } $propertyChgService->addUserChg(); $this->_rspData( array( 'award' => $awardService->toDto(), 'property_chg' => $propertyChgService->toDto(), 'goods_chg' => $goodsDto ) ); break; case ShopController::TOKEN_TYPE_CEG: case ShopController::TOKEN_TYPE_CEC: if ($isFreeBuy) { $propertyChgService = new services\PropertyChgService(); $this->addFreeBuyRecord($row); $itemMeta = mt\Item::get($row['goods_id']); $this->internalAddItem($propertyChgService, $itemMeta, $goods_count); $this->_rspOk(); } else { $price = $this->normalizeWeb3Price($goods_num * $need_price); $item_id = $row['goods_id']; $item_count = $goods_num; $response = services\BlockChainService::gameItemMallBuy( Transaction::BUY_GOODS_ACTION_TYPE, $price, $item_id, $item_count ); BcOrder::upsert($response['trans_id'], array( 'item_id' => $item_id, 'item_num' => $item_count, 'order_type' => 1, 'ext_data' => json_encode(array( 'mode' => SHOP_BUY_MODE_NORMAL, )), )); $response['item_id'] = $item_id; $response['item_num'] = $item_count; $this->_rspData( array( "block_chain" => $response ) ); } break; case ShopController::TOKEN_TYPE_BCEG: break; case ShopController::TOKEN_TYPE_USDT: case ShopController::TOKEN_TYPE_USDC: case ShopController::TOKEN_TYPE_BUSD: case ShopController::TOKEN_TYPE_MATIC: case ShopController::TOKEN_TYPE_BNB: default: $this->_rspErr(1, "token_type is unsupport, {$token_type}"); } } public function buyGoodsDS() { $account = $this->_getAccountId(); $address = $this->_getAddress(); if (empty($address)) { $this->_rspErr(2, 'address is empty'); return; } $idx = getReqVal('idx', 0); $grid = getReqVal('grid', 0); $count = getReqVal('count', 0); $conn = $this->_getMysql(''); $row = SqlHelper::selectOne( $conn, 't_shop_dailyselection', array( 'idx', 'address', 'grid_' . $grid, 'count_' . $grid, ), array('idx' => $idx) ); if (!$row) { $this->_rspErr(2, 'idx is invalid'); return; } if ($row['address'] != $account) { $this->_rspErr(2, 'address is invalid'); return; } if ($row['grid_' . $grid] == 0) { $this->_rspErr(2, 'grid is invalid'); return; } if ($row['count_' . $grid] < $count) { $this->_rspErr(2, 'count is invalid'); return; } $sel_id = $row['grid_' . $grid]; $goods = mt\Dailyselection::get($sel_id); $price = $this->normalizeWeb3Price($goods['price'] * $count); $item_id = $goods['goods_id']; $item_count = $goods['goods_num'] * $count; $response = services\BlockChainService::gameItemMallBuy( Transaction::BUY_GOODS_ACTION_TYPE, $price, $item_id, $item_count ); BcOrder::upsert($response['trans_id'], array( 'item_id' => $item_id, 'item_num' => $item_count, 'order_type' => 1, 'ext_data' => json_encode(array( 'mode' => SHOP_BUY_MODE_DAILY_SELECTION, 'idx' => $idx, 'grid' => $grid, 'count' => $count, )), )); $this->_rspData( array( 'idx' => $idx, 'grid' => $grid, 'count' => $count, 'block_chain' => $response, ) ); } private function buyGoodsFree() { } private function decDailySelectionItem($idx, $grid, $count) { $self = myself(); if (!$self) return false; $conn = $self->_getMysql(''); $sql = "SELECT count_$grid FROM t_shop_dailyselection WHERE idx = $idx"; $chk = $conn->execQuery($sql); if (!$chk) return false; if ($chk[0]['count_' . $grid] < $count) return false; $sql = "UPDATE t_shop_dailyselection SET count_$grid = count_$grid - $count WHERE idx = $idx"; $chk = $conn->execScript($sql); return $chk; } private function clearBeforeTodayDailySelections() { $self = myself(); if (!$self) return; $conn = $self->_getMysql(''); $nowTime = $this->_getNowTime(); $dayTime = $this->_getDaySeconds($nowTime); $sql = "DELETE FROM t_shop_dailyselection WHERE refresh_time < $dayTime"; $chk = $conn->execScript($sql); return $chk; } private function countTodayRefreshTimes($address) { $self = myself(); if (!$self) return; $conn = $self->_getMysql(''); $nowTime = $this->_getNowTime(); $dayTime = $this->_getDaySeconds($nowTime); $sql = "SELECT COUNT(*) AS cnt FROM t_shop_dailyselection WHERE address = '$address' AND refresh_mode = 1 AND refresh_time >= $dayTime"; $row = $conn->execQuery($sql); return $row[0]['cnt']; } private function getTodayLastDailySelection($address) { $self = myself(); if (!$self) return; $conn = $self->_getMysql(''); $nowTime = $this->_getNowTime(); $dayTime = $this->_getDaySeconds($nowTime); $sql = "SELECT * FROM t_shop_dailyselection WHERE address = '$address' AND refresh_time >= $dayTime ORDER BY idx DESC LIMIT 1"; $row = $conn->execQuery($sql); return $row; } private function refreshDailySelectionWithMode($address, $mode) { $selection = $this->randomNewDailySelection(); $self = myself(); if (!$self) return; $conn = $self->_getMysql(''); $nowTime = $this->_getNowTime(); $chk = SqlHelper::insert( $conn, 't_shop_dailyselection', array( 'address' => $address, 'refresh_mode' => $mode, 'refresh_time' => $nowTime, 'grid_1' => $selection[1]['id'], 'grid_2' => $selection[2]['id'], 'grid_3' => $selection[3]['id'], 'grid_4' => $selection[4]['id'], 'grid_5' => $selection[5]['id'], 'grid_6' => $selection[6]['id'], 'count_1' => 1, 'count_2' => 1, 'count_3' => 1, 'count_4' => 1, 'count_5' => 1, 'count_6' => 1, ) ); return $chk; } private function randomNewDailySelection() { $newDailySelection = array(); for ($i = 1; $i <= 6; $i++) { $store = mt\Dailyselection::getBySlot($i); $newDailySelection[$i] = $this->weighted_random($store); } return $newDailySelection; } private function weighted_random($array) { // 计算数组元素的总权重 $total_weight = array_sum(array_column($array, "weight")); // 生成一个随机数 $rand = mt_rand(1, $total_weight); // 遍历数组,找到随机数对应的元素 foreach ($array as $item) { // 如果随机数小于或等于当前元素的权重,返回该元素 if ($rand <= $item["weight"]) { return $item; } // 否则,减去当前元素的权重,继续循环 $rand -= $item["weight"]; } } private function countBuyGoodsRequestTimesByGoodsId($address, $goodsId) { $self = myself(); if (!$self) return; $conn = $self->_getMysql(''); $sql = "SELECT COUNT(*) AS cnt FROM t_bc_order WHERE address = '$address' AND item_id = '$goodsId'"; $row = $conn->execQuery($sql); return $row[0]['cnt']; } private function outsideBuy($shopId, $itemId, $itemNum, $costItemId) { $propertyChgService = new services\PropertyChgService(); $itemMeta = mt\Item::get($itemId); if (!$itemMeta) { $this->_rspErr(1, 'item_id parameter error'); return; } if ($itemNum != 1) { $this->_rspErr(1, 'the item_num parameter must be equal to 1'); return; } $costItemMeta = mt\Item::get($costItemId); if (!$costItemMeta) { $this->_rspErr(1, 'cost_item_id parameter error'); return; } $types = array( mt\Item::HERO_TYPE, mt\Item::HERO_SKIN_TYPE, mt\Item::GUN_SKIN_TYPE ); if (!mt\Item::inTypes($itemMeta, $types)) { $this->_rspErr(1, 'item_id parameter error'); return; } { $errCode = 0; $errMsg = ''; if (!$this->canBuy($itemMeta, $errCode, $errMsg)) { $this->_rspErr($errCode, $errMsg); return; } } $priceInfo = mt\Item::getPriceInfo($itemMeta); if (empty($priceInfo)) { $this->_rspErr(2, 'config error'); return; } $costItems = $this->getCostItems($priceInfo, $costItemId); if (empty($costItems)) { $this->_rspErr(2, 'config error2'); return; } $lackItem = null; if (!$this->_hasEnoughItems($costItems, $lackItem)) { $this->_rspErr(3, $this->_getLackItemErrMsg($lackItem)); return; } $this->_decItems($costItems); $this->internalAddItem($propertyChgService, $itemMeta, 1); $awardService = new services\AwardService(); $awardService->addItem($itemId, $itemNum); ShopBuyRecord::add($itemId, $itemNum); $goodsDto = array( 'goods_id' => $itemMeta['id'], 'item_id' => $itemMeta['id'], 'price_info' => array( 'item_id' => $itemMeta['id'], 'cost_list' => array(), 'discount_begin_time' => phpcommon\datetimeToTimestamp($itemMeta['discount_begin']), 'discount_end_time' => phpcommon\datetimeToTimestamp($itemMeta['discount_end']) ), 'flag_icon' => '', 'limit_type' => $itemMeta['limit_type'], 'bought_times' => 0, 'total_buy_times' => $itemMeta['limit_num'], ); { $priceInfo = mt\Item::getPriceInfo($itemMeta); if (!empty($priceInfo)) { $goodsDto['price_info'] = $priceInfo; } } $propertyChgService->addUserChg(); $this->_rspData( array( 'award' => $awardService->toDto(), 'property_chg' => $propertyChgService->toDto(), 'goods_chg' => $goodsDto ) ); } private function getOutsideShopInfo() { $items = array(); { $types = array( mt\Item::HERO_TYPE, mt\Item::HERO_SKIN_TYPE, mt\Item::GUN_SKIN_TYPE ); mt\Item::filter(function ($meta) use (&$items, &$types) { if (mt\Item::inTypes($meta, $types)) { array_push($items, $meta); } return true; }); } $goodsDtoList1 = array(); $goodsDtoList2 = array(); array_walk($items, function ($val) use (&$priceList, &$goodsDtoList1, &$goodsDtoList2) { $goodsDto = array( 'goods_id' => $val['id'], 'item_id' => $val['id'], 'price_info' => null, 'flag_icon' => '', 'limit_type' => $val['limit_type'], 'bought_times' => 0, 'total_buy_times' => $val['limit_num'], ); $priceInfo = mt\Item::getPriceInfo($val); if (!empty($priceInfo)) { $goodsDto['price_info'] = $priceInfo['price_info']; array_push($goodsDtoList1, $goodsDto); } }); $this->_rspData( array( 'info' => array( 'shop_id' => mt\Shop::OUTSIDE_SHOP, 'goods_list1' => $goodsDtoList1, 'goods_list2' => $goodsDtoList2, ) ) ); } private function getCostItems($priceInfo, $costItemId) { $costGroup = null; array_walk($priceInfo['price_info']['cost_list'], function ($val) use (&$costGroup, $costItemId) { if ($costGroup) { return; } if (count($val) > 0 && $val[0]['item_id'] == $costItemId) { $costGroup = $val; return; } }); if (!$costGroup) { return null; } $costItems = array(); array_walk($costGroup, function ($val) use (&$costItems, $priceInfo) { if ( $val['discount'] > 0 && $this->_getNowTime() >= $priceInfo['discount_begin_time'] && $this->_getNowTime() <= $priceInfo['discount_end_time'] ) { array_push( $costItems, array( 'item_id' => $val['item_id'], 'item_num' => (int) ($val['item_num'] * ($priceInfo['discount'] / 100)), ) ); } else { array_push( $costItems, array( 'item_id' => $val['item_id'], 'item_num' => $val['item_num'], ) ); } }); return $costItems; } private function internalAddItem($propertyChgService, $itemMeta, $count) { switch ($itemMeta['type']) { case mt\Item::HERO_TYPE: { Hero::addHero($itemMeta); $propertyChgService->addHeroChg(); $propertyChgService->addUserChg(); } break; case mt\Item::HERO_SKIN_TYPE: { HeroSkin::addSkin($itemMeta); $propertyChgService->addHeroSkinChg(); } break; case mt\Item::GUN_TYPE: { Gun::addGun($itemMeta); $propertyChgService->addGunChg(); } break; case mt\Item::GUN_SKIN_TYPE: { GunSkin::addSkin($itemMeta); $propertyChgService->addGunSkinChg(); } break; case mt\Item::CHIP_TYPE: { Chip::addChip($itemMeta); $propertyChgService->addChip(); } break; default: { if ($this->_isVirtualItem($itemMeta['id'])) { $this->_addVirtualItem($itemMeta['id'], $count, null, $propertyChgService); $propertyChgService->addUserChg(); // 充值就尝试开启首充活动 if ($itemMeta['id'] == 10001) { $this->beginFirstTupop(); } } else { Bag::addItem($itemMeta['id'], $count); $propertyChgService->addBagChg(); } } break; } } private function canBuy($itemMeta, &$errCode, &$errMsg) { $errCode = 0; $errMsg = ''; switch ($itemMeta['type']) { case mt\Item::HERO_TYPE: { $heroDb = Hero::find($itemMeta['id']); if ($heroDb) { $errCode = 10; $errMsg = 'You already have the hero'; return false; } } break; case mt\Item::HERO_SKIN_TYPE: { $heroSkinDb = HeroSkin::find($itemMeta['id']); if ($heroSkinDb) { $errCode = 10; $errMsg = 'You already have the skin'; return false; } } break; case mt\Item::GUN_SKIN_TYPE: { $gunSkinDb = GunSkin::find($itemMeta['id']); if ($gunSkinDb) { $errCode = 10; $errMsg = 'You already have the skin'; return false; } } break; default: { return true; } break; } return true; } private function beginFirstTupop() { $conn = myself()->_getMysql(''); $exist = SqlHelper::selectOne( $conn, 't_first_topup', array('address'), array('address' => myself()->_getAddress()) ); if ($exist) { return; } // 开始首充奖励活动进程 $chk = SqlHelper::insert( $conn, 't_first_topup', array( 'address' => myself()->_getAddress(), 'createtime' => myself()->_getNowTime(), 'status1' => 0, 'status2' => 0, 'status3' => 0, ) ); if (!$chk) { return; } } private function lastInsertId($conn) { $row = $conn->execQueryOne('SELECT LAST_INSERT_ID() as lastId;', array()); return $row['lastId']; } private function normalizeWeb3Price($price) { $bn1 = phpcommon\bnInit($price * pow(10, 8)); $bn2 = phpcommon\bnInit('1000000000000000000'); $price = phpcommon\bnDiv(phpcommon\bnMul($bn1, $bn2), pow(10, 8)); return $price; } private function countFreeBuyTimes($goods) { $conn = myself()->_getMysql(''); $account = myself()->_getAccountId(); switch($goods['free_type']) { case 1: { $dayTime = myself()->_getNowDaySeconds(); $sql = 'SELECT COUNT(*) as cnt FROM t_shop_free_record WHERE account_id = ? AND `id` = ? AND goods_id = ? AND createtime >= ?'; $row = $conn->execQueryOne($sql, array($account, $goods['id'], $goods['goods_id'], $dayTime)); return $row['cnt']; } break; } return 0; } private function addFreeBuyRecord($goods) { $conn = myself()->_getMysql(''); $account = myself()->_getAccountId(); switch($goods['free_type']) { case 1: { $dayTime = myself()->_getNowTime(); SqlHelper::insert( $conn, 't_shop_free_record', array( 'account_id' => $account, 'shop_id' => $goods['shop_id'], 'id' => $goods['id'], 'goods_id' => $goods['goods_id'], 'goods_num' => $goods['goods_num'], 'free_type' => $goods['free_type'], 'free_num' => $goods['free_num'], 'createtime' => $dayTime, ) ); } break; } } }