274 lines
9.7 KiB
PHP
274 lines
9.7 KiB
PHP
<?php
|
|
|
|
require 'classes/Quest.php';
|
|
require 'classes/AddReward.php';
|
|
|
|
class QuestController{
|
|
|
|
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 getQuest($quest_id)
|
|
{
|
|
$g_conf_quest_cluster = require('../res/task@task.php');
|
|
$quest_conf = getQuestConfig($g_conf_quest_cluster, $quest_id);
|
|
$q = array(
|
|
'id' => $quest_conf['id'],
|
|
'type' => $quest_conf['type'],
|
|
'condition' => $quest_conf['condition'],
|
|
'value' => $quest_conf['value'],
|
|
// 'active_value' => $quest_conf['active_value'],
|
|
'reward' => $quest_conf['reward'],
|
|
);
|
|
return $q;
|
|
}
|
|
|
|
protected function getParameter($para_id)
|
|
{
|
|
$parameter_meta_cluster = require('../res/parameter@parameter.php');
|
|
$parameter_meta = getParameterConfig($parameter_meta_cluster, $para_id);
|
|
$p = array(
|
|
'id' => $parameter_meta['id'],
|
|
'name' => $parameter_meta['param_name'],
|
|
'value' => $parameter_meta['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 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;
|
|
}
|
|
|
|
|
|
public function questInfo()
|
|
{
|
|
$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);
|
|
$quest_list = array();
|
|
$active_list = array();
|
|
$achievement_list = array();
|
|
$active_sum = 0;
|
|
$active_num = 0;
|
|
if (!$conn) {
|
|
phpcommon\sendError(ERR_USER_BASE + 1, '没有这个玩家');
|
|
return;
|
|
}
|
|
|
|
$rowCount = $conn->execQueryRowCount('SELECT * FROM quest WHERE accountid=:accountid;',
|
|
array(
|
|
':accountid' => $account_id,
|
|
));
|
|
if ($rowCount != 0) {
|
|
$rows = $conn->execQuery('SELECT * FROM quest WHERE accountid=:accountid AND quest_type=:quest_type;',
|
|
array(
|
|
':accountid' => $account_id,
|
|
':quest_type' => 1
|
|
));
|
|
if ($rows) {
|
|
foreach ($rows as $row) {
|
|
array_push($quest_list, array(
|
|
'type' => $row['quest_type'],
|
|
'quest_id' => $row['quest_id'],
|
|
'quest_num' => $row['quest_num'],
|
|
'quest_state' => $row['quest_state']
|
|
));
|
|
}
|
|
}
|
|
}
|
|
echo json_encode(array(
|
|
'errcode' => 0,
|
|
'errmsg' => '',
|
|
'quest_list' => $quest_list,
|
|
));
|
|
}
|
|
|
|
public function doubleQuestReward()
|
|
{
|
|
$account_id = $_REQUEST['account_id'];
|
|
//登录校验
|
|
$login = loginVerify($account_id, $_REQUEST['session_id']);
|
|
if (!$login) {
|
|
phpcommon\sendError(ERR_USER_BASE + 1, 'session无效');
|
|
return;
|
|
}
|
|
$quest_id = $_REQUEST['quest_id'];
|
|
$quest_type = $_REQUEST['type'];
|
|
$p = $this->getParameter(REWARD_TIMES);
|
|
$times = $p['value'] - 1;
|
|
//发奖励
|
|
if ($quest_type == 0) {
|
|
$t = $this->getTaskReward($quest_id);
|
|
if (!$t) {
|
|
phpcommon\sendError(ERR_USER_BASE + 2, '没有这个任务');
|
|
return;
|
|
}
|
|
$d = $this->getDrop($t['reward']);
|
|
if (!$d) {
|
|
phpcommon\sendError(ERR_USER_BASE + 3, '没有这个奖励');
|
|
return;
|
|
}
|
|
$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] * $times;
|
|
$addreward = new classes\AddReward();
|
|
$addreward->addReward($item_id, $item_num, $account_id, 0, 0);
|
|
} else {
|
|
$q = $this->getQuest($quest_id);
|
|
if (!$q) {
|
|
phpcommon\sendError(ERR_USER_BASE + 2, '没有这个任务');
|
|
return;
|
|
}
|
|
$array = $this->getExplode($q['reward']);
|
|
$reward_id = $array[0][0];
|
|
$reward_num = $array[0][1] * $times;
|
|
$addreward = new classes\AddReward();
|
|
$addreward->addReward($reward_id, $reward_num, $account_id, 0, 0);
|
|
}
|
|
$addreward = new classes\AddReward();
|
|
$coin_num = $addreward->getCoinNum($account_id);
|
|
$diamond_num = $addreward->getDiamondNum($account_id);
|
|
echo json_encode(array(
|
|
'errcode' => 0,
|
|
'errmsg' => '',
|
|
'coin_nums' => $coin_num,
|
|
'diamond_nums' => $diamond_num,
|
|
));
|
|
}
|
|
|
|
public function submitQuest()
|
|
{
|
|
$account_id = $_REQUEST['account_id'];
|
|
//登录校验
|
|
$login = loginVerify($account_id, $_REQUEST['session_id']);
|
|
if (!$login) {
|
|
phpcommon\sendError(ERR_USER_BASE + 1, 'session无效');
|
|
return;
|
|
}
|
|
$quest_id = $_REQUEST['quest_id'];
|
|
$quest_type = $_REQUEST['type'];
|
|
$conn = $this->getMysql($account_id);
|
|
if (!$conn) {
|
|
phpcommon\sendError(ERR_USER_BASE + 1, '没有这个玩家');
|
|
return;
|
|
}
|
|
$row = $conn->execQueryOne('SELECT * FROM quest WHERE accountid=:accountid AND quest_id=:quest_id AND quest_type=:quest_type;',
|
|
array(
|
|
':accountid' => $account_id,
|
|
':quest_id' => $quest_id,
|
|
':quest_type' => $quest_type
|
|
));
|
|
if (!$row) {
|
|
phpcommon\sendError(ERR_USER_BASE + 2, '没有这个任务');
|
|
return;
|
|
}
|
|
$item_list = array();
|
|
$addreward = new classes\AddReward();
|
|
$all_item_list = array();
|
|
if ($row['quest_state'] != 1) {
|
|
phpcommon\sendError(ERR_USER_BASE + 3, '任务未完成');
|
|
return;
|
|
}
|
|
if ($row['quest_state'] == 1) {
|
|
$ret = $conn->execScript('UPDATE quest SET quest_state=:quest_state, modify_time=:modify_time ' .
|
|
' WHERE accountid=:accountid AND quest_id =:quest_id AND quest_type=:quest_type;',
|
|
array(
|
|
':accountid' => $account_id,
|
|
':quest_id' => $quest_id,
|
|
':quest_type' => $quest_type,
|
|
':quest_state' => 2,
|
|
':modify_time' => time()
|
|
));
|
|
if (!$ret) {
|
|
die();
|
|
return;
|
|
}
|
|
|
|
//发奖励
|
|
$q = $this->getQuest($quest_id);
|
|
if (!$q) {
|
|
phpcommon\sendError(ERR_USER_BASE + 2, '没有这个任务');
|
|
return;
|
|
}
|
|
$re_array = $this->getExplode($q['reward']);
|
|
$reward_id = $re_array[0][0];
|
|
$reward_num = $re_array[0][1];
|
|
$reward_time = $re_array[0][2];
|
|
$all_item_list = $addreward->addReward($reward_id, $reward_num, $account_id, $reward_time, 0);
|
|
array_push($item_list, array(
|
|
'item_id' => $reward_id,
|
|
'item_num' => $reward_num,
|
|
'time' => 0
|
|
));
|
|
}
|
|
|
|
$coin_num = $addreward->getCoinNum($account_id);
|
|
$diamond_num = $addreward->getDiamondNum($account_id);
|
|
echo json_encode(array(
|
|
'errcode' => 0,
|
|
'errmsg' => '',
|
|
'item_list' => $item_list,
|
|
'coin_nums' => $coin_num,
|
|
'diamond_nums' => $diamond_num,
|
|
'all_item_list' => $all_item_list
|
|
));
|
|
}
|
|
|
|
}
|
|
?>
|