GameLogic.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643
  1. <?php
  2. /**
  3. * GameModel.php UTF-8
  4. * 游戏model
  5. *
  6. * @date : 2017/11/20 12:06
  7. *
  8. * @license 这不是一个自由软件,未经授权不许任何使用和传播。
  9. * @author : wuyonghong <wyh@huosdk.com>
  10. * @version : HUOSDK 8.0
  11. */
  12. namespace huo\logic\game;
  13. use huo\controller\game\GameCache;
  14. use huo\model\common\CommonModel;
  15. use huo\model\game\CategoryModel;
  16. use huo\model\game\GamecategoryModel;
  17. use huo\model\game\GameextModel;
  18. use huo\model\game\GameModel;
  19. use huo\model\game\GameserverModel;
  20. use huo\model\game\GameversionModel;
  21. use huo\model\game\GiftModel;
  22. use huo\model\member\MemGameModel;
  23. use huolib\constant\CommonConst;
  24. use huolib\constant\GameConst;
  25. use huolib\status\GameStatus;
  26. use huolib\tool\Time;
  27. use think\db\exception\DataNotFoundException;
  28. use think\db\exception\ModelNotFoundException;
  29. use think\Exception;
  30. use think\exception\DbException;
  31. use think\Log;
  32. class GameLogic extends CommonModel {
  33. protected $base_field
  34. = [
  35. 'id' => 'game_id',
  36. 'name' => 'gamename',
  37. 'icon' => 'icon',
  38. 'category' => 'type',
  39. 'tags' => 'tags',
  40. 'package_name' => 'package_name',
  41. 'publicity' => 'oneword',
  42. 'description' => 'desc',
  43. 'run_time' => 'runtime',
  44. 'classify' => 'classify',
  45. 'promote_switch' => 'promote_switch',
  46. 'hot_order' => 'hot_order',
  47. ];
  48. /**
  49. * @param $app_id
  50. *
  51. * @return bool|mixed
  52. */
  53. public function getCpPaybackUrl($app_id) {
  54. $_map['id'] = $app_id;
  55. $_cp_payback_url = GameModel::where($_map)->value('cp_url');
  56. if (false === $_cp_payback_url) {
  57. return false;
  58. }
  59. return $_cp_payback_url;
  60. }
  61. /**
  62. * 获取玩家游戏列表
  63. *
  64. * @param int $mem_id
  65. * @param int $from
  66. * @param string $page
  67. *
  68. * @return int|array
  69. */
  70. public function getMemGameIDs($mem_id, $from, $page = '1,10') {
  71. $_map['mem_id'] = $mem_id;
  72. $_map['classify'] = $from;
  73. $_mg_model = new MemGameModel();
  74. $_cnt = $_mg_model->with('game')->where($_map)->count('app_id');
  75. if (empty($_cnt)) {
  76. return GameStatus::GAME_CNT_ZERO;
  77. }
  78. $_data = $_mg_model->with('game')->where($_map)->page($page)->column('app_id');
  79. $_rdata['count'] = $_cnt;
  80. $_rdata['list'] = $_data;
  81. return $_rdata;
  82. }
  83. /**
  84. * 通过类型获取条件
  85. *
  86. * @param int $server_type
  87. *
  88. * @return array
  89. */
  90. public function getServerMapByType($server_type) {
  91. $_map = [];
  92. $_today_time = Time::today();
  93. switch ($server_type) {
  94. case GameConst::GAME_SERVER_TODAY:
  95. $_map['start_time'] = ['between', $_today_time];
  96. $_order = 'start_time asc';
  97. break;
  98. case GameConst::GAME_SERVER_WILL:
  99. $_map['start_time'] = ['gt', time()];
  100. $_order = 'start_time asc';
  101. break;
  102. case GameConst::GAME_SERVER_OPENED:
  103. $_map['start_time'] = ['lt', time()];
  104. $_order = 'start_time desc';
  105. break;
  106. case GameConst::GAME_SERVER_TODAY_WILL:
  107. /* 默认今日以后 */
  108. $_map['start_time'] = ['egt', $_today_time[0]];
  109. $_order = 'start_time asc';
  110. break;
  111. default:
  112. $_order = 'start_time desc';
  113. }
  114. return [$_map, $_order];
  115. }
  116. /**
  117. * 获取当个游戏开服列表
  118. *
  119. * @param int $game_id 游戏ID
  120. * @param int $server_type 类型
  121. *
  122. * @return array|false
  123. */
  124. public function getGameServerList($game_id, $server_type) {
  125. list($_map, $_order) = $this->getServerMapByType($server_type);
  126. $_map['app_id'] = $game_id;
  127. $_field = 'id ser_id,app_id,start_time,ser_name,ser_desc,status';
  128. $_datas = (new GameserverModel())->field($_field)->where($_map)->order($_order)->select();
  129. if (is_object($_datas)) {
  130. $_datas = $_datas->toArray();
  131. }
  132. return $_datas;
  133. }
  134. /**
  135. * @param int $server_type
  136. * @param $from
  137. * @param string $page
  138. *
  139. * @param int $game_id
  140. *
  141. * @return int|array
  142. */
  143. public function getServerList($server_type, $from, $page = '1,10', $game_id = 0) {
  144. list($_map, $_order) = $this->getServerMapByType($server_type);
  145. if (!empty($game_id)) {
  146. $_map['app_id'] = $game_id;
  147. }
  148. if (!empty($from)) {
  149. $_map['classify'] = $this->getClassify($from);
  150. }
  151. $_map['gameserver_model.is_delete'] = CommonConst::CONST_NOT_DELETE;
  152. $_gs_model = new GameserverModel();
  153. $_cnt = $_gs_model->with('game')->where($_map)->count('app_id');
  154. if (empty($_cnt)) {
  155. return GameStatus::SERVER_CNT_ZERO;
  156. }
  157. $_field = 'app_id,start_time,ser_name,ser_desc';
  158. $_data = $_gs_model->with('game')->field($_field)->where($_map)->page($page)->order($_order)->select();
  159. if (is_object($_data)) {
  160. $_data = $_data->toArray();
  161. }
  162. $_list = [];
  163. if (!empty($_data)) {
  164. foreach ($_data as $_k => $_v) {
  165. $_ser_data['ser_id'] = $_v['id'];
  166. $_ser_data['app_id'] = $_v['app_id'];
  167. $_ser_data['game_id'] = $_v['app_id'];
  168. $_ser_data['gamename'] = $_v['game']['name'];
  169. $_ser_data['icon'] = $_v['game']['icon'];
  170. $_ser_data['type'] = $_v['game']['category'];
  171. $_ser_data['tags'] = $_v['game']['tags'];
  172. $_ser_data['package_name'] = $_v['game']['package_name'];
  173. $_ser_data['oneword'] = $_v['game']['publicity'];
  174. $_ser_data['runtime'] = $_v['game']['run_time'];
  175. $_ser_data['start_time'] = $_v['start_time'];
  176. $_ser_data['ser_name'] = $_v['ser_name'];
  177. $_ser_data['ser_desc'] = $_v['ser_desc'];
  178. $_list[] = $_ser_data;
  179. }
  180. }
  181. $_rdata['count'] = $_cnt;
  182. $_rdata['list'] = $_list;
  183. return $_rdata;
  184. }
  185. /**
  186. * @param array $where
  187. * @param string $page
  188. * @param string $order
  189. *
  190. * @param array $field
  191. *
  192. * @return int| array
  193. */
  194. public function getList($where = [], $page = '1,10', $order = '', $field = []) {
  195. $where['status'] = GameConst::GAME_STATUS_ON;
  196. if (isset($where['classify'])) {
  197. $where['classify'] = $this->getClassify($where['classify']);
  198. }
  199. $_game_model = new GameModel();
  200. $_count = $_game_model->with('ext')->with('gv')->where($where)->count();
  201. if (empty($_count)) {
  202. $_rdata['count'] = 0;
  203. $_rdata['list'] = [];
  204. return $_rdata;
  205. }
  206. $_field = $this->base_field;
  207. if (!empty($field)) {
  208. $_field = array_merge($_field, $field);
  209. }
  210. $_field['classify'] = 'classify_label';
  211. $_field['promote_switch'] = 'promote_switch_label';
  212. $_order = $this->orderFilter($order);
  213. try {
  214. $_games = $_game_model
  215. ->with('ext')
  216. ->with('gv')
  217. ->field($_field)
  218. ->where($where)
  219. ->order($_order)
  220. ->page($page)
  221. ->select();
  222. if (is_object($_games)) {
  223. $_games = $_games->toArray();
  224. }
  225. $_list = [];
  226. foreach ($_games as $_k => $_v) {
  227. $_data = [];
  228. $_data['game_id'] = $_v['game_id'];
  229. $_data['gamename'] = $_v['gamename'];
  230. $_data['icon'] = $_v['icon'];
  231. $_data['size'] = isset($_v['gv'][0]) ? $_v['gv'][0]['size'] : '';
  232. $_data['type'] = $_v['type'];
  233. $_data['tags'] = $_v['tags'];
  234. $_data['down_cnt'] = $_v['ext']['down_cnt'];
  235. $_data['down_url'] = isset($_v['gv'][0]) ? $_v['gv'][0]['package_url'] : '';
  236. $_data['version'] = isset($_v['gv'][0]) ? $_v['gv'][0]['version'] : '';
  237. $_data['gift_cnt'] = 0;
  238. $_data['package_name'] = $_v['package_name'];
  239. $_data['oneword'] = $_v['oneword'];
  240. $_data['runtime'] = $_v['runtime'];
  241. $_data['classify'] = $_v['classify'];
  242. $_data['classify_label'] = $_v['classify_label'];
  243. $_data['promote_switch'] = $_v['promote_switch_label'];
  244. $_data['fine_image']
  245. = isset($_v['ext_info']['fine_image']) ? $_v['ext_info']['fine_image'] : ''; /* 2018.08.02 合并字段 */
  246. $_data['rate'] = 1; /* 2018.08.02 默认折扣为1 表示无折扣 */
  247. $_data['is_bt'] = $_v['is_bt']; /* 2018.08.02 添加是否BT */
  248. $_data['single_tag'] = $_v['single_tag']; /* 2018.08.02 BT单个标签 */
  249. if (isset($_v['is_online'])) {
  250. $_data['is_online'] = $_v['is_online'];
  251. }
  252. if (isset($_v['list_order'])) {
  253. $_data['list_order'] = $_v['list_order'];
  254. }
  255. if (isset($_v['hot_order'])) {
  256. $_data['hot_order'] = $_v['hot_order'];
  257. }
  258. if (isset($_v['like_order'])) {
  259. $_data['like_order'] = $_v['like_order'];
  260. }
  261. if (isset($_v['status'])) {
  262. $_data['status'] = $_v['status'];
  263. }
  264. $_list[] = $_data;
  265. }
  266. if (empty($_games)) {
  267. $_list = null;
  268. }
  269. $_rdata['count'] = $_count;
  270. $_rdata['list'] = $_list;
  271. return $_rdata;
  272. } catch (DataNotFoundException $e) {
  273. return GameStatus::DATA_NOT_FOUND_EXCEPTION;
  274. } catch (ModelNotFoundException $e) {
  275. return GameStatus::MODEL_NOT_FOUND_EXCEPTION;
  276. } catch (DbException $e) {
  277. return GameStatus::DB_EXCEPTION;
  278. }
  279. }
  280. /**
  281. * @param $_cates
  282. * @param $page
  283. * @param $_order
  284. *
  285. * @return array|int
  286. */
  287. public function getCateList($_cates, $page, $_order) {
  288. if (empty($_cates)) {
  289. return 2000;
  290. }
  291. // 设置id,ids
  292. if (!empty($_cates)) {
  293. $_ids = $this->strToArr($_cates);
  294. foreach ($_ids as $_k => $_v) {
  295. $_ids[$_k] = intval($_v);
  296. }
  297. }
  298. $_find_category = CategoryModel::whereIn('id', $_ids)->column('id');
  299. if (empty($_find_category)) {
  300. return 2000;
  301. }
  302. $_app_ids = GamecategoryModel::whereIn('cate_id', $_find_category)->column('app_id');
  303. $_map['id'] = ['in', $_app_ids];
  304. $_param['page'] = $page;
  305. $_param['order'] = $_order;
  306. return $this->getList($_map, $page, $_order);
  307. }
  308. /**
  309. * 获取游戏类别筛选条件
  310. * 3开头为android 4 开头为IOS 5 开头为H5 对应于game_class中的ID
  311. *
  312. * @param int $classify 传入的值
  313. *
  314. * @return array|int
  315. */
  316. public function getClassify($classify = 3) {
  317. $_classify = $classify;
  318. if (3 == substr($_classify, 0, 1)) {
  319. $_classify = [
  320. ['eq', 3],
  321. ['between', [300, 399]],
  322. 'or'
  323. ];
  324. } elseif (4 == substr($_classify, 0, 1) && 401 != $_classify) {
  325. $_classify = [
  326. ['eq', 4],
  327. ['between', [400, 499]],
  328. 'or'
  329. ];
  330. }
  331. return $_classify;
  332. }
  333. /**
  334. * 通过游戏ID 查询游戏信息
  335. *
  336. * @param $app_id
  337. *
  338. * @return array|bool
  339. */
  340. public function getInfoByAppId($app_id) {
  341. if (empty($app_id)) {
  342. return false;
  343. }
  344. $_map['id'] = $app_id;
  345. try {
  346. $_game_data = (new GameModel())->with('gv')->where($_map)->find();
  347. if (is_object($_game_data)) {
  348. $_game_data = $_game_data->toArray();
  349. }
  350. return $_game_data;
  351. } catch (DataNotFoundException $_e) {
  352. Log::write(
  353. "func=".__FUNCTION__."&class=".__CLASS__."&code=".GameStatus::DATA_NOT_FOUND_EXCEPTION."&Exception"
  354. .$_e->getMessage(),
  355. LOG::ERROR
  356. );
  357. return false;
  358. } catch (ModelNotFoundException $_e) {
  359. Log::write(
  360. "func=".__FUNCTION__."&class=".__CLASS__."&code=".GameStatus::MODEL_NOT_FOUND_EXCEPTION."&Exception"
  361. .$_e->getMessage(),
  362. LOG::ERROR
  363. );
  364. return false;
  365. } catch (DbException $_e) {
  366. Log::write(
  367. "func=".__FUNCTION__."&class=".__CLASS__."&code=".GameStatus::DB_EXCEPTION."&Exception"
  368. .$_e->getMessage(),
  369. LOG::ERROR
  370. );
  371. return false;
  372. } catch (Exception $_e) {
  373. Log::write(
  374. "func=".__FUNCTION__."&class=".__CLASS__."&code=".GameStatus::INNER_ERROR."&Exception"
  375. .$_e->getMessage(),
  376. LOG::ERROR
  377. );
  378. return false;
  379. }
  380. }
  381. /**
  382. * 获取游戏详情
  383. *
  384. * @param array $where
  385. * @param string $field
  386. *
  387. * @return mixed
  388. */
  389. public function getDetail($where, $field = '') {
  390. $_field = $this->base_field;
  391. $_game = (new GameModel())->with('ext')->with('gv')->field($_field)->where($where)->find();
  392. if (empty($_game)) {
  393. return GameStatus::GAME_NOT_EXISTS;
  394. }
  395. $_data['game_id'] = $_game['game_id'];
  396. $_data['gamename'] = $_game['gamename'];
  397. $_data['icon'] = $_game['icon'];
  398. $_data['size'] = $_game['gv'][0]['size'];
  399. $_data['type'] = $_game['type'];
  400. $_data['tags'] = $_game['tags'];
  401. $_data['down_cnt'] = $_game['ext']['down_cnt'];
  402. $_data['star_cnt'] = $_game['ext']['star_cnt'];
  403. $_data['gift_cnt'] = $_game['ext']['real_gift_cnt'];
  404. $_data['down_url'] = $_game['gv'][0]['package_url'];
  405. $_data['url'] = $_game['gv'][0]['package_url'];
  406. $_data['gift_cnt'] = 0;
  407. $_data['package_name'] = $_game['package_name'];
  408. $_data['version'] = $_game['gv'][0]['version'];
  409. $_data['oneword'] = $_game['oneword'];
  410. $_data['desc'] = $_game['description'];
  411. $_data['runtime'] = $_game['runtime'];
  412. $_data['classify'] = $_game['classify'];
  413. $_data['hot_order'] = $_game['hot_order'];
  414. $_data['image'] = $this->getImages($_game['image']);
  415. /* 2018.08.02 合并字段 */
  416. $_data['rebate_description'] = isset($_game['ext_info']['fine_image']) ? $_game['ext_info']['fine_image'] : '';
  417. /* 2018.08.02 合并字段 */
  418. $_data['vip_description '] = isset($_game['ext_info']['vip_description '])
  419. ? $_game['ext_info']['vip_description '] : '';
  420. return $_data;
  421. }
  422. public function getImages($value) {
  423. $_images = $value;
  424. if (!empty($_images)) {
  425. foreach ($_images as $_key => $_image) {
  426. $_images[$_key] = cmf_get_image_url($_image['url']);
  427. }
  428. }
  429. return $_images;
  430. }
  431. /**
  432. * 获取类型
  433. *
  434. * @param array $where
  435. * @param string $page
  436. * @param string $order
  437. *
  438. * @return mixed
  439. */
  440. public function getCategory($where = [], $page = '1,10', $order = '') {
  441. $_cate_model = new CategoryModel();
  442. if (!empty($where['parent_id'])) {
  443. $_datas = $_cate_model->getList($where['parent_id'], $order);
  444. } else {
  445. $_datas = $_cate_model->getList(0, $order);
  446. }
  447. $_count = count($_datas);
  448. if (empty($_count)) {
  449. $_rdata['count'] = 0;
  450. $_rdata['list'] = [];
  451. } else {
  452. $_rdata['count'] = $_count;
  453. $_rdata['list'] = $_datas;
  454. }
  455. return $_rdata;
  456. }
  457. /**
  458. * 根据游戏获取礼包数量
  459. *
  460. * @param int $app_id
  461. *
  462. * @return int|string
  463. */
  464. public function getGiftCnt($app_id = 0) {
  465. $_map['remain_cnt'] = ['gt', 0];
  466. $_map['end_time'] = ['gt', time()];
  467. $_map['app_id'] = $app_id;
  468. $_cnt = GiftModel::where($_map)->count();
  469. if (empty($_cnt)) {
  470. return 0;
  471. }
  472. return $_cnt;
  473. }
  474. /**
  475. * 获取游戏下载地址
  476. *
  477. * @param int $app_id
  478. *
  479. * @return int|mixed|string
  480. */
  481. public function getDownUrl($app_id = 0) {
  482. if (empty($app_id)) {
  483. return '';
  484. }
  485. $_map['app_id'] = $app_id;
  486. $_gv = GameversionModel::get($_map);
  487. if (empty($_gv->package_url)) {
  488. return '';
  489. }
  490. return $_gv->package_url;
  491. }
  492. /**
  493. * 获取游戏下载次数
  494. *
  495. * @param int $app_id
  496. *
  497. * @return int|string
  498. */
  499. public function getDownCnt($app_id = 0) {
  500. if (empty($app_id)) {
  501. return '';
  502. }
  503. $_map['app_id'] = $app_id;
  504. $_cnt = GameextModel::where($_map)->value('down_cnt');
  505. if (empty($_cnt)) {
  506. return 0;
  507. }
  508. return $_cnt;
  509. }
  510. /**
  511. * 获取版本key
  512. *
  513. * @param $app_id
  514. * @param $client_id
  515. *
  516. * @return bool
  517. */
  518. public function getVersionKey($app_id, $client_id) {
  519. if (empty($app_id) || empty($client_id)) {
  520. return false;
  521. }
  522. $_map['app_id'] = $app_id;
  523. $_map['id'] = $client_id;
  524. $_version_key = (new GameversionModel())->getVersionKey($_map);
  525. if (empty($_version_key)) {
  526. return false;
  527. }
  528. return $_version_key;
  529. }
  530. /**
  531. * 获取游戏 id name 键值对
  532. *
  533. * @param array $where
  534. * int $status 是否上线
  535. * int $is_delete 是否删除 2 删除
  536. * int $is_sdk 是否是SDK
  537. * int $classify 游戏平台
  538. * bool $game_flag
  539. * bool $add_plat
  540. *
  541. * @param string $page
  542. *
  543. * @return array
  544. */
  545. public function getIdNames($where = [], $page = '') {
  546. $_status = 2;
  547. $_is_delete = 2;
  548. $_is_sdk = 0;
  549. $_classify = 0;
  550. $_game_flag = false;
  551. $_add_plat = false;
  552. if (isset($where['status']) && is_numeric($where['status'])) {
  553. $_status = $where['status'];
  554. unset($where['status']);
  555. }
  556. if (isset($where['is_delete']) && is_numeric($where['is_delete'])) {
  557. $_is_delete = $where['is_delete'];
  558. unset($where['is_delete']);
  559. }
  560. if (isset($where['is_sdk']) && is_numeric($where['is_sdk'])) {
  561. $_is_sdk = $where['is_sdk'];
  562. unset($where['is_sdk']);
  563. }
  564. if (isset($where['classify']) && is_numeric($where['classify'])) {
  565. $_classify = $where['classify'];
  566. unset($where['classify']);
  567. }
  568. if (isset($where['game_flag'])) {
  569. $_game_flag = $where['game_flag'];
  570. unset($where['game_flag']);
  571. }
  572. if (isset($where['add_plat'])) {
  573. $_add_plat = $where['add_plat'];
  574. unset($where['add_plat']);
  575. }
  576. $_map = $where;
  577. $_data = (new GameModel())->getIdNames(
  578. $_status, $_is_delete, $_is_sdk, $_classify, $_game_flag, $_add_plat, $_map, $page
  579. );
  580. return $_data;
  581. }
  582. /**
  583. * 验证游戏是否存在
  584. *
  585. * @param $app_id
  586. *
  587. * @return bool|int
  588. */
  589. public function checkGame($app_id) {
  590. if (empty($app_id)) {
  591. return GameStatus::GAME_ID_EMPTY;
  592. }
  593. $_game_info = GameCache::ins()->getInfoByAppId($app_id);
  594. if (empty($_game_info)) {
  595. return GameStatus::GAME_NOT_EXISTS;
  596. }
  597. return true;
  598. }
  599. }