| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643 | <?php/** * GameModel.php UTF-8 * 游戏model * * @date    : 2017/11/20 12:06 * * @license 这不是一个自由软件,未经授权不许任何使用和传播。 * @author  : wuyonghong <wyh@huosdk.com> * @version : HUOSDK 8.0 */namespace huo\logic\game;use huo\controller\game\GameCache;use huo\model\common\CommonModel;use huo\model\game\CategoryModel;use huo\model\game\GamecategoryModel;use huo\model\game\GameextModel;use huo\model\game\GameModel;use huo\model\game\GameserverModel;use huo\model\game\GameversionModel;use huo\model\game\GiftModel;use huo\model\member\MemGameModel;use huolib\constant\CommonConst;use huolib\constant\GameConst;use huolib\status\GameStatus;use huolib\tool\Time;use think\db\exception\DataNotFoundException;use think\db\exception\ModelNotFoundException;use think\Exception;use think\exception\DbException;use think\Log;class GameLogic extends CommonModel {    protected $base_field        = [            'id'             => 'game_id',            'name'           => 'gamename',            'icon'           => 'icon',            'category'       => 'type',            'tags'           => 'tags',            'package_name'   => 'package_name',            'publicity'      => 'oneword',            'description'    => 'desc',            'run_time'       => 'runtime',            'classify'       => 'classify',            'promote_switch' => 'promote_switch',            'hot_order'      => 'hot_order',        ];    /**     * @param $app_id     *     * @return bool|mixed     */    public function getCpPaybackUrl($app_id) {        $_map['id'] = $app_id;        $_cp_payback_url = GameModel::where($_map)->value('cp_url');        if (false === $_cp_payback_url) {            return false;        }        return $_cp_payback_url;    }    /**     * 获取玩家游戏列表     *     * @param int    $mem_id     * @param int    $from     * @param string $page     *     * @return int|array     */    public function getMemGameIDs($mem_id, $from, $page = '1,10') {        $_map['mem_id'] = $mem_id;        $_map['classify'] = $from;        $_mg_model = new MemGameModel();        $_cnt = $_mg_model->with('game')->where($_map)->count('app_id');        if (empty($_cnt)) {            return GameStatus::GAME_CNT_ZERO;        }        $_data = $_mg_model->with('game')->where($_map)->page($page)->column('app_id');        $_rdata['count'] = $_cnt;        $_rdata['list'] = $_data;        return $_rdata;    }    /**     * 通过类型获取条件     *     * @param int $server_type     *     * @return array     */    public function getServerMapByType($server_type) {        $_map = [];        $_today_time = Time::today();        switch ($server_type) {            case GameConst::GAME_SERVER_TODAY:                $_map['start_time'] = ['between', $_today_time];                $_order = 'start_time asc';                break;            case GameConst::GAME_SERVER_WILL:                $_map['start_time'] = ['gt', time()];                $_order = 'start_time asc';                break;            case GameConst::GAME_SERVER_OPENED:                $_map['start_time'] = ['lt', time()];                $_order = 'start_time desc';                break;            case GameConst::GAME_SERVER_TODAY_WILL:                /* 默认今日以后 */                $_map['start_time'] = ['egt', $_today_time[0]];                $_order = 'start_time asc';                break;            default:                $_order = 'start_time desc';        }        return [$_map, $_order];    }    /**     * 获取当个游戏开服列表     *     * @param int $game_id     游戏ID     * @param int $server_type 类型     *     * @return array|false     */    public function getGameServerList($game_id, $server_type) {        list($_map, $_order) = $this->getServerMapByType($server_type);        $_map['app_id'] = $game_id;        $_field = 'id ser_id,app_id,start_time,ser_name,ser_desc,status';        $_datas = (new GameserverModel())->field($_field)->where($_map)->order($_order)->select();        if (is_object($_datas)) {            $_datas = $_datas->toArray();        }        return $_datas;    }    /**     * @param int    $server_type     * @param        $from     * @param string $page     *     * @param int    $game_id     *     * @return int|array     */    public function getServerList($server_type, $from, $page = '1,10', $game_id = 0) {        list($_map, $_order) = $this->getServerMapByType($server_type);        if (!empty($game_id)) {            $_map['app_id'] = $game_id;        }        if (!empty($from)) {            $_map['classify'] = $this->getClassify($from);        }        $_map['gameserver_model.is_delete'] = CommonConst::CONST_NOT_DELETE;        $_gs_model = new GameserverModel();        $_cnt = $_gs_model->with('game')->where($_map)->count('app_id');        if (empty($_cnt)) {            return GameStatus::SERVER_CNT_ZERO;        }        $_field = 'app_id,start_time,ser_name,ser_desc';        $_data = $_gs_model->with('game')->field($_field)->where($_map)->page($page)->order($_order)->select();        if (is_object($_data)) {            $_data = $_data->toArray();        }        $_list = [];        if (!empty($_data)) {            foreach ($_data as $_k => $_v) {                $_ser_data['ser_id'] = $_v['id'];                $_ser_data['app_id'] = $_v['app_id'];                $_ser_data['game_id'] = $_v['app_id'];                $_ser_data['gamename'] = $_v['game']['name'];                $_ser_data['icon'] = $_v['game']['icon'];                $_ser_data['type'] = $_v['game']['category'];                $_ser_data['tags'] = $_v['game']['tags'];                $_ser_data['package_name'] = $_v['game']['package_name'];                $_ser_data['oneword'] = $_v['game']['publicity'];                $_ser_data['runtime'] = $_v['game']['run_time'];                $_ser_data['start_time'] = $_v['start_time'];                $_ser_data['ser_name'] = $_v['ser_name'];                $_ser_data['ser_desc'] = $_v['ser_desc'];                $_list[] = $_ser_data;            }        }        $_rdata['count'] = $_cnt;        $_rdata['list'] = $_list;        return $_rdata;    }    /**     * @param array  $where     * @param string $page     * @param string $order     *     * @param array  $field     *     * @return int| array     */    public function getList($where = [], $page = '1,10', $order = '', $field = []) {        $where['status'] = GameConst::GAME_STATUS_ON;        if (isset($where['classify'])) {            $where['classify'] = $this->getClassify($where['classify']);        }        $_game_model = new GameModel();        $_count = $_game_model->with('ext')->with('gv')->where($where)->count();        if (empty($_count)) {            $_rdata['count'] = 0;            $_rdata['list'] = [];            return $_rdata;        }        $_field = $this->base_field;        if (!empty($field)) {            $_field = array_merge($_field, $field);        }        $_field['classify'] = 'classify_label';        $_field['promote_switch'] = 'promote_switch_label';        $_order = $this->orderFilter($order);        try {            $_games = $_game_model                ->with('ext')                ->with('gv')                ->field($_field)                ->where($where)                ->order($_order)                ->page($page)                ->select();            if (is_object($_games)) {                $_games = $_games->toArray();            }            $_list = [];            foreach ($_games as $_k => $_v) {                $_data = [];                $_data['game_id'] = $_v['game_id'];                $_data['gamename'] = $_v['gamename'];                $_data['icon'] = $_v['icon'];                $_data['size'] = isset($_v['gv'][0]) ? $_v['gv'][0]['size'] : '';                $_data['type'] = $_v['type'];                $_data['tags'] = $_v['tags'];                $_data['down_cnt'] = $_v['ext']['down_cnt'];                $_data['down_url'] = isset($_v['gv'][0]) ? $_v['gv'][0]['package_url'] : '';                $_data['version'] = isset($_v['gv'][0]) ? $_v['gv'][0]['version'] : '';                $_data['gift_cnt'] = 0;                $_data['package_name'] = $_v['package_name'];                $_data['oneword'] = $_v['oneword'];                $_data['runtime'] = $_v['runtime'];                $_data['classify'] = $_v['classify'];                $_data['classify_label'] = $_v['classify_label'];                $_data['promote_switch'] = $_v['promote_switch_label'];                $_data['fine_image']                    = isset($_v['ext_info']['fine_image']) ? $_v['ext_info']['fine_image'] : ''; /* 2018.08.02 合并字段 */                $_data['rate'] = 1; /* 2018.08.02 默认折扣为1 表示无折扣 */                $_data['is_bt'] = $_v['is_bt']; /* 2018.08.02 添加是否BT */                $_data['single_tag'] = $_v['single_tag']; /* 2018.08.02 BT单个标签 */                if (isset($_v['is_online'])) {                    $_data['is_online'] = $_v['is_online'];                }                if (isset($_v['list_order'])) {                    $_data['list_order'] = $_v['list_order'];                }                if (isset($_v['hot_order'])) {                    $_data['hot_order'] = $_v['hot_order'];                }                if (isset($_v['like_order'])) {                    $_data['like_order'] = $_v['like_order'];                }                if (isset($_v['status'])) {                    $_data['status'] = $_v['status'];                }                $_list[] = $_data;            }            if (empty($_games)) {                $_list = null;            }            $_rdata['count'] = $_count;            $_rdata['list'] = $_list;            return $_rdata;        } catch (DataNotFoundException $e) {            return GameStatus::DATA_NOT_FOUND_EXCEPTION;        } catch (ModelNotFoundException $e) {            return GameStatus::MODEL_NOT_FOUND_EXCEPTION;        } catch (DbException $e) {            return GameStatus::DB_EXCEPTION;        }    }    /**     * @param $_cates     * @param $page     * @param $_order     *     * @return array|int     */    public function getCateList($_cates, $page, $_order) {        if (empty($_cates)) {            return 2000;        }        // 设置id,ids        if (!empty($_cates)) {            $_ids = $this->strToArr($_cates);            foreach ($_ids as $_k => $_v) {                $_ids[$_k] = intval($_v);            }        }        $_find_category = CategoryModel::whereIn('id', $_ids)->column('id');        if (empty($_find_category)) {            return 2000;        }        $_app_ids = GamecategoryModel::whereIn('cate_id', $_find_category)->column('app_id');        $_map['id'] = ['in', $_app_ids];        $_param['page'] = $page;        $_param['order'] = $_order;        return $this->getList($_map, $page, $_order);    }    /**     * 获取游戏类别筛选条件     * 3开头为android 4 开头为IOS 5 开头为H5 对应于game_class中的ID     *     * @param int $classify 传入的值     *     * @return array|int     */    public function getClassify($classify = 3) {        $_classify = $classify;        if (3 == substr($_classify, 0, 1)) {            $_classify = [                ['eq', 3],                ['between', [300, 399]],                'or'            ];        } elseif (4 == substr($_classify, 0, 1) && 401 != $_classify) {            $_classify = [                ['eq', 4],                ['between', [400, 499]],                'or'            ];        }        return $_classify;    }    /**     * 通过游戏ID 查询游戏信息     *     * @param $app_id     *     * @return array|bool     */    public function getInfoByAppId($app_id) {        if (empty($app_id)) {            return false;        }        $_map['id'] = $app_id;        try {            $_game_data = (new GameModel())->with('gv')->where($_map)->find();            if (is_object($_game_data)) {                $_game_data = $_game_data->toArray();            }            return $_game_data;        } catch (DataNotFoundException $_e) {            Log::write(                "func=".__FUNCTION__."&class=".__CLASS__."&code=".GameStatus::DATA_NOT_FOUND_EXCEPTION."&Exception"                .$_e->getMessage(),                LOG::ERROR            );            return false;        } catch (ModelNotFoundException $_e) {            Log::write(                "func=".__FUNCTION__."&class=".__CLASS__."&code=".GameStatus::MODEL_NOT_FOUND_EXCEPTION."&Exception"                .$_e->getMessage(),                LOG::ERROR            );            return false;        } catch (DbException $_e) {            Log::write(                "func=".__FUNCTION__."&class=".__CLASS__."&code=".GameStatus::DB_EXCEPTION."&Exception"                .$_e->getMessage(),                LOG::ERROR            );            return false;        } catch (Exception $_e) {            Log::write(                "func=".__FUNCTION__."&class=".__CLASS__."&code=".GameStatus::INNER_ERROR."&Exception"                .$_e->getMessage(),                LOG::ERROR            );            return false;        }    }    /**     * 获取游戏详情     *     * @param array  $where     * @param string $field     *     * @return mixed     */    public function getDetail($where, $field = '') {        $_field = $this->base_field;        $_game = (new GameModel())->with('ext')->with('gv')->field($_field)->where($where)->find();        if (empty($_game)) {            return GameStatus::GAME_NOT_EXISTS;        }        $_data['game_id'] = $_game['game_id'];        $_data['gamename'] = $_game['gamename'];        $_data['icon'] = $_game['icon'];        $_data['size'] = $_game['gv'][0]['size'];        $_data['type'] = $_game['type'];        $_data['tags'] = $_game['tags'];        $_data['down_cnt'] = $_game['ext']['down_cnt'];        $_data['star_cnt'] = $_game['ext']['star_cnt'];        $_data['gift_cnt'] = $_game['ext']['real_gift_cnt'];        $_data['down_url'] = $_game['gv'][0]['package_url'];        $_data['url'] = $_game['gv'][0]['package_url'];        $_data['gift_cnt'] = 0;        $_data['package_name'] = $_game['package_name'];        $_data['version'] = $_game['gv'][0]['version'];        $_data['oneword'] = $_game['oneword'];        $_data['desc'] = $_game['description'];        $_data['runtime'] = $_game['runtime'];        $_data['classify'] = $_game['classify'];        $_data['hot_order'] = $_game['hot_order'];        $_data['image'] = $this->getImages($_game['image']);        /* 2018.08.02 合并字段 */        $_data['rebate_description'] = isset($_game['ext_info']['fine_image']) ? $_game['ext_info']['fine_image'] : '';        /* 2018.08.02 合并字段 */        $_data['vip_description '] = isset($_game['ext_info']['vip_description '])            ? $_game['ext_info']['vip_description '] : '';        return $_data;    }    public function getImages($value) {        $_images = $value;        if (!empty($_images)) {            foreach ($_images as $_key => $_image) {                $_images[$_key] = cmf_get_image_url($_image['url']);            }        }        return $_images;    }    /**     * 获取类型     *     * @param array  $where     * @param string $page     * @param string $order     *     * @return mixed     */    public function getCategory($where = [], $page = '1,10', $order = '') {        $_cate_model = new CategoryModel();        if (!empty($where['parent_id'])) {            $_datas = $_cate_model->getList($where['parent_id'], $order);        } else {            $_datas = $_cate_model->getList(0, $order);        }        $_count = count($_datas);        if (empty($_count)) {            $_rdata['count'] = 0;            $_rdata['list'] = [];        } else {            $_rdata['count'] = $_count;            $_rdata['list'] = $_datas;        }        return $_rdata;    }    /**     * 根据游戏获取礼包数量     *     * @param int $app_id     *     * @return int|string     */    public function getGiftCnt($app_id = 0) {        $_map['remain_cnt'] = ['gt', 0];        $_map['end_time'] = ['gt', time()];        $_map['app_id'] = $app_id;        $_cnt = GiftModel::where($_map)->count();        if (empty($_cnt)) {            return 0;        }        return $_cnt;    }    /**     * 获取游戏下载地址     *     * @param int $app_id     *     * @return int|mixed|string     */    public function getDownUrl($app_id = 0) {        if (empty($app_id)) {            return '';        }        $_map['app_id'] = $app_id;        $_gv = GameversionModel::get($_map);        if (empty($_gv->package_url)) {            return '';        }        return $_gv->package_url;    }    /**     * 获取游戏下载次数     *     * @param int $app_id     *     * @return int|string     */    public function getDownCnt($app_id = 0) {        if (empty($app_id)) {            return '';        }        $_map['app_id'] = $app_id;        $_cnt = GameextModel::where($_map)->value('down_cnt');        if (empty($_cnt)) {            return 0;        }        return $_cnt;    }    /**     * 获取版本key     *     * @param $app_id     * @param $client_id     *     * @return bool     */    public function getVersionKey($app_id, $client_id) {        if (empty($app_id) || empty($client_id)) {            return false;        }        $_map['app_id'] = $app_id;        $_map['id'] = $client_id;        $_version_key = (new GameversionModel())->getVersionKey($_map);        if (empty($_version_key)) {            return false;        }        return $_version_key;    }    /**     * 获取游戏 id name 键值对     *     * @param array  $where     * int   $status    是否上线     * int   $is_delete 是否删除  2 删除     * int   $is_sdk    是否是SDK     * int   $classify  游戏平台     * bool  $game_flag     * bool  $add_plat     *     * @param string $page     *     * @return array     */    public function getIdNames($where = [], $page = '') {        $_status = 2;        $_is_delete = 2;        $_is_sdk = 0;        $_classify = 0;        $_game_flag = false;        $_add_plat = false;        if (isset($where['status']) && is_numeric($where['status'])) {            $_status = $where['status'];            unset($where['status']);        }        if (isset($where['is_delete']) && is_numeric($where['is_delete'])) {            $_is_delete = $where['is_delete'];            unset($where['is_delete']);        }        if (isset($where['is_sdk']) && is_numeric($where['is_sdk'])) {            $_is_sdk = $where['is_sdk'];            unset($where['is_sdk']);        }        if (isset($where['classify']) && is_numeric($where['classify'])) {            $_classify = $where['classify'];            unset($where['classify']);        }        if (isset($where['game_flag'])) {            $_game_flag = $where['game_flag'];            unset($where['game_flag']);        }        if (isset($where['add_plat'])) {            $_add_plat = $where['add_plat'];            unset($where['add_plat']);        }        $_map = $where;        $_data = (new GameModel())->getIdNames(            $_status, $_is_delete, $_is_sdk, $_classify, $_game_flag, $_add_plat, $_map, $page        );        return $_data;    }    /**     * 验证游戏是否存在     *     * @param $app_id     *     * @return bool|int     */    public function checkGame($app_id) {        if (empty($app_id)) {            return GameStatus::GAME_ID_EMPTY;        }        $_game_info = GameCache::ins()->getInfoByAppId($app_id);        if (empty($_game_info)) {            return GameStatus::GAME_NOT_EXISTS;        }        return true;    }}
 |