@page "/handle/{identifier}/{userId}/{gameId}.html"
@model CloudGaming.Web.Pages.Handle.IndexModel
@{
    Layout = "";
    ViewData["Title"] = "手柄";
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0,user-scalable=no" />
    <meta name="full-screen" content="yes">
    <meta name="browsermode" content="application">
    <title>H5Handler</title>
    <style>
        * {
            -webkit-user-select: none;
            user-select: none;
            -webkit-touch-callout: none;
            -webkit-user-select: none;
            -webkit-touch-callout: none;
        }

        body {
            padding: 0;
            margin: 0;
            background-color: #000;
            overflow-x: hidden;
        }

            body.connected {
                transform: rotate(90deg);
            }

        img[src*=shoubing2].active {
            filter: brightness(2);
        }

        #vGamepad.connected {
            transform: rotate(180deg);
        }

        .hint {
            color: white;
            font-size: 5vw;
            position: fixed;
            left: 50vh;
            transform: translateX(-50%);
            top: 25%;
            transition: opacity .3s ease-in, .3s z-index .3s;
            opacity: 1;
            z-index: 0;
            width: 35%;
            text-align: center;
        }

            .hint.hide {
                opacity: 0;
                z-index: -1;
            }

        .conversation {
            width: 100%;
            height: 100%;
            background: #fff;
            position: absolute;
            left: 50%;
            transform: translate(-50%,-150%);
            border-radius: 1.5%;
            opacity: 0;
            transition: opacity .3s ease-in, transform .3s ease-in;
            text-align: center;
            overflow: hidden;
            border-radius: 6vw;
            display: flex;
            flex-direction: column;
        }

            .conversation.active {
                opacity: 1;
                transform: translate(-50%,0);
            }

            .conversation > .cv-title {
                display: flex;
                justify-content: center;
                align-items: center;
                box-sizing: border-box;
                background: #fecd06;
            }

            .conversation > .cv-content {
                padding: 2%;
                display: flex;
                justify-content: center;
                align-items: center;
            }

            .conversation > .cv-control {
                display: flex;
                justify-content: center;
                align-items: center;
                box-sizing: border-box;
            }

                .conversation > .cv-control > button {
                    margin: 0 10%;
                    height: 60%;
                    width: 20%;
                    text-indent: 8px;
                    letter-spacing: 8px;
                    text-align: center;
                    border: none;
                    padding: 0%;
                    border-radius: 100vw;
                    outline: none;
                }

            .conversation.conversation-1 {
                border-radius: 6vw;
                width: 50%;
                height: 55%;
                top: 10%;
                opacity: .95;
            }

                .conversation.conversation-1 > .cv-title {
                    flex: 3;
                }

                .conversation.conversation-1 > .cv-content {
                    flex: 5;
                }

                .conversation.conversation-1 > .cv-control {
                    flex: 3;
                }

            .conversation.conversation-2 {
                border-radius: 0;
                z-index: 0;
                height: 0;
                width: 100vw;
                position: fixed;
                top: 0;
            }

                .conversation.conversation-2.active {
                    z-index: 1;
                    height: 100vh;
                }

                .conversation.conversation-2 > .cv-title {
                    flex: 0;
                }

                .conversation.conversation-2 > .cv-content {
                    flex: 5;
                    display: flex;
                    flex-direction: column;
                    font-weight: 600;
                    letter-spacing: .3vw;
                }

                .conversation.conversation-2 > .cv-control {
                    flex: 4;
                    justify-content: start;
                    display: flex;
                    flex-direction: column;
                }

                    .conversation.conversation-2 > .cv-control > button {
                        width: 35%;
                        height: 12%;
                        border-radius: 100vh;
                        border: none;
                        outline: none;
                        font-weight: 600;
                    }


        @@media screen and (orientation: portrait) {
        }
    </style>
</head>
<body>
    <div style="position: relative; width: 100vh; height: 100vw;overflow: hidden;" id="videoContainer">
        <div>
            <video id="remoteVideo" autoplay muted style="width: 100%; height: 100%; background-color: #000; object-fit: fill; opacity: 0;"
                   x5-video-player-type="h5"
                   x5-video-player-fullscreen="true"
                   x5-video-orientation="h5"
                   ontouchstart="return false;" />
        </div>
        <div style="display: block; position: absolute; left: 0; bottom: 0; height: 100%; width: 100%;">
            <div id="vGamepad" style="display: block; position: absolute; left: 0; bottom: 0; height: 100%; width: 100%;">
                <div style="position: absolute; left: 3vw; bottom: 25vw; width: 39vw; display: flex; align-items: center; justify-content: center; height: 39vw;transform: rotate(90deg)">
                    <img src="https://cos.steamcloud.co/web/shoubing2/joystick.png" style="width: 100%; position: absolute; bottom: 0;" />
                    @*<img src="https://cos.steamcloud.co/web/shoubing2/Joystick_active.png" style="width: 100%; position: absolute; bottom: 0;display:none;" />*@
                    <img id="joystick" src="https://cos.steamcloud.co/web/shoubing2/joystick_round.png" style="position: absolute; width: 18.5vw;" />
                </div>
                <div style="position: absolute;right: 52vw;bottom: 3vw;width: 39vw;display: flex;align-items: center;justify-content: center;height: 39vw;transform: rotate(90deg);">
                    <img src="https://cos.steamcloud.co/web/shoubing2/joystick.png" style="width: 100%; position: absolute; bottom: 0;">
                    <img id="joystick2" src="https://cos.steamcloud.co/web/shoubing2/joystick_round.png" style="position: absolute;width: 18.5vw;transform: translate(0, 0);">
                </div>
                <img src="https://cos.steamcloud.co/web/shoubing2/a.png" key="0" style="width: 20vw;position: absolute;right: 15vw;bottom: 10vw;">
                <img src="https://cos.steamcloud.co/web/shoubing2/x.png" key="2" style="width: 20vw;position: absolute;right: 30vw;bottom: 25vw;">
                <img src="https://cos.steamcloud.co/web/shoubing2/b.png" key="1" style="width: 20vw; position: absolute; right: 0vw; bottom: 25vw;">
                <img src="https://cos.steamcloud.co/web/shoubing2/y.png" key="3" style="width: 20vw;position: absolute;right: 15vw;bottom: 40vw;">
                <img src="https://cos.steamcloud.co/web/shoubing2/lb.png" key="4" style="width: 20vw; position: absolute; left: 22vh; top: 12vw;">
                <img src="https://cos.steamcloud.co/web/shoubing2/lt.png" key="6" style="width: 20vw; position: absolute; left: 10vh; top: 0;">
                <img src="https://cos.steamcloud.co/web/shoubing2/rb.png" key="5" style="width: 20vw; position: absolute; right: 22vh; top: 10vw;">
                <img src="https://cos.steamcloud.co/web/shoubing2/rt.png" key="7" style="width: 20vw; position: absolute; right: 10vh; top: 0;">
                <img src="https://cos.steamcloud.co/web/shoubing2/back.png" key="8" style="width: 22vw; position: absolute; left: calc(50vh - 33vw); top: 0;">
                <img src="https://cos.steamcloud.co/web/shoubing2/start.png" key="9" style="width: 22vw; position: absolute; right: calc(50vh - 33vw); top: 0;">
                <img src="https://cos.steamcloud.co/web/shoubing2/ls.png" key="10" style="width: 20vw; position: absolute; left: 0; top: 15vw;">
                <img src="https://cos.steamcloud.co/web/shoubing2/rs.png" key="11" style="width: 20vw; position: absolute; right: 0; top: 15vw;">
                <div src="https://cos.steamcloud.co/web/shoubing2/bg.png" style="width: 35.5vw;position: absolute;left: 32.5vw;bottom: 3vw;height: 35.5vw;background-image: url(/images/shoubing2/bg.png);background-size: 100%;"></div>
                <img src="https://cos.steamcloud.co/web/shoubing2/shang.png" key="12" style="width: 11vw;position: absolute;left: 44.8vw;bottom: 21.5vw;">
                <img src="https://cos.steamcloud.co/web/shoubing2/xia.png" key="13" style="bottom: 3vw;width: 11vw;position: absolute;left: 44.8vw;">
                <img src="https://cos.steamcloud.co/web/shoubing2/zuo.png" key="14" style="width: 18vw;position: absolute;bottom: 14.5vw;left: 32.5vw;">
                <img src="https://cos.steamcloud.co/web/shoubing2/you.png" key="15" style="width: 18vw;position: relative;left: 53vw;left: 50vw;bottom: -73.5vw;">
                <div style="width: 14vw;left: 43.5vw;bottom: 14vw;height: 14vw;display: inline-block;position: absolute;">
                </div>
                <div class="hint">
                    @*长按遥控器上方向键<br />打开设置菜单*@
                </div>
                <div class="conversation conversation-1">
                    <div class="cv-title">
                        <b>确定关闭虚拟手柄连接吗?</b>
                    </div>
                    <div class="cv-content">断开虚拟手柄后请使用实体手柄操作</div>
                    <div class="cv-control">
                        <button onclick="cvsActive(1)">取消</button>
                        <button onclick="exit()">确定</button>
                    </div>
                </div>
                <div class="conversation conversation-2 active">
                    <div class="cv-title">
                    </div>
                    <div class="cv-content">
                        <img src="https://cos.steamcloud.co/web/shoubing2/tip.png" style="width: 60%; margin-bottom: 15%;" /><br />
                        <div>是否使用手机虚拟手柄操控?</div>
                    </div>
                    <div class="cv-control">
                        <button onclick="start()" style="background: #fed229;margin-bottom: 8%;">确定</button>
                        <button onclick="quitHandler()" style="border: 1px solid #fed229; box-sizing: border-box;">取消</button>
                    </div>
                </div>
            </div>
        </div>
    </div>
    <script type="text/javascript" src="https://cos.steamcloud.co/web/js/jquery.min.js"></script>
    <script type="text/javascript" src="https://cos.steamcloud.co/web/js/jysdk-cloudgame-h5.min.js?v=1.2.6"></script>
    <script type="text/javascript" src="~/js/common.js"></script>

    <script type="text/javascript" src="~/js/JYConnection.js"></script>
    <script>
            //全局变量
            var touchInfo = [0, 0];//左右摇杆时间戳
            var jsX = 0;//摇杆中心坐标
            var jsY = 0;
            var jsX2 = 0;
            var jsY2 = 0;
            var keyMap = {
                87: 12,
                65: 14,
                83: 13,
                68: 15,
                85: 0,
                73: 1,
                74: 2,
                75: 3,
                32: 9,
                8: 8
            };
            var userId =@Model.UserId;
            var gameId ="@(Model.GameId)";
            var bossId ="";
            var vgpid = null; //虚拟手柄ID
            var gamepad = getNullGamepad();
            var isPlaying = false;
            var $jy = new JYConnection();
            var tid = null;
            var handlerData ="";
            $jy.bsUrl = '@(Model.AppConfig?.GameConfig?.BsUrl ?? "")/';
            $jy.channelId ='@(Model.AppConfig?.GameConfig?.ChannelId ?? "")';
            $jy.token = '';
            $jy.ip = '@(Model.IP)';
            $jy.sn ='@(Model.Sn)';
            $jy.gameId = gameId;
            $jy.clientSid = userId;

            //设置参数
            function initGame() {
                //设置回调
                $jy.setCallback(function(json) {
                    switch (json.code) {
                    case "51000":
                        $("#progress-bar").css("width", "30%");
                        break;
                    case "52000":
                        $("#progress-bar").css("width", "50%");
                        break;
                    case "54000":
                        $("#progress-bar").css("width", "80%");
                        break;
                    case "53100":
                        break;
                    case "51102": //enqueue
                        isPlaying = false;
                        break;
                    case "55000": //operation event
                        return;
                    case "50000":
                        if (!vgpid) {
                            vgpid = $jy.addVirtualGamepad('self-defined Gamepad');
                        }
                        break;
                    default:
                        break;
                    }
                    showInfo(JSON.stringify(json));
                });
                $jy.initSdk();
            }
            function taskBtn(active) {
                if (active == undefined) {
                    active = 'keydown';
                }
                var ctrl = {
                    altKey: true,
                    shiftKey: true,
                    keyCode: 17,
                    code: 'KeyCtrl'
                };
                var alt = {
                    altKey: true,
                    shiftKey: true,
                    keyCode: 18,
                    code: 'KeyAlt'
                };
                var del = {
                    altKey: true,
                    shiftKey: true,
                    keyCode: 46,
                    code: 'KeyDel'
                };
                $jy.sendKeyMouseStatus('KeyboardEvent', new KeyboardEvent(active, ctrl));
                $jy.sendKeyMouseStatus('KeyboardEvent', new KeyboardEvent(active, alt));
                $jy.sendKeyMouseStatus('KeyboardEvent', new KeyboardEvent(active, del));
                if (active == 'keydown') {
                    setTimeout('taskBtn("keyup");', 5);
                }
            }
            function winBtn(active) {
                if (active == undefined) {
                    active = 'keydown';
                }
                var win = {
                    altKey: true,
                    shiftKey: true,
                    keyCode: 91,
                    code: 'KeyWin'
                };
                var l = {
                    altKey: true,
                    shiftKey: true,
                    keyCode: 76,
                    code: 'KeyL'
                };
                if (active == 'keydown') {
                    $jy.sendKeyMouseStatus('KeyboardEvent', new KeyboardEvent(active, win));
                    $jy.sendKeyMouseStatus('KeyboardEvent', new KeyboardEvent(active, l));
                } else {
                    $jy.sendKeyMouseStatus('KeyboardEvent', new KeyboardEvent(active, l));
                    $jy.sendKeyMouseStatus('KeyboardEvent', new KeyboardEvent(active, win));
                }
                if (active == 'keydown') {
                    setTimeout('winBtn("keyup");', 5);
                }
            }
            function switchVGamepad() {
                $("#vGamepad").toggle();
            }
            function switchVKeyboard() {
                if ($('#vkeyboard').prop('checked')) {
                    document.onkeydown = keyDown;
                    document.onkeyup = keyUp;
                    $('#vKeyMap').show();
                } else {
                    document.onkeydown = '';
                    document.onkeyup = '';
                    $('#vKeyMap').hide();
                }
            }
            function mouseDown(event) {
                touchInfo[1] = event.timeStamp
                showInfo('Button down:' + event);
                var ele = event.srcElement ? event.srcElement : event.target;
                $(ele).addClass("active");
                var buttonKey = parseInt($(ele).attr('key'));
                if ("vibrate" in navigator)
                    window.navigator.vibrate(100); //震动
                if (buttonKey >= 0 && buttonKey < 17) {
                    gamepad.buttons[buttonKey].pressed = 1;
                    gamepad.buttons[buttonKey].value = 1;
                    $jy.sendGamepadStatus(vgpid, gamepad);
                }
                return false;
            }
            function mouseUp(event) {
                showInfo('Button down:' + event);
                var ele = event.srcElement ? event.srcElement : event.target;
                $(ele).removeClass("active");
                var buttonKey = parseInt($(ele).attr('key'));
                if (buttonKey >= 0 && buttonKey < 17) {
                    gamepad.buttons[buttonKey].pressed = 0;
                    gamepad.buttons[buttonKey].value = 0;
                    $jy.sendGamepadStatus(vgpid, gamepad);
                }
                return false;
            }
            function keyDown(event) {
                var keyCode = event.keyCode;
                showInfo('key down:' + keyCode);
                if (!isPlaying) {
                    return;
                }
                if (keyCode in keyMap) {
                    gamepad.buttons[keyMap[keyCode]].pressed = 1;
                    gamepad.buttons[keyMap[keyCode]].value = 1;
                    $jy.sendGamepadStatus(vgpid, gamepad);
                }
                return false;
            }
            function keyUp(event) {
                var keyCode = event.keyCode;
                showInfo('key up:' + keyCode);
                if (!isPlaying) {
                    return;
                }
                if (keyCode in keyMap) {
                    gamepad.buttons[keyMap[keyCode]].pressed = 0;
                    gamepad.buttons[keyMap[keyCode]].value = 0;
                    $jy.sendGamepadStatus(vgpid, gamepad);
                }
                return false;
                }
            function joystickStart(event) {
                if (event.currentTarget.id !== "joystick")
                    return false;
                if ("vibrate" in navigator)
                    window.navigator.vibrate(100); //震动
                touchInfo[0] = event.timeStamp;
                jsX = $(event.currentTarget).offset().left + joystick.clientWidth * .5;
                jsY = $(event.currentTarget).offset().top + joystick.clientWidth * .5;
                joystickMove(event);
                return false;
            }
            function joystickMove(event) {
                if (event.target.id !== "joystick")
                    return false;

                //摇杆底盘大小
                var jsSize = event.target.parentNode.clientWidth - joystick.clientWidth;
                //光标触点坐标
                var touchIndex = touchInfo[0] > touchInfo[1] ? 1 : 0;
                touchIndex = event.originalEvent.touches.length === 1 ? 0 : touchIndex;
                var msX = event.originalEvent.touches[touchIndex].pageX;
                var msY = event.originalEvent.touches[touchIndex].pageY;

                var translateX = (msX - jsX);
                var translateY = (msY - jsY);
                translateX = Math.abs(translateX) > jsSize ? (translateX > 0 ? jsSize : -jsSize) : translateX;
                translateY = Math.abs(translateY) > jsSize ? (translateY > 0 ? jsSize : -jsSize) : translateY;
                if (Math.abs(translateX) > jsSize * .7 && Math.abs(translateY)>jsSize*.7) {
                    translateX = (translateX > 0 ? 1 : -1) * jsSize * .7;
                    translateY = (translateY > 0 ? 1 : -1) * jsSize * .7;
                }
                $(event.target).css("transform", "translate(" + (translateX) + "px," + (translateY) + "px)");
                try {
                    gamepad.axes[0] = (-translateY / jsSize).toFixed(2);
                    gamepad.axes[1] = (translateX / jsSize).toFixed(2);
                    //斜角时
                    if (Math.abs(Math.abs(gamepad.axes[0]) - Math.abs(gamepad.axes[1]))<=.1) {
                        gamepad.axes[0] = Number(gamepad.axes[0]) + (gamepad.axes[0] > 0 ? 1 : -1) * Math.abs(gamepad.axes[0]) / .7 * .3;
                        gamepad.axes[1] = Number(gamepad.axes[1]) + (gamepad.axes[1] > 0 ? 1 : -1) * Math.abs(gamepad.axes[1]) / .7 * .3;
                    }
                    $jy.sendGamepadStatus(vgpid, gamepad);
                } catch (e) {

                }
                showInfo(`
        jsX:${jsX}
        jsY:${jsY}
        msX:${msX}
        msY:${msY}
        translateX:${translateX}
        translateY:${translateY}
        gamepad.axes:${gamepad.axes}
        `);
                return false;
            }
            function joystickEnd(event) {
                if (event.target.id !== "joystick")
                    return false;
                $(event.target).css("transform", "translate(0,0)");
                //else if (event.target.id === "joystick2") {
                //    gamepad.axes[2] = 0;
                //    gamepad.axes[3] = 0;
                //}
                try {
                    gamepad.axes[0] = 0;
                    gamepad.axes[1] = 0;
                    $jy.sendGamepadStatus(vgpid, gamepad);
                } catch (e) {

                }
                return false;
            }
            function joystickStart2(event) {
                if (event.currentTarget.id !== "joystick2")
                    return false;
                if ("vibrate" in navigator)
                    window.navigator.vibrate(100); //震动
                touchInfo[1] = event.timeStamp;
                jsX2 = $(event.currentTarget).offset().left + joystick2.clientWidth * .5;
                jsY2 = $(event.currentTarget).offset().top + joystick2.clientWidth * .5;
                joystickMove2(event);
                return false;
            }
            function joystickMove2(event) {
                if (event.target.id !== "joystick2")
                    return false;
                //摇杆底盘大小
                var jsSize = event.target.parentNode.clientWidth - joystick2.clientWidth;
                //光标触点坐标
                var touchIndex = touchInfo[0] > touchInfo[1] ? 0 : 1;
                touchIndex = event.originalEvent.touches.length === 1 ? 0 : touchIndex;
                var msX = event.originalEvent.touches[touchIndex].pageX;
                var msY = event.originalEvent.touches[touchIndex].pageY;

                var translateX = (msX - jsX2);
                var translateY = (msY - jsY2);
                translateX = Math.abs(translateX) > jsSize ? (translateX > 0 ? jsSize : -jsSize) : translateX;
                translateY = Math.abs(translateY) > jsSize ? (translateY > 0 ? jsSize : -jsSize) : translateY;
                if (Math.abs(translateX) > jsSize * .7 && Math.abs(translateY) > jsSize * .7) {
                    translateX = (translateX > 0 ? 1 : -1) * jsSize * .7;
                    translateY = (translateY > 0 ? 1 : -1) * jsSize * .7;
                }
                $(event.target).css("transform", "translate(" + (translateX) + "px," + (translateY) + "px)");
                try {
                    gamepad.axes[2] = (-translateY / jsSize).toFixed(2);
                    gamepad.axes[3] = (translateX / jsSize).toFixed(2);
                    //斜角时
                    if (Math.abs(Math.abs(gamepad.axes[2]) - Math.abs(gamepad.axes[3])) <= .1) {
                        gamepad.axes[2] = Number(gamepad.axes[2]) + (gamepad.axes[2] > 0 ? 1 : -1) * Math.abs(gamepad.axes[2]) / .7 * .3;
                        gamepad.axes[3] = Number(gamepad.axes[3]) + (gamepad.axes[3] > 0 ? 1 : -1) * Math.abs(gamepad.axes[3]) / .7 * .3;
                    }
                    $jy.sendGamepadStatus(vgpid, gamepad);
                } catch (e) {

                }
                console.log(`
        jsX:${jsX}
        jsY:${jsY}
        msX:${msX}
        msY:${msY}
        translateX:${translateX}
        translateY:${translateY}
        gamepad.axes:${gamepad.axes}
        `);
                return false;
            }
            function joystickEnd2(event) {
                if (event.target.id !== "joystick2")
                    return false;
                $(event.target).css("transform", "translate(0,0)");
                try {
                    gamepad.axes[2] = 0;
                    gamepad.axes[3] = 0;
                    $jy.sendGamepadStatus(vgpid, gamepad);
                } catch (e) {

                }
                return false;
            }
            function showInfo(msg) {
                console.log(msg);
            }
            function cvsActive(index) {
                if (index===undefined) {
                    $(".conversation").toggleClass("active");
                } else {
                    $(".conversation-"+index).toggleClass("active");
                }
            }
            function exit() {
                if ($jy.jy.core_){
                    $jy.stop();
                    //异步微信开发工具正常，同步手机正常，都什么呀
                    $.ajaxSettings.async = false;
                    $.post("DisconnectHandler",
                        {
                            userId: userId,
                            bossId: bossId,
                            gameId: gameId,
                        },
                        function (res) {
                            console.log(JSON.stringify(res));
                            quitHandler();
                        });

                } else {
                    quitHandler();
                }
            }
            function quitHandler() {
                try {
                    WeixinJSBridge.call('closeWindow');
                } catch (e) {

                }

                //window.close();
            }
            async  function  start() {
                try {
                    WeixinJSBridge.call('hideOptionMenu'); //隐藏右上角三个点按钮。
                    WeixinJSBridge.call('hideAllNonBaseMenuItem'); //隐藏所有非基础按钮接口。
                } catch (e) {

                }
                alert("使用前请将手机横向放置,并关闭系统内的「屏幕旋转」.");
                cvsActive(2);
                $("body").addClass("connected");
                $("#vGamepad").addClass("connected");
                $("#vGamepad > img").on("mousedown", mouseDown);
                $("#vGamepad > img").on("mouseup", mouseUp);
                $("#vGamepad > img").on("touchstart", mouseDown);
                $("#vGamepad > img").on("touchend", mouseUp);
                $("#joystick").on("touchstart", joystickStart);
                $("#joystick").on("touchmove", joystickMove);
                $("#joystick").on("touchend", joystickEnd);
                $("#joystick2").on("touchstart", joystickStart2);
                $("#joystick2").on("touchmove", joystickMove2);
                $("#joystick2").on("touchend", joystickEnd2);
                //配置（客户端）
                var token=  await $.get("@(Model.AppConfig?.Site?.Api ?? "")/api/PlayGame/GetToken?userId=@(Model.UserId)&sn=@(Model.Sn)");
                console.log("tokentokentokentokentokentoken", token.data);
                if(token==null||token.data==null||token.data.token==null){
                    alert("初始化失败！请刷新页面重试！！！");
                return ;
                }
                $jy.token = token.data.token;
                initGame();
                var handlerData=null;
                  var handler=  await $.get("@(Model.AppConfig?.Site?.Api ?? "")/api/PlayGame/CreateSimpleHandler?userId=@(Model.UserId)&gameId=@(Model.GameId)&sn=@(Model.Sn)");
                  if(handler==null||handler.data==null){
                        alert("创建手柄失败！请刷新页面重试！！！");
                    return ;
                  }
                  handlerData=handler.data;
                setTimeout(function() {
                    try {

                        //实例化
                        $jy.simpleHandler(handlerData);
                        //检查手柄状态
                        setInterval(function() {
                            // $.post("CheckHandlerState",
                            //     {
                            //         userId: userId,
                            //         bossId: bossId,
                            //         gameId: gameId,
                            //         isOneself: true,
                            //     }, function (res) {
                            //         游戏已结束
                            //         if (res.Code === 102) {
                            //             alert("游戏已结束,手柄断开连接.");
                            //             exit();
                            //             quitHandler();
                            //         }
                            //     });
                        },3000);
                    } catch (e) {
                            alert("发生未知错误!");
                            quitHandler();
                        }
                    },
                    500);

                //setTimeout(function () {
                //    $(".hint").addClass("hide");
                //}, 3000);
            }
            document.body.addEventListener('mousedown',
                function() {

                },
                false);

            $(function() {
                // if (!handlerData || JSON.parse(handlerData).status === 422) {
                //     alert("未找到正在运行的游戏!\r" + (!handlerData?"数据丢失!":handlerData));
                //     setTimeout(quitHandler(),500);
                // }

                $("body").on("touchstart", function (event) { if (event.currentTarget.tagName === "BODY") return false; });
                window.onbeforeunload = function (event) {
                    exit();   //浏览器窗体关闭之前调用关闭鲸云sdk
                    // return confirm("确定离开此页面吗？");
                }
                window.addEventListener('pagehide', () => {
                    exit();   //浏览器窗体关闭之前调用关闭鲸云sdk
                    // return confirm("确定离开此页面吗？");
                })

            });

    </script>
</body>
</html>
