function get_popup_messages($browser_save, &$session_pv, &$global_pv) { $NS_device_block = NS_SCHED_DEVICE; $NS_all_tasks = KEY_SCHED_LIST; $NS_data_lstname = DATA_SCHED_LIST; /****************************************** 新设备检查和颁发机会任务消息 ******************************************/ $sched_block = get_device_block($NS_device_block, $browser_save); $runing_items =& $sched_block['items']; $all_tasks = upddate_runing_items($NS_all_tasks, $browser_save, $runing_items); $session =& $sched_block['session']; $global =& $sched_block['global']; $now = time(); $session_pv = $session['pageviews']; $global_pv = $global['pageviews']; /************************************* 获取计划任务消息 *************************************/ $items_expired = array(); $items_result = array(); foreach ($runing_items as $task_id => &$task_info) { /************************************* 通用任务匹配模块 *************************************/ $task = $all_tasks[$task_id]; $task_print = $task_info['name'] . '(' . $task_info['run_times'] . '/' . $task['times'] . ')'; list($is_matched, $is_expired) = general_match_tasks($NS_data_lstname, $browser_save, $task_id, $task_info, $task); if ($is_expired) { $items_expired[] = $task_id; } if (!$is_matched) { continue; } /************************************* 任务其他内容匹配 *************************************/ //如果前面已经有了返回信息,这时遇到“互斥”只能忽略掉 if ($task['repel'] === 'true' && count($items_result) > 0) { omp_trace($task_print . ' repel'); continue; } //在时间区间内,但看看前面一个消息距离是否足够 if ($now - $session['last_time'] < $task['time_interval_pre']) { omp_trace($task_print . ' pre time not reach '); continue; } //检查发送周期,还没到时间的,则忽略 if ($task['time_interval_mode'] === 'relative') { $time_point = $task_info['last_time'] + $task['time_interval']; if ($now < $time_point) { omp_trace($task_print . ' time relative until ' . date(DATE_RFC822, $time_point)); continue; } } else { $interval = $task['time_interval']; $base_time = $task['start_time']; $lasttime_pass = $task_info['last_time'] - $base_time; $time_point = $base_time + intval($lasttime_pass / $interval + 1) * $interval; if ($now < $time_point) { omp_trace($task_print . ' time absolute until ' . date(DATE_RFC822, $time_point)); continue; } } //第一次时重建msg queue if ($task_info['pageviews'] === 1) { remake_msgque($task_info, $task); omp_trace('make queue: ' . json_encode($task_info['msg_queue'])); } //处理用户改变消息模式 $messages = $task['messages']; if ($task_info['ori_seq'] !== $task['msg_sequence'] || $task_info['ori_msglen'] !== count($messages) || $task_info['ori_times'] < $task['times']) { //如果用户增大了发生次数 remake_msgque($task_info, $task); $messages = $task['messages']; } //ok, 条件吻合了,可以发送信息了 $msg_queue = $task_info['msg_queue']; $msg_index = $msg_queue[$task_info['run_times']]; $selected_message = @$messages[$msg_index]; //居然取不到消息,那也不用发了 if (empty($selected_message)) { omp_trace($task_print . ' cant fetch message '); continue; } //保存要显示到客户端的消息 $items_result[] = $selected_message; //统计和日志 //成功取出了消息,设置状态 $task_info['run_times'] += 1; $task_info['last_time'] = $now; $session['last_time'] = $now; $session['run_times'] += 1; omp_trace($task_print . ' succeed ' . $selected_message['name']); //如果是互斥的信息,后面不用再匹配了 if ($task['repel'] === 'true') { omp_trace($task_print . 'break for repel'); break; } } //删除被撤销的任务 if (!empty($items_expired)) { omp_trace('expired ' . implode(',', $items_expired)); async_timer('/sched_list.php?force', 10); } //需要保存状态到memcached save_device_block($NS_device_block, $browser_save, $sched_block); return count($items_result) ? $items_result : false; }
function get_popup_messages() { global $CONFIGS, $DEVSAV; $confs = $CONFIGS['popup']; /****************************************** 新设备检查和颁发计划任务消息 ******************************************/ $runing_items =& $DEVSAV['popup_tasks']; $all_tasks = update_running_tasks($DEVSAV, $runing_items, $confs); $now = time(); $session_pv = $DEVSAV['session_pv']; $global_pv = $DEVSAV['pageview_count']; /************************************* 获取计划任务消息 *************************************/ $items_expired = array(); $items_result = array(); foreach ($runing_items as $task_id => &$task_info) { /************************************* 通用任务匹配模块 *************************************/ $task = $all_tasks[$task_id]; $task_print = $task_info['name'] . '(' . $task_info['run_times'] . '/' . $task['times'] . ')'; list($is_matched, $is_expired) = general_match_tasks($DEVSAV, $task_id, $task_info, $task); if ($is_expired) { $items_expired[] = $task_id; } if (!$is_matched) { continue; } /************************************* 任务其他内容匹配 *************************************/ //如果前面已经有了返回信息,这时遇到“互斥”只能忽略掉 if ($task['repel'] === 'true' && count($items_result) > 0) { omp_trace($task_print . ' repel'); continue; } //在时间区间内,但看看前面一个消息距离是否足够 if ($now - $task_info['last_time'] < $task['time_interval_pre']) { omp_trace($task_print . ' pre time not reach '); continue; } //检查发送周期,还没到时间的,则忽略 if ($task['time_interval_mode'] === 'relative') { $time_point = $task_info['last_time'] + $task['time_interval']; if ($now < $time_point) { omp_trace($task_print . ' time relative until ' . date(DATE_RFC822, $time_point)); continue; } } else { $interval = $task['time_interval']; $base_time = $task['start_time']; $lasttime_pass = $task_info['last_time'] - $base_time; $time_point = $base_time + intval($lasttime_pass / $interval + 1) * $interval; if ($now < $time_point) { omp_trace($task_print . ' time absolute until ' . date(DATE_RFC822, $time_point)); continue; } } //第一次时重建msg queue if ($task_info['pageviews'] === 1) { remake_msgque($task_info, $task); omp_trace('make queue: ' . json_encode($task_info['msg_queue'])); } //处理用户改变消息模式 $messages = $task['messages']; if ($task_info['ori_seq'] !== $task['msg_sequence'] || $task_info['ori_msglen'] !== count($messages) || $task_info['ori_times'] < $task['times']) { //如果用户增大了发生次数 remake_msgque($task_info, $task); $messages = $task['messages']; } //ok, 条件吻合了,可以发送信息了 $msg_queue = $task_info['msg_queue']; $try_index = $task_info['run_times'] % count($msg_queue); $msg_index = $msg_queue[$try_index]; $msg_index = fix_index($msg_index, $messages); $selected_message = @$messages[$msg_index]; //居然取不到消息,那也不用发了 if (empty($selected_message)) { omp_trace($task_print . ' cant fetch message index(' . $msg_index . ')'); continue; } //保存要显示到客户端的消息 $items_result[] = $selected_message; //统计和日志 //成功取出了消息,设置状态 $task_info['run_times'] += 1; $task_info['last_time'] = $now; $DEVSAV['popup_lasttime'] = $now; $DEVSAV['popup_times'] += 1; omp_trace($task_print . ' succeed ' . $selected_message['name']); //如果是互斥的信息,后面不用再匹配了 if ($task['repel'] === 'true') { omp_trace($task_print . 'break for repel'); break; } } //删除被撤销的任务 if (!empty($items_expired)) { omp_trace('expired ' . implode(',', $items_expired)); } return count($items_result) ? $items_result : false; }