234 lines
6.8 KiB
PHP
234 lines
6.8 KiB
PHP
<?php
|
||
/**任务入库操作
|
||
* Created by PhpStorm.
|
||
* User: bo
|
||
* Date: 2018/12/07
|
||
* Time: 09:30
|
||
*/
|
||
class ajax_task extends ajax_base{
|
||
|
||
//把用户每日的订单任务入库
|
||
public function save_user_lottery_task(){
|
||
//获取设置的值
|
||
$arr = $this->arr;
|
||
$conn = $GLOBALS['conn'];
|
||
$myredis = $GLOBALS['myredis'];
|
||
$uid = $arr['token']['uid'];
|
||
$ip = $arr["ip"];
|
||
|
||
//任务信息
|
||
$lottery_info = $GLOBALS["SYS_FLOWER_LOTTERY"];
|
||
$lottery_arr = array();
|
||
foreach($lottery_info as $k=>$v){
|
||
$lottery_arr[$k] = array(//任务id
|
||
'weight' => $v['weight'],//任务权重
|
||
'grade' => $v['grade'],//任务所需最低等级
|
||
'grade2' => $v['grade2'] //任务所需最高等级
|
||
);
|
||
}
|
||
//获取所有用户数组
|
||
$user_data = $this -> _get_user_arr($uid,$lottery_arr,$conn,$myredis);
|
||
|
||
if(!$user_data){
|
||
/*$return_arr = array(
|
||
'ga' => 'HTTP_ERROR_BAK',
|
||
"code" => 2,//1成功,2失败,3停服维护
|
||
"msg" => '没有数据' //登陆信息提示
|
||
);*/
|
||
$return_arr = array(
|
||
"ga" => 'save_user_lottery_task_bak',
|
||
"code" => 1,
|
||
"gd" => "没有数据"
|
||
);
|
||
die(return_ajax($return_arr));
|
||
}
|
||
|
||
//任务入库
|
||
$succ_unm = 0;
|
||
$fail_unm = 0;
|
||
foreach($user_data as $k=>$v){
|
||
if($v){
|
||
foreach($v as $val){
|
||
$add_arr = array();
|
||
$tmp_lottery = '';
|
||
$tmp_lottery = $lottery_info[$val];
|
||
|
||
$user_prop = get_game_user_prop($k,$conn,$myredis);
|
||
$grade = count($GLOBALS["SYS_FLOWER_MEMBER_2"]);//配置表的总会员等级
|
||
//获取用户等级可接任务数量
|
||
$user_grade = $GLOBALS["SYS_FLOWER_MEMBER_2"][($user_prop['grade']>$grade?$grade:$user_prop['grade'])]['lottery_num'];
|
||
//用户已接任务
|
||
$where = "select * from ".get_table("game_global_lottery_log")." where 1 and `uid`='".$k."' and left(add_time,10)='".date("Y-m-d",time())."' ";
|
||
$field_tol = $conn->NumRows( $conn->Query($where) );
|
||
if($field_tol>=$user_grade){
|
||
break;
|
||
}else{
|
||
//如果当天没有,则进行添加
|
||
$where = " and `uid`='".$k."' and `lottery_id`='".$val."' and left(add_time,10)='".date("Y-m-d",time())."' ";
|
||
$exist = "select 1 from ".get_table('game_global_lottery_log')." where 1 ".$where;
|
||
$result = $conn->NumRows( $conn->Query($exist) );
|
||
if(!$result){
|
||
$add_arr = array(
|
||
'uid' => $k,// 用户ID
|
||
'lottery_id' => $val,// 任务ID
|
||
'task_uid' => '0',// 分配任务用户id
|
||
'type' => $tmp_lottery['type'],// 任务类型
|
||
'add_time' => date("Y-m-d H:i:s",time()),// 接任务时间
|
||
'add_ip' => $ip,// 接任务时IP
|
||
'info' => '',// 说明
|
||
'get_good' => json_encode($tmp_lottery['goods3']),// 实际获取物品
|
||
'num' => 0,// 接任务的次数
|
||
'is_ok' => 1,// 是否已完成任务
|
||
'up_time' => '0000-00-00 00:00:00',// 完成时间
|
||
'up_ip' => '' // 完成时IP
|
||
|
||
);
|
||
$add_rs = add_record($conn,'game_global_lottery_log',$add_arr);
|
||
if(!$add_rs){
|
||
sys_log_write_content( 'game_global_lottery_log:'.json_encode($add_arr) ,"error_log","game_global_lottery_log_error");
|
||
unset($add_rs,$add_rs);
|
||
$fail_unm ++;
|
||
}else{
|
||
$succ_unm ++;
|
||
}
|
||
}
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
$return_arr = array(
|
||
"ga" => 'save_user_lottery_task_bak',
|
||
"code" => 1,
|
||
"gd" => "完成数量:".$succ_unm."条,失败:".$fail_unm."条。"
|
||
);
|
||
die(return_ajax($return_arr));
|
||
|
||
}
|
||
|
||
/*
|
||
* 获取所有用户数组
|
||
* $lottery_arr['任务id']=>array(//任务数组
|
||
'weight'=>'任务权重',
|
||
'grade'=>'任务所需最低等级'
|
||
'grade2'=>'任务所需最高等级'
|
||
)
|
||
*/
|
||
private function _get_user_arr($uid,$lottery_arr,$conn=false, $myredis=false){
|
||
if(empty($lottery_arr)){
|
||
return false;
|
||
}
|
||
$user_arr = array();
|
||
$lottery = array();
|
||
$lottery_result = array();
|
||
if($uid){
|
||
$sql = "select uid,grade from ".get_table("game_member_prop")." where uid='".$uid."'";
|
||
}else{
|
||
$sql = "select uid,grade from ".get_table("game_member_prop");
|
||
}
|
||
$query = $conn->Query($sql);
|
||
while($value = $conn -> FetchArray($query)) {
|
||
$grade = count($GLOBALS["SYS_FLOWER_MEMBER_2"]);//配置表的总会员等级
|
||
//获取用户等级可接任务数量
|
||
$user_grade = $GLOBALS["SYS_FLOWER_MEMBER_2"][($value['grade']>$grade?$grade:$value['grade'])]['lottery_num'];
|
||
//用户已接任务
|
||
$where = "select * from ".get_table("game_global_lottery_log")." where 1 and `uid`='".$value['uid']."' and left(add_time,10)='".date("Y-m-d",time())."' ";
|
||
$field_tol = $conn->NumRows( $conn->Query($where) );
|
||
if($field_tol<$user_grade){
|
||
//获取用户可接的任务数组
|
||
$lottery = $this -> _get_my_lottery_data($value['grade'],$lottery_arr);
|
||
//获取用户最终的任务id数组
|
||
$user_grade = $user_grade>count($lottery)?count($lottery):$user_grade;
|
||
$lottery_result = $this -> _get_user_lottery_id($lottery,$user_grade);
|
||
$user_arr[$value['uid']] = $lottery_result;
|
||
}
|
||
}
|
||
return $user_arr;
|
||
}
|
||
|
||
/**
|
||
* 获取用户最终的任务id数组
|
||
* $num => 可接任务数量
|
||
* $data['任务id'] => array(
|
||
'权重值'
|
||
)
|
||
* 返回 $result[]=>'任务id'
|
||
*/
|
||
private function _get_user_lottery_id($data=array(),$num=1){
|
||
if(empty($data)){
|
||
return false;
|
||
}
|
||
$result = array();
|
||
for($i=0;$i<$num;$i++){
|
||
$result[] = $this -> _do_rand_data($data);
|
||
}
|
||
if (count($result) != count(array_unique($result))) {//检测是否有重复 如果有则重来
|
||
return $this -> _get_user_lottery_id($data,$num);
|
||
}else{
|
||
return $result;
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 获取用户可接的任务数组
|
||
* $grade=>用户等级
|
||
* $data['任务id']=>array(
|
||
'weight'=>'任务权重',
|
||
'grade'=>'任务所需最低等级'
|
||
'grade2'=>'任务所需最高等级'
|
||
)
|
||
* 返回 $result['任务id']=>array(
|
||
'权重值'
|
||
)
|
||
*/
|
||
private function _get_my_lottery_data($grade=1,$data=array()){
|
||
if(empty($data)){
|
||
return false;
|
||
}
|
||
$result = array();
|
||
foreach($data as $k=>$v){
|
||
if($grade>=$v['grade'] && $grade<=$v['grade2']){
|
||
$result[$k] = array($v['weight']);
|
||
}
|
||
}
|
||
return $result;
|
||
}
|
||
|
||
//按比率出现对应类型的数据
|
||
/*
|
||
* $type=array(
|
||
'1'=>340,
|
||
'2'=>340,
|
||
'3'=>340
|
||
);
|
||
*/
|
||
private function _do_rand_data($type){
|
||
$all_sum = 0;//总分数
|
||
foreach($type as $key=>$val){
|
||
if(is_array($val)){//如果是数组
|
||
$all_sum += array_sum($val);//计算总分数
|
||
}
|
||
}
|
||
$tmp_num = mt_rand(1, $all_sum);//产生一个随机数
|
||
$tmp_i = 0;//起始数据
|
||
foreach($type as $key=>$val){
|
||
if(is_array($val)){
|
||
foreach($val as $key2=>$val2){
|
||
$tmp_i2 = $tmp_i+$val2;
|
||
//如果在机率范转内,则反回对应的类型key
|
||
if($tmp_i < $tmp_num && $tmp_num<=$tmp_i2){
|
||
return $key;
|
||
}
|
||
$tmp_i = $tmp_i2;
|
||
}
|
||
}
|
||
}
|
||
return $this -> _do_rand_data($type);//如果没有找到,则再找
|
||
}
|
||
|
||
|
||
|
||
|
||
|
||
|
||
} |