379 lines
13 KiB
PHP
379 lines
13 KiB
PHP
<?php
|
|
|
|
require 'classes/AddReward.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' => 'gamedb2001_' . $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);
|
|
$it = array(
|
|
'id' => $item_conf['id'],
|
|
'price' => $item_conf['price'],
|
|
);
|
|
return $it;
|
|
}
|
|
|
|
protected function getShop($shop_id)
|
|
{
|
|
$g_conf_shop_cluster = require('../res/shop@shop.php');
|
|
$shop_conf = getShopConfig($g_conf_shop_cluster, $shop_id);
|
|
$s = array(
|
|
'shop_id' => $shop_conf['shop_id'],
|
|
'item_id' => $shop_conf['item_id'],
|
|
'num' => $shop_conf['num'],
|
|
'item_weight' => $shop_conf['item_weight'],
|
|
'discount' => $shop_conf['discount'],
|
|
'discount_weight' => $shop_conf['discount_weight'],
|
|
'tip' => $shop_conf['tip'],
|
|
);
|
|
return $s;
|
|
}
|
|
|
|
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;
|
|
}
|
|
|
|
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;
|
|
}
|
|
$shop_uuid = 'game2001api_shop_uuid: ' . md5($_REQUEST['account_id']);
|
|
$shop_list = array();
|
|
$r = $this->getRedis($shop_uuid);
|
|
if (!$r) {
|
|
die();
|
|
return;
|
|
}
|
|
$user_db_str = $r->get($shop_uuid);
|
|
if (empty($user_db_str)) {
|
|
$shop_list = $this->randomShop();
|
|
$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'],
|
|
));
|
|
}
|
|
}
|
|
$conn = $this->getMysql($account_id);
|
|
if (!$conn) {
|
|
phpcommon\sendError(ERR_USER_BASE + 2, '没有这个玩家');
|
|
return;
|
|
}
|
|
$row = $conn->execQueryOne('SELECT shop_flush_times FROM user WHERE accountid=:accountid;',
|
|
array(
|
|
':accountid' => $account_id
|
|
));
|
|
if (!$row) {
|
|
phpcommon\sendError(ERR_USER_BASE + 2, '没有这个玩家');
|
|
return;
|
|
}
|
|
echo json_encode(array(
|
|
'errcode' => 0,
|
|
'errmsg'=> '',
|
|
'shop_uuid' => $shop_uuid,
|
|
'shop_list' => $shop_list,
|
|
'shop_flush_times' => $row['shop_flush_times'],
|
|
));
|
|
}
|
|
|
|
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;
|
|
}
|
|
$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'];
|
|
$flag = 1;
|
|
break;
|
|
}
|
|
}
|
|
if ($flag == 0) {
|
|
phpcommon\sendError(ERR_USER_BASE + 3, '没有这个商品');
|
|
return;
|
|
}
|
|
if ($status == 1) {
|
|
phpcommon\sendError(ERR_USER_BASE + 4, '商品已购买');
|
|
return;
|
|
}
|
|
//扣除货币
|
|
$this->SubCoin($price, $account_id);
|
|
//修改购买状态
|
|
foreach ($user_db['shop_list'] as &$shop) {
|
|
if ($shop['shop_id'] == $shop_id) {
|
|
$shop['status'] = 1;
|
|
}
|
|
}
|
|
$r->set($shop_uuid, json_encode($user_db));
|
|
//增加奖励
|
|
$addreward = new classes\AddReward();
|
|
$addreward->addReward($item_id, $item_num, $account_id);
|
|
|
|
echo json_encode(array(
|
|
'errcode' => 0,
|
|
'errmsg'=> '',
|
|
));
|
|
}
|
|
|
|
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;
|
|
}
|
|
$shop_uuid = $_REQUEST['shop_uuid'];
|
|
$shop_list = array();
|
|
$conn = $this->getMysql($account_id);
|
|
if (!$conn) {
|
|
phpcommon\sendError(ERR_USER_BASE + 2, '没有这个玩家');
|
|
return;
|
|
}
|
|
$row = $conn->execQueryOne('SELECT shop_flush_times FROM user WHERE accountid=:accountid;',
|
|
array(
|
|
':accountid' => $account_id
|
|
));
|
|
if (!$row) {
|
|
phpcommon\sendError(ERR_USER_BASE + 2, '没有这个玩家');
|
|
return;
|
|
}
|
|
$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;
|
|
}
|
|
$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;
|
|
}
|
|
if ($_REQUEST['type'] == 3) {
|
|
$p = $this->getParameter(RAND_SHOP_GOLD);
|
|
$this->SubCoin($p['param_value'], $account_id);
|
|
}
|
|
unset($user_db['shop_list']);
|
|
$shop_list = $this->randomShop();
|
|
$user_db['shop_list'] = $shop_list;
|
|
$r -> set($shop_uuid, json_encode($user_db));
|
|
echo json_encode(array(
|
|
'errcode' => 0,
|
|
'errmsg'=> '',
|
|
'shop_uuid' => $shop_uuid,
|
|
'shop_list' => $shop_list,
|
|
'shop_flush_times' => $row['shop_flush_times'] + 1
|
|
));
|
|
}
|
|
|
|
protected function randomShop()
|
|
{
|
|
$shop_list = array();
|
|
$g_conf_shop_cluster = require('../res/shop@shop.php');
|
|
for ($i = 1; $i <= count($g_conf_shop_cluster); $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);
|
|
if ($s['tip'] != 3) {
|
|
$discount = $s['discount'];
|
|
$price = $it['price'];
|
|
} 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 = round($it['price'] * $item_num * ($discount * 1.0 / 10));
|
|
}
|
|
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,
|
|
));
|
|
}
|
|
return $shop_list;
|
|
}
|
|
|
|
protected function subCoin($coin_num, $account_id)
|
|
{
|
|
$conn = $this->getMysql($account_id);
|
|
if (!$conn) {
|
|
phpcommon\sendError(ERR_USER_BASE + 2, '没有这个玩家');
|
|
return;
|
|
}
|
|
//扣除货币
|
|
$rowCoin = $conn->execQueryOne('SELECT coin_num FROM user WHERE accountid=:accountid;',
|
|
array(
|
|
':accountid' => $account_id
|
|
));
|
|
if ($rowCoin['coin_num'] < $coin_num) {
|
|
phpcommon\sendError(ERR_USER_BASE + 5, '金币不足');
|
|
die();
|
|
}
|
|
$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();
|
|
}
|
|
}
|
|
}
|
|
?>
|