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

502 lines
18 KiB
PHP

<?php
require 'classes/AddReward.php';
require 'classes/Quest.php';
class ShareController{
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 getDrop($drop_id)
{
$drop_meta_table = require('../res/drop@drop.php');
$drop_meta = getDropConfig($drop_meta_table, $drop_id);
$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 getShare($share_id)
{
$share_meta_table = require('../res/share@share.php');
$share_meta = getShareConfig($share_meta_table, $share_id);
$sh = array(
'id' => $share_meta['id'],
'rewards' => $share_meta['rewards'],
'people' => $share_meta['people'],
);
return $sh;
}
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)
{
$parameter_meta_cluster = require('../res/parameter@parameter.php');
$parameter_meta = getParameterConfig($parameter_meta_cluster, $para_id);
$p = array(
'id' => $parameter_meta['id'],
'param_name' => $parameter_meta['param_name'],
'param_value' => $parameter_meta['param_value'],
);
return $p;
}
public function keyBoxInfo()
{
$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;
}
//扣除钥匙
$row = $conn->execQueryOne('SELECT keys_num FROM user WHERE accountid=:accountid;',
array(
':accountid' => $account_id
));
if (!$row) {
phpcommon\sendError(ERR_USER_BASE + 2, '没有这个玩家');
return;
}
if ($row['keys_num'] < 1) {
phpcommon\sendError(ERR_USER_BASE + 3, '钥匙不足');
die();
return;
}
$ret = $conn->execScript('UPDATE user SET keys_num=:keys_num, modify_time=:modify_time ' .
' WHERE accountid=:accountid;',
array(
':accountid' => $account_id,
':keys_num' => $row['keys_num'] - 1,
':modify_time' => time()
));
if (!$ret) {
die();
return;
}
//随机奖励
$free = $_REQUEST['free'];
$drop_id = 0;
if ($free != 0) {
$drop_id = 24002;
} else {
$drop_id = 24001;
}
$d = $this->getDrop($drop_id);
if (!$d) {
phpcommon\sendError(ERR_USER_BASE + 3, '没有这个奖励');
return;
}
$item_id_array = $this->getExplode($d['item_id']);
$weight_sum = 0;
$keys = 0;
$item_num_array = $this->getExplode($d['num']);
$weight_array = $this->getExplode($d['weight']);
for ($i = 0; $i < count($weight_array); $i++) {
$weight_sum += $weight_array[$i][0];
}
srand(crc32($account_id . $row['keys_num']));
$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];
$addreward = new classes\AddReward();
$addreward->addReward($item_id, $item_num, $account_id);
$item_list = array();
array_push($item_list, array(
'item_id' => $item_id,
'item_num' => $item_num,
));
$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,
'keys_num' => $row['keys_num'] - 1
));
}
public function keyBoxReward()
{
$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;
}
$row = $conn->execQueryOne('SELECT keys_num FROM user WHERE accountid=:accountid;',
array(
':accountid' => $account_id
));
if (!$row) {
phpcommon\sendError(ERR_USER_BASE + 2, '没有这个玩家');
return;
}
//随机奖励
$free = $_REQUEST['free'];
$drop_id = 0;
if ($free != 0) {
$drop_id = 24002;
} else {
$drop_id = 24001;
}
$d = $this->getDrop($drop_id);
if (!$d) {
phpcommon\sendError(ERR_USER_BASE + 3, '没有这个奖励');
return;
}
$item_id_array = $this->getExplode($d['item_id']);
$weight_sum = 0;
$keys = 0;
$item_num_array = $this->getExplode($d['num']);
$weight_array = $this->getExplode($d['weight']);
for ($i = 0; $i < count($weight_array); $i++) {
$weight_sum += $weight_array[$i][0];
}
srand(crc32($account_id . $row['keys_num']));
$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];
$addreward = new classes\AddReward();
$addreward->addReward($item_id, $item_num, $account_id);
echo json_encode(array(
'errcode' => 0,
'errmsg' => '',
));
}
public function shareFriendInfo()
{
$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;
}
$info_list = array();
$rows = $conn->execQuery('SELECT ach_id, status FROM share_achievement WHERE accountid=:accountid;',
array(
':accountid' => $account_id
));
if (count($rows) == 0) {
$share_meta_table = require('../res/share@share.php');
for ($i = 1; $i <= count($share_meta_table); $i++) {
$ret = $conn->execScript('INSERT INTO share_achievement(accountid, ach_id, status, create_time, modify_time) ' .
' VALUES(:account_id, :ach_id, :status, :create_time, :modify_time) ' .
' ON DUPLICATE KEY UPDATE accountid=:account_id, ach_id=:ach_id, status=:status, modify_time=:modify_time;',
array(
':account_id' => $account_id,
':ach_id' => $i,
':status' => 0,
':create_time' => time(),
':modify_time' => time()
));
if(!$ret){
die();
return;
}
array_push($info_list, array(
'achivement_id' => $i,
'status' => 0
));
}
} else {
foreach ($rows as $row) {
array_push($info_list, array(
'achivement_id' => $row['ach_id'],
'status' => $row['status']
));
}
}
echo json_encode(array(
'errcode' => 0,
'errmsg' => '',
'info_list' => $info_list
));
}
public function shareFriendReward()
{
$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;
}
$ach_id = $_REQUEST['ach_id'];
$double = $_REQUEST['double'];
$row = $conn->execQueryOne('SELECT status FROM share_achievement WHERE accountid=:accountid AND ach_id=:ach_id;',
array(
':accountid' => $account_id,
':ach_id' => $ach_id,
));
if (!$row) {
phpcommon\sendError(ERR_USER_BASE + 2, '没有这个玩家');
return;
}
if ($row['status'] != 0) {
phpcommon\sendError(ERR_USER_BASE + 3, '奖励已领取');
return;
}
//获取奖励信息
$url = '';
if (SERVER_ENV == _ONLINE) {
$url = 'https://service.kingsome.cn/webapp/index.php?c=AchievementShare&a=getInviteeNumSvr&';
} else {
$url = 'https://service-test.kingsome.cn/webapp/index.php?c=AchievementShare&a=getInviteeNumSvr&';
}
$timestamp = time();
$params = array(
'account_id' => $_REQUEST['account_id'],
'achievement_ids' => $ach_id,
);
$sign = phpcommon\md5Sign($params,
'70e32abc60367adccaa9eb7b56ed821b',
$timestamp);
$params['sign'] = $sign;
$params['timestamp'] = $timestamp;
$response = '';
if (!phpcommon\HttpClient::get($url, $params, $response)) {
phpcommon\sendError(ERR_RETRY, '系统繁忙');
return;
}
//领取奖励
$data = json_decode($response, true);
$ach_list = $data['invitee_nums'];
$peo_num = 0;
$sh = $this->getShare($ach_id);
$num = 0;
if ($double != 0) {
$num = 2;
} else {
$num = 1;
}
foreach($ach_list as $ach){
$ach_id = $ach['achievement_id'];
$peo_num = $ach['invitee_num'];
}
if ($peo_num == 0) {
phpcommon\sendError(ERR_USER_BASE + 4, '未达到人数要求');
return;
}
$array = $this->getExplode($sh['rewards']);
$addreward = new classes\AddReward();
$addreward->addReward($array[0][0], $array[0][1] * $num, $account_id);
//更新状态
$ret = $conn->execScript('UPDATE share_achievement SET status=1, modify_time=:modify_time ' .
' WHERE accountid=:accountid AND ach_id=:ach_id;',
array(
':accountid' => $account_id,
':modify_time' => time(),
':ach_id' => $ach_id
));
if (!$ret) {
die();
return;
}
echo json_encode(array(
'errcode' => 0,
'errmsg' => '',
));
}
public function getKefuReward()
{
$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;
}
$mail_id = $_REQUEST['mail_ids'];
$url = '';
if (SERVER_ENV == _ONLINE) {
$url = 'https://gamemail.kingsome.cn/webapp/index.php?c=Mail&a=getAttachment&';
} else {
$url = 'https://gamemail-test.kingsome.cn/webapp/index.php?c=Mail&a=getAttachment&';
}
$params = array(
'account_id' => $_REQUEST['account_id'],
'mail_ids' => $mail_id,
'session_id' => $_REQUEST['session_id']
);
if (!phpcommon\HttpClient::get($url, $params, $response)) {
phpcommon\sendError(ERR_RETRY, '系统繁忙');
return;
}
$row = $conn->execQueryOne('SELECT kefu_status FROM user WHERE accountid=:accountid;',
array(
':accountid' => $account_id
));
if (!$row) {
phpcommon\sendError(ERR_USER_BASE + 2, '没有这个玩家');
return;
}
if ($row['kefu_status'] != 0) {
phpcommon\sendError(ERR_USER_BASE + 3, '客服奖励已领取');
die();
return;
}
$item_list = array();
$item_id = 0;
$item_num = 0;
$data = json_decode($response, true);
$errcode = $data['errcode'];
$errmsg = $data['errmsg'];
if ($errcode == 0) {
foreach($data['attachments'] as $kefu){
$item_id = $kefu['itemid'];
$item_num = $kefu['itemnum'];
}
$addreward = new classes\AddReward();
$addreward->addReward($item_id, $item_num, $account_id);
array_push($item_list, array(
'item_id' => $item_id,
'item_num' => $item_num,
));
//更新状态
$ret = $conn->execScript('UPDATE user SET kefu_status=1, modify_time=:modify_time ' .
' WHERE accountid=:accountid;',
array(
':accountid' => $account_id,
':modify_time' => time(),
));
if (!$ret) {
die();
return;
}
}
echo json_encode(array(
'errcode' => $errcode,
'errmsg' => $errmsg,
'item_list' => $item_list
));
}
public function kefuDoubleReward()
{
$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;
}
$mail_id = $_REQUEST['mail_ids'];
$d = $this->getDrop(24005);
if (!$d) {
phpcommon\sendError(ERR_USER_BASE + 3, '没有这个奖励');
return;
}
$item_id_array = $this->getExplode($d['item_id']);
$weight_sum = 0;
$keys = 0;
$item_num_array = $this->getExplode($d['num']);
$weight_array = $this->getExplode($d['weight']);
for ($i = 0; $i < count($weight_array); $i++) {
$weight_sum += $weight_array[$i][0];
}
srand(crc32($account_id . $mail_id));
$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];
$addreward = new classes\AddReward();
$addreward->addReward($item_id, $item_num, $account_id);
echo json_encode(array(
'errcode' => 0,
'errmsg' => '',
));
}
}
?>