} // 更新用户组 user_update_group($uid); return TRUE; } return FALSE; } // 获取任务日志 function task_log_read($uid, $tid) { $log = db_find_one('task_log', array('uid' => $uid, 'tid' => $tid)); return $log; } // 创建任务日志 function task_log_create($arr) { $r = db_insert('task_log', $arr); return $r; } // 更新任务日志 function task_log_update($uid, $tid, $arr) { // 如果是增加进度的操作 if(isset($arr['progress+'])) { // 先获取当前任务信息 $task = task_read($tid); if($task) { $log = task_log_read($uid, $tid); if($log) { // 如果是日常任务,检查是否需要重置 if($task['type'] == 2) { // 检查是否需要重置日常任务 $today_start = strtotime(date('Y-m-d 00:00:00')); // 解析任务条件 $condition = json_decode($task['condition'], true); $target = isset($condition['target']) ? intval($condition['target']) : 1; if($log['complete_date'] > 0 && $log['complete_date'] < $today_start) { // 已完成的任务且是昨天或更早完成的,需要重置 $arr = array( 'progress' => 1, 'status' => 0, 'complete_date' => 0 ); } else { // 未完成或今天完成的任务,检查是否达到目标次数 if($log['progress'] < $target) { // 未达到目标次数,继续累加 $new_progress = $log['progress'] + $arr['progress+']; $arr = array( 'progress' => $new_progress ); // 检查累加后是否达到目标 if($new_progress >= $target) { $arr['status'] = 1; $arr['complete_date'] = time(); } } else { // 已达到目标次数,不再累加 return 0; } } } else { // 成就任务,直接累加进度 $new_progress = $log['progress'] + $arr['progress+']; $arr = array( 'progress' => $new_progress ); // 解析任务条件 $condition = json_decode($task['condition'], true); $target = isset($condition['target']) ? intval($condition['target']) : 1; // 检查是否达到目标 if($new_progress >= $target && $log['status'] == 0) { $arr['status'] = 1; $arr['complete_date'] = time(); } } } else { // 没有任务日志,创建新的 $arr = array( 'progress' => 1, 'status' => 0, 'complete_date' => 0 ); } } } // 移除progress+字段 unset($arr['progress+']); $r = db_update('task_log', array('uid' => $uid, 'tid' => $tid), $arr); return $r; } // 获取用户任务统计 function task_stat_read($uid) { $stat = db_find_one('task_stat', array('uid' => $uid)); if(empty($stat)) { $stat = array( 'uid' => $uid, 'total_complete' => 0, 'novice_complete' => 0, 'daily_complete' => 0, 'achieve_complete' => 0, 'activity_complete' => 0, 'last_daily_date' => 0 ); db_insert('task_stat', $stat); } return $stat; } // 检查任务是否过期 function task_check_expired($task) { if(empty($task['end_date']) || $task['end_date'] == 0) { return FALSE; } return time() > $task['end_date']; } // 清理过期任务日志 function task_clean_expired_logs() { $tasklist = task_find(array('available' => 1)); $expired_tids = array(); foreach($tasklist as $task) { if(task_check_expired($task)) { $expired_tids[] = $task['tid']; } } if(!empty($expired_tids)) { // 将未完成的过期任务日志标记为失败 db_update('task_log', array( 'tid' => array('IN', $expired_tids), 'status' => 0 ), array( 'status' => 2 // 2表示任务失败 )); } } // 手动更新用户任务统计 function task_stat_rebuild($uid) { // 获取用户所有已完成的任务 $logs = db_find('task_log', array( 'uid' => $uid, 'status' => 1 // 1=已完成 )); // 初始化统计数据 $stat = array( 'uid' => $uid, 'total_complete' => 0, 'novice_complete' => 0, 'daily_complete' => 0, 'achieve_complete' => 0, 'last_daily_date' => 0 ); if(!empty($logs)) { foreach($logs as $log) { $task = task_read($log['tid']); if($task) { $stat['total_complete']++; switch($task['type']) { case 1: // 新手任务 $stat['novice_complete']++; break; case 2: // 日常任务 $stat['daily_complete']++; if($log['complete_date'] > $stat['last_daily_date']) { $stat['last_daily_date'] = $log['complete_date']; } break; case 3: // 成就任务 $stat['achieve_complete']++; break; } } } } // 更新或插入统计数据 $exists = db_find_one('task_stat', array('uid' => $uid)); if($exists) { db_update('task_stat', array('uid' => $uid), $stat); } else { db_insert('task_stat', $stat); } return $stat; } ?>