TaskHandle.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. <?php
  2. namespace app\crontab\mlbb;
  3. use app\api\library\mlbb\LvRenSdk;
  4. use app\api\service\mlbb\HandleService;
  5. use think\console\Command;
  6. use think\console\Input;
  7. use think\console\Output;
  8. use think\Db;
  9. set_time_limit(0);
  10. /**
  11. * 玩家参与活动的任务状态处理
  12. */
  13. class TaskHandle extends Command
  14. {
  15. private $now_time;
  16. protected function configure() {
  17. $this->setName('MlbbTaskHandle')->setDescription('【旅人】处理参与活动玩家任务状态(每天10点30分执行)');
  18. }
  19. protected function execute(Input $input, Output $output) {
  20. ini_set('memory_limit', '1024M');
  21. $this->now_time = date('Y-m-d H:i:s', time());
  22. echo "\n handle.now_time: {$this->now_time}";
  23. $result = (new HandleService())->getCacheGiftNum();
  24. if($result['code'] == 200){
  25. echo "\n";
  26. $output->writeln("## li_bao_info");
  27. foreach ($result['data'] as $key => $value) {
  28. $output->writeln("{$value['name']}-{$value['mark_code']}: 余量={$value['remaining_num']}, 缓量={$value['cache_num']}");
  29. }
  30. }
  31. echo "\n";
  32. $output->writeln("TaskHandle start: ". date('Y-m-d H:i:s'));
  33. $this->handle($output);
  34. $output->writeln("TaskHandle end: ".date('Y-m-d H:i:s'));
  35. }
  36. // 处理任务
  37. public function handle($output){
  38. $page = 1;
  39. $limit = 1000;
  40. $handle_sum = 0; // 处理总数
  41. $LvRenSdk = new LvRenSdk();
  42. do {
  43. $list = Db::table('lr_user_info')->field('id,user_id,username,sub_username,server_id,server_name,role_id,role_name')
  44. ->where('game_id', '>', 0)
  45. // ->where('user_id', 'in', [266035]) // 测试用户
  46. ->page($page, $limit) // 分页查询
  47. ->select();
  48. if (empty($list)) {
  49. break;
  50. }
  51. $markValue = implode(',', array_column($list, 'user_id'));
  52. echo "handle.uids: {$markValue} \n";
  53. foreach ($list as $row) {
  54. // ## 查询玩家在游戏那边的任务完成情况
  55. $lrRes = $LvRenSdk->getTagAndValByRole($row['server_id'], $row['role_id']);
  56. if($lrRes['code'] != 200){
  57. continue;
  58. }
  59. // ## 处理玩家已完成任务,并发放抽奖次数
  60. $taskRes = $this->handleTask($row['user_id'], $lrRes['data']);
  61. if($taskRes['code'] == 200){
  62. $handle_sum++;
  63. }
  64. }
  65. $page++;
  66. // 释放内存
  67. unset($list);
  68. // 可适当 sleep,避免对数据库压力过大
  69. usleep(10000);
  70. } while (true);
  71. $output->writeln("handle_sum=".$handle_sum);
  72. }
  73. /**
  74. * 处理玩家已完成任务并发放抽奖次数
  75. *
  76. * @param $user_id string 用户ID
  77. * @param $lrInfo array 游戏那边已完成任务情况
  78. *
  79. * @return array
  80. */
  81. public function handleTask($user_id, $lrInfo)
  82. {
  83. // 已完成任务情况 - 范例
  84. // $lrInfo = [
  85. // "1" => "5", // 登录天数
  86. // "2" => "1", // 游戏完成一次充值
  87. // "3" => "20000", // 累计充值魔币
  88. // // "4" => "1", // 累计登录3天
  89. // "5" => "55", // 等级
  90. // "6" => "1", // 完成一次职业进阶
  91. // "7" => "12000" // 个人评分
  92. // ];
  93. // 任务列表
  94. $taskList = [
  95. 1 => ['mark' => "1", 'title' => '累计登录魔力宝贝旅人 1天', 'status' => 1, 'key' => '1', 'value' => '1'], // status: 1=未完成,2=已完成,3=已领取
  96. 2 => ['mark' => "2", 'title' => '累计登录魔力宝贝旅人 3天', 'status' => 1, 'key' => '1', 'value' => '3'],
  97. 3 => ['mark' => "3", 'title' => '累计登录魔力宝贝旅人 5天', 'status' => 1, 'key' => '1', 'value' => '5'],
  98. 4 => ['mark' => "4", 'title' => '完成一次 职业进阶', 'status' => 1, 'key' => '6', 'value' => '1'],
  99. 5 => ['mark' => "5", 'title' => '等级达到 Lv33', 'status' => 1, 'key' => '5', 'value' => '33'],
  100. 6 => ['mark' => "6", 'title' => '等级达到 Lv41', 'status' => 1, 'key' => '5', 'value' => '41'],
  101. 7 => ['mark' => "7", 'title' => '等级达到 Lv55', 'status' => 1, 'key' => '5', 'value' => '55'],
  102. 8 => ['mark' => "8", 'title' => '个人评分达到 8000', 'status' => 1, 'key' => '7', 'value' => '8000'],
  103. 9 => ['mark' => "9", 'title' => '个人评分达到 12000', 'status' => 1, 'key' => '7', 'value' => '12000'],
  104. 10 => ['mark' => "10", 'title' => '游戏内完成一次 充值', 'status' => 1, 'key' => '2', 'value' => '1'],
  105. 11 => ['mark' => "11", 'title' => '累计充值达到 10000魔币', 'status' => 1, 'key' => '3', 'value' => '10000'],
  106. 12 => ['mark' => "12", 'title' => '累计充值达到 20000魔币', 'status' => 1, 'key' => '3', 'value' => '20000'],
  107. ];
  108. // 查询任务完成情况
  109. $userTaskMark = Db::table('lr_user_task')->where(['user_id' => $user_id])->column('mark');
  110. $lrInfoKey = array_keys($taskList); // 游戏已完成任务
  111. $handleTaskKey = array_values(array_diff($lrInfoKey, $userTaskMark)); // 待处理完成的任务(去除库中已完成的任务)
  112. $userTaskData = [];
  113. foreach ($handleTaskKey as $v) {
  114. if(empty($taskList[$v]) || empty($lrInfo[$taskList[$v]['key']])){
  115. continue;
  116. }
  117. if($lrInfo[$taskList[$v]['key']] >= $taskList[$v]['value']){ // 完成任务
  118. $userTaskData[] = [
  119. 'user_id' => $user_id,
  120. 'mark' => $taskList[$v]['mark'],
  121. 'title' => $taskList[$v]['title'],
  122. 'status' => 2, // 1=已完成,2=已领取
  123. 'craete_time' => $this->now_time
  124. ];
  125. }
  126. }
  127. if(!$userTaskData){
  128. return ['code' => -110, 'msg' => '没有可处理任务', 'data' => []];
  129. }
  130. Db::startTrans();
  131. try {
  132. $res = Db::table('lr_user_task')->insertAll($userTaskData);
  133. if(!$res){
  134. throw new \Exception('任务存储失败!');
  135. }
  136. // 完成任务,发放抽奖次数
  137. $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]);
  138. if(!$res){
  139. throw new \Exception('抽奖次数发放失败!');
  140. }
  141. $markValue = implode(',', array_column($userTaskData, 'mark'));
  142. echo "SUCCESS: uid={$user_id}, data={$markValue} \n";
  143. Db::commit();
  144. } catch (\Exception $e) {
  145. Db::rollback();
  146. echo "FAIL: uid={$user_id}, msg={$e->getMessage()} \n";
  147. return ['code' => -199, 'msg' => '任务处理失败:' . $e->getMessage(), 'data' => []];
  148. }
  149. return ['code' => 200, 'msg' => '任务处理成功', 'data' => []];
  150. }
  151. }