missionModel.class.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. <?php
  2. /**
  3. *
  4. */
  5. class missionModel extends NewModel
  6. {
  7. /**
  8. * redis实体
  9. * @var boolean
  10. */
  11. protected static $user_redis = false;
  12. /**
  13. * 任务类型字典
  14. * @var array
  15. */
  16. protected static $title = [
  17. 0 => '在线任务',
  18. 1 => '玩游戏任务',
  19. 2 => '打赏主播任务',
  20. 3 => '分享主播任务',
  21. 4 => '关注主播任务',
  22. ];
  23. public function __construct($table_name)
  24. {
  25. parent::__construct($table_name);
  26. fanwe_require(APP_ROOT_PATH . 'mapi/lib/redis/UserRedisService.php');
  27. self::$user_redis = new UserRedisService();
  28. }
  29. /**
  30. * 根据日期、用户id、任务类型获取redis键值
  31. * @param integer $user_id 用户id
  32. * @param integer $type 任务类型
  33. * @return string redis键值
  34. */
  35. protected static function getRedisKey($user_id, $type)
  36. {
  37. return "mission{$type}:{$user_id}:" . date('Y-m-d');
  38. }
  39. /**
  40. * 获取任务redis信息
  41. * @param integer $user_id 用户id
  42. * @param integer $type 任务类型
  43. * @return array
  44. */
  45. protected static function getMission($user_id, $type = 0)
  46. {
  47. return self::$user_redis->getRow_db(self::getRedisKey($user_id, $type), ['time', 'times', 'progress']);
  48. }
  49. /**
  50. * 更新任务redis信息
  51. * @param [type] $user_id [description]
  52. * @param [type] $data [description]
  53. * @param integer $type [description]
  54. * @return [type] [description]
  55. */
  56. protected static function updateMission($user_id, $data, $type = 0)
  57. {
  58. return self::$user_redis->update_db(self::getRedisKey($user_id, $type), $data);
  59. }
  60. /**
  61. * 获取剩余次数
  62. * @param [type] $times [description]
  63. * @param integer $type [description]
  64. * @return [type] [description]
  65. */
  66. public function getLeftTimes($times, $type = 0)
  67. {
  68. return intval($this->count(['is_effect' => 1, 'sort' => ['>', $times], 'type' => $type]));
  69. }
  70. /**
  71. * 获取下一次任务信息
  72. * @param [type] $times [description]
  73. * @param array $field [description]
  74. * @param integer $type [description]
  75. * @return [type] [description]
  76. */
  77. public function getNextMission($times, $field = [], $type = 0)
  78. {
  79. return $this->field($field)->order('sort')->selectOne(['is_effect' => 1, 'sort' => ['>', $times], 'type' => $type]);
  80. }
  81. /**
  82. * 任务完成次数递增(如完成一局游戏)
  83. * @param [type] $user_id [description]
  84. * @param integer $type [description]
  85. * @return [type] [description]
  86. */
  87. public function incProgress($user_id, $type = 0)
  88. {
  89. return self::$user_redis->inc_field(self::getRedisKey($user_id, $type), 'progress', 1);
  90. }
  91. /**
  92. * 获取任务信息
  93. * @param [type] $user_id [description]
  94. * @param integer $type [description]
  95. * @return [type] [description]
  96. */
  97. public function getMissionInfo($user_id, $type = 0)
  98. {
  99. $mission = self::getMission($user_id, $type);
  100. $next_mission = $this->getNextMission($mission['times'], 'name,time,target,money', $type);
  101. if (!$next_mission) {
  102. $next_mission = $this->getNextMission($mission['times'] - 1, 'name,time,target,money', $type);
  103. }
  104. $max_times = $this->getLeftTimes(0, $type);
  105. $left_times = $this->getLeftTimes($mission['times'], $type);
  106. $time = intval($mission['time'] - NOW_TIME);
  107. if ($time < 0) {
  108. $time = 0;
  109. }
  110. $progress = 0;
  111. $target = 0;
  112. $current = 0;
  113. switch ($type) {
  114. case 0:
  115. $current = intval(($next_mission['time'] - $time) / 60);
  116. $target = intval($next_mission['time'] / 60);
  117. break;
  118. default:
  119. $current = $mission['progress'];
  120. $target = $next_mission['target'];
  121. break;
  122. }
  123. if ($current > $target) {
  124. $current = $target;
  125. }
  126. $progress = $target ? $current / $target : 1;
  127. $image = '';
  128. switch ($type) {
  129. case 0:
  130. $image = '/public/images/watch_live.png';
  131. break;
  132. case 1:
  133. $image = '/public/images/play_chess_game.png';
  134. break;
  135. case 2:
  136. $image = '/public/images/play_the_anchor.png';
  137. break;
  138. case 3:
  139. $image = '/public/images/share.png';
  140. break;
  141. case 4:
  142. $image = '/public/images/new_concern.png';
  143. break;
  144. }
  145. if (defined('OPEN_DIAMOND_GAME_MODULE') && OPEN_DIAMOND_GAME_MODULE == 1) {
  146. $coin_field = '💎';
  147. } else {
  148. $coin_field = '💰';
  149. }
  150. return [
  151. 'image' => get_domain() . $image,
  152. 'title' => $next_mission['name'],
  153. 'desc' => '奖励 ' . $next_mission['money'] . ' ' . $coin_field,
  154. 'money' => intval($next_mission['money']),
  155. 'time' => intval($time),
  156. 'max_times' => intval($max_times),
  157. 'left_times' => intval($left_times),
  158. 'current' => intval($current),
  159. 'target' => intval($target),
  160. 'progress' => intval($progress * 100),
  161. 'type' => intval($type),
  162. ];
  163. }
  164. public function commitMission($user_id, $type = 0)
  165. {
  166. $mission = self::getMission($user_id, $type);
  167. if (!$mission['time']) {
  168. $mission['time'] = NOW_TIME;
  169. }
  170. $left_times = $this->getLeftTimes($mission['times'], $type);
  171. if (!$left_times) {
  172. return '今日领取次数已用完';
  173. }
  174. $current_mission = $this->getNextMission($mission['times'], 'money,target', $type);
  175. if ($type) {
  176. // 计数任务,判断进程
  177. if ($mission['progress'] < $current_mission['target']) {
  178. return '任务未完成';
  179. }
  180. } else {
  181. // 计时任务,判断时间,增加时间
  182. if (NOW_TIME < $mission['time']) {
  183. return '领取时间未到';
  184. }
  185. $next_mission = $this->getNextMission($mission['times'], 'time', $type);
  186. $mission['time'] = NOW_TIME + $next_mission['time'];
  187. }
  188. $mission['times'] = $mission['times'] + 1;
  189. self::updateMission($user_id, $mission, $type);
  190. /**
  191. * 奖励钻石或游戏币
  192. */
  193. $money = intval($current_mission['money']);
  194. $user_model = self::build('user');
  195. $user_model->coin($user_id, $money);
  196. $account_diamonds = $user_model->coin($user_id);
  197. self::build('coin_log')->addLog($user_id, -1, $money, $account_diamonds, '每日' . self::$title[$type] . '领取(第' . $mission['times'] . '次)');
  198. $diamonds = self::$user_redis->getOne_db($user_id, 'diamonds');
  199. $coin = self::$user_redis->getOne_db($user_id, 'coin');
  200. $mission = $this->getMissionInfo($user_id, $type);
  201. $is_mission = intval($mission['left_times'] > 0);
  202. return compact('mission', 'diamonds', 'coin', 'is_mission');
  203. }
  204. public function getMissionList($user_id)
  205. {
  206. $types = $this->field('type')->group('type')->order('type')->select(['is_effect' => 1]);
  207. $list = [];
  208. foreach ($types as $type) {
  209. $list[] = $this->getMissionInfo($user_id, $type['type']);
  210. }
  211. return $list;
  212. }
  213. }