setName('MlbbTaskHandle')->setDescription('【旅人】处理参与活动玩家任务状态(每天10点30分执行)'); } protected function execute(Input $input, Output $output) { ini_set('memory_limit', '1024M'); $this->now_time = date('Y-m-d H:i:s', time()); echo "\n handle.now_time: {$this->now_time}"; $result = (new HandleService())->getCacheGiftNum(); if($result['code'] == 200){ echo "\n"; $output->writeln("## li_bao_info"); foreach ($result['data'] as $key => $value) { $output->writeln("{$value['name']}-{$value['mark_code']}: 余量={$value['remaining_num']}, 缓量={$value['cache_num']}"); } } echo "\n"; $output->writeln("TaskHandle start: ". date('Y-m-d H:i:s')); $this->handle($output); $output->writeln("TaskHandle end: ".date('Y-m-d H:i:s')); } // 处理任务 public function handle($output){ $page = 1; $limit = 1000; $handle_sum = 0; // 处理总数 $LvRenSdk = new LvRenSdk(); do { $list = Db::table('lr_user_info')->field('id,user_id,username,sub_username,server_id,server_name,role_id,role_name') ->where('game_id', '>', 0) // ->where('user_id', 'in', [266035]) // 测试用户 ->page($page, $limit) // 分页查询 ->select(); if (empty($list)) { break; } $markValue = implode(',', array_column($list, 'user_id')); echo "handle.uids: {$markValue} \n"; foreach ($list as $row) { // ## 查询玩家在游戏那边的任务完成情况 $lrRes = $LvRenSdk->getTagAndValByRole($row['server_id'], $row['role_id']); if($lrRes['code'] != 200){ continue; } // ## 处理玩家已完成任务,并发放抽奖次数 $taskRes = $this->handleTask($row['user_id'], $lrRes['data']); if($taskRes['code'] == 200){ $handle_sum++; } } $page++; // 释放内存 unset($list); // 可适当 sleep,避免对数据库压力过大 usleep(10000); } while (true); $output->writeln("handle_sum=".$handle_sum); } /** * 处理玩家已完成任务并发放抽奖次数 * * @param $user_id string 用户ID * @param $lrInfo array 游戏那边已完成任务情况 * * @return array */ public function handleTask($user_id, $lrInfo) { // 已完成任务情况 - 范例 // $lrInfo = [ // "1" => "5", // 登录天数 // "2" => "1", // 游戏完成一次充值 // "3" => "20000", // 累计充值魔币 // // "4" => "1", // 累计登录3天 // "5" => "55", // 等级 // "6" => "1", // 完成一次职业进阶 // "7" => "12000" // 个人评分 // ]; // 任务列表 $taskList = [ 1 => ['mark' => "1", 'title' => '累计登录魔力宝贝旅人 1天', 'status' => 1, 'key' => '1', 'value' => '1'], // status: 1=未完成,2=已完成,3=已领取 2 => ['mark' => "2", 'title' => '累计登录魔力宝贝旅人 3天', 'status' => 1, 'key' => '1', 'value' => '3'], 3 => ['mark' => "3", 'title' => '累计登录魔力宝贝旅人 5天', 'status' => 1, 'key' => '1', 'value' => '5'], 4 => ['mark' => "4", 'title' => '完成一次 职业进阶', 'status' => 1, 'key' => '6', 'value' => '1'], 5 => ['mark' => "5", 'title' => '等级达到 Lv33', 'status' => 1, 'key' => '5', 'value' => '33'], 6 => ['mark' => "6", 'title' => '等级达到 Lv41', 'status' => 1, 'key' => '5', 'value' => '41'], 7 => ['mark' => "7", 'title' => '等级达到 Lv55', 'status' => 1, 'key' => '5', 'value' => '55'], 8 => ['mark' => "8", 'title' => '个人评分达到 8000', 'status' => 1, 'key' => '7', 'value' => '8000'], 9 => ['mark' => "9", 'title' => '个人评分达到 12000', 'status' => 1, 'key' => '7', 'value' => '12000'], 10 => ['mark' => "10", 'title' => '游戏内完成一次 充值', 'status' => 1, 'key' => '2', 'value' => '1'], 11 => ['mark' => "11", 'title' => '累计充值达到 10000魔币', 'status' => 1, 'key' => '3', 'value' => '10000'], 12 => ['mark' => "12", 'title' => '累计充值达到 20000魔币', 'status' => 1, 'key' => '3', 'value' => '20000'], ]; // 查询任务完成情况 $userTaskMark = Db::table('lr_user_task')->where(['user_id' => $user_id])->column('mark'); $lrInfoKey = array_keys($taskList); // 游戏已完成任务 $handleTaskKey = array_values(array_diff($lrInfoKey, $userTaskMark)); // 待处理完成的任务(去除库中已完成的任务) $userTaskData = []; foreach ($handleTaskKey as $v) { if(empty($taskList[$v]) || empty($lrInfo[$taskList[$v]['key']])){ continue; } if($lrInfo[$taskList[$v]['key']] >= $taskList[$v]['value']){ // 完成任务 $userTaskData[] = [ 'user_id' => $user_id, 'mark' => $taskList[$v]['mark'], 'title' => $taskList[$v]['title'], 'status' => 2, // 1=已完成,2=已领取 'craete_time' => $this->now_time ]; } } if(!$userTaskData){ return ['code' => -110, 'msg' => '没有可处理任务', 'data' => []]; } Db::startTrans(); try { $res = Db::table('lr_user_task')->insertAll($userTaskData); if(!$res){ throw new \Exception('任务存储失败!'); } // 完成任务,发放抽奖次数 $res = Db::table('lr_user_info')->where(['user_id' => $user_id])->update(['lottery_count' => Db::raw('lottery_count + '. count($userTaskData)), 'update_time' => $this->now_time]); if(!$res){ throw new \Exception('抽奖次数发放失败!'); } $markValue = implode(',', array_column($userTaskData, 'mark')); echo "SUCCESS: uid={$user_id}, data={$markValue} \n"; Db::commit(); } catch (\Exception $e) { Db::rollback(); echo "FAIL: uid={$user_id}, msg={$e->getMessage()} \n"; return ['code' => -199, 'msg' => '任务处理失败:' . $e->getMessage(), 'data' => []]; } return ['code' => 200, 'msg' => '任务处理成功', 'data' => []]; } }