game2004api/webapp/controller/ShopController.class.php
2025-05-04 09:50:02 +08:00

1353 lines
51 KiB
PHP

<?php
use classes\GameLog;
require 'classes/AddReward.php';
require 'classes/RechargeActivity.php';
require 'classes/GameLog.php';
require_once 'metatable/shop.php';
require_once 'metatable/shopGoods.php';
class ShopController{
protected function getRedis($shop_uuid)
{
$redis_conf = getRedisConfig(crc32($shop_uuid));
$r = new phpcommon\Redis(array(
'host' => $redis_conf['host'],
'port' => $redis_conf['port'],
'passwd' => $redis_conf['passwd']
));
return $r;
}
protected function getMysql($account_id)
{
$mysql_conf = getMysqlConfig(crc32($account_id));
$conn = new phpcommon\Mysql(array(
'host' => $mysql_conf['host'],
'port' => $mysql_conf['port'],
'user' => $mysql_conf['user'],
'passwd' => $mysql_conf['passwd'],
'dbname' => DBNAME_PREFIX . $mysql_conf['instance_id']
));
return $conn;
}
protected function getItem($item_id)
{
$g_conf_item_cluster = require('../res/item@item.php');
$item_conf = getItemConfig($g_conf_item_cluster, $item_id);
if (!$item_conf) {
return null;
}
$it = array(
'id' => $item_conf['id'],
'price' => $item_conf['gold'],
'dprice' => $item_conf['diamond_price'],
'discount' => $item_conf['discount'],
'shop_type' => $item_conf['shop_type'],
'type' => $item_conf['fuction'],
'bug_groupnum' => $item_conf['bug_groupnum'],
'shop_list' => $item_conf['shop_list'],
'Isbug_again' => $item_conf['Isbug_again'],
'fuctionindex' => $item_conf['fuctionindex'],
);
return $it;
}
protected function getItemEx(&$g_conf_item_cluster, $item_id)
{
$item_conf = getItemConfig($g_conf_item_cluster, $item_id);
$it = array(
'id' => $item_conf['id'],
'price' => $item_conf['gold'],
'dprice' => $item_conf['diamond_price'],
'discount' => $item_conf['discount'],
'shop_type' => $item_conf['shop_type'],
'type' => $item_conf['fuction'],
'bug_groupnum' => $item_conf['bug_groupnum'],
'shop_list' => $item_conf['shop_list'],
'Isbug_again' => $item_conf['Isbug_again'],
);
return $it;
}
protected function getDrop($drop_id)
{
$drop_meta_table = require('../res/drop@drop.php');
$drop_meta = getDropConfig($drop_meta_table, $drop_id);
if (!$drop_meta) {
return null;
}
$d = array(
'drop_id' => $drop_meta['drop_id'],
'item_id' => $drop_meta['item_id'],
'num' => $drop_meta['num'],
'weight' => $drop_meta['weight'],
'type' => $drop_meta['type']
);
return $d;
}
protected function getParameter($para_id)
{
$g_conf_para_cluster = require('../res/parameter@parameter.php');
$para_conf = getParameterConfig($g_conf_para_cluster, $para_id);
$p = array(
'id' => $para_conf['id'],
'param_name' => $para_conf['param_name'],
'param_value' => $para_conf['param_value'],
);
return $p;
}
protected function getExplode($string)
{
$delim = "|";
$drop_multiply = explode($delim, $string);
$delim1 = ":";
$arr = array();
for ($i = 0; $i < count($drop_multiply); $i++) {
$mul = explode($delim1, $drop_multiply[$i]);
array_push($arr, $mul);
}
return $arr;
}
protected function readShopDB($account_id) {
$conn = $this->getMysql($account_id);
$row = $conn->execQueryOne('SELECT blobdata FROM shop_data WHERE accountid=:accountid;',
array(
':accountid' => $account_id
));
if (!empty($row)) {
$shop_db_str = $row['blobdata'];
$shop_db = json_decode($shop_db_str, true);
return $shop_db;
} else {
return null;
}
}
protected function saveShopDB($account_id, $shop_db) {
$conn = $this->getMysql($account_id);
$row = $conn->execQueryOne('SELECT accountid FROM shop_data WHERE accountid=:accountid;',
array(
':accountid' => $account_id
));
$shop_db_str = "";
if (!empty($shop_db)) {
$shop_db_str = json_encode($shop_db);
}
if (!empty($row)) {
//update
$row = $conn->execScript('UPDATE shop_data SET blobdata=:blobdata, modify_time=:modify_time WHERE accountid=:accountid;',
array(
':accountid' => $account_id,
':blobdata' => $shop_db_str,
':modify_time' => time()
));
} else {
//insert
$row = $conn->execScript('INSERT INTO shop_data(accountid, blobdata, create_time, modify_time) ' .
' VALUES(:account_id, :blobdata, :create_time, :modify_time) ' .
' ON DUPLICATE KEY UPDATE accountid=:account_id, blobdata=:blobdata, modify_time=:modify_time;',
array(
':account_id' => $account_id,
':blobdata' => $shop_db_str,
':create_time' => time(),
':modify_time' => time(),
));
}
}
//新版商店
public function newShopInfo()
{
$account_id = $_REQUEST['account_id'];
//登录校验
$login = loginVerify($account_id, $_REQUEST['session_id']);
if (!$login) {
phpcommon\sendError(ERR_USER_BASE + 1, 'session无效');
return;
}
$conn = $this->getMysql($account_id);
if (!$conn) {
phpcommon\sendError(ERR_USER_BASE + 2, '没有这个玩家');
return;
}
$new_shop_uuid = 'game2004api_shop_uuid_new:' . $_REQUEST['account_id'];
//商店信息
$this->getNewShopInfo($new_shop_uuid, $account_id);
$user_db = $this->readShopDB($account_id);
echo json_encode(array(
'errcode' => 0,
'errmsg'=> '',
'shop_uuid' => $new_shop_uuid,
'active_time' => $user_db['active_time'],
'act_list' => $user_db['act_list'],
'free_list' => $user_db['free_list'],
'sel_list' => $user_db['sel_list'],
'equ_list' => $user_db['equ_list'],
'clo_list' => $user_db['clo_list'],
'item_list' => $user_db['item_list'],
'guild_list' => $user_db['guild_list'],
));
}
protected function getNewShopInfo($shop_uuid, $account_id)
{
$act_list = array();
$free_list = array();
$sel_list = array();
$equ_list = array();
$clo_list = array();
$item_list = array();
$user_db = $this->readShopDB($account_id);
$shop_conf = metatable\getShopConf();
if (!$shop_conf) {
phpcommon\sendError(ERR_USER_BASE + 1,'session失效');
return;
}
$active_time = 0;
if (empty($user_db)) {
$act_list = $this->randomNewShop(1);
$free_list = $this->randomNewShop(2);
$sel_list = $this->randomNewShop(3);
$equ_list = $this->randomNewShop(4);
$clo_list = $this->randomNewShop(5);
$item_list = $this->randomNewShop(6);
$guild_list = $this->randomNewShop(7);
$active_time = time();
} else {
if (empty($user_db['guild_list'])) {
$guild_list = $this->randomNewShop(7);
} else {
$guild_list = $user_db['guild_list'];
}
$act_list = $user_db['act_list'];
$free_list = $user_db['free_list'];
$sel_list = $user_db['sel_list'];
$equ_list = $user_db['equ_list'];
$clo_list = $user_db['clo_list'];
$item_list = $user_db['item_list'];
$active_time = $user_db['active_time'];
$nowTime = phpcommon\getdayseconds(time());
$passed_days = floor(($nowTime - phpcommon\getdayseconds($user_db['active_time'])) / (3600 * 24));
//$passed_days = 2;
if ($passed_days >= 1) {
for ($i = 1; $i <= count($shop_conf); $i++) {
if ($shop_conf[$i]['isrefresh'] == 0) {
continue;
}
if ($shop_conf[$i]['shop_id'] == 1) {
$act_list = $this->randomNewShop(1);
} else if($shop_conf[$i]['shop_id'] == 2) {
$free_list = $this->randomNewShop(2);
} else if($shop_conf[$i]['shop_id'] == 3) {
$sel_list = $this->randomNewShop(3);
} else if($shop_conf[$i]['shop_id'] == 4) {
$equ_list = $this->randomNewShop(4);
} else if($shop_conf[$i]['shop_id'] == 5) {
$clo_list = $this->randomNewShop(5);
} else if($shop_conf[$i]['shop_id'] == 6) {
$item_list = $this->randomNewShop(6);
} else if ($shop_conf[$i]['shop_id'] == 7) {
$guild_list = $this->randomNewShop(7);
}
}
$active_time = $nowTime;
}
}
$shop_db = array(
'shop_uuid' => $shop_uuid,
'active_time' => $active_time,
'act_list' => $act_list,
'free_list' => $free_list,
'sel_list' => $sel_list,
'equ_list' => $equ_list,
'clo_list' => $clo_list,
'item_list' => $item_list,
'guild_list' => $guild_list,
);
$this->saveShopDB($account_id, $shop_db);
}
protected function randomNewShop($id)
{
$shop_list = array();
$shop_conf = metatable\getShopById($id);
if (!$shop_conf) {
die();
return;
}
return metatable\randGoods($shop_conf, $shop_list);
}
public function newBuyShopItem()
{
$account_id = $_REQUEST['account_id'];
//登录校验
$login = loginVerify($account_id, $_REQUEST['session_id']);
if (!$login) {
phpcommon\sendError(ERR_USER_BASE + 1, 'session无效');
return;
}
$id = $_REQUEST['id'];
$type = $_REQUEST['type'];
$num = $_REQUEST['num'];
if ($num < 0) {
phpcommon\sendError(ERR_USER_BASE + 2,'没有这个商品');
return;
}
$user_db = $this->readShopDB($account_id);
$redis_goods = $this->getGoodsDiscount($id, $type, $user_db);
$shop_conf = metatable\getShopById($type);
if (!$shop_conf || !$redis_goods) {
phpcommon\sendError(ERR_USER_BASE + 2,'没有这个商品');
return;
}
if ($redis_goods['status'] == 1) {
phpcommon\sendError(ERR_USER_BASE + 4,'商品已购买');
return;
}
$price = $redis_goods['price'] * $num;
if ($redis_goods['isdiscount']) {
$price = ceil($price * $redis_goods['dis_num'] / 100);
}
$item_conf = $this->getItem($id);
$item_fuctionindex = $item_conf['fuctionindex'];
$drop_conf = $this->getDrop($item_fuctionindex);
if (!$item_conf) {
phpcommon\sendError(ERR_USER_BASE + 5,'没有这个道具');
return;
}
//扣除货币
if ($redis_goods['buy'] != 0) {
$this->subCoin($price, $account_id, $redis_goods['buy'], $shop_conf['istodayad']);
if ($redis_goods['buy'] == 2) {
GameLog::Log('diamond cost: 7,'.$account_id.','.$price.','.$id.','.$type);
}
}
//增加奖励
$addreward = new classes\AddReward();
$item_list = array();
$all_item_list = array();
if ($item_conf['type'] == 13) {
$item_list = $this->getRandomShopReward($item_fuctionindex);
foreach ($item_list as $it) {
$item_id = $it['item_id'];
$num = $it['item_num'];
$time = 0;
$items = $addreward->addReward($item_id, $num, $account_id, $time,0);
foreach($items as $j) {
array_push($all_item_list, array(
'item_id' => $j['item_id'],
'item_num' => $j['item_num'],
'time' => $j['time'],
));
}
}
} else {
array_push($item_list,array(
'item_id' => $redis_goods['id'],
'item_num' => $num,
'time' => 0,
));
$items = $addreward->addReward($redis_goods['id'], $num, $account_id, 0, 0);
foreach($items as $j) {
array_push($all_item_list, array(
'item_id' => $j['item_id'],
'item_num' => $j['item_num'],
'time' => $j['time'],
));
}
}
//修改购买状态
if ($shop_conf['isrepeat'] == 0) {
$this->updateShopRedis($account_id, $id, $type, $user_db);
}
$coin_num = $addreward->getCoinNum($account_id);
$diamond_num = $addreward->getDiamondNum($account_id);
$adfree = $addreward->getAdfree($account_id);
$medals = $addreward->getMedals($account_id);
echo json_encode(array(
'errcode' => 0,
'errmsg'=> '',
'coin_nums' => $coin_num,
'diamond_nums' => $diamond_num,
'adfree' => $adfree,
'medals' => $medals,
'item_list' => $item_list,
'all_item_list' => $all_item_list,
'id' => $id,
));
}
protected function getRandomShopReward($drop_id)
{
$airReward_list = array();
//随机奖励
$d = $this->getDrop($drop_id);
if (!$d) {
phpcommon\sendError(ERR_USER_BASE + 3, '没有这个奖励');
die();
}
$item_id_array = $this->getExplode($d['item_id']);
$item_num_array = $this->getExplode($d['num']);
$weight_array = $this->getExplode($d['weight']);
if ($d['type'] == 2) {
$weight_sum = 0;
$keys = 0;
for ($i = 0; $i < count($weight_array); $i++) {
$weight_sum += $weight_array[$i][0];
}
$random = Rand(0, $weight_sum);
$weight = 0;
for ($i = 0; $i < count($weight_array); $i++) {
$weight += $weight_array[$i][0];
if ($weight > $random) {
$keys = $i;
break;
}
}
$item_id = $item_id_array[$keys][0];
$item_num = $item_num_array[$keys][0];
array_push($airReward_list, array(
'item_id' => $item_id,
'item_num' => $item_num,
'time' => 0,
));
}
if ($d['type'] == 1) {
for ($i = 0; $i < count($weight_array); $i++) {
$random = Rand(0, 10000);
if ($weight_array[$i][0] < $random) {
continue;
}
array_push($airReward_list, array(
'item_id' => $item_id_array[$i][0],
'item_num' => $item_num_array[$i][0],
'time' => 0,
));
}
}
return $airReward_list;
}
protected function updateShopRedis($account_id, $id, $type, $user_db)
{
if ($type == 1) {
foreach ($user_db['act_list'] as &$re) {
if ($re['id'] == $id) {
$re['status'] = 1;
break;
}
}
} else if ($type == 2) {
foreach ($user_db['free_list'] as &$re) {
if ($re['id'] == $id) {
$re['status'] = 1;
break;
}
}
} else if ($type == 3) {
foreach ($user_db['sel_list'] as &$re) {
if ($re['id'] == $id) {
$re['status'] = 1;
break;
}
}
} else if ($type == 4) {
foreach ($user_db['equ_list'] as &$re) {
if ($re['id'] == $id) {
$re['status'] = 1;
break;
}
}
} else if ($type == 5) {
foreach ($user_db['clo_list'] as &$re) {
if ($re['id'] == $id) {
$re['status'] = 1;
break;
}
}
} else if ($type == 6) {
foreach ($user_db['item_list'] as &$re) {
if ($re['id'] == $id) {
$re['status'] = 1;
break;
}
}
}
$this->saveShopDB($account_id, $user_db);
}
protected function getGoodsDiscount($id, $type, $user_db)
{
if ($type == 1) {
foreach ($user_db['act_list'] as $re) {
if ($re['id'] == $id) {
return $re;
}
}
} else if ($type == 2) {
foreach ($user_db['free_list'] as $re) {
if ($re['id'] == $id) {
return $re;
}
}
} else if ($type == 3) {
foreach ($user_db['sel_list'] as $re) {
if ($re['id'] == $id) {
return $re;
}
}
} else if ($type == 4) {
foreach ($user_db['equ_list'] as $re) {
if ($re['id'] == $id) {
return $re;
}
}
} else if ($type == 5) {
foreach ($user_db['clo_list'] as $re) {
if ($re['id'] == $id) {
return $re;
}
}
} else if ($type == 6) {
foreach ($user_db['item_list'] as $re) {
if ($re['id'] == $id) {
return $re;
}
}
} else if ($type == 7) {
foreach ($user_db['guild_list'] as $re) {
if ($re['id'] == $id) {
return $re;
}
}
}
return null;
}
//旧版商店/////////////////////////////////////////////
protected function getShopInfo($shop_uuid)
{
$r = $this->getRedis($shop_uuid);
if (!$r) {
die();
return;
}
$shop_list = array();
$user_db_str = $r->get($shop_uuid);
if (empty($user_db_str)) {
$shop_list = $this->randomShop(1);
$shop_db = array(
'shop_uuid' => $shop_uuid,
'shop_list' => $shop_list,
);
$r -> set($shop_uuid, json_encode($shop_db));
$r -> pexpire($shop_uuid, 1000 * 3600 * 24);
} else {
$user_db = json_decode($user_db_str, true);
if (empty($user_db)) {
phpcommon\sendError(ERR_USER_BASE + 1,'session失效');
return;
}
foreach ($user_db['shop_list'] as $shop) {
array_push($shop_list, array(
'shop_id' => $shop['shop_id'],
'item_id' => $shop['item_id'],
'item_num' => $shop['item_num'],
'discount' => $shop['discount'],
'price' => $shop['price'],
'tip' => $shop['tip'],
'status' => $shop['status'],
'time' => $shop['time'],
));
}
}
return $shop_list;
}
public function shopInfo()
{
$account_id = $_REQUEST['account_id'];
//登录校验
$login = loginVerify($account_id, $_REQUEST['session_id']);
if (!$login) {
phpcommon\sendError(ERR_USER_BASE + 1, 'session无效');
return;
}
$conn = $this->getMysql($account_id);
if (!$conn) {
phpcommon\sendError(ERR_USER_BASE + 2, '没有这个玩家');
return;
}
$shop_uuid = 'game2004api_shop_uuid: ' . md5($_REQUEST['account_id']);
$diamond_shop_uuid = 'game2004api_diamond_shop_uuid:' . md5($_REQUEST['account_id']);
//商店信息
$shop_list = $this->getShopInfo($shop_uuid);
$rand_coinshop = array();
$rand_diamondshop = array();
$g_conf_item_cluster = require('../res/item@item.php');
$rows = $conn->execQuery('SELECT * FROM bag WHERE accountid=:accountid;',
array(
':accountid' => $account_id
));
$status = 0;
$coin_shop = array();
$diamond_shop = array();
$num = 1;
foreach($g_conf_item_cluster as $items) {
$it = $this->getItemEx($g_conf_item_cluster, $items['id']);
$status = 0;
foreach($rows as $r) {
if ($r['id'] != $it['id']) {
continue;
}
if ($r['status'] == 2 || $r['active_time'] != 0) {
continue;
}
if ($it['Isbug_again'] == 1) {
continue;
}
$status = 1;
break;
}
if ($it['shop_type'] == 1) {
if ($status == 0) {
array_push($rand_coinshop,array(
'id' => $it['id'],
));
}
if ($it['Isbug_again'] == 1) {
$num = $it['bug_groupnum'];
}
array_push($coin_shop,array(
'id' => $it['id'],
'price' => $it['price'],
'status' => $status,
'discount' => 0,
'coin_id' => 10001,
'num' => $num,
'shop_list' => $it['shop_list'],
));
} else if ($it['shop_type'] == 2) {
if ($status == 0) {
array_push($rand_diamondshop,array(
'id' => $it['id'],
));
}
if ($it['Isbug_again'] == 1) {
$num = $it['bug_groupnum'];
}
array_push($diamond_shop,array(
'id' => $it['id'],
'price' => $it['dprice'],
'status' => $status,
'discount' => 0,
'coin_id' => 10003,
'num' => $num,
'shop_list' => $it['shop_list'],
));
} else if ($it['shop_type'] == 3) {
if ($status == 0) {
array_push($rand_coinshop,array(
'id' => $it['id'],
));
}
if ($it['Isbug_again'] == 1) {
$num = $it['bug_groupnum'];
}
array_push($coin_shop,array(
'id' => $it['id'],
'price' => $it['price'],
'status' => $status,
'discount' => 0,
'coin_id' => 10001,
'num' => $num,
'shop_list' => $it['shop_list'],
));
if ($status == 0) {
array_push($rand_diamondshop,array(
'id' => $it['id'],
));
}
array_push($diamond_shop,array(
'id' => $it['id'],
'price' => $it['dprice'],
'status' => $status,
'discount' => 0,
'coin_id' => 10003,
'num' => $num,
'shop_list' => $it['shop_list'],
));
}
}
$row_shop = $conn->execQueryOne('SELECT * FROM shop WHERE accountid=:accountid;',
array(
':accountid' => $account_id
));
$randcoin_id = 0;
$coin_discount = 0;
if (count($rand_coinshop) >= 1) {
$random_coin = Rand(0, count($rand_coinshop) - 1);
$randcoin_id = $rand_coinshop[$random_coin]['id'];
$cinfo = $this->getItem($randcoin_id);
$coin_discount = $this->randdiscount($cinfo['discount']);
}
$randdiamnod_id = 0;
$dia_discount = 0;
if (count($rand_diamondshop) >= 1) {
$random_diamond = Rand(0, count($rand_diamondshop) - 1);
$randdiamnod_id = $rand_diamondshop[$random_diamond]['id'];
$dinfo = $this->getItem($randdiamnod_id);
$dia_discount = $this->randdiscount($dinfo['discount']);
}
if (!$row_shop) {
$ret = $conn->execScript('INSERT INTO shop(accountid, coin_id, coin_discount, diamond_id, diamond_discount, create_time, modify_time) ' .
' VALUES(:account_id, :coin_id, :coin_discount, :diamond_id, :diamond_discount, :create_time, :modify_time) ' .
' ON DUPLICATE KEY UPDATE accountid=:account_id, coin_id=:coin_id, coin_discount=:coin_discount, diamond_id=:diamond_id, diamond_discount=:diamond_discount, modify_time=:modify_time;',
array(
':account_id' => $account_id,
':coin_id' => $randcoin_id,
':coin_discount' => $coin_discount,
':diamond_id' => $randdiamnod_id,
':diamond_discount' => $dia_discount,
':create_time' => time(),
':modify_time' => time()
));
if(!$ret){
die();
return;
}
} else {
if (phpcommon\getdayseconds(time()) - phpcommon\getdayseconds($row_shop['modify_time']) > 0) {
$ret = $conn->execScript('UPDATE shop SET coin_id=:coin_id, coin_discount=:coin_discount, diamond_id=:diamond_id, diamond_discount=:diamond_discount, modify_time=:modify_time ' .
' WHERE accountid=:accountid;',
array(
':accountid' => $account_id,
':coin_id' => $randcoin_id,
':coin_discount' => $coin_discount,
':diamond_id' => $randdiamnod_id,
':diamond_discount' => $dia_discount,
':modify_time' => time()
));
if (!$ret) {
die();
return;
}
} else {
$randcoin_id = $row_shop['coin_id'];
$coin_discount = $row_shop['coin_discount'];
$randdiamnod_id = $row_shop['diamond_id'];
$dia_discount = $row_shop['diamond_discount'];
}
}
foreach($coin_shop as &$co) {
if ($co['id'] == $randcoin_id && $co['status'] != 1) {
$co['discount'] = $coin_discount;
$co['price'] = round($co['price'] * $coin_discount / 10);
}
}
unset($co);
foreach($diamond_shop as &$di) {
if ($di['id'] == $randdiamnod_id && $di['status'] != 1) {
$di['discount'] = $dia_discount;
$di['price'] = round($di['price'] * $dia_discount / 10);
}
}
unset($di);
echo json_encode(array(
'errcode' => 0,
'errmsg'=> '',
'shop_uuid' => $shop_uuid,
'shop_list' => $shop_list,
'shop_flush_times' => 0,
'coin_shop' => $coin_shop,
'diamond_shop' => $diamond_shop,
));
}
protected function randdiscount($array)
{
$keys = 0;
$weight_sum = 0;
$weight_array = $this->getExplode($array);
for ($ii = 0; $ii < count($weight_array); $ii++) {
$weight_sum += $weight_array[$ii][1];
}
$random = Rand(0, $weight_sum);
$weight = 0;
for ($ii = 0; $ii < count($weight_array); $ii++) {
$weight += $weight_array[$ii][1];
if ($weight > $random) {
$keys = $ii;
break;
}
}
return $weight_array[$keys][0];
}
public function shopBuyMore()
{
$account_id = $_REQUEST['account_id'];
//登录校验
$login = loginVerify($account_id, $_REQUEST['session_id']);
if (!$login) {
phpcommon\sendError(ERR_USER_BASE + 1, 'session无效');
return;
}
$id = $_REQUEST['id'];
$type = $_REQUEST['type'];
$num = $_REQUEST['num'];
$conn = $this->getMysql($account_id);
if (!$conn) {
phpcommon\sendError(ERR_USER_BASE + 2, '没有这个玩家');
return;
}
$i = $this->getItem($id);
$price = 0;
if ($i['shop_type'] == 1) {
$row_c = $conn->execQueryOne('SELECT * FROM shop WHERE accountid=:accountid AND coin_id=:id;',
array(
':accountid' => $account_id,
':id' => $id
));
$price = $i['price'] * $num;
if ($row_c) {
$price = round($i['price'] * $num * $row_c['coin_discount'] / 10);
}
$this->SubCoin($price, $account_id, 1, 0);
} else if ($i['shop_type'] == 2) {
$price = $i['dprice'] * $num;
$row_d = $conn->execQueryOne('SELECT * FROM shop WHERE accountid=:accountid AND diamond_id=:id;',
array(
':accountid' => $account_id,
':id' => $id
));
if ($row_d) {
$price = round($i['dprice'] * $num * $row_d['diamond_discount'] / 10);
}
$this->SubCoin($price, $account_id, 2, 0);
} else if ($i['shop_type'] == 3) {
if ($type == 0) {
$row_c = $conn->execQueryOne('SELECT * FROM shop WHERE accountid=:accountid AND coin_id=:id;',
array(
':accountid' => $account_id,
':id' => $id
));
$price = $i['price'] * $num;
if ($row_c) {
$price = round($i['price'] * $num * $row_c['coin_discount'] / 10);
}
$this->SubCoin($price, $account_id, 1, 0);
} else if ($type == 1) {
$price = $i['dprice'] * $num;
$row_d = $conn->execQueryOne('SELECT * FROM shop WHERE accountid=:accountid AND diamond_id=:id;',
array(
':accountid' => $account_id,
':id' => $id
));
if ($row_d) {
$price = round($i['dprice'] * $num * $row_d['diamond_discount'] / 10);
}
$this->SubCoin($price, $account_id, 2, 0);
}
}
//增加奖励
$addreward = new classes\AddReward();
$all_item_list = $addreward->addReward($id, $num, $account_id, 0, 0);
$coin_num = $addreward->getCoinNum($account_id);
$diamond_num = $addreward->getDiamondNum($account_id);
$item_list = array();
array_push($item_list,array(
'item_id' => $id,
'item_num' => $num,
'time' => 0,
));
echo json_encode(array(
'errcode' => 0,
'errmsg'=> '',
'coin_nums' => $coin_num,
'diamond_nums' => $diamond_num,
'item_list' => $item_list,
'all_item_list' => $all_item_list
));
}
public function buyItem()
{
$account_id = $_REQUEST['account_id'];
//登录校验
$login = loginVerify($account_id, $_REQUEST['session_id']);
if (!$login) {
phpcommon\sendError(ERR_USER_BASE + 1, 'session无效');
return;
}
$id = $_REQUEST['id'];
$type = $_REQUEST['type'];
$conn = $this->getMysql($account_id);
if (!$conn) {
phpcommon\sendError(ERR_USER_BASE + 2, '没有这个玩家');
return;
}
$row = $conn->execQueryOne('SELECT status, active_time FROM bag WHERE accountid=:accountid AND id=:id;',
array(
':accountid' => $account_id,
':id' => $id
));
if ($row) {
$it = $this->getItem($id);
if ($row['status'] != 2 && $row['active_time'] == 0 && $it['Isbug_again'] != 1) {
phpcommon\sendError(ERR_USER_BASE + 4, '商品已购买');
return;
}
}
//扣除货币
$i = $this->getItem($id);
$price = 0;
$num = 1;
if ($i['shop_type'] == 1) {
$row_c = $conn->execQueryOne('SELECT * FROM shop WHERE accountid=:accountid AND coin_id=:id;',
array(
':accountid' => $account_id,
':id' => $id
));
$price = $i['price'];
if ($i['type'] == 10) {
$num = $i['bug_groupnum'];
}
if ($row_c) {
$price = round($i['price'] * $row_c['coin_discount'] / 10);
}
$this->SubCoin($price, $account_id, 1, 0);
} else if ($i['shop_type'] == 2) {
$price = $i['dprice'];
if ($i['type'] == 10) {
$num = $i['bug_groupnum'];
}
$row_d = $conn->execQueryOne('SELECT * FROM shop WHERE accountid=:accountid AND diamond_id=:id;',
array(
':accountid' => $account_id,
':id' => $id
));
if ($row_d) {
$price = round($i['dprice'] * $row_d['diamond_discount'] / 10);
}
$this->SubCoin($price, $account_id, 2,0);
} else if ($i['shop_type'] == 3) {
if ($type == 0) {
$row_c = $conn->execQueryOne('SELECT * FROM shop WHERE accountid=:accountid AND coin_id=:id;',
array(
':accountid' => $account_id,
':id' => $id
));
$price = $i['price'];
if ($i['type'] == 10) {
$num = $i['bug_groupnum'];
}
if ($row_c) {
$price = round($i['price'] * $row_c['coin_discount'] / 10);
}
$this->SubCoin($price, $account_id, 1, 0);
} else if ($type == 1) {
$price = $i['dprice'];
if ($i['type'] == 10) {
$num = $i['bug_groupnum'];
}
$row_d = $conn->execQueryOne('SELECT * FROM shop WHERE accountid=:accountid AND diamond_id=:id;',
array(
':accountid' => $account_id,
':id' => $id
));
if ($row_d) {
$price = round($i['dprice'] * $row_d['diamond_discount'] / 10);
}
$this->SubCoin($price, $account_id, 2, 0);
}
}
//增加奖励
$addreward = new classes\AddReward();
$all_item_list = $addreward->addReward($id, $num, $account_id, 0, 0);
$coin_num = $addreward->getCoinNum($account_id);
$diamond_num = $addreward->getDiamondNum($account_id);
$item_list = array();
array_push($item_list,array(
'item_id' => $id,
'item_num' => $num,
'time' => 0,
));
echo json_encode(array(
'errcode' => 0,
'errmsg'=> '',
'coin_nums' => $coin_num,
'diamond_nums' => $diamond_num,
'item_list' => $item_list,
'all_item_list' => $all_item_list
));
}
public function buyDoubleItem()
{
$account_id = $_REQUEST['account_id'];
//登录校验
$login = loginVerify($account_id, $_REQUEST['session_id']);
if (!$login) {
phpcommon\sendError(ERR_USER_BASE + 1, 'session无效');
return;
}
if (!isset($_REQUEST['shop_uuid'])) {
return;
}
$shop_id = $_REQUEST['shop_id'];
$shop_uuid = $_REQUEST['shop_uuid'];
$item_id = 0;
$item_num = 0;
$price = 0;
$status = 0;
$flag = 0;
$r = $this->getRedis($shop_uuid);
$user_db_str = $r->get($shop_uuid);
if (empty($user_db_str)) {
phpcommon\sendError(ERR_USER_BASE + 1,'session失效');
return;
}
$user_db = json_decode($user_db_str, true);
if (empty($user_db)) {
phpcommon\sendError(ERR_USER_BASE + 1,'session失效');
return;
}
foreach ($user_db['shop_list'] as $shop) {
if ($shop['shop_id'] == $shop_id) {
$item_id = $shop['item_id'];
$item_num = $shop['item_num'];
$price = $shop['price'];
$status = $shop['status'];
$time = $shop['time'];
$flag = 1;
break;
}
}
if ($flag == 0) {
phpcommon\sendError(ERR_USER_BASE + 3, '没有这个商品');
return;
}
//增加奖励
$p = $this->getParameter(REWARD_TIMES);
$times = $p['param_value'] - 1;
$addreward = new classes\AddReward();
$addreward->addReward($item_id, $item_num * $times, $account_id, $time, 0);
$coin_num = $addreward->getCoinNum($account_id);
echo json_encode(array(
'errcode' => 0,
'errmsg'=> '',
'coin_nums' => $coin_num,
));
}
public function flushShop()
{
$account_id = $_REQUEST['account_id'];
//登录校验
$login = loginVerify($account_id, $_REQUEST['session_id']);
if (!$login) {
phpcommon\sendError(ERR_USER_BASE + 1, 'session无效');
return;
}
if (!isset($_REQUEST['shop_uuid'])) {
return;
}
$shop_uuid = $_REQUEST['shop_uuid'];
$shop_type = $_REQUEST['shop_type'];
$shop_list = array();
$flush_times = 0;
if ($_REQUEST['type'] == 3) {
$p = $this->getParameter(RAND_SHOP_GOLD);
if ($shop_type == 2) {
$p = $this->getParameter(RAND_DIAMONDSHOP_GOLD);
}
$this->SubCoin($p['param_value'], $account_id, $_REQUEST['type'], 0);
}
$conn = $this->getMysql($account_id);
if (!$conn) {
phpcommon\sendError(ERR_USER_BASE + 2, '没有这个玩家');
return;
}
$row = $conn->execQueryOne('SELECT shop_flush_times, diamond_shop_flush_times FROM user WHERE accountid=:accountid;',
array(
':accountid' => $account_id
));
if (!$row) {
phpcommon\sendError(ERR_USER_BASE + 2, '没有这个玩家');
return;
}
if ($shop_type == 1) {
$p_flush = $this->getParameter(MAX_SHOP_REFRESH);
if ($p_flush['param_value'] <= $row['shop_flush_times']) {
phpcommon\sendError(ERR_USER_BASE + 3, '今日刷新次数已满');
return;
}
$ret = $conn->execScript('UPDATE user SET shop_flush_times=:shop_flush_times, modify_time=:modify_time ' .
' WHERE accountid=:accountid;',
array(
':accountid' => $account_id,
':shop_flush_times' => $row['shop_flush_times'] + 1,
':modify_time' => time()
));
if (!$ret) {
die();
return;
}
$flush_times = $row['shop_flush_times'] + 1;
}
if ($shop_type == 2) {
$p_flush = $this->getParameter(RAND_DIAMONDSHOP_TIME);
if ($p_flush['param_value'] <= $row['diamond_shop_flush_times']) {
phpcommon\sendError(ERR_USER_BASE + 3, '今日刷新次数已满');
return;
}
$ret = $conn->execScript('UPDATE user SET diamond_shop_flush_times=:diamond_shop_flush_times, modify_time=:modify_time ' .
' WHERE accountid=:accountid;',
array(
':accountid' => $account_id,
':diamond_shop_flush_times' => $row['diamond_shop_flush_times'] + 1,
':modify_time' => time()
));
if (!$ret) {
die();
return;
}
$flush_times = $row['diamond_shop_flush_times'] + 1;
}
$r = $this->getRedis($shop_uuid);
$user_db_str = $r->get($shop_uuid);
if (empty($user_db_str)) {
phpcommon\sendError(ERR_USER_BASE + 1,'session失效');
return;
}
$user_db = json_decode($user_db_str, true);
if (empty($user_db)) {
phpcommon\sendError(ERR_USER_BASE + 1,'session失效');
return;
}
unset($user_db['shop_list']);
$shop_list = $this->randomShop($shop_type);
$user_db['shop_list'] = $shop_list;
$r -> set($shop_uuid, json_encode($user_db));
$r -> pexpire($shop_uuid, 1000 * 3600 * 24);
$addreward = new classes\AddReward();
$coin_num = $addreward->getCoinNum($account_id);
$diamond_num = $addreward->getDiamondNum($account_id);
echo json_encode(array(
'errcode' => 0,
'errmsg'=> '',
'shop_uuid' => $shop_uuid,
'shop_list' => $shop_list,
'flush_times' => $flush_times,
'coin_nums' => $coin_num,
'diamond_nums' => $diamond_num
));
}
protected function randomShop($type)
{
$shop_list = array();
$g_conf_shop_cluster = require('../res/shop@shop.php');
$coin_len = 0;
$diamond_len = 0;
$len = 0;
$id = 0;
for ($i = 1; $i <= count($g_conf_shop_cluster); $i++)
{
$s = $this->getShop($i);
$coin_len++;
}
if ($type == 1) {
$id = 1;
$len = $coin_len;
}
if ($type == 2) {
$id = $coin_len + 1;
$len = $diamond_len + $coin_len;
}
for ($i = $id; $i <= $len; $i++)
{
$item_id = 0;
$item_num = 0;
$discount = 0;
$price = 0;
$key = 0;
$s = $this->getShop($i);
//确定商品id和数量
$weight_sum = 0;
$weight_array = $this->getExplode($s['item_weight']);
for ($ii = 0; $ii < count($weight_array); $ii++) {
$weight_sum += $weight_array[$ii][0];
}
$random = Rand(0, $weight_sum);
$weight = 0;
for ($ii = 0; $ii < count($weight_array); $ii++) {
$weight += $weight_array[$ii][0];
if ($weight > $random) {
$key = $ii;
break;
}
}
$item_id_array = $this->getExplode($s['item_id']);
$num_array = $this->getExplode($s['num']);
$item_id = $item_id_array[$key][0];
$item_num = $num_array[$key][0];
//确定折扣和价格
//$it = $this->getItem($item_id);
$keys = 0;
if ($s['tip'] == 1 || $s['tip'] == 2) {
$discount = 0;
$price = 0;
} else {
$keys = 0;
$weight_sum = 0;
$weight_array = $this->getExplode($s['discount_weight']);
for ($ii = 0; $ii < count($weight_array); $ii++) {
$weight_sum += $weight_array[$ii][0];
}
$random = Rand(0, $weight_sum);
$weight = 0;
for ($ii = 0; $ii < count($weight_array); $ii++) {
$weight += $weight_array[$ii][0];
if ($weight > $random) {
$keys = $ii;
break;
}
}
$discount_array = $this->getExplode($s['discount']);
$discount = $discount_array[$keys][0];
$price_array = $this->getExplode($s['price']);
$price = round($price_array[$key][0] * $item_num * ($discount * 1.0 / 10));
/*if ($s['tip'] == 4) {
$price = round($it['dprice'] * $item_num * ($discount * 1.0 / 10));
}*/
}
$time_array = $this->getExplode($s['time']);
$time = $time_array[$key][0];
array_push($shop_list, array(
'shop_id' => $s['shop_id'],
'item_id' => $item_id,
'item_num' => $item_num,
'discount' => $discount,
'price' => $price,
'tip' => $s['tip'],
'status' => 0,
'time' => $time
));
}
return $shop_list;
}
protected function subCoin($coin_num, $account_id, $tips, $isview)
{
$conn = $this->getMysql($account_id);
if (!$conn) {
phpcommon\sendError(ERR_USER_BASE + 2, '没有这个玩家');
return;
}
$rowCoin = $conn->execQueryOne('SELECT coin_num, diamond_num, guildcoin_num, shop_view_times, first_day_ad FROM user WHERE accountid=:accountid;',
array(
':accountid' => $account_id
));
if ($tips == 1) {
//扣除货币
if ($rowCoin['coin_num'] < $coin_num) {
phpcommon\sendError(ERR_USER_BASE + 3, '金币不足');
die();
return;
}
$ret = $conn->execScript('UPDATE user SET coin_num=:coin_num, modify_time=:modify_time ' .
' WHERE accountid=:accountid;',
array(
':accountid' => $account_id,
':coin_num' => $rowCoin['coin_num'] - $coin_num,
':modify_time' => time()
));
if (!$ret) {
die();
}
}
if ($tips == 2) {
//扣除货币
if ($rowCoin['diamond_num'] < $coin_num) {
phpcommon\sendError(ERR_USER_BASE + 5, '钻石不足');
die();
return;
}
$ret = $conn->execScript('UPDATE user SET diamond_num=:diamond_num, modify_time=:modify_time ' .
' WHERE accountid=:accountid;',
array(
':accountid' => $account_id,
':diamond_num' => $rowCoin['diamond_num'] - $coin_num,
':modify_time' => time()
));
if (!$ret) {
die();
}
$rechargeactivity = new classes\RechargeActivity();
$rechargeactivity->triggerConsume($account_id, $coin_num);
}
if ($tips == 3) {
if ($isview == 1) {
if ($rowCoin['first_day_ad'] < $coin_num) {
phpcommon\sendError(ERR_USER_BASE + 7, '每日视频次数不足');
die();
return;
}
} else {
if ($rowCoin['shop_view_times'] < $coin_num) {
phpcommon\sendError(ERR_USER_BASE + 6, '视频次数不足');
die();
return;
}
}
}
if ($tips == 4) {
if ($rowCoin['guildcoin_num'] < $coin_num) {
phpcommon\sendError(ERR_USER_BASE + 8, '战队货币不足');
die();
return;
}
$ret = $conn->execScript('UPDATE user SET guildcoin_num=:guildcoin_num, modify_time=:modify_time ' .
' WHERE accountid=:accountid;',
array(
':accountid' => $account_id,
':guildcoin_num' => $rowCoin['guildcoin_num'] - $coin_num,
':modify_time' => time()
));
if (!$ret) {
die();
}
}
}
}
?>