| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142 | <?php/** * GameCache.php UTF-8 * 游戏缓存 * * @date    : 2018/4/28 1:34 * * @license 这不是一个自由软件,未经授权不许任何使用和传播。 * @author  : wuyonghong <wyh@huosdk.com> * @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);    }}
 |