GameCache.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. <?php
  2. /**
  3. * GameCache.php UTF-8
  4. * 游戏缓存
  5. *
  6. * @date : 2018/4/28 1:34
  7. *
  8. * @license 这不是一个自由软件,未经授权不许任何使用和传播。
  9. * @author : wuyonghong <wyh@huosdk.com>
  10. * @version : HUOSDK 8.0
  11. */
  12. namespace huo\controller\game;
  13. use huo\controller\common\Base;
  14. use huo\logic\game\GameLogic;
  15. use huo\model\game\GameModel;
  16. use huolib\constant\CacheConst;
  17. use huolib\constant\GameConst;
  18. use huomp\model\game\GameMiniModel;
  19. use think\Cache;
  20. class GameCache extends Base {
  21. public static function ins() {
  22. return new static();
  23. }
  24. /**
  25. * 获取订单KEY
  26. *
  27. * @param string $app_id
  28. *
  29. * @return string
  30. */
  31. private function getGameKey($app_id) {
  32. return 'game_id_key_'.$app_id;
  33. }
  34. /**
  35. * 判断是否红包版本
  36. *
  37. * @param $app_id
  38. *
  39. * @return bool
  40. */
  41. public function isRpBox($app_id) {
  42. $_game_data = $this->getInfoByAppId($app_id);
  43. if (empty($_game_data)) {
  44. return false;
  45. }
  46. if (GameConst::GAME_MP_RPBOX == $_game_data['classify']) {
  47. return true;
  48. }
  49. return false;
  50. }
  51. /**
  52. * 判断是否金币版本盒子
  53. *
  54. * @param $app_id
  55. *
  56. * @return bool
  57. */
  58. public function isGoldBox($app_id) {
  59. $_game_data = $this->getInfoByAppId($app_id);
  60. if (empty($_game_data)) {
  61. return false;
  62. }
  63. if (GameConst::GAME_MP_BOX == $_game_data['classify']) {
  64. return true;
  65. }
  66. return false;
  67. }
  68. /**
  69. * 获取游戏信息
  70. *
  71. * @param string $app_id
  72. *
  73. * @return array|bool|mixed
  74. */
  75. public function getInfoByAppId($app_id) {
  76. $_key = $this->getGameKey($app_id);
  77. $_game_data_json = Cache::get($_key);
  78. $_game_data = json_decode($_game_data_json, true);
  79. if (!is_array($_game_data)) {
  80. $_game_data = $_game_data_json;
  81. }
  82. if (!is_array($_game_data) || empty($_game_data)) {
  83. $_game_data = (new GameLogic())->getInfoByAppId($app_id);
  84. if (empty($_game_data)) {
  85. return false;
  86. }
  87. $this->saveGameCache($app_id, $_game_data);
  88. }
  89. return $_game_data;
  90. }
  91. /**
  92. * 保存玩家cache 数据
  93. *
  94. * @param $app_id
  95. * @param $_game_data
  96. * @param int $ttl
  97. */
  98. public function saveGameCache($app_id, $_game_data, $ttl = 3600) {
  99. $_key = $this->getGameKey($app_id);
  100. Cache::set($_key, json_encode($_game_data), $ttl);
  101. }
  102. /**
  103. * 更新游戏信息
  104. *
  105. * @param string $app_id 子游戏ID
  106. * @param array $game_data 父游戏信息
  107. *
  108. * @param bool $rm_cache 是否删除缓存
  109. *
  110. * @return bool
  111. */
  112. public function updateGame($app_id, $game_data, $rm_cache = false) {
  113. if (true == $rm_cache) {
  114. $_key = $this->getGameKey($app_id);
  115. Cache::rm($_key);
  116. $_game_data = $game_data;
  117. } else {
  118. $_old_data = $this->getInfoByAppId($app_id);
  119. $_game_data = array_merge($_old_data, $game_data);
  120. $this->saveGameCache($app_id, $_game_data);
  121. }
  122. /* 更新客服回复引导二维码 */
  123. $_mp_id = (new GameMiniModel())->getMpIdByAppId($app_id);
  124. $_cache_key = CacheConst::CACHE_MP_QRCODE_PREFIX.$_mp_id;
  125. Cache::rm($_cache_key);
  126. return (new GameModel())->updateGame($_game_data, $app_id);
  127. }
  128. }