game2002api/webapp/controller/SupplyBoxController.class.php
wangwei01 0bd95356b7 1
2019-07-08 15:01:32 +08:00

404 lines
16 KiB
PHP

<?php
require 'classes/Quest.php';
require 'classes/AddReward.php';
class SupplyBoxController{
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 getSupplyBox($box_id)
{
$g_conf_supply_cluster = require('../res/supply@supply.php');
$supply_conf = getSupplyConfig($g_conf_supply_cluster, $box_id);
$s = array(
'id' => $supply_conf['id'],
'drop' => $supply_conf['drop'],
'num' => $supply_conf['num'],
'drop_free' => $supply_conf['drop_free'],
'num_free' => $supply_conf['num_free'],
'price' => $supply_conf['price'],
'parameter' => $supply_conf['parameter'],
);
return $s;
}
protected function getDrop($drop_id)
{
$g_conf_drop_cluster = require('../res/drop@drop.php');
$drop_conf = getDropConfig($g_conf_drop_cluster, $drop_id);
$d = array(
'drop_id' => $drop_conf['drop_id'],
'item_id' => $drop_conf['item_id'],
'num' => $drop_conf['num'],
'weight' => $drop_conf['weight'],
'type' => $drop_conf['type']
);
return $d;
}
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 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;
}
public function supplyBoxInfo()
{
$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);
$box_list = array();
if (!$conn) {
phpcommon\sendError(ERR_USER_BASE + 1, '没有这个玩家');
return;
}
$rowCount = $conn->execQueryRowCount('SELECT * FROM supplybox WHERE accountid=:account_id;',
array(
':account_id' => $account_id
));
if ($rowCount != 0) {
$rows = $conn->execQuery('SELECT * FROM supplybox WHERE accountid = :account_id;',
array(
':account_id' => $account_id
));
foreach ($rows as $row) {
if (phpcommon\getdayseconds(time()) - phpcommon\getdayseconds($row['last_buy_time']) > 0) {
$ret = $conn->execScript('UPDATE supplybox SET free_times=0, buy_times=0, last_buy_time=:time, modify_time=:modify_time ' .
' WHERE accountid=:accountid AND box_id=:box_id;',
array(
':accountid' => $account_id,
':box_id' => $row['box_id'],
':time' => time(),
':modify_time' => time()
));
if (!$ret) {
die();
return;
}
}
array_push($box_list, array(
'box_id' => $row['box_id'],
'buy_times' => $row['buy_times'],
'free_times' => $row['free_times']
));
}
}
echo json_encode(array(
'errcode' => 0,
'errmsg' => '',
'supplybox_list' => $box_list
));
}
public function openSupplyBox()
{
$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 + 1, '没有这个玩家');
return;
}
$box_id = $_REQUEST['box_id'];
$free_open = $_REQUEST['free_open'];
$free_times = 0;
$buy_times = 0;
$s = $this->getSupplyBox($box_id);
if (!$s) {
phpcommon\sendError(ERR_USER_BASE + 2, '没有这个宝箱');
return;
}
$drop_id = 0;
if ($free_open == 0) {
$drop_id = $s['drop'];
} else {
$drop_id = $s['drop_free'];
}
$d = $this->getDrop($drop_id);
if (!$d) {
phpcommon\sendError(ERR_USER_BASE + 3, '没有这个奖励');
return;
}
$item_list = array();
$row = $conn->execQueryOne('SELECT * FROM supplybox WHERE accountid=:accountid AND box_id=:box_id;',
array(
':accountid' => $account_id,
':box_id' => $box_id
));
if (!$row) {
if ($free_open == 0) {
phpcommon\sendError(ERR_USER_BASE + 2, '没有这个宝箱');
return;
} else if ($free_open == 1) {
$free_times = 1;
$buy_times = 0;
$ret = $conn->execScript('INSERT INTO supplybox(accountid, box_id, buy_times, free_times, last_buy_time, create_time, modify_time) ' .
' VALUES(:accountid, :box_id, 0, 1, :last_buy_time, :create_time, :modify_time) ' .
' ON DUPLICATE KEY UPDATE accountid=:accountid, box_id=:box_id, buy_times=0, free_times=1, last_buy_time=:last_buy_time, modify_time=:modify_time;',
array(
':accountid' => $account_id,
':box_id' => $box_id,
':last_buy_time' => time(),
':create_time' => time(),
':modify_time' => time()
));
if (!$ret) {
die();
return;
}
}
}
if ($free_open == 1) {
$p_times = $this->getParameter(FREE_DRESS_MAX_TIME);
if ($row['free_times'] >= $p_times['param_value']) {
phpcommon\sendError(ERR_USER_BASE + 3, '免费次数不足');
return;
}
$free_times = $row['free_times'] + 1;
$buy_times = $row['buy_times'];
$ret = $conn->execScript('UPDATE supplybox SET free_times=:free_times, last_buy_time=:last_buy_time, modify_time=:modify_time ' .
' WHERE accountid=:accountid AND box_id=:box_id;',
array(
':accountid' => $account_id,
':box_id' => $box_id,
':free_times' => $free_times,
':last_buy_time' => time(),
':modify_time' => time()
));
if (!$ret) {
die();
return;
}
}
//确定奖励倍数
$sum = 0;
$mul = 0;
$array = $this->getExplode($s['num']);
for ($i = 0; $i < count($array); $i++) {
$sum += $array[$i][1];
}
$rand = Rand(0, $sum);
$multiply = 0;
for ($i = 0; $i < count($array); $i++) {
$multiply += $array[$i][1];
if ($multiply > $rand) {
$mul = $array[$i][0];
break;
}
}
//发送奖励
$weight_sum = 0;
$weight_array = $this->getExplode($d['weight']);
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) {
$key = $i;
break;
}
}
$item_id_array = $this->getExplode($d['item_id']);
$num_array = $this->getExplode($d['num']);
$item_id = $item_id_array[$key][0];
$item_num = $num_array[$key][0] * $mul;
array_push($item_list, array(
'item_id' => $item_id,
'item_num' => $item_num,
));
$addreward = new classes\AddReward();
$addreward->addReward($item_id, $item_num, $account_id);
$quest = new classes\Quest();
$quest->triggerQuest(71004, 1, 1, $account_id);
$quest->triggerQuest(72004, 2, 1, $account_id);
echo json_encode(array(
'errcode' => 0,
'errmsg' => '',
'item_list' => $item_list,
/*'box_id' => $box_id,
'buy_times' => $buy_times,
'free_times' => $free_times*/
));
}
public function buySupplyBox()
{
$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 + 1, '没有这个玩家');
return;
}
$box_id = $_REQUEST['box_id'];
$coin_num = 0;
$buy_times = 0;
$free_times = 0;
$s = $this->getSupplyBox($box_id);
if (!$s) {
phpcommon\sendError(ERR_USER_BASE + 2, '没有这个宝箱');
return;
}
$row = $conn->execQueryOne('SELECT * FROM supplybox WHERE accountid=:account_id AND box_id=:box_id;',
array(
':account_id' => $account_id,
':box_id' => $box_id
));
if (!$row) {
$row = $conn->execQueryOne('SELECT * FROM user WHERE accountid=:accountid;',
array(
':accountid' => $account_id
));
if ($row['coin_num'] < $s['price']) {
phpcommon\sendError(ERR_USER_BASE + 3, '金币不足');
return;
}
$coin_num = $s['price'];
$buy_times = 1;
$free_times = 0;
$ret = $conn->execScript('UPDATE user SET coin_num=:coin_num, modify_time=:modify_time ' .
' WHERE accountid=:accountid;',
array(
':accountid' => $account_id,
':coin_num' => $row['coin_num'] - $s['price'],
':modify_time' => time()
));
$ret = $conn->execScript('INSERT INTO supplybox(accountid, box_id, buy_times, free_times, last_buy_time, create_time, modify_time) ' .
' VALUES(:accountid, :box_id, 1, 0, :last_buy_time, :create_time, :modify_time) ' .
' ON DUPLICATE KEY UPDATE accountid=:accountid, box_id=:box_id, buy_times=1, free_times=0, last_buy_time=0, modify_time=:modify_time;',
array(
':accountid' => $account_id,
':box_id' => $box_id,
':last_buy_time' => time(),
':create_time' => time(),
':modify_time' => time()
));
if (!$ret) {
die();
return;
}
} else {
$row1 = $conn->execQueryOne('SELECT * FROM user WHERE accountid=:accountid;',
array(
':accountid' => $account_id
));
if ($row1['coin_num'] < $s['price'] * pow($s['parameter'], ($row['buy_times']))) {
phpcommon\sendError(ERR_USER_BASE + 3, '金币不足');
return;
}
$coin_num = $s['price'] * pow($s['parameter'], ($row['buy_times']));
$buy_times = $row['buy_times'] + 1;
$free_times = $row['free_times'];
$ret = $conn->execScript('UPDATE user SET coin_num=:coin_num, modify_time=:modify_time ' .
' WHERE accountid=:accountid;',
array(
':accountid' => $account_id,
':coin_num' => $row1['coin_num'] - $coin_num,
':modify_time' => time()
));
$ret = $conn->execScript('UPDATE supplybox SET buy_times=:buy_times, last_buy_time=:time, modify_time=:modify_time ' .
' WHERE accountid=:accountid AND box_id=:box_id;',
array(
':accountid' => $account_id,
':box_id' => $box_id,
':buy_times' => $row['buy_times'] + 1,
':time' => time(),
':modify_time' => time()
));
if (!$ret) {
die();
return;
}
}
echo json_encode(array(
'errcode' => 0,
'errmsg' => '',
/*"box_id" => $box_id,
"buy_times" => $buy_times,
"free_times" => $free_times,
"coin_num" => -$coin_num,*/
));
}
public function supplyReward()
{
$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 + 1, '没有这个玩家');
return;
}
$item_id = $_REQUEST['item_id'];
$p_num = $this->getParameter(DOUBLE_BOX);
$item_num = $_REQUEST['item_num'] * ($p_num['param_value'] - 1);
$addreward = new classes\AddReward();
$addreward->addReward($item_id, $item_num, $account_id);
echo json_encode(array(
'errcode' => 0,
'errmsg' => '',
));
}
}
?>