* @version : HUOSDK 8.0 */ namespace huo\controller\game; use huo\controller\common\Base; use huo\logic\game\GameLogic; use huo\model\game\GameModel; use huolib\constant\CacheConst; use huolib\constant\GameConst; use huomp\model\game\GameMiniModel; use think\Cache; class GameCache extends Base { public static function ins() { return new static(); } /** * 获取订单KEY * * @param string $app_id * * @return string */ private function getGameKey($app_id) { return 'game_id_key_'.$app_id; } /** * 判断是否红包版本 * * @param $app_id * * @return bool */ public function isRpBox($app_id) { $_game_data = $this->getInfoByAppId($app_id); if (empty($_game_data)) { return false; } if (GameConst::GAME_MP_RPBOX == $_game_data['classify']) { return true; } return false; } /** * 判断是否金币版本盒子 * * @param $app_id * * @return bool */ public function isGoldBox($app_id) { $_game_data = $this->getInfoByAppId($app_id); if (empty($_game_data)) { return false; } if (GameConst::GAME_MP_BOX == $_game_data['classify']) { return true; } return false; } /** * 获取游戏信息 * * @param string $app_id * * @return array|bool|mixed */ public function getInfoByAppId($app_id) { $_key = $this->getGameKey($app_id); $_game_data_json = Cache::get($_key); $_game_data = json_decode($_game_data_json, true); if (!is_array($_game_data)) { $_game_data = $_game_data_json; } if (!is_array($_game_data) || empty($_game_data)) { $_game_data = (new GameLogic())->getInfoByAppId($app_id); if (empty($_game_data)) { return false; } $this->saveGameCache($app_id, $_game_data); } return $_game_data; } /** * 保存玩家cache 数据 * * @param $app_id * @param $_game_data * @param int $ttl */ public function saveGameCache($app_id, $_game_data, $ttl = 3600) { $_key = $this->getGameKey($app_id); Cache::set($_key, json_encode($_game_data), $ttl); } /** * 更新游戏信息 * * @param string $app_id 子游戏ID * @param array $game_data 父游戏信息 * * @param bool $rm_cache 是否删除缓存 * * @return bool */ public function updateGame($app_id, $game_data, $rm_cache = false) { if (true == $rm_cache) { $_key = $this->getGameKey($app_id); Cache::rm($_key); $_game_data = $game_data; } else { $_old_data = $this->getInfoByAppId($app_id); $_game_data = array_merge($_old_data, $game_data); $this->saveGameCache($app_id, $_game_data); } /* 更新客服回复引导二维码 */ $_mp_id = (new GameMiniModel())->getMpIdByAppId($app_id); $_cache_key = CacheConst::CACHE_MP_QRCODE_PREFIX.$_mp_id; Cache::rm($_cache_key); return (new GameModel())->updateGame($_game_data, $app_id); } }