game2005api/webapp/controller/NShopController.class.php
aozhiwei 6a98515527 1
2021-11-18 20:00:55 +08:00

644 lines
24 KiB
PHP

<?php
require_once('mt/NewShop.php');
require_once('mt/Player.php');
require_once('mt/NewItem.php');
require_once('mt/PlayerSkin.php');
require_once('mt/GunSkin.php');
class NShopController extends BaseAuthedController {
private $m_timeSeed = 0;
public function getShopData()
{
$account_id = $_REQUEST['account_id'];
$conn = $this->getMysql($account_id);
$sqlStr = "SELECT id FROM hero WHERE accountid=:accountid; ";
$heroData = $conn->execQuery($sqlStr,array(':accountid' => $account_id));
$sqlStr = "SELECT skin_id FROM hero_skin WHERE accountid=:accountid; ";
$heroSkinData = $conn->execQuery($sqlStr,array(':accountid' => $account_id));
$sqlStr = "SELECT skin_id FROM gun_skin WHERE accountid=:accountid; ";
$gunSkinData = $conn->execQuery($sqlStr,array(':accountid' => $account_id));
$goodsID = "";
$allItemCfg = $this->getAllItem();
$resultArr = array("heroData"=>$heroData,"heroSkin"=>$heroSkinData,"gunSkin"=>$gunSkinData,"allItem"=>$allItemCfg);
$this->sendDataToClient(100,"getShopData",$resultArr);
}
private function getAllItem()
{
$draw_uuid = 'game2005api_newShopAllItem_uuid:' . $_REQUEST['account_id'];
$allItemCfg = null;
$r = $this->getRedis($draw_uuid);
if($r)
{
$tmpItemCfg = null;
$tmpItemCfg = $r->get($draw_uuid);
if(!empty($tmpItemCfg))
{
$allItemCfg = json_decode($tmpItemCfg);
}
else
{
$randN = rand(1,10000);
$this->resetTimeSeed($randN);
}
if($allItemCfg)
{
$allItemCfg->goodsData;//$tmpAllData["goodsData"];
$goodsData = null;
$len = count($allItemCfg->goodsData);
for($i = 0 ;$i < $len; $i++)
{
$len2 = count($allItemCfg->goodsData[$i]);
for($j = 0; $j < $len2; $j ++)
{
$limitType = $allItemCfg->goodsData[$i][$j]->limitType;
if($limitType !=0 )
{
$itemID01 = $allItemCfg->goodsData[$i][$j]->goodsID;
$tmpCnt= $this->getLimitTypeCnt($itemID01);
$allItemCfg->goodsData[$i][$j]->buyCnt = $tmpCnt;
}
}
}
}
}
else
{
error_log("ErrorRedis====");
}
if(!$allItemCfg)
{
$allItemCfg = $this->initAllItem();
$r -> set($draw_uuid, json_encode($allItemCfg));
//if (phpcommon\getMondayseconds(phpcommon\getNowTime()) - phpcommon\getMondayseconds($rowuser['update_time']) > 0)
$nowSeconds = phpcommon\getNowTime();
$nextDaySeconds = phpcommon\getNextDaySeconds($nowSeconds);
$daySeconds = phpcommon\getdayseconds($nowSeconds);
$nextWeekSeconds = phpcommon\getMondaySeconds($nowSeconds);
//$deadline = phpcommon\getNextDaySeconds()-phpcommon\getNowTime();
$dayDeadLine = $nextDaySeconds - $nowSeconds;
$weekDeadLine = ($nextWeekSeconds + 86400*7)-$nowSeconds;
$r -> pexpire($draw_uuid, 200 * 1000);
$encodeArr = json_encode($allItemCfg);
$allItemCfg = json_decode($encodeArr);
//error_log("已经过期重新获得====".$nextDaySeconds."==".$nowSeconds."==".$daySeconds."==".$nextWeekSeconds);
}
return $allItemCfg;
}
private function getLimitTypeCnt($_itemID)
{
$tmpCnt = 0;
$draw_uuid = 'game2005api_newShopLimit'.$_itemID.'_uuid:' . $_REQUEST['account_id'];
$r = $this->getRedis($draw_uuid);
$limitGoods = $r->get($draw_uuid);
if(empty($limitGoods))
{
}
else
{
$limitGoods = json_decode($limitGoods);
$buyCnt = $limitGoods->buyCnt;
$tmpCnt = $buyCnt;
}
return $tmpCnt;
}
private function rnd($_seed)
{
$tmpSeed = ($_seed * 9301 + 49297) % 233280; //神奇数字
$this->m_timeSeed = $tmpSeed;
return $tmpSeed / (233280.0);
}
private function resetTimeSeed($_timeSeed)
{
$this->m_timeSeed = $_timeSeed;
}
private function getRandN($_num)
{
$tmpN = $this->rnd($this->m_timeSeed);
$randN = floor($tmpN*$_num);
return $randN;
}
private function getItemIDByGoodsID($_goodsID)
{
$itemCfg = null;
$shopMeta = mt\NewItem::getItemCfgByID($_goodsID);
if($shopMeta)
{
$itemType = $shopMeta["type"];
//error_log("道具测试01=====".$itemType);
if($itemType == 19)//英雄表
{
$itemCfg = mt\Player::getPlayerCfgByGoodsID($_goodsID);
}
else if($itemType == 13)//枪皮肤
{
$itemCfg = mt\GunSkin::getGunSkinCfgByGoodsID($_goodsID);
}
else if($itemType == 12)//英雄皮肤
{
$itemCfg = mt\PlayerSkin::getPlayerSkinCfgByGoodsID($_goodsID);
}
else
{
$itemCfg = $shopMeta;//道具信息
}
}
return $itemCfg;
}
private function buyGunSkin($gunID,$skinID)
{
phpcommon\SqlHelper::insertOrUpdate
($this->getSelfMysql(),
'gun_skin',
array(
'accountid' => $this->getAccountId(),
'gun_id' => $gunID,
'skin_id' => $skinID
),
array(
'accountid' => $this->getAccountId(),
'gun_id' => $gunID,
'skin_id' =>$skinID,
'trytime' =>0,
'createtime' => $this->getNowTime(),
'modifytime' => $this->getNowTime()
),
array(
'trytime' => 0,
'modifytime' => $this->getNowTime()
)
);
}
public function buyItem()
{
$code = 100;
$account_id = $_REQUEST['account_id'];
$goodsID = $_REQUEST['goodsID'];
$goodsType = $_REQUEST['goodsType'];
$buyCnt = $_REQUEST['buyCnt'];
$conn = $this->getMysql($account_id);
$sqlStr = "SELECT id FROM hero WHERE accountid=:accountid; ";
$itemCfg = null;
$skinData = null;
$priceID = 0;
$priceNum = 0;
$leftMoney = 0;
$leftDiamond = 0;
$curBuyCnt = -1;
$buyGoodsCfg = $this->calculateLimitGoods($goodsID);//当前购买的道具的配置信息
if(!$buyGoodsCfg)
{
$code = 91;//道具被重新刷新,请重新打开页面
}
if($code == 100)
{
if($buyGoodsCfg->limitType !=0)
{
$tmpCnt = $buyGoodsCfg->buyCnt;
$curBuyCnt = $tmpCnt ;
$maxCntStrArr = explode(":",$buyGoodsCfg->limitType);
$maxCnt = $maxCntStrArr[1];
//error_log("购买限购商品======".$curBuyCnt."==".$maxCnt);
if($tmpCnt+1 > $maxCnt)
{
$code = 92;//购买上限
}
}
}
if($code == 100)
{
$shopMeta = mt\NewShop::getShopItemByID($goodsID);
if($shopMeta)
{
$priceNum = $shopMeta["priceNum"];
$priceID = $shopMeta["priceID"];
$priceNum = $priceNum * $buyCnt;
if($buyGoodsCfg->isSpecail == 1)
{
$srcPrice = $priceNum;
$zkN = (int)$buyGoodsCfg->specailPrice;
$priceNum = $priceNum * ($zkN/100);
//error_log("购买的价格是否打折======".$srcPrice."==".$zkN."==".$priceNum);
}
$code = $this->checkPriceEnough($priceNum,$priceID);//检查是否现有货币是否足够购买道具
}
else
{
$code = 99;//商品不存在
}
if($code == 100)
{
$itemCfg = $this->getItemIDByGoodsID($goodsID);
if(!$itemCfg)
{
//error_log("商品部存在===".$goodsID);
$code = 98;//商品对应的道具不存在
}
}
}
$itemID = 0;
if($code == 100)
{
$itemID = null;
if(array_key_exists("itemid",$itemCfg))
{
$itemID = $itemCfg["itemid"];
}
else
{
$itemID = $itemCfg["id"];
}
//error_log("购买道具======".$itemID);
if($goodsType == 19)//购买英雄
{
$heroCfg = mt\Player::getPlayerCfgByGoodsID($itemID);
$this->addHeroToDB($itemID,$heroCfg);
//error_log("准备购买英雄======".json_encode($heroCfg));
}
else if($goodsType == 12)//购买英雄皮肤
{
$tmpHeroID = $itemCfg["playerid"];
$this->buySkin($itemID,$tmpHeroID);
}
else if($goodsType == 13)//购买枪皮肤
{
$tmpGunID = $itemCfg["gunid"];
$this->buyGunSkin($tmpGunID,$itemID);
}
else //背包道具
{
$itemData =array("item_id"=>$itemID,"item_num"=>1);
$item_list = array();
array_push($item_list,$itemData);
$this->addItem($item_list);
}
// if ($item['item_id'] == $this->goldID) {
// $this->addGold($item['item_num']);
// error_log("添加金币=====".$item['item_num']);
// }
// else if ($item['item_id'] == $this->lotteryID){
// $this->addLottery($item['item_num']);
// error_log("添加奖券=====".$item['item_num']);
// }
}
if($code == 100)//更新货币
{
$conn = $this->getMysql($account_id);
$sqlStr3 = "SELECT coin_num,diamond_num FROM user WHERE accountid=:accountid; ";
$row = $conn->execQuery($sqlStr3,array(':accountid' => $account_id));
if($row)
{
$money = $row[0]["coin_num"];
$diamond = $row[0]["diamond_num"];
$leftMoney = $money;
$leftDiamond = $diamond;
}
else
{
$code = 90;
}
if($priceID == "10001")
{
$leftMoney = $leftMoney -$priceNum;
}
else if($priceID == "10003")
{
$leftDiamond = $leftDiamond -$priceNum;
}
//更新货币到数据库
$this->updateCurrency($leftMoney,$leftDiamond);
// error_log("限购测试=====".$itemID."==".$curBuyCnt);
if($curBuyCnt >=0)
{
$curBuyCnt ++;
$draw_uuid = 'game2005api_newShopLimit'.$itemID.'_uuid:' . $_REQUEST['account_id'];
$r = $this->getRedis($draw_uuid);
$limitGoods = $r->get($draw_uuid);
$tmpData = array("goodsID"=>$itemID,"buyCnt"=>$curBuyCnt);
$r -> set($draw_uuid, json_encode($tmpData));
}
}
$resultArr = array("goodsID"=>$goodsID,"goodsType"=>$goodsType,"buyCnt"=>$curBuyCnt);
$this->sendDataToClient($code,"getShopData",$resultArr);
}
private function calculateLimitGoods($_goodsID)
{
$tmpAllData = $this->getAllItem();
$allItemCfg = $tmpAllData->goodsData;//$tmpAllData["goodsData"];
$goodsData = null;
$len = count($allItemCfg);
for($i = 0 ;$i < $len; $i++)
{
$len2 = count($allItemCfg[$i]);
//error_log("当前列表======".$len2."==".$i);
for($j = 0; $j < $len2; $j ++)
{
$tmpGoodsID = $allItemCfg[$i][$j]->goodsID;
// error_log("获得道具信息========="."==".$tmpGoodsID."==".$_goodsID.json_encode($allItemCfg[$i][$j]));
if($tmpGoodsID == $_goodsID)
{
$goodsData = $allItemCfg[$i][$j];
break;
}
}
}
return $goodsData;
}
private function initAllItem()
{
$shopCfg = mt\NewShop::getShopCfg();
$tmpInex = 0;
$randRnage = 1000;
$allItemCfg = array();
$tableNameArr = array();
$limitGoodsArr = array();//限购商品
$perDayArr = array();//每日刷新
foreach ($shopCfg as $item)
{
$tmpCfg = $item;
$shopTableName = $tmpCfg["shopname"];//页签名称
$goodsListStr = $tmpCfg["goods"];
$goodsListStr1 = explode("|",$goodsListStr);
$allItemIDArr = array();
$tmpItemIDArr = array();
$randN = 0;
$bargainNum = $tmpCfg["bargain"];
$shopUIIndex = $tmpCfg["shopui"];
$len1 = count($goodsListStr1);
for($i = 0; $i < $len1 ;$i++)
{
$goodsListStrArr2 = explode(":",$goodsListStr1[$i]);
$tmpItemID = $goodsListStrArr2[0];
array_push($tmpItemIDArr,$tmpItemID);
}
if($tmpCfg["random"])
{
$randN = $tmpCfg["random"];
}
if($randN > 0)
{
while($randN > 0)
{
$randN --;
$tmpN2 = $this->getRandN($randRnage);
$per = $tmpN2 / $randRnage;
$index = floor($per * count($tmpItemIDArr));
$tmpID = $tmpItemIDArr[$index];
array_push($allItemIDArr,$tmpID);
array_splice($tmpItemIDArr,$index,1);
//error_log("随机结果====".$tmpN2."==".json_encode($tmpItemIDArr));
}
}
else
{
$allItemIDArr = $tmpItemIDArr;
}
$itemCfg = array();
$specialArr = array();
//获得特价商品开始--
if($bargainNum > 0)
{
$tmpSpecailArr = array();
$len2 = count($allItemIDArr);
for($i = 0; $i < $len2;$i++)
{
$tmpGoods = mt\NewShop::getShopItemByID($allItemIDArr[$i]);
$tmpNum = $tmpGoods["specailPrice"];
if($tmpNum > 0)
{
array_push($tmpSpecailArr,$tmpGoods["goodsID"]);
}
}
//获得随机特价商品开始--
for($i = 0; $i < $bargainNum; $i++)
{
$specailRandN = $this->getRandN($randRnage);
$per = $specailRandN / $randRnage;
$len3 = count($tmpSpecailArr);
$index02 = floor($per * $len3);
if($len3 > 0)
{
//error_log("获得随机的特价商品====".$index02."==".json_encode($tmpSpecailArr));
array_push($specialArr,$tmpSpecailArr[$index02]);
array_splice($tmpSpecailArr,$index02,1);
}
}
//error_log("特价商品===".json_encode($specialArr));
//获得随机特价商品结束--
}
//获得特价商品结束--
//记录向客户端发送的商品列表开始--
$len4 = count($allItemIDArr);
for($i = 0; $i < $len4 ;$i++)
{
$tmpGoods = mt\NewShop::getShopItemByID($allItemIDArr[$i]);
$isSpecail = 0;
$len5 = count($specialArr);
for($j =0 ; $j < $len5 ; $j++)
{
$tmpGoodsID = $specialArr[$j];
if($tmpGoodsID == $tmpGoods["goodsID"])
{
$isSpecail = 1;
}
}
$tmpGoods["isSpecail"] = $isSpecail;
$tmpGoods["shopUIIndex"] = $shopUIIndex;
$tmpGoods["buyCnt"] = 0;//已经购买了多少个
array_push($itemCfg,$tmpGoods);
}
array_push($tableNameArr,$shopTableName);
array_push($allItemCfg,$itemCfg);
//记录向客户端发送的商品列表结束--
}
$nowSeconds = phpcommon\getNowTime();
$nextDaySeconds = phpcommon\getNextDaySeconds($nowSeconds);
//$daySeconds = phpcommon\getdayseconds($nowSeconds);
$nextWeekSeconds = phpcommon\getMondaySeconds($nowSeconds);
//$deadline = phpcommon\getNextDaySeconds()-phpcommon\getNowTime();
$dayDeadLine = $nextDaySeconds - $nowSeconds;
$weekDeadLine = ($nextWeekSeconds + 86400*7)-$nowSeconds;
//查询周限购 和 每日限购 开始
$lenAllItem = count($allItemCfg);
for($i = 0 ; $i < $lenAllItem; $i++)
{
$lenRows = count($allItemCfg[$i]);
for($j = 0; $j < $lenRows; $j++)
{
$itemID = $allItemCfg[$i][$j]["goodsID"];
$limitType = $allItemCfg[$i][$j]["limitType"];
if($limitType != 0)//是限购商品
{
$limitTypeStrArr = explode(":",$limitType);
$draw_uuid = 'game2005api_newShopLimit'.$itemID.'_uuid:' . $_REQUEST['account_id'];
$updateTime = 0;
if($limitTypeStrArr[0] == 1)//每日更新
{
$updateTime = $dayDeadLine;
}
else if($limitTypeStrArr[0] == 2)//每周更新
{
$updateTime = $weekDeadLine;
}
$r = $this->getRedis($draw_uuid);
$limitGoods = $r->get($draw_uuid);
if(empty($limitGoods))
{
$tmpData = array("goodsID"=>$itemID,"buyCnt"=>0);
$r -> set($draw_uuid, json_encode($tmpData));
$r -> pexpire($draw_uuid, $updateTime * 1000);
//error_log("过期了,不存在======".$itemID);
}
else
{
$limitGoods = json_decode($limitGoods);
$buyCnt = $limitGoods->buyCnt;
$allItemCfg[$i][$j]["buyCnt"] = $buyCnt;//赋值已经购买了多少个
//error_log("FromRadis======".json_encode($limitGoods));
}
}
}
}
//查询周限购 和 每日限购 结束
$resultArr = array("goodsData"=>$allItemCfg,"tableName"=>$tableNameArr);
return $resultArr;
//$this->sendDataToClient(100,"InitAllItem",$resultArr);
}
private function addHeroToDB($heroID,$_heroCfg)
{
$itemArr = array();
$resultArr = array();
$heroCfg = $_heroCfg;
$skinListStr = $heroCfg["skinlist"];
$skinIDArr = explode("|",$skinListStr);
$skinID = $skinIDArr[0];
$itemData = array("hero_id"=>$heroID,"skin_id"=>$skinID);
array_push($itemArr,$itemData);
$skinData = array(
'accountid' => $this->getAccountId(),'hero_id' => $heroID,'skin_id' => $skinID,
'skin_state' => 0,
'get_from' => 0,
'consume_num' => 0,
'trytime' => 0
);
$this->addHeroSkin($skinData);
$this->addAllHero($itemArr);
}
private function addAllHero($items)
{
foreach ($items as $item) {
phpcommon\SqlHelper::insertOrUpdate
($this->getSelfMysql(),
'hero',
array(
'accountid' => $this->getAccountId(),
'id' => $item['hero_id']
),
array(
'accountid' => $this->getAccountId(),
'id' => $item['hero_id'],
'lv' => 1,
'skinid' => $item['skin_id'],
'skill1_lv1' =>1,
'skill1_lv2' =>1,
'yokeexp' => 0,
'yoketotalexp' => 0,
'createtime' => $this->getNowTime(),
'modifytime' => $this->getNowTime()
),
array(
'skill_lv1' => 1,
'modifytime' => $this->getNowTime()
)
);
}
}
private function addHeroSkin($item)
{
phpcommon\SqlHelper::insertOrUpdate
($this->getSelfMysql(),
'hero_skin',
array(
'accountid' => $this->getAccountId(),
'hero_id' => $item['hero_id'],
'skin_id' => $item['skin_id']
),
array(
'accountid' => $this->getAccountId(),
'hero_id' => $item['hero_id'],
'skin_id' => $item['skin_id'],
'skin_state' => $item['skin_state'],
'get_from' => $item['get_from'],
'consume_num' => $item['consume_num'],
'trytime' => $item['trytime'],
'createtime' => $this->getNowTime(),
'modifytime' => $this->getNowTime()
),
array(
'skin_state' => 3,
'modifytime' => $this->getNowTime()
)
);
}
private function buySkin($_skinID,$_heroID)//购买英雄皮肤
{
$skinID = $_skinID;
$heroID = $_heroID;
$code = 100;
$skinData = array(
'accountid' => $this->getAccountId(),'hero_id' => $heroID,'skin_id' => $skinID,
'skin_state' => 0,
'get_from' => 0,
'consume_num' => 0,
'trytime' => 0
);
$this->addHeroSkin($skinData);
}
private function updateCurrency($_money,$_diamond)
{
$account_id = $_REQUEST['account_id'];
$conn = $this->getMysql($account_id);
$sqlStr2 = "Update user set coin_num =:coin_num,diamond_num =:diamond_num WHERE accountid=:accountid ";
$row = $conn->execQuery($sqlStr2,array(':accountid' => $account_id,':coin_num' => $_money,':diamond_num' => $_diamond));
}
}