<?php

/* * **************************************************
 * 此代码为员工在公司工作期间创作.
 * 使用权和所有权归属于公司和员工共有.
 * 收益的权利归属于公司.
 *         ——石家庄龙游科技有限公司
 * ************************************************** */

namespace loyalsoft;

include_once __DIR__ . '/../../main.php';

class UpdateRetInfo {

    public $err = 0;
    public $msg = "";
    public $newVer = "";
    public $cdn = "";

    /**
     * @var string 整包更新地址
     */
    public $fullDownloadPage = "";

    /**
     * @var  \sm_clientVersionHistory  版本信息
     */
    public $versionInfo = null;
}

/**
 * 检查更新(基于和王解的update模块的约定)
 * @author gwang
 * @version 1.0.0 Created by wanggangzero at 2020.4.27 13:48:33.
 */
class CheckUpdate {

    /**
     * 打印消息并且停止游戏
     */
    const HaltWithMsg = false;
    const Md5_file = "UpdateFile";

    static function CDN_Root() {
        return match (PLAT) {
            default => "https://loyalsoft.oss-cn-shanghai.aliyuncs.com/ylsj/Res/" # 外网CDN
        };
    }

    /**
     * 检查客户端版本是否需要强制更新
     * @param Req $req
     * @return int ErrCode
     */
    public static function Check() {
        $clientVer = self::GetClientVersion();                                  # 客户端基带版本号
        $platform = self::GetClientPlatform();                                  # 客户端平台(安卓/IOS)
        $ret = new UpdateRetInfo();                                             # 返回值

        $url = self::CDN_Root() . self::Md5_file . $platform . ".json";         # 资源文件MD5列表
        $lisJson = HttpUtil::makeRequest($url, array(), array(), array(), "get"); # 取CDN上版本列表

        if (isset($lisJson["result"]) && $lisJson["result"]) {
            $fileList = JsonUtil::decode($lisJson["msg"]);
            $newVer = $fileList->Version;                                       # 取出最新版本号
            $channel = self::GetClientChannel();                                # 取出客户端的渠道字符串

            $ret->versionInfo = GameConfig::clientVersionHistory_getItem($newVer);
            $ret->newVer = $newVer;
            $ret->cdn = self::CDN_Root();
            $ret->fullDownloadPage = self::GetFullDownloadPage($channel);
            if (self::HaltWithMsg #                                             # 显示消息并且退出游戏
            ) {
                $ret->msg = "sorry, x点到y服务暂停.";
                $ret->err = ErrCode::clientPrintMsg_Halt;
            } else
            if ($clientVer < 22                                                 # 临时代码 2021.8.20 后面更新的时候需要注释掉
            // || ($newVer - $clientVer) >= 10 #                                 # 版本号差异大于10=>整包更
            ) {
                $ret->err = ErrCode::clientversionlow_err;                      # 返回错误码，强制更新1
                $ret->msg = "游戏已经发布新版本，只有更新才能进入游戏。是否立即更新?";
            } else if (($newVer - $clientVer) > 0) { #                          # 修订版本号不一致, 提示更新
                $ret->err = ErrCode::clientnewversion_msg;
                $ret->msg = "需要更新部分资源文件，是否立即更新?";
            } else {
                $ret->err = ErrCode::ok;                                        # 平安无事
                $ret->msg = "平安无事";
            }
        } else {                                                                # 检查更新失败
            $ret->err = 1031;
            $ret->msg = "检查更新失败!";
        }
        return $ret;                                                            # 返回
    }

    /**
     * 读取参数中的客户端版本
     * @return int
     */
    static function GetClientVersion() {
        $valueMap = query_paras(); // array
//        $valueMap = array('clientVer' => 22, 'plat' => 'Android', 'channel' => 'TapTap');

        if (!is_array($valueMap)) {
            exit("无参数");
        }
        $clientVer = "";
        if (isset($valueMap["clientVer"])) {
            $clientVer = urldecode($valueMap['clientVer']);
        } else {
            exit("缺少参数 'clientVer'!");
        }
        return intval($clientVer);
    }

    /**
     * 读取参数中的客户端平台(Android/IOS)
     * @return string
     */
    static function GetClientPlatform() {
        $valueMap = query_paras(); // array
//        $valueMap = array('clientVer' => 22, 'plat' => 'Android', 'channel' => 'TapTap');
        if (!is_array($valueMap)) {
            exit("无参数");
        }
        $plat = "";
        if (isset($valueMap["plat"])) {
            $plat = urldecode($valueMap['plat']);
        } else {
            exit("缺少参数 'plat'!");
        }
        return $plat;
    }

    /**
     * 读取参数中的客户端渠道(Taptap/Apple/yyb/UC/huawei/xiaomi/vivo/oppo...)
     * @return string
     */
    static function GetClientChannel() {
        $valueMap = query_paras(); // array
//        $valueMap = array('clientVer' => 22, 'plat' => 'Android', 'channel' => 'TapTap');
        if (!is_array($valueMap)) {
            exit("无参数");
        }
        $plat = "";
        if (isset($valueMap["channel"])) {
            $plat = urldecode($valueMap['channel']);
        } else {
            exit("缺少参数 'channel'!");
        }
        return $plat;
    }

    /**
     * 获取对应渠道的整包下载地址
     * @param type $channel
     * @return string
     */
    static function GetFullDownloadPage($channel) {
        $url = "https://loyalsoft.oss-cn-shanghai.aliyuncs.com/ylsj/and/com.loyalsoft.ylsj-50.apk";  # taptap
        switch ($channel) {
            case "yyb";
                $url = "https://loyalsoft.oss-cn-shanghai.aliyuncs.com/ylsj/and_yyb/com.tencent.tmgp.ylsjtx_59.apk";  # 腾讯
                break;
            default :
                $url = "https://www.taptap.com/developer/221344";                # taptap测试服
                break;
        }
        return $url;
    }
}

echo(JsonUtil::encode(CheckUpdate::Check()));                                   # 打印json给客户端
