function get_replace_messages($browser_save, $PARAMS) { $NS_device_block = NS_PLANS_DEVICE; $NS_all_tasks = KEY_PLANS_LIST; $NS_data_lstname = DATA_PLANS_LIST; /****************************************** 新设备检查和颁发机会任务消息 ******************************************/ $replace_block = get_device_block($NS_device_block, $browser_save); $runing_items =& $replace_block['items']; $all_tasks = upddate_runing_items($NS_all_tasks, $browser_save, $runing_items); $session =& $replace_block['session']; $global =& $replace_block['global']; $now = time(); //如果没有消息,则不必要触发下面的任务检查流程了 $accept_posi_nums = []; foreach ($PARAMS as $name => $value) { if (filter_md5_keys($name)) { $accept_posi_nums[$name] = $value; } } if (empty($accept_posi_nums)) { save_device_block($NS_device_block, $browser_save, $replace_block); omp_trace('no posi input'); return false; } if (empty($runing_items)) { save_device_block($NS_device_block, $browser_save, $replace_block); omp_trace('no runing_items exists'); return false; } /************************************* 获取计划任务消息 *************************************/ $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) { omp_trace($task_print . ' general match tasks false'); continue; } /************************************* 任务其他内容匹配 *************************************/ $task_messages = $task['messages']; $messages = []; omp_trace('apply position: ' . implode(',', array_keys($accept_posi_nums))); foreach ($task_messages as $item) { $key = $item['position']; $dbg_str = "has msg \"" . $item['title'] . "\""; if (array_key_exists($key, $accept_posi_nums)) { $dbg_str .= ' matched key:' . $key . ' num:' . $accept_posi_nums[$key]; $messages[] = $item; } omp_trace($dbg_str); } //提交的未知,没有适合的消息,后面不需处理和统计 if (empty($messages)) { omp_trace($task_print . ' no messages retrive'); continue; } $max_num = $task['max_perpage']; if ($max_num == 0) { omp_trace($task_print . ' max_perpage === 0'); continue; } //哇,有消息,做任务统计了 $task_info['pageviews'] += 1; //在时间区间内,但看看前面一个消息距离是否足够 $valid_pageview = $task_info['pageviews'] - $task['interval_pre']; if ($valid_pageview <= 0) { omp_trace($task_print . ' pre pageviews not reach '); continue; } //看看间隔是否达到了 if ($task['interval'] > 0) { $step = $valid_pageview % ($task['interval'] + 1); if ($step !== 0) { omp_trace($task_print . ' pageviews internal not reach '); continue; } } $task_msg_count = count($task_messages); $used_queue = fill_replace_queue($task['msg_sequence'], @$task_info['used_queue'], $task_msg_count); omp_trace("fill queue({$task['msg_sequence']}): " . implode(',', $used_queue)); while (count($used_queue)) { //根据pop出的index选取消息 $msg_index = array_shift($used_queue); if ($msg_index === null) { $used_queue = fill_replace_queue($task['msg_sequence'], [], $task_msg_count); $msg_index = array_shift($used_queue); omp_trace('remake used_queue'); } $selected_message = @$task_messages[$msg_index]; //如果取不到,队列有错了吧 if (empty($selected_message)) { omp_trace('selected_message empty'); continue; } //这个消息,不符合客户端提交的位置 if (!array_key_exists($selected_message['position'], $accept_posi_nums)) { omp_trace('not mine: ' . $selected_message['title']); continue; } //这个消息所在的位置,已经用完了 $posi_key = $selected_message['position']; if ($accept_posi_nums[$posi_key] === 0) { omp_trace('posi done: ' . $posi_key . ' by ' . $selected_message['title']); continue; } //肯定能找到接收的,这个消息将收录为结果 $items_result[] = $selected_message; $accept_posi_nums[$posi_key] -= 1; omp_trace('<--- result msg(' . count($items_result) . '): ' . $selected_message['title']); //统计最大的客户端接收 个数 if ($max_num > 0) { $max_num -= 1; if ($max_num === 0) { omp_trace('break of reaching max allow number'); break; } } $posi_maxnum = 0; foreach ($accept_posi_nums as $key => $val) { $posi_maxnum += $val; } if ($posi_maxnum === 0) { omp_trace('break of reaching max posi number'); break; } } //保存这次剩下的"随机"列表,供下次使用 $task_info['used_queue'] = $used_queue; omp_trace("after queue({$task['msg_sequence']}): " . implode(',', $used_queue)); //统计和日志 //成功取出了消息,设置状态 $task_info['run_times'] += 1; $task_info['last_time'] = $now; $session['last_time'] = $now; $session['run_times'] += 1; } //删除被撤销的任务 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, $replace_block); return count($items_result) ? $items_result : false; }
function get_replace_messages() { global $CONFIGS, $DEVSAV, $PARAMS; $confs = $CONFIGS['replace']; /****************************************** 新设备检查和颁发机会任务消息 ******************************************/ $runing_items =& $DEVSAV['replace_tasks']; $all_tasks = update_running_tasks($DEVSAV, $runing_items, $confs); $now = time(); $session_pv = $DEVSAV['session_pv']; $global_pv = $DEVSAV['pageview_count']; //如果没有消息,则不必要触发下面的任务检查流程了 $accept_posi_nums = []; foreach ($PARAMS as $name => $value) { if (is_md5_keys($name)) { $accept_posi_nums[$name] = $value; } } if (empty($accept_posi_nums)) { omp_trace('no posi input'); return false; } if (empty($runing_items)) { omp_trace('no runing_items exists'); return false; } /************************************* 获取计划任务消息 *************************************/ $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) { omp_trace($task_print . ' general match tasks false'); continue; } /************************************* 任务其他内容匹配 *************************************/ $task_messages = $task['messages']; $messages = []; omp_trace('apply position: ' . implode(',', array_keys($accept_posi_nums))); foreach ($task_messages as $item) { $dbg_str = "has msg \"" . $item['title'] . "\""; $posi_list = $item['position']; foreach ($posi_list as $key) { if (array_key_exists($key, $accept_posi_nums)) { $dbg_str .= ' matched key:' . $key . ' num:' . $accept_posi_nums[$key]; $messages[] = $item; } } omp_trace($dbg_str); } //提交的未知,没有适合的消息,后面不需处理和统计 if (empty($messages)) { omp_trace($task_print . ' no messages retrive'); continue; } $max_num = $task['max_perpage']; if ($max_num == 0) { omp_trace($task_print . ' max_perpage === 0'); continue; } //哇,有消息,做任务统计了 $task_info['pageviews'] += 1; //在时间区间内,但看看前面一个消息距离是否足够 $valid_pageview = $task_info['pageviews'] - $task['pv_interval_pre']; if ($valid_pageview <= 0) { omp_trace($task_print . ' pre pageviews not reach '); continue; } //看看间隔是否达到了 if ($task['pv_interval'] > 0) { $step = $valid_pageview % ($task['pv_interval'] + 1); if ($step !== 0) { omp_trace($task_print . ' pageviews internal not reach '); continue; } } $task_msg_count = count($task_messages); $used_queue = fill_replace_queue($task['msg_sequence'], @$task_info['used_queue'], $task_msg_count); omp_trace("fill queue({$task['msg_sequence']}): " . implode(',', $used_queue)); while (count($used_queue)) { //根据pop出的index选取消息 $msg_index = array_shift($used_queue); if ($msg_index === null) { $used_queue = fill_replace_queue($task['msg_sequence'], [], $task_msg_count); $msg_index = array_shift($used_queue); omp_trace('remake used_queue'); } $selected_message = @$task_messages[$msg_index]; //如果取不到,队列有错了吧 if (empty($selected_message)) { omp_trace('selected_message empty'); continue; } //这个消息,不符合客户端提交的位置 $accept_posis = array_keys($accept_posi_nums); $msg_posis = $selected_message['position']; $matched_posi = array_intersect($accept_posis, $msg_posis); if (empty($matched_posi)) { omp_trace('not mine: ' . $selected_message['title']); continue; } //随便挑选一个位置 $picked_succ = false; foreach ($accept_posi_nums as $posi_key => &$nums) { if (in_array($posi_key, $matched_posi)) { $nums -= 1; $selected_message['position'] = [$posi_key]; $picked_succ = true; break; } } //这个消息所在的位置,已经用完了 if ($picked_succ === false) { omp_trace('posi done: ' . $posi_key . ' by ' . $selected_message['title']); continue; } //肯定能找到接收的,这个消息将收录为结果 $items_result[] = $selected_message; omp_trace('<--- result msg(' . count($items_result) . '): ' . $selected_message['title']); //统计最大的客户端接收 个数 if ($max_num > 0) { $max_num -= 1; if ($max_num === 0) { omp_trace('break of reaching max allow number'); break; } } $posi_maxnum = 0; foreach ($accept_posi_nums as $key => $val) { $posi_maxnum += $val; } if ($posi_maxnum === 0) { omp_trace('break of reaching max posi number'); break; } } //保存这次剩下的"随机"列表,供下次使用 $task_info['used_queue'] = $used_queue; omp_trace("after queue({$task['msg_sequence']}): " . implode(',', $used_queue)); //统计和日志 //成功取出了消息,设置状态 $task_info['run_times'] += 1; $task_info['last_time'] = $now; $DEVSAV['replace_lasttime'] = $now; $DEVSAV['replace_times'] += 1; } //删除被撤销的任务 if (!empty($items_expired)) { omp_trace('expired ' . implode(',', $items_expired)); } return count($items_result) ? $items_result : false; }