task.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501
  1. <?php
  2. /**
  3. * 任务相关
  4. */
  5. class ModelTask{
  6. public $contypes = [];
  7. public $errCode = 0;
  8. protected $taskConfig = NULL;//任务配置
  9. public function __construct(){
  10. $this->dbToConfig();
  11. }
  12. /**
  13. * 获取用户当前缓存状态
  14. * @param int $uid
  15. * @param array $types
  16. * @return array
  17. */
  18. public function getCondition($uid, $types = array()) {
  19. if (!$uid || !$types || !is_array($types)) {
  20. return array();
  21. }
  22. $ret = array();
  23. foreach ($types as $type) {
  24. if (!in_array($type, $this->contypes)) {
  25. continue;
  26. }
  27. $key = okeys::task($uid, $type);
  28. $ret[$type] = (int)oo::commonOprRedis('usercache')->get($key);
  29. }
  30. return $ret;
  31. }
  32. /**
  33. * 获取已经奖励缓存
  34. * @param int $uid
  35. * @return array
  36. */
  37. public function getRewarded($uid) {
  38. $cacheKey = okeys::taskRewarded($uid);
  39. $ret = oo::commonOprRedis('usercache')->hGetAll($cacheKey);
  40. return ($ret && is_array($ret)) ? $ret : array();
  41. }
  42. /**
  43. * 领取奖励
  44. * @param int $uid
  45. * @param string $taskType 任务类型
  46. * @param bool $versionFalg 任务类型
  47. * @return mixed
  48. */
  49. public function reward($uid = 0, $taskType = '',$versionFalg = false){
  50. $data = ['code' => -1, 'msg' => '数据错误','data'=>[]];
  51. if (!$uid || !in_array($taskType, $this->contypes)) {
  52. return $data;
  53. }
  54. //判断是否已经领取了
  55. $alreadyRewarded = $this->getRewarded($uid);
  56. if($alreadyRewarded && $alreadyRewarded[$taskType]){
  57. return ['code' => -2, 'msg' => '已经领取了','data'=>[]];;
  58. }
  59. //获取次数 然后判断奖励
  60. $condition = $this->getCondition($uid, array($taskType));
  61. //任务配置
  62. $taskConfig = $this->taskConfig;
  63. //任务奖励
  64. $taskReward = [];
  65. //判断是否可以领取奖励
  66. foreach ($taskConfig as $k=>$v){
  67. if($v['task_type'] == $taskType){
  68. $taskReward = $v;
  69. if($condition[$taskType] < $v['target_times']){
  70. $data = array('code' => -2, 'msg' => '未达到领取奖励次数','data'=>[]);
  71. return $data;
  72. }
  73. break;
  74. }
  75. }
  76. //先发奖励 奖励类型 1:金币,2:体力,3:卡片,4:星星
  77. switch($taskReward['reward_type']){
  78. case 1:
  79. $reward_type = 'money';
  80. $coins = oo::commonOprModel('goods')->getGoodCoins($uid,'coins01');
  81. $taskReward['reward_num'] = $taskReward['reward_num']*$coins;
  82. break;
  83. case 2:
  84. $reward_type = 'spins';
  85. break;
  86. case 3:
  87. //执行卡片逻辑
  88. $reward_type = 'card';
  89. $card = oo::commonOprModel('card')->getCardByType($uid,6,1);
  90. break;
  91. case 4:
  92. $reward_type = 'star';
  93. break;
  94. default:
  95. $data = array('code' => -3, 'msg' => '类型错误','data'=>[]);
  96. return $data;
  97. break;
  98. }
  99. if($reward_type){
  100. $bet = 1;
  101. if($reward_type != 'card'){
  102. if(oo::commonOprModel('newvip')->checkVip($uid)){
  103. $config = oo::commonOprModel('newvip')->getUserVipLv($uid, true,false,155);
  104. $bet = max(intval($config['taskRewardBet']), 1);
  105. }
  106. $ret = oo::commonOprModel('member')->optProperty($uid,array($reward_type=>$taskReward['reward_num']*$bet),'+',60,'完成每日任务');
  107. }else{
  108. $ret = 1;
  109. }
  110. if($ret){
  111. // 先设置缓存
  112. $flg = $this->setRewarded($uid,$taskType);
  113. if ($flg) { // 设置领奖励缓存标识失败
  114. //玩家资产信息
  115. $memberAssets = oo::commonOprModel('member')->getUserAssetsInfo($uid);
  116. return [
  117. 'code' =>1,
  118. 'msg' =>'领取成功',
  119. 'data' =>[
  120. 'type' => $taskReward['reward_type'],
  121. 'addNum' => $taskReward['reward_num'] * $bet,
  122. 'info' => $memberAssets,
  123. 'card' => $card,
  124. 'newTask'=> self::refreshTaskList($uid,$taskType,$versionFalg),
  125. ]
  126. ];
  127. }
  128. }
  129. }
  130. return $data;
  131. }
  132. /**
  133. * 设置已领奖励缓存
  134. * @param int $uid
  135. * @param int $type
  136. * @return bool
  137. */
  138. public function setRewarded($uid, $type) {
  139. if (!$uid || !$type) {
  140. $this->errCode = -201;
  141. return false;
  142. }
  143. $cacheKey = okeys::taskRewarded($uid);
  144. $cache = oo::commonOprRedis('usercache')->hGetAll($cacheKey);
  145. !$cache && $cache = array();
  146. $cache[$type] = 1;
  147. $res = oo::commonOprRedis('usercache')->hMset($cacheKey,$cache);
  148. oo::commonOprRedis('usercache')->expire($cacheKey,oo::todayDeadline());
  149. return $res;
  150. }
  151. /**
  152. * 累加条件缓存
  153. * @param int $uid
  154. * @param string $type slotSpins(-),slotMaxCoins(+),Getshield(+),slotSteal(+)
  155. * @param int $num
  156. * @return mixed
  157. */
  158. public function incCondition($uid, $type, $num = 1) {
  159. return true;
  160. if (!$uid || !in_array($type, $this->contypes)) {
  161. return false;
  162. }
  163. if(!in_array($type, array_column(self::getTaskList($uid),'taskType'))){
  164. return false;
  165. }
  166. $expire = strtotime("23:59:59") - time();
  167. // $ret = oo::commonOprCache('task')->increment(okeys::task($uid, $type), (int)$num, $expire);
  168. $ret = oo::commonOprRedis('usercache')->incrBy(okeys::task($uid, $type), (int)$num);
  169. oo::commonOprRedis('usercache')->expire(okeys::task($uid, $type), $expire);
  170. $getReward = self::getCondition($uid,$this->contypes);
  171. $list = $this->taskConfig;
  172. $getRewarded = self::getRewarded($uid);
  173. //重组数组
  174. foreach ($list as $k=>$v){
  175. if($v['task_type'] != $type){
  176. continue;
  177. }
  178. $fulfillNum = intval($getReward[$v['task_type']]);
  179. if($fulfillNum >= $v['target_times']){
  180. if($getRewarded[$v['task_type']]){
  181. $flag = true;
  182. }else{
  183. $flag = false;
  184. }
  185. $fulfillNum = $v['target_times'];
  186. }else{
  187. $flag = false;
  188. }
  189. $is_push = oo::commonOprRedis('usercache')->get(okeys::redPointLock($uid,"Task-{$type}"));
  190. if($fulfillNum == $v['target_times'] && $flag==false && !$is_push){
  191. oo::commonOprRedis('usercache')->setex(okeys::redPointLock($uid,"Task-{$type}"),1,oo::todayDeadline());
  192. oo::commonOprModel('Workerman')->push($uid,ocmd::$redPoint,['type'=>10]);
  193. }
  194. }
  195. return $ret;
  196. }
  197. /**
  198. * 数据库内容读入配置文件
  199. */
  200. public function dbToConfig(){
  201. $key = okeys::taskConfig();
  202. $cacheData = oo::commonOprRedis('common')->get($key);
  203. if ($cacheData) {
  204. $tempArr = json_decode($cacheData, 1);
  205. $this->taskConfig = $tempArr;
  206. $this->contypes = array_column($tempArr,'task_type');
  207. return;
  208. }
  209. $table = otable::taskConfig();
  210. $sql = "SELECT * FROM $table where is_open = 1 ";
  211. $list = oo::commonOprDb('config')->getAll($sql, 1);
  212. $task_type = [];
  213. foreach ($list as $k=>$v){
  214. array_push($task_type,$v['task_type']);
  215. }
  216. $taskListJson = json_encode($list);
  217. oo::commonOprRedis('common')->set($key, $taskListJson);
  218. oo::commonOprRedis('common')->expireAt($key, oo::redisRandomExpire(strtotime("23:59:59")));//任务默认是一天
  219. $this->taskConfig = $list;
  220. $this->contypes = $task_type;
  221. return $list;
  222. }
  223. public function getTaskConfig($uid){
  224. if( empty($uid) && !$this->taskConfig ){
  225. return false;
  226. }
  227. $list = $this->taskConfig;
  228. if(count($list) < 1){
  229. return false;
  230. }
  231. // $curTaskList = ocache::t_userinfo($uid)->get(okeys::CurTaskList($uid));
  232. $curTaskList =oo::commonOprRedis('usercache')->hGetAll(okeys::CurTaskList($uid));
  233. if(empty($curTaskList)){
  234. $rand = array_rand($list,3);
  235. foreach ($list as $key=>$row){
  236. if(!in_array($key,$rand)){
  237. unset($list[$key]);
  238. }
  239. }
  240. sort($list);
  241. // ocache::t_userinfo($uid)->set(okeys::CurTaskList($uid),array_column($list,'task_type'),oo::todayDeadline());
  242. oo::commonOprRedis('usercache')->hMset(okeys::CurTaskList($uid),array_column($list,'task_type'));
  243. oo::commonOprRedis('usercache')->expire(okeys::CurTaskList($uid),oo::todayDeadline());
  244. }else{
  245. foreach ($list as $key=>$row){
  246. if(!in_array($row['task_type'],$curTaskList)){
  247. unset($list[$key]);
  248. }
  249. }
  250. sort($list);
  251. }
  252. return $list;
  253. }
  254. public function getList($uid){
  255. return $data = [
  256. 'code' => 1,
  257. 'msg' => '请求成功',
  258. 'data' => [
  259. 'task' =>self::getTaskList($uid),
  260. 'achieve' =>oo::commonOprModel('achievement')->getAchieveList($uid),
  261. 'progress'=>self::getTaskProgress($uid),
  262. 'time' =>strtotime("23:59:59") - time(),
  263. 'invite' =>[
  264. 'reward'=>['type'=>2, 'num' =>oo::commonOprModel('invite')->inviteConfig($uid)['base']],
  265. 'friendsList' =>oo::commonOprModel('invite')->getInviteRewardList($uid)
  266. ],
  267. 'taskFinishNum' => count(self::getRewarded($uid))
  268. ]
  269. ];
  270. }
  271. public function getTaskList($uid,$taskType = ''){
  272. $list = self::getTaskConfig($uid);
  273. if(!$list){
  274. return array('code' => -2,'msg'=>'数据错误', 'data' => []);
  275. }
  276. $lang = oo::getDefinedLang($uid);
  277. $taskArr = [];
  278. $getReward = self::getCondition($uid,$this->contypes); //查询奖励
  279. //查询用户领取领取状态
  280. $getRewarded = self::getRewarded($uid);
  281. //重组数组
  282. $coins = oo::commonOprModel('goods')->getGoodCoins($uid,'coins01');
  283. foreach ($list as $k=>$v){
  284. $fulfillNum = intval($getReward[$v['task_type']]);
  285. if($fulfillNum >= $v['target_times']){
  286. //如果有领取记录 那就是领取了
  287. if($getRewarded[$v['task_type']]){
  288. $flag = 2;
  289. }else{
  290. $flag = 1;
  291. }
  292. $fulfillNum = $v['target_times'];
  293. }else{
  294. $flag = 0;
  295. }
  296. if($lang == 'zh'){
  297. $task_title = $v['task_title'];
  298. }else{
  299. $task_title = $v['task_title_'.$lang];
  300. }
  301. if($v['reward_type'] == 1){
  302. $num = $v['reward_num']*$coins;
  303. }else{
  304. $num = $v['reward_num'];
  305. }
  306. $taskArr[$k] = [
  307. 'rewardType'=>$v['reward_type'],
  308. 'num'=>$num,
  309. 'desc'=>$task_title,
  310. 'progress'=>$fulfillNum,
  311. 'target'=>$v['target_times'],
  312. 'status'=>$flag,
  313. 'taskType'=>$v['task_type'],
  314. ];
  315. if($v['task_type'] == $taskType){
  316. return $taskArr[$k];
  317. }
  318. }
  319. return $taskArr;
  320. }
  321. /**
  322. * 任务进度
  323. * @param $uid
  324. * @return mixed
  325. * Created by: Owen
  326. * Created on: 2020/5/9 16:50
  327. */
  328. public function getTaskProgress($uid){
  329. $ret = oo::commonOprModel('config')->getTaskProgressConfig();
  330. foreach ($ret as $key=>$row){
  331. $ret[$key]['status'] = intval(oo::commonOprRedis('common')->hGet(okeys::taskProgress(),okeys::taskProgress($uid,$row['progress'])));
  332. }
  333. return $ret;
  334. }
  335. /**
  336. * 任务进度奖励
  337. * @param $uid
  338. * @param $progress
  339. * @return array|bool
  340. * Created by: Owen
  341. * Created on: 2020/5/9 16:50
  342. */
  343. public function getTaskProgressReward($uid,$progress){
  344. if(oo::commonOprRedis('common')->hGet(okeys::taskProgress(),okeys::taskProgress($uid,$progress))){
  345. return false;
  346. }
  347. $list = $this->taskConfig;
  348. $getReward = $this->getCondition($uid,$this->contypes);
  349. $serverProgress = 0;
  350. foreach ($list as $k=>$v){
  351. $fulfillNum = intval($getReward[$v['task_type']]);
  352. if($fulfillNum >= $v['target_times']){
  353. $serverProgress++;
  354. }
  355. }
  356. if($serverProgress<$progress){
  357. return false;
  358. }
  359. $ret = oo::commonOprModel('config')->getTaskProgressConfig($progress);
  360. if(empty($ret)){
  361. return false;
  362. }
  363. oo::commonOprModel('member')->optProperty($uid,array('spins'=>intval($ret['spins'])),'+',61,'每日任务进度:'.$progress);
  364. oo::commonOprRedis('common')->hSet(okeys::taskProgress(),okeys::taskProgress($uid,$progress),1);
  365. return ['spins'=>$ret['spins']];
  366. }
  367. /**
  368. * 刷新当前任务
  369. * @param $uid
  370. * @param $taskType
  371. * @param $versionFlag
  372. * @return array
  373. * Created by: Owen
  374. * Created on: 2020/1/8 19:46
  375. */
  376. public function refreshTaskList($uid,$taskType,$versionFlag = false){
  377. // $curTaskList = ocache::t_userinfo($uid)->get(okeys::CurTaskList($uid));
  378. $curTaskList =oo::commonOprRedis('usercache')->hGetAll(okeys::CurTaskList($uid));
  379. if(empty($curTaskList)){
  380. return [];
  381. }
  382. $curTaskListNum = count($curTaskList);
  383. $list = array_column($this->taskConfig,'task_type');
  384. $list = array_diff($list,$curTaskList);
  385. $list = array_diff($list,array_keys(self::getRewarded($uid)));
  386. $key = array_search($taskType,$curTaskList);
  387. unset($curTaskList[$key]);
  388. if($curTaskListNum == count($curTaskList)){
  389. return $versionFlag ? self::getTaskList2($uid,$taskType) :self::getTaskList($uid,$taskType);
  390. }
  391. $rand = array_rand($list,1);
  392. foreach ($list as $key=>$row){
  393. if($key==$rand){
  394. $curTaskList[$key] = $row;
  395. $taskType = $row;
  396. break;
  397. }
  398. }
  399. $newTaskList = [];
  400. foreach ($curTaskList as $row){
  401. $newTaskList[] = $row;
  402. }
  403. // ocache::t_userinfo($uid)->set(okeys::CurTaskList($uid),array_unique($newTaskList),oo::todayDeadline());
  404. oo::commonOprRedis('usercache')->hMset(okeys::CurTaskList($uid),array_unique($newTaskList));
  405. oo::commonOprRedis('usercache')->expire(okeys::CurTaskList($uid),oo::todayDeadline());
  406. return $versionFlag ? self::getTaskList2($uid,$taskType) :self::getTaskList($uid,$taskType);
  407. }
  408. /**
  409. * 1.5.0 新版配置 --------------------------------------------------------------------------------------------------
  410. */
  411. /**
  412. * id 与 task_type 映射
  413. * @param $id
  414. * @return mixed
  415. * Created by: Owen
  416. * Created on: 2020/5/9 17:42
  417. */
  418. public function taskMap($id){
  419. $task = array_column($this->taskConfig,null,'id');
  420. return $task[$id]['task_type'];
  421. }
  422. /**
  423. * 保护
  424. * @param $uid
  425. * @return string
  426. * Created by: Owen
  427. * Created on: 2020/5/9 16:57
  428. */
  429. public function getList2($uid){
  430. return oo::response(1,[
  431. 'task' =>self::getTaskList2($uid),
  432. 'achieve' =>oo::commonOprModel('achievement')->getAchieveList2($uid),
  433. 'progress'=>array_column(self::getTaskProgress($uid),'status'),
  434. 'time' =>strtotime("23:59:59") - time(),
  435. 'taskFinishNum' => count(self::getRewarded($uid))
  436. ],'请求成功',true);
  437. }
  438. /**
  439. * 任务列表 -- 新版
  440. * @param $uid
  441. * @param string $taskType
  442. * @return array|mixed
  443. * Created by: Owen
  444. * Created on: 2020/5/9 16:50
  445. */
  446. public function getTaskList2($uid,$taskType = ''){
  447. $list = self::getTaskConfig($uid);
  448. if(!$list){
  449. return array('code' => -2,'msg'=>'数据错误', 'data' => []);
  450. }
  451. $taskArr = [];
  452. $getReward = self::getCondition($uid,array_column($list,'task_type'));
  453. $getRewarded = self::getRewarded($uid);
  454. foreach ($list as $k=>$v){
  455. $fulfillNum = intval($getReward[$v['task_type']]);
  456. if($fulfillNum >= $v['target_times']){
  457. if($getRewarded[$v['task_type']]){
  458. $flag = 2;
  459. }else{
  460. $flag = 1;
  461. }
  462. $fulfillNum = $v['target_times'];
  463. }else{
  464. $flag = 0;
  465. }
  466. $taskArr[$k] = [
  467. 'id' =>$v['id'],
  468. 'progress' =>$fulfillNum,
  469. 'status' =>$flag,
  470. ];
  471. if($v['task_type'] == $taskType){
  472. return $taskArr[$k];
  473. }
  474. }
  475. return $taskArr;
  476. }
  477. }