game2006api/webapp/controller/MallController.class.php
aozhiwei aa36eea861 1
2023-08-07 12:31:26 +08:00

253 lines
6.6 KiB
PHP

<?php
require_once('mt/Item.php');
require_once('mt/Parameter.php');
require_once('models/BcOrder.php');
require_once('services/BlockChainService.php');
require_once('services/LogService.php');
use models\BcOrder;
use phpcommon\SqlHelper;
use models\Transaction;
use services\LogService;
use services\BlockChainService;
class MallController extends BaseAuthedController {
public function productList()
{
$page = getReqVal('page', 1);
}
public function sell()
{
$address = $this->_getAddress();
if (!$address) {
$this->_rspErr(1, 'address not found');
return;
}
$item_id = getReqVal('item_id', '');
if ($item_id != V_ITEM_GOLD) {
$this->_rspErr(1, 'only support gold');
return;
}
$itemMeta = mt\Item::get($item_id);
if (!$itemMeta) {
$this->_rspErr(1, 'item_id not found');
return;
}
$s_price = getReqVal('s_price', '');
if (empty($s_price)) {
$this->_rspErr(1, 's_price not found');
return;
}
if (!is_numeric($s_price)) {
$this->_rspErr(1, 's_price must be number');
return;
}
if ($s_price <= 0) {
$this->_rspErr(1, 's_price must > 0');
return;
}
$amount = getReqVal('amount', 1);
if (empty($amount)) {
$this->_rspErr(1, 'amount not found');
return;
}
if (!is_numeric($amount)) {
$this->_rspErr(1, 'amount must be number');
return;
}
if ($amount <= 0) {
$this->_rspErr(1, 'amount must > 0');
return;
}
$conn = myself()->_getSelfMysql();
// 检查是否有足够的物品
$costItems = $this->makeCostItems($item_id, $amount);
$lackItem = null;
if (!$this->_hasEnoughItems($costItems, $lackItem)) {
$this->_rspErr(2, $this->_getLackItemErrMsg($lackItem));
return;
}
$this->_decItems($costItems);
{
//埋点
$event = [
'name' => LogService::MARKET_SELL_GOLD,
'val' => $amount
];
LogService::consumeGold($event);
}
$c_name = $itemMeta['name'];
$c_job = 0;
$c_lv = 0;
$c_quality = $itemMeta['quality'];
$c_durability = 0;
$c_type = 0;
$c_id = $item_id;
$r = SqlHelper::insert(
$conn,
't_market_store',
array(
'token_id' => '',
'item_id' => $item_id,
'status' => 0,
'owner_address' => $address,
'token_type' => 0,
'amount' => $amount,
'createtime' => myself()->_getNowTime(),
'modifytime' => myself()->_getNowTime(),
's_price' => $s_price,
'c_name' => $c_name,
'c_job' => $c_job,
'c_lv' => $c_lv,
'c_quality' => $c_quality,
'c_durability' => $c_durability,
'c_type' => $c_type,
'c_id' => $c_id,
)
);
if (!$r) {
$this->_rspErr(3, "sell failed");
return;
}
$lastId = $this->lastInsertId($conn);
$order_id = $this->genOrderId($lastId);
$test = SqlHelper::update($conn, 't_market_store', array('idx' => $lastId), array('order_id' => $order_id));
if (!$test) {
$this->_rspErr(6, "sell failed");
return;
}
$this->_rspOk();
}
public function cancel()
{
$idx = getReqVal('idx', '');
$address = $this->_getAddress();
if (!$address) {
$this->_rspErr(1, 'address not found');
return;
}
$goods = $this->getGoodsByIdx($idx);
if (!$goods) {
$this->_rspErr(1, 'goods not found, idx:' . $idx);
return;
}
if ($goods['owner_address'] != $address) {
$this->_rspErr(1, 'not your goods, idx:' . $idx);
return;
}
$conn = $this->_getSelfMysql();
$r = SqlHelper::update(
$conn,
't_market_store',
array(
'idx' => $idx,
),
array(
'status' => 1,
'modifytime' => $this->_getNowTime(),
)
);
if ($r) {
$items = array(
array(
'item_id' => $goods['item_id'],
'item_num' => $goods['amount'],
)
);
$awardService = new services\AwardService();
$propertyChgService = new services\PropertyChgService();
$this->_addItems($items, $awardService, $propertyChgService);
{
//埋点
$event = [
'name' => LogService::MARKET_CANCEL_SELL_GOLD,
'val' => $goods['amount']
];
LogService::productGold($event);
}
$this->_rspData(
array(
'idx' => $idx,
'property_chg' => $propertyChgService->toDto(),
)
);
} else {
$this->_rspErr(1, 'cancel failed');
}
}
public function modifyPrice()
{
$idx = getReqVal('idx', '');
$s_price = getReqVal('s_price', '');
if (empty($s_price)) {
$this->_rspErr(1, 's_price not found');
return;
}
if (!is_numeric($s_price)) {
$this->_rspErr(1, 's_price must be number');
return;
}
$address = $this->_getAddress();
if (!$address) {
$this->_rspErr(1, 'address not found');
return;
}
$goods = $this->getGoodsByIdx($idx);
if (!$goods) {
$this->_rspErr(1, 'goods not found, idx:' . $idx);
return;
}
if ($goods['owner_address'] != $address) {
$this->_rspErr(1, 'not your goods, idx:' . $idx);
return;
}
$conn = $this->_getSelfMysql();
$r = SqlHelper::update(
$conn,
't_market_store',
array(
'idx' => $idx,
),
array(
's_price' => $s_price,
'modifytime' => $this->_getNowTime(),
)
);
if (!$r) {
$this->_rspErr(1, 'update price failed');
return;
}
$this->_rspOk();
}
}