public static function getRandomAI($room_id){
if (isset(static::$AIMap[$room_id]) && (time() - static::$AIMap[$room_id]['time']) < 8) {
var_dump('静态变量机器人');var_dump(static::$AIMap[$room_id]['list']);
return static::$AIMap[$room_id]['list'];
}
//$tmpAI = array();
//$num = isset(static::$AIMap[$room_id]) ? count(static::$AIMap[$room_id]) : mt_rand(25, 65);
$virtual = Db::instance('dbDefault')->query("select isvirtual, virtualguest from ss_member where curroomnum = $room_id ");
$global_robot = Db::instance('dbDefault')->query("select robot_switch, robot_max from ss_siteconfig where id = 1 ");
$countAI = Db::instance('dbDefault')->query("select count( id ) num from ss_member where is_robot = 1 ");
if( $global_robot[0]['robot_switch'] == '1' ){
if( $virtual[0]['isvirtual'] == 'y' ){
$AInum = $virtual[0]['virtualguest'] > $countAI[0]['num'] ? $countAI[0]['num'] : $virtual[0]['virtualguest'];
//刚开播i
var_dump('房间允许最大机器人数量'.$AInum);
if(!Mem::get('TIME_AI_'.$room_id)){
$tmpAI = array();
//设置播放时间
Mem::set('TIME_AI_'.$room_id,time());
//开播初始机器人缓存
Mem::set('ROOM_AI_'.$room_id,0);
var_dump('该房间第一次循环机器人,机器人数量:'.Mem::get('ROOM_AI'.$room_id));
//初始化机器人数量
$currentAIcount = 0;
}else{
$tmpAI = static::$AIMap[$room_id]['list'];
//已有开播记录 获取开播时间
$time = Mem::get('TIME_AI_'.$room_id);
$timehalfmin = time() - $time;
var_dump('已有开播时间,开播时间为'.$time);
//五秒循环一次 更新播放时间
if($timehalfmin > 5){
var_dump('开始更新机器人,重新设置当前房间时间戳');
Mem::set('TIME_AI_'.$room_id,time());
$currentAIcount = Mem::get('ROOM_AI_'.$room_id);
//如果设置机器人数量大于当前累加机器人数量 继续累加
if($AInum > $currentAIcount){
$currentAIcount = $currentAIcount + 1;
Mem::set('ROOM_AI_'.$room_id,$currentAIcount);
}
}
}
var_dump('设置结束缓存,开始循环');
var_dump('当前currentAIcount'.$currentAIcount);
var_dump('当前tmpAI:'.count($tmpAI));
//while防止死循环导致聊天服务崩溃
$whiletime = 1;
while(count($tmpAI) < $currentAIcount && static::$AIList) {
var_dump('****************************************'.$whiletime);
$whiletime ++;
if($whiletime > 10){
break;
}
$key = array_rand(static::$AIList);
if(!$key) {
continue;
}
$ai = static::$AIList[$key];
if (!isset($tmpAI[$ai['username']])) {
$tmpAI[$ai['username']] = $ai;
}
}
static::$AIMap[$room_id] = array('time'=>time(), 'list'=>$tmpAI);
return $tmpAI;
}else{
//如果总开关打开 但是个人开关关闭
$AInum = $global_robot[0]['robot_max'] > $countAI[0]['num'] ? $countAI[0]['num'] : $global_robot[0]['robot_max'];
$whiletime = 1;
while(count($tmpAI) < $AInum && static::$AIList) {
var_dump('****************************************'.$whiletime);
$whiletime ++;
if($whiletime > 10){
break;
}
$key = array_rand(static::$AIList);
if(!$key) {
continue;
}
$ai = static::$AIList[$key];
if (!isset($tmpAI[$ai['username']])) {
$tmpAI[$ai['username']] = $ai;
}
}
static::$AIMap[$room_id] = array('time'=>time(), 'list'=>$tmpAI);
return $tmpAI;
//return array();
}
}else{
//总开关关闭 个人开关关闭
return array();
}
}
if (isset(static::$AIMap[$room_id]) && (time() - static::$AIMap[$room_id]['time']) < 8) {
var_dump('静态变量机器人');var_dump(static::$AIMap[$room_id]['list']);
return static::$AIMap[$room_id]['list'];
}
//$tmpAI = array();
//$num = isset(static::$AIMap[$room_id]) ? count(static::$AIMap[$room_id]) : mt_rand(25, 65);
$virtual = Db::instance('dbDefault')->query("select isvirtual, virtualguest from ss_member where curroomnum = $room_id ");
$global_robot = Db::instance('dbDefault')->query("select robot_switch, robot_max from ss_siteconfig where id = 1 ");
$countAI = Db::instance('dbDefault')->query("select count( id ) num from ss_member where is_robot = 1 ");
if( $global_robot[0]['robot_switch'] == '1' ){
if( $virtual[0]['isvirtual'] == 'y' ){
$AInum = $virtual[0]['virtualguest'] > $countAI[0]['num'] ? $countAI[0]['num'] : $virtual[0]['virtualguest'];
//刚开播i
var_dump('房间允许最大机器人数量'.$AInum);
if(!Mem::get('TIME_AI_'.$room_id)){
$tmpAI = array();
//设置播放时间
Mem::set('TIME_AI_'.$room_id,time());
//开播初始机器人缓存
Mem::set('ROOM_AI_'.$room_id,0);
var_dump('该房间第一次循环机器人,机器人数量:'.Mem::get('ROOM_AI'.$room_id));
//初始化机器人数量
$currentAIcount = 0;
}else{
$tmpAI = static::$AIMap[$room_id]['list'];
//已有开播记录 获取开播时间
$time = Mem::get('TIME_AI_'.$room_id);
$timehalfmin = time() - $time;
var_dump('已有开播时间,开播时间为'.$time);
//五秒循环一次 更新播放时间
if($timehalfmin > 5){
var_dump('开始更新机器人,重新设置当前房间时间戳');
Mem::set('TIME_AI_'.$room_id,time());
$currentAIcount = Mem::get('ROOM_AI_'.$room_id);
//如果设置机器人数量大于当前累加机器人数量 继续累加
if($AInum > $currentAIcount){
$currentAIcount = $currentAIcount + 1;
Mem::set('ROOM_AI_'.$room_id,$currentAIcount);
}
}
}
var_dump('设置结束缓存,开始循环');
var_dump('当前currentAIcount'.$currentAIcount);
var_dump('当前tmpAI:'.count($tmpAI));
//while防止死循环导致聊天服务崩溃
$whiletime = 1;
while(count($tmpAI) < $currentAIcount && static::$AIList) {
var_dump('****************************************'.$whiletime);
$whiletime ++;
if($whiletime > 10){
break;
}
$key = array_rand(static::$AIList);
if(!$key) {
continue;
}
$ai = static::$AIList[$key];
if (!isset($tmpAI[$ai['username']])) {
$tmpAI[$ai['username']] = $ai;
}
}
static::$AIMap[$room_id] = array('time'=>time(), 'list'=>$tmpAI);
return $tmpAI;
}else{
//如果总开关打开 但是个人开关关闭
$AInum = $global_robot[0]['robot_max'] > $countAI[0]['num'] ? $countAI[0]['num'] : $global_robot[0]['robot_max'];
$whiletime = 1;
while(count($tmpAI) < $AInum && static::$AIList) {
var_dump('****************************************'.$whiletime);
$whiletime ++;
if($whiletime > 10){
break;
}
$key = array_rand(static::$AIList);
if(!$key) {
continue;
}
$ai = static::$AIList[$key];
if (!isset($tmpAI[$ai['username']])) {
$tmpAI[$ai['username']] = $ai;
}
}
static::$AIMap[$room_id] = array('time'=>time(), 'list'=>$tmpAI);
return $tmpAI;
//return array();
}
}else{
//总开关关闭 个人开关关闭
return array();
}
}