mining.php 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673
  1. <?php
  2. defined('IN_WEB') or die('Include Error!');
  3. class ModelMining
  4. {
  5. const GAMECOUNT = 1;
  6. /**
  7. * 获取矿工信息
  8. * @param $uid
  9. * @return array
  10. * Created by: Owen
  11. * Created on: 2020/4/20 11:46
  12. */
  13. public function getInfo($uid){
  14. $info = $this->getUserMiningInfo($uid);
  15. if(!$info){
  16. return ['code'=>-1,'msg'=>'获取失败','data'=>[]];
  17. }
  18. $info['current'] = intval(self::CountIncome($uid)); //当前收益 金币/天
  19. $info['offline'] = self::returnUserIncome($uid); //离线收益
  20. oo::commonOprModel('currency')->participation($uid, 'mining', 0); //矿工小游戏参与率统计
  21. return ['code'=>1,'msg'=>'获取成功','data'=>$info];
  22. }
  23. /**
  24. * 获取矿工小游戏当前信息
  25. * @param $uid
  26. * @param $versionFlag
  27. * @return array
  28. * Created by: Owen
  29. * Created on: 2020/3/5 19:51
  30. */
  31. public function getGameInfo($uid,$versionFlag = 0){
  32. $count = intval(oo::commonOprRedis('common')->get(okeys::MiningGame($uid)));
  33. $ttl = max(0,oo::commonOprRedis('common')->ttl(okeys::MiningGame($uid)));
  34. return [
  35. 'maxCount' => self::GAMECOUNT,
  36. 'status' => max(0,self::GAMECOUNT - abs($count)),
  37. 'refreshTime' => $ttl + time(),
  38. 'gid' => 201,
  39. 'bet' => 0.25,
  40. 'stageReward' => oo::commonOprRedis('common')->sMembers(okeys::MiningGameStateReward($uid)),
  41. 'refreshDiamond' => $this->getRefreshTimeDiamond(),
  42. 'maxLevelId' => intval(oo::commonOprRedis('common')->get(okeys::MiningGameLevel($uid))),
  43. 'preLevelId' => intval(oo::commonOprRedis('common')->get(okeys::MiningGamePreLevelId($uid))),
  44. ];
  45. }
  46. /**
  47. * 升级旷工等级信息
  48. * 2019年5月22日
  49. * YANG丶
  50. */
  51. public function UpdateMining($uid,$levelId)
  52. {
  53. $userMoney = oo::commonOprModel('member')->getUserAssetsInfo($uid);
  54. $userInfo = self::getUserMiningInfo($uid);
  55. $info = $userInfo['islandLevel'];
  56. if(intval($info[$levelId]) >= 5){
  57. return ['code' => -2, 'msg' => '岛屿id数据错误','data'=>[
  58. 'money' => intval($userMoney['money']),
  59. 'curMiningLv' => intval($info[$levelId]),
  60. ]];
  61. }
  62. //当前矿工最高等级
  63. $max_level = $info[$levelId];
  64. if(is_null($max_level)){
  65. self::miningBug($uid);
  66. $userInfo = self::getUserMiningInfo($uid);
  67. $info = $userInfo['islandLevel'];
  68. $max_level= $info[$levelId];
  69. }
  70. $config = self::upgradeMiners_Config($levelId);
  71. $miningUpConfig = json_decode($config['config'],true);
  72. $miningUpConfigs = $miningUpConfig[$max_level];
  73. if(intval($userMoney['money']) < intval($miningUpConfigs[0])){
  74. return ['code' => -3, 'msg' => '升级矿工所需金币不足','data'=>[
  75. 'money' => intval($userMoney['money']),
  76. 'curMiningLv' => intval($info[$levelId]),
  77. ]];
  78. }
  79. if($miningUpConfigs[0] > 0){
  80. $ret = oo::commonOprModel('member')->optProperty($uid,array('money'=>$miningUpConfigs[0]),'-',21,'升级矿工扣除金币:关卡'.$levelId.",等级:".$max_level);
  81. if($ret){
  82. $info[$levelId] = ($info[$levelId] + 1) >5 ? 5 : $info[$levelId] + 1;
  83. if(self::updateUserMiningInfo($uid,['islandLevel'=>$info])){
  84. oo::commonOprRedis('common')->delete(okeys::userCountIncome($uid));
  85. $income = self::CountIncome($uid);
  86. //用户uid 时间 当前收益 之前收益
  87. $key = okeys::upgradeIncome($uid);
  88. $st = oo::commonOprRedis('common')->rPush($key, json_encode(['uid'=>$uid,'time'=>time(),'current'=>$income]));
  89. oo::commonOprRedis('common')->expire($key,self::getExpireTime($uid));
  90. if($st){
  91. $userMoney = oo::commonOprModel('member')->getUserAssetsInfo($uid);
  92. $data = ['code'=>1,'msg'=>'升级矿工成功','data'=>[
  93. 'info'=>$info,
  94. 'current'=>$income,
  95. 'upCoins'=>$miningUpConfigs[0],
  96. 'money' => intval($userMoney['money']),
  97. 'curMiningLv' => intval($info[$levelId]),
  98. ]];
  99. }
  100. }
  101. }
  102. }
  103. return $data;
  104. }
  105. /**
  106. * 玩家领取矿工收益
  107. * YANG丶 2019年5月23日
  108. * $uid 用户uid
  109. */
  110. public function receiveUserIncome($uid,$money){
  111. if( empty($uid) || empty($money)){
  112. return ['code' => -1, 'desc' => '参数错误'];
  113. }
  114. //获取玩家的收益
  115. $num = self::returnUserIncome($uid);
  116. if($num <= 0){
  117. return ['code' => -2, 'desc' => '收益为0'];
  118. }
  119. //玩家领取操作
  120. //先判断前端的金币与后端计算的金币差 10
  121. if($money > $num){
  122. if($money - $num <= 10){
  123. $num = $money;
  124. }
  125. }
  126. $ret = oo::commonOprModel('member')->optProperty($uid,array('money'=>$num),'+',20,'领取矿岛收益');
  127. if($ret){
  128. $userMoney = oo::commonOprModel('member')->getUserAssetsInfo($uid);
  129. $userMoney['coins'] = $userMoney['money'];
  130. unset($userMoney['money']);
  131. $key = okeys::upgradeIncome($uid);
  132. oo::commonOprRedis('common')->delete($key);
  133. $income = self::CountIncome($uid);
  134. $upgradeData = json_encode(['uid'=>$uid,'time'=>time(),'current'=>$income]);
  135. oo::commonOprRedis('common')->rPush($key, $upgradeData);
  136. oo::commonOprRedis('common')->expire($key,self::getExpireTime($uid));
  137. $data = ['code'=>1,'msg'=>'矿工奖励领取成功','data'=>['current'=>$income,'receiveCoins'=>$num,'info'=>$userMoney]];
  138. ////金矿领取率统计,取消该统计
  139. //oo::commonOprModel('statistics')->miningReceiveSta( $uid, $num);
  140. }
  141. return $data;
  142. }
  143. /**
  144. * 返回玩家的收益
  145. */
  146. public function returnUserIncome($uid){
  147. if(empty($uid)){
  148. return 0;
  149. }
  150. $key = okeys::upgradeIncome($uid);
  151. $st = oo::commonOprRedis('common')->lGetRange($key,0,-1);
  152. if(empty($st)){
  153. if(self::CountIncome($uid) <=0 ){
  154. return 0;
  155. }else{
  156. return self::CountIncome($uid,true);
  157. }
  158. }else{
  159. //这个循环主要是格式化数据
  160. foreach ($st as $k=>$v){
  161. $st[$k] = json_decode($v,true);
  162. }
  163. $sum = 0;
  164. foreach ($st as $k=>$v){
  165. if($k+1 <count($st)){
  166. //计算时间差
  167. $st[$k]['time'] = $st[$k+1]['time'] - $v['time'];
  168. //根据收益 计算时间差每秒之后的收益
  169. $sum += intval((intval($v['current']) / 86400) * $st[$k]['time']);
  170. }else{
  171. $st[$k]['time'] = time() - $v['time'];
  172. $sum += intval((intval($v['current']) / 86400) * $st[$k]['time']);
  173. }
  174. }
  175. $maxNum = self::CountIncome($uid,true);
  176. if($sum>$maxNum){
  177. $sum = $maxNum;
  178. }
  179. return intval($sum);
  180. }
  181. }
  182. /**
  183. * 获取玩家岛数据
  184. */
  185. public function getUserMiningInfo($uid){
  186. $cache = oo::commonOprRedis('common')->get(okeys::UserMining($uid));
  187. if($cache != false){
  188. return json_decode($cache,true);
  189. }
  190. $table = otable::miningInfo();
  191. $info = oo::commonOprDb('mining')->getOne("SELECT * FROM {$table} where uid={$uid} LIMIT 1", MYSQLI_ASSOC);
  192. if(empty($info)){
  193. $userInfo = oo::commonOprModel('member')->getUserAssetsInfo($uid);
  194. $level= $userInfo['levelId'];
  195. $temp = [];
  196. for($i=1;$i<$level;$i++){
  197. $temp[$i] = 0;
  198. }
  199. $one = addslashes(json_encode($temp));
  200. $table = otable::miningInfo();
  201. $sql = "INSERT INTO {$table} (`uid`, `islandLevel`) VALUES ({$uid},'{$one}')";
  202. oo::commonOprDb('mining')->query($sql,false);
  203. $info['islandLevel'] = $temp;
  204. $info['uid'] = $uid;
  205. return $info;
  206. }
  207. $info['islandLevel'] = json_decode($info['islandLevel'],true);
  208. oo::commonOprRedis('common')->setex(okeys::UserMining($uid),json_encode($info),oo::redisRandomExpire(7*24*60*60));
  209. return $info;
  210. }
  211. /**
  212. * 更新缓存
  213. * @param $uid
  214. * @param $update
  215. * @return bool
  216. * Created by: Owen
  217. * Created on: 2020/4/20 12:18
  218. */
  219. public function updateUserMiningInfo($uid,$update){
  220. $info = self::getUserMiningInfo($uid);
  221. $info['islandLevel'] = $update['islandLevel'];
  222. $ret = oo::commonOprRedis('common')->setex(okeys::UserMining($uid),json_encode($info),oo::redisRandomExpire(7*24*60*60));
  223. /** 数据落地标识 */
  224. oo::commonOprRedis('common')->zAdd(okeys::Landing("MINING", $uid),time(),$uid);
  225. return $ret;
  226. }
  227. /**
  228. * 获取矿工升级配置
  229. * @param $levelId
  230. * @return mixed
  231. * Created by: Owen
  232. * Created on: 2019/8/6 14:48
  233. * Description:
  234. */
  235. public function upgradeMiners_Config($levelId){
  236. $config = oo::commonOprRedis('common')->hGet(okeys::MiningConfig(),$levelId);
  237. if(!empty($config)){
  238. return json_decode($config,true);
  239. }
  240. $table = otable::upgradeMiners_Config();
  241. $sql = "SELECT config,initIncome,growIncome FROM {$table} where id={$levelId}" ;
  242. $info = oo::commonOprDb('config')->getOne($sql, MYSQLI_ASSOC);
  243. oo::commonOprRedis('common')->hSet(okeys::MiningConfig(),$levelId,json_encode($info));
  244. return $info;
  245. }
  246. /**
  247. * 矿工奖励倒计时间
  248. * @param $uid
  249. * @return bool|int
  250. * Created by: Owen
  251. * Created on: 2020/1/9 17:59
  252. */
  253. public function getExpireTime($uid){
  254. $userDayilyInCome = self::CountIncome($uid);
  255. if($userDayilyInCome == 0){
  256. return false;
  257. }else{
  258. $userMaxInCome = self::CountIncome($uid,true);
  259. $offlineInCome = self::returnUserIncome($uid);
  260. return intval((($userMaxInCome-$offlineInCome)/$userDayilyInCome)*24*60*60);
  261. }
  262. }
  263. /**
  264. * 计算收益
  265. * @param $uid
  266. * @param bool $max
  267. * @return int
  268. * Created by: Owen
  269. * Created on: 2020/4/20 11:43
  270. */
  271. public function CountIncome($uid,$max = false){
  272. $incomeCache = oo::commonOprRedis('common')->hGetAll(okeys::userCountIncome($uid));
  273. if(!empty($incomeCache) && intval($incomeCache['maxIncome']) == 0){
  274. oo::commonOprRedis('common')->delete(okeys::userCountIncome($uid));
  275. }
  276. if(empty($incomeCache)){
  277. $initIncome = 500000;
  278. $growIncome = 0;
  279. $userInfo = $this->getUserMiningInfo($uid);
  280. $landLevel = $userInfo['islandLevel'];
  281. $income = 0;
  282. foreach ($landLevel as $key=>$row){
  283. $config = $this->upgradeMiners_Config($key);
  284. if($config['initIncome'] > $initIncome){
  285. $initIncome = intval($config['initIncome']);
  286. }
  287. $growIncome += intval($config['growIncome']);
  288. if($row>0){
  289. $config = json_decode($config['config'],true);
  290. $income += intval($config[$row-1][1]);
  291. }
  292. }
  293. $maxIncome = $initIncome+$growIncome;
  294. oo::commonOprRedis('common')->hSet(okeys::userCountIncome($uid),'income',$income);
  295. oo::commonOprRedis('common')->hSet(okeys::userCountIncome($uid),'maxIncome',$maxIncome);
  296. oo::commonOprRedis('common')->expire(okeys::userCountIncome($uid),3*24*60*60);
  297. }else{
  298. $income = intval($incomeCache['income']);
  299. $maxIncome = intval($incomeCache['maxIncome']);
  300. }
  301. return $max?intval($maxIncome):intval($income);
  302. }
  303. /**
  304. * 插入新的矿工岛
  305. * @param $uid
  306. * Created by: Owen
  307. * Created on: 2019/8/13 16:58
  308. * Description:
  309. */
  310. public function newLevelMining($uid){
  311. $islandLevel = self::getUserMiningInfo($uid)['islandLevel'];
  312. $userInfo = oo::commonOprModel('member')->getUserAssetsInfo($uid);
  313. $level = $userInfo['levelId'];
  314. $islandLevel[$level] = 0;
  315. self::updateUserMiningInfo($uid,['islandLevel'=>$islandLevel]);
  316. }
  317. /**
  318. * 补丁 -- 例子~~到了7关,矿工数据只有1关
  319. * Created by: Owen
  320. * Created on: 2019/8/27 11:18
  321. * Description:
  322. */
  323. public function miningBug($uid){
  324. $islandLevel = self::getUserMiningInfo($uid)['islandLevel'];
  325. $userInfo = oo::commonOprModel('member')->getUserAssetsInfo($uid);
  326. $level = $userInfo['levelId'];
  327. for($i=1;$i<$level;$i++){
  328. if(empty($islandLevel[$i])){
  329. $islandLevel[$i] = 0;
  330. }
  331. }
  332. self::updateUserMiningInfo($uid,['islandLevel'=>$islandLevel]);
  333. }
  334. /**
  335. * 矿工小游戏奖励
  336. * @param $uid
  337. * @param $mineral
  338. * @return string
  339. * Created by: Owen
  340. * Created on: 2020/4/20 11:45
  341. */
  342. public function getMiningGameReward($uid,$mineral){
  343. $cacheKey = okeys::MiningGame();
  344. $cache = intval(oo::commonOprRedis('common')->hGet($cacheKey,$uid));
  345. if(self::GAMECOUNT - $cache <= 0){
  346. return oo::response(-2,[],"");
  347. }
  348. $mineral = explode(',',$mineral);
  349. $rewardNum = [
  350. 'coins' => 0,
  351. 'purse' => 0,
  352. 'mouse' => 0,
  353. 'stone' => 0,
  354. 'spins' => 0,
  355. ];
  356. //ID:金币1,金袋2,体力3,小石头4,中石头5,大石头6,老鼠7,金老鼠8,钱袋鼠9,体力老鼠10
  357. foreach ($mineral as $row){
  358. if(in_array($row,[1,8])){
  359. $rewardNum['coins'] += 1;
  360. }
  361. if(in_array($row,[2,9])){
  362. $rewardNum['purse'] += 1;
  363. }
  364. if(in_array($row,[7,8,9,10])){
  365. $rewardNum['mouse'] += 1;
  366. }
  367. if(in_array($row,[4,5,6])){
  368. $rewardNum['stone'] += 1;
  369. }
  370. if(in_array($row,[3,10])){
  371. $rewardNum['spins'] += 1;
  372. }
  373. }
  374. $property = oo::commonOprModel('member')->getUserAssetsInfo($uid);
  375. $level = intval($property['levelId']);
  376. $config = oo::commonOprModel('config')->getMiningGameConfig($level);
  377. $reward = [
  378. 'money' => 0,
  379. 'spins' => 0
  380. ];
  381. foreach ($rewardNum as $key=>$row){
  382. if($key == "spins"){
  383. $reward['spins'] += $config['reward'][$key] * $row;
  384. }else{
  385. $reward['money'] += $config['reward'][$key] * $row;
  386. }
  387. }
  388. oo::commonOprModel('member')->optProperty($uid,$reward,'+',22,'矿工游戏');
  389. oo::commonOprRedis('common')->hIncrBy($cacheKey,$uid,1);
  390. return oo::response(1,$reward,"",true);
  391. }
  392. /**
  393. * 矿工小游戏奖励
  394. * @param $uid
  395. * @param $mineral
  396. * @param $levelId
  397. * @return string
  398. * Created by: Owen
  399. * Created on: 2020/4/20 11:45
  400. */
  401. public function getMiningGameReward2($uid,$mineral,$levelId){
  402. $cacheKey = okeys::MiningGame($uid);
  403. $cache = intval(oo::commonOprRedis('common')->get($cacheKey));
  404. if(self::GAMECOUNT - $cache <= 0){
  405. return oo::response(-2,[],"");
  406. }
  407. $mineral = json_decode(stripslashes($mineral),true);
  408. $rewardNum = [
  409. 'coins' => 0,
  410. 'purse' => 0,
  411. 'mouse' => 0,
  412. 'stone' => 0,
  413. 'spins' => 0,
  414. 'diamond' => 0,
  415. ];
  416. //ID:金币1,金袋2,体力3,小石头4,中石头5,大石头6,老鼠7,金老鼠8,钱袋鼠9,体力老鼠10
  417. foreach ($mineral as $row){
  418. if(in_array($row['minerId'],[1,8])){
  419. $rewardNum['coins'] += $row['num'];
  420. }
  421. if(in_array($row['minerId'],[2,9])){
  422. $rewardNum['purse'] += $row['num'];
  423. }
  424. if(in_array($row['minerId'],[7,8,9,10])){
  425. $rewardNum['mouse'] += $row['num'];
  426. }
  427. if(in_array($row['minerId'],[4,5,6])){
  428. $rewardNum['stone'] += $row['num'];
  429. }
  430. if(in_array($row['minerId'],[3,10])){
  431. $rewardNum['spins'] += $row['num'];
  432. }
  433. if(in_array($row['minerId'],[11,12])){
  434. $rewardNum['diamond'] += $row['num'];
  435. }
  436. }
  437. $maxData = $this->getMaxMineralByLevelId($levelId);
  438. foreach ($maxData as $key=>$value){
  439. $rewardNum[$key] = min($rewardNum[$key],$value);
  440. }
  441. $property = oo::commonOprModel('member')->getUserAssetsInfo($uid);
  442. $level = intval($property['levelId']);
  443. $rewardConfig = oo::commonOprModel('readconfig')->getSysCon('MiningGame','Reward');
  444. $rewardConfig = json_decode($rewardConfig,true);
  445. foreach ($rewardConfig['reward'] as $key=>$row){
  446. $rewardNum[$key] = $rewardNum[$key] * $row;
  447. }
  448. $bet = 1;
  449. foreach ($rewardConfig['rewardBet'] as $row){
  450. if($row['minLv'] <= $level && $level <= $row['maxLv']){
  451. $bet = $row['bet'];
  452. }
  453. }
  454. $totalCoins = ($rewardNum['coins'] + $rewardNum['purse'] + $rewardNum['mouse'] + $rewardNum['stone']) * $bet;
  455. $reward = [
  456. 'money' => intval($totalCoins),
  457. 'spins' => $rewardNum['spins'],
  458. 'diamond' => $rewardNum['diamond'],
  459. ];
  460. oo::commonOprModel('member')->optProperty($uid,$reward,'+',22,'矿工游戏');
  461. oo::commonOprRedis('User')->setex(okeys::LastMiningGameCoinsNum($uid),$reward['money'],2*60*60);
  462. $ttl = oo::commonOprRedis('common')->ttl(okeys::MiningGame($uid));
  463. if($ttl <= 0){
  464. $ttl = 24*60*60;
  465. }
  466. $cache == 0 ? oo::commonOprRedis('common')->setex($cacheKey,1,$ttl) : oo::commonOprRedis('common')->incr($cacheKey);
  467. $curGameLevel = intval(oo::commonOprRedis('common')->get(okeys::MiningGameLevel($uid)));
  468. if($levelId > $curGameLevel){
  469. oo::commonOprRedis('common')->setex(okeys::MiningGameLevel($uid),max($curGameLevel,$levelId),$ttl);
  470. }
  471. oo::commonOprRedis('common')->setex(okeys::MiningGamePreLevelId($uid),$levelId,$ttl);
  472. $reward['refreshTime'] = time() + max(0,$ttl);
  473. for($i=1;$i<=$levelId;$i++){
  474. oo::commonOprRedis('common')->hIncrBy(okeys::MiningGameSta("PLAY",0),$i,1);
  475. $i < $levelId && oo::commonOprRedis('common')->hIncrBy(okeys::MiningGameSta("PASS",0),$i,1);
  476. }
  477. /**
  478. * 通行证任务9 -- 玩矿工游戏
  479. */
  480. oo::commonOprModel('activitynew')->addTrafficPermitNum($uid,9);
  481. $tb = otable::MiningGameSta();
  482. $time = time();
  483. $diamond = intval(oo::commonOprRedis('common')->get(okeys::MiningGameSta('diamond',$uid)));
  484. $resurrection = intval(oo::commonOprRedis('common')->get(okeys::MiningGameSta('resurrection',$uid)));
  485. oo::commonOprRedis('common')->delete([okeys::MiningGameSta('resurrection',$uid),okeys::MiningGameSta('diamond',$uid)]);
  486. $levelId > 0 && oo::commonOprDb('statistics')->query("INSERT INTO {$tb} VALUES(NULL,'{$uid}', '{$levelId}','{$totalCoins}','{$rewardNum['spins']}','{$rewardNum['diamond']}','{$diamond}','{$resurrection}','{$time}')");
  487. return oo::response(1,$reward,"",true);
  488. }
  489. public function getMaxMineralByLevelId($levelId){
  490. if(!$levelId){
  491. $levelId =19;
  492. }
  493. $levelId= $levelId + 1;
  494. $config = oo::commonOprModel('config')->getMiningGameConfig(0,2);
  495. $rewardNum = [
  496. 'coins' => 0,
  497. 'purse' => 0,
  498. 'mouse' => 0,
  499. 'stone' => 0,
  500. 'spins' => 0,
  501. 'diamond' => 0,
  502. ];
  503. foreach ($config as $row){
  504. if ($row['lv'] <= $levelId){
  505. foreach ($row['mineralData'] as $value){
  506. if(in_array($value['id'],[1,8])){
  507. $rewardNum['coins'] += $value['num'];
  508. }
  509. if(in_array($value['id'],[2,9])){
  510. $rewardNum['purse'] += $value['num'];
  511. }
  512. if(in_array($value['id'],[7,8,9,10])){
  513. $rewardNum['mouse'] += $value['num'];
  514. }
  515. if(in_array($value['id'],[4,5,6])){
  516. $rewardNum['stone'] += $value['num'];
  517. }
  518. if(in_array($value['id'],[3,10])){
  519. $rewardNum['spins'] += $value['num'];
  520. }
  521. if(in_array($value['id'],[11,12])){
  522. $rewardNum['diamond'] += $value['num'];
  523. }
  524. }
  525. }else{
  526. break;
  527. }
  528. }
  529. return $rewardNum;
  530. }
  531. /**
  532. * 矿工游戏复活
  533. * @param $uid
  534. * @param $levelId
  535. * @return string
  536. * Created by: Owen
  537. * Created on: 2020/11/4 17:12
  538. */
  539. public function gameResurrection($uid,$levelId){
  540. $config = oo::commonOprModel('config')->getMiningGameConfig($levelId,2);
  541. if(empty($config['resurrectionDiamond'])){
  542. return oo::response(errorCode::RESURRECTION_CONFIG_ERROR);
  543. }
  544. $assetsInfo = oo::commonOprModel('member')->getUserAssetsInfo($uid);
  545. if($assetsInfo['diamond'] < $config['resurrectionDiamond']){
  546. return oo::response(errorCode::DIAMOND_NOT_ENOUGH);
  547. }
  548. if(oo::commonOprModel('member')->optProperty($uid,['diamond'=>$config['resurrectionDiamond']],'-',23,"矿工游戏复活:{$levelId}")){
  549. oo::commonOprRedis('common')->setex(okeys::MiningGameSta('diamond',$uid),intval(oo::commonOprRedis('common')->get(okeys::MiningGameSta('diamond',$uid))) + $config['resurrectionDiamond'],1800);
  550. oo::commonOprRedis('common')->setex(okeys::MiningGameSta('resurrection',$uid),intval(oo::commonOprRedis('common')->get(okeys::MiningGameSta('resurrection',$uid))) + 1,1800);
  551. return oo::response(errorCode::SUCCESS);
  552. }
  553. return oo::response(errorCode::SYSTEM_ERROR);
  554. }
  555. /**
  556. * 购买过关奖励
  557. * @param $uid
  558. * @param $levelId
  559. * @return string
  560. * Created by: Owen
  561. * Created on: 2020/11/4 17:19
  562. */
  563. public function gameUpLevel($uid,$levelId,$free = 0){
  564. $config = oo::commonOprModel('config')->getMiningGameConfig($levelId,2);
  565. if(empty($config['upLevelDiamond'])){
  566. return oo::response(errorCode::RESURRECTION_CONFIG_ERROR);
  567. }
  568. $assetsInfo = oo::commonOprModel('member')->getUserAssetsInfo($uid);
  569. if($assetsInfo['diamond'] < $config['upLevelDiamond']){
  570. return oo::response(errorCode::DIAMOND_NOT_ENOUGH);
  571. }
  572. if($levelId == 1 && $free){
  573. $payStatus = 1;
  574. }else{
  575. $payStatus = oo::commonOprModel('member')->optProperty($uid,['diamond'=>$config['upLevelDiamond']],'-',24,"矿工游戏购买过关金币:{$levelId}");
  576. oo::commonOprRedis('common')->setex(okeys::MiningGameSta('diamond',$uid),intval(oo::commonOprRedis('common')->get(okeys::MiningGameSta('diamond',$uid))) + $config['upLevelDiamond'],1800);
  577. }
  578. if($payStatus){
  579. $property = oo::commonOprModel('member')->getUserAssetsInfo($uid);
  580. $level = intval($property['levelId']);
  581. $rewardConfig = oo::commonOprModel('readconfig')->getSysCon('MiningGame','Reward');
  582. $rewardConfig = json_decode($rewardConfig,true);
  583. $bet = 1;
  584. foreach ($rewardConfig['rewardBet'] as $row){
  585. if($row['minLv'] <= $level && $level <= $row['maxLv']){
  586. $bet = $row['bet'];
  587. }
  588. }
  589. oo::commonOprModel('member')->optProperty($uid,['money'=>$config['diamondCoins'] * $bet],'+',24,"矿工游戏购买过关金币:{$levelId}");
  590. return oo::response(errorCode::SUCCESS);
  591. }
  592. return oo::response(errorCode::SYSTEM_ERROR);
  593. }
  594. /**
  595. * 领取阶段奖励
  596. * @param $uid
  597. * @param $levelId
  598. * @return string
  599. * Created by: Owen
  600. * Created on: 2020/11/4 18:52
  601. */
  602. public function gameStageReward($uid,$levelId){
  603. $ttl = oo::commonOprRedis('common')->ttl(okeys::MiningGame($uid));
  604. if(!$ttl){
  605. return oo::response(errorCode::TODAY_NO_PLAY_MINING_GAME);
  606. }
  607. if(oo::commonOprRedis('common')->sContains(okeys::MiningGameStateReward($uid),$levelId)){
  608. return oo::response(errorCode::MINING_GAME_STAGE_REWARDED);
  609. }
  610. $config = json_decode(oo::commonOprModel('readconfig')->getSysCon('MiningGame','Reward'),true);
  611. $reward = array_column($config['rewardStage'],null,'levelId');
  612. $reward = $reward[$levelId];
  613. if(empty($reward)){
  614. return oo::response(errorCode::MINING_GAME_STAGE_REWARD_CONFIG_ERROR);
  615. }
  616. foreach ($reward['props'] as $key=>$row){
  617. $ret = oo::commonOprModel('member')->sendReward($uid,$row['propsId'],$row['num'], 311, "矿工游戏阶段奖励:{$levelId}");
  618. if(in_array($row['propsId'],[18,19,20,21,29,30])){
  619. $reward['props'][$key]['value'] = $ret;
  620. }
  621. }
  622. oo::commonOprRedis('common')->sAdd(okeys::MiningGameStateReward($uid),$levelId);
  623. oo::commonOprRedis('common')->expire(okeys::MiningGameStateReward($uid),$ttl);
  624. if(!oo::compareVersion(oo::commonOprModel('user')->getUserVersion($uid),'1.7.7')){
  625. oo::commonOprRedis('common')->delete(okeys::MiningGameLevel($uid));
  626. }
  627. return oo::response(errorCode::SUCCESS,$reward);
  628. }
  629. /**
  630. * 获取刷新矿工小游戏钻石价格
  631. * @return int
  632. * Created by: Owen
  633. * Created on: 2020/11/23 14:54
  634. */
  635. public function getRefreshTimeDiamond(){
  636. $config = json_decode(oo::commonOprModel('readconfig')->getSysCon('MiningGame','Reward'),true);
  637. return intval($config['refreshTimeDiamond'] ?? 10);
  638. }
  639. }
  640. ?>