<?php
/**
 * 热更新相关
 */
class ModelGameupdate
{

    private $gu_updateurl = array(
        1 => 'https://play.google.com/store/apps/details?id=',
        2 => 'https://itunes.apple.com/us/app/%E0%B9%84%E0%B8%9E-%E0%B9%80%E0%B8%97-%E0%B8%81%E0%B8%8B-%E0%B8%AA%E0%B8%8B-%E0%B9%80%E0%B8%9B%E0%B8%AD%E0%B8%A3/id1267293867?l=zh&ls=1&mt=8',
    );

    private $gu_commonurl = array(
        1 => 'https://play.google.com/store/apps/details?id=',
        2 => 'itms-apps://itunes.apple.com/WebObjects/MZStore.woa/wa/viewContentsUserReviews?type=Purple+Software&id=1267293867',
    );

    private $gu_hotfix = array(
        1 => 'https://spin.gworldtek.com/public/hotupdatefile/android/',
        2 => 'https://spin.gworldtek.com/public/hotupdatefile/ios/',
    );

    private $hw_commonurl = 'https://appgallery.cloud.huawei.com/uowap/index.html#/detailApp/C100440289';
    private $hw_updateurl = 'https://appgallery.cloud.huawei.com/uowap/index.html#/detailApp/C100440289';

    public function __construct()
    {
    }

    /**
     *	获取评论地址
     */
    public function getCommentUrl($device = 1, $sid = 1)
    {
        if( file_exists(WWWROOT.'config/config.sids.php') ){
            include WWWROOT.'config/config.sids.php';
        }
        if($device == 1) {//设备是安卓
            $packageName = $config['packageName'][$sid] ?? '';
            $url = empty($packageName) ? $this->gu_commonurl[1].'=com.superant.superpoker.th2' : $this->gu_commonurl[1].$packageName;
        }else {
            $url = $this->gu_commonurl[2];
        }
        return $url;
    }


    /**
     * 获取sid类型列表
     * @param int $type 1 安卓 2 苹果
     * @return array
     */
    public function getSidTypeList($type = 1)
    {
        if( file_exists(WWWROOT.'config/config.sids.php') ){
            include WWWROOT.'config/config.sids.php';
        }
        if($type == 1) {
            return $config['androidSidList'] ?? [];
        }else {
            return $config['iosSidList'] ?? [];
        }
    }


    public function createEmptyFlist()
    {
        $dir = '/data/texas/hotupdatefile';

        for($i = 1; $i < 3; $i++){
            for($j = 0; $j < 10; $j++){
                for($k = 0; $k < 10; $k++){
                    $gu_version = $i.'.'.$j.'.'.$k;
                    $anFilePath = "$dir/android/flist$gu_version";
                    $iosFilePath = "$dir/ios/flist$gu_version";
                    if(!file_exists( $anFilePath )){
                        shell_exec(" cp $dir/android/flistempty $anFilePath ");
                    }
                    if(!file_exists( $iosFilePath )){
                        shell_exec(" cp $dir/ios/flistempty $iosFilePath ");
                    }
                }
            }
        }

        return true;
    }


    public function getGameUpdateHotfix($sid, $version){
        $hotFix = oo::commonOprRedis('server')->get(okeys::GameUpdateHotFix($sid, $version));
        if(!empty($hotFix)){
            return json_decode($hotFix,true);
        }
        $device = [
            1=>'android',
            2=>'ios',
        ];
        $device = $device[$sid];
        $tb = otable::gameupdate();
        $sql = " SELECT * FROM {$tb} WHERE gu_version like '%{$version}%' AND gu_device='{$device}'  LIMIT 1 ";
        $ret = oo::commonOprDb('admin')->getOne($sql,MYSQLI_ASSOC);
        if(!empty($ret)){
            $tb = otable::GameHotFix();
            $sql = " SELECT * FROM {$tb} WHERE id='{$ret['id']}' ORDER BY version DESC LIMIT 1 ";
            $ret = oo::commonOprDb('admin')->getOne($sql,MYSQLI_ASSOC);
            if(!empty($ret)){
                $hotFix = [
                    'version'=>$ret['version'],
                    'isForce'=>$ret['isForce'],
                ];
                oo::commonOprRedis('server')->setex(okeys::GameUpdateHotFix($sid, $version),json_encode($hotFix,JSON_NUMERIC_CHECK),oo::redisRandomExpire(7*86400));
                return $hotFix;
            }
        }
        $hotFix = [
            'version'=>$version,
            'isForce'=>0,
        ];
        oo::commonOprRedis('server')->setex(okeys::GameUpdateHotFix($sid, $version),json_encode($hotFix,JSON_NUMERIC_CHECK),oo::redisRandomExpire(7*24*60*60));
        return $hotFix;
    }

}
