Parcourir la source

【更新】:
1.浏览器适配;
2.修改数据上报接口;
3.增加游戏计时;

youhaitao il y a 2 ans
Parent
commit
cf04b1b9e6

+ 71 - 63
assets/main/SceneMain.ts

@@ -8,8 +8,8 @@ import GameStart, { PropsStart } from "../res/luojigouStart/src/LuojigouStart";
 import GameFinish, { PropsFinish } from "../res/luojigouFinish/src/LuojigouFinish";
 import AudioCB from "../src/audioUitls/AudioCB";
 import Tools from "../src/common/Tools";
-import PopupManager, { PopupCacheMode } from "../src/popUp/PopupManager";
 import AudioManager from "../src/audioUitls/AudioManager";
+import PopupManager, { PopupCacheMode } from "../src/popUp/PopupManager";
 
 const { ccclass, property } = cc._decorator;
 @ccclass
@@ -44,12 +44,19 @@ export default class Scene extends cc.Component {
         prefabs: cc.Prefab | any,
     } = { bundles: {}, prefabs: {} };
 
+    protected onLoad(): void {
+        // 设置游戏窗口变化的回调(仅 Web 平台有效)
+        cc.view.setResizeCallback(() => this.onResize());
+    }
+
     /** 注册监听事件 */
     protected onEnable(): void {
         NotifierCenter.listen(CConst.EVENT_LJG_START, this.showLgjStart, this, false);
         NotifierCenter.listen(CConst.EVENT_LJG_FINISH, this.showLgjFinish, this, false);
         NotifierCenter.listen(CConst.EVENT_ENTER_UNIT, this.msgResultEnterUnit, this, false);
         NotifierCenter.listen(CConst.EVENT_ENTER_GAME, this.msgResultEnterGame, this, false);
+
+        this.adapt();
     }
 
     /** 取消监听事件 */
@@ -60,47 +67,16 @@ export default class Scene extends cc.Component {
     /** 游戏开始 */
     async start() {
         common.hideLoading();
-        let result = common.isDebug ? true : await this.checkUser();
+        let result = common.isDebug ? true : await common.httpCheckToken();
         if (result) {
             this.enterGame();
         }
         else {
-            // const options = {
-            //     title: '',
-            //     content: '请下载应用!'
-            // };
-            // const params = {
-            //     mode: PopupCacheMode.Once,
-            // };
-            // let prefab = await PopupManager.load('prefab/popUp');
-            // PopupManager.show(cc.instantiate(prefab), options, params);
             window.location.href = common.urlClass;
         }
         return;
     }
 
-    /** 用户检测 */
-    async checkUser(): Promise<boolean> {
-        // 验证token
-        let token = Tools.getLocalStorage('token', null);
-        return new Promise((resolve) => {
-            let xhr = new XMLHttpRequest();
-            xhr.open("GET", common.urlToken, true);
-            xhr.setRequestHeader('token', token);
-            xhr.onreadystatechange = function () {
-                if (xhr.readyState === 4 && (xhr.status >= 200 && xhr.status < 300)) {
-                    common.log(xhr.responseText);
-                    let json = JSON.parse(xhr.responseText);
-                    resolve(json.status == 200);
-                }
-            };
-            xhr.onerror = function () {
-                resolve(false);
-            }
-            xhr.send();
-        });
-    }
-
     /** 进入游戏 */
     async enterGame() {
         // 获取网络地址信息
@@ -123,16 +99,15 @@ export default class Scene extends cc.Component {
             }
         }
         else {
-            // const options = {
-            //     title: '',
-            //     content: '当前动画游戏不存在!',
-            // };
-            // const params = {
-            //     mode: PopupCacheMode.Once,
-            // };
-            // let prefab = await PopupManager.load('prefab/popUp');
-            // PopupManager.show(cc.instantiate(prefab), options, params);
-            window.location.href = common.urlClass;
+            const options = {
+                title: '',
+                content: '当前动画游戏不存在!',
+            };
+            const params = {
+                mode: PopupCacheMode.Once,
+            };
+            let prefab = await PopupManager.load('prefab/popUp');
+            PopupManager.show(cc.instantiate(prefab), options, params);
         }
     };
 
@@ -235,7 +210,7 @@ export default class Scene extends cc.Component {
         if (common.isDebug) {
             this.btnNext.active = true;
         }
-        else{
+        else {
             this.btnNext.active = false;// 显示下一步按钮
             if (!show) return;
             let gameOne = common.getGameFormLocal();
@@ -283,30 +258,14 @@ export default class Scene extends cc.Component {
         let curPage = common.getPageNum();
         let attribute: AttributeUnit = common.attributeMap[curUint];
         let length = attribute.config.length;
-        common.log('curPage: ', curPage + 1, '; length: ', length);
         if (curPage < length) {
             this.addPrefab(curUint, curPage + 1);
         }
-        else{
-            let funcSB = (req, right)=>{
-                common.log('上报 ', right, ' : ', JSON.stringify(req));
+        else {
+            this.scheduleOnce(()=>{
                 // 返回课程详情页
                 window.location.href = common.urlClass;
-            };
-            let token = Tools.getLocalStorage('token', null);
-            let xhr = new XMLHttpRequest();
-            xhr.open("GET", common.urlOver + common.getItemId(), true);
-            xhr.setRequestHeader('token', token);
-            xhr.onreadystatechange = function () {
-                if (xhr.readyState === 4 && (xhr.status >= 200 && xhr.status < 300)) {
-                    funcSB(xhr, true);
-                }
-            };
-            xhr.onerror = function () {
-                funcSB(xhr, false);
-            }
-            xhr.send();
-            
+            }, 0.5);
         }
     }
 
@@ -324,6 +283,55 @@ export default class Scene extends cc.Component {
         return script;
     };
 
+    /**
+     * 窗口变化回调
+     */
+    protected onResize() {
+        // 适配
+        this.adapt();
+    }
+
+    /**
+    * 适配
+    */
+    protected adapt() {
+        // 实际屏幕比例
+        const winSize = cc.winSize,
+            screenRatio = winSize.width / winSize.height;
+        // 设计比例
+        const designResolution = cc.Canvas.instance.designResolution,
+            designRatio = designResolution.width / designResolution.height;
+
+        // 对比判断 
+        common.log('win ratio: ', screenRatio, '; w: ', winSize.width, '; h: ', winSize.height);
+        common.log('des ratio: ', designRatio, '; w: ', designResolution.width, '; h: ', designResolution.height);
+        if (screenRatio < designRatio) {
+            common.log("适配宽  屏幕宽高比 小于 设计比例");
+            this.setFitWidth();
+        } else {
+            common.log("适配高  屏幕宽高比 大于等于 设计比例");
+            this.setFitHeight();
+        }
+    }
+
+    /**
+     * 适配高度模式
+     */
+    protected setFitHeight() {
+        const canvas = cc.Canvas.instance;
+        canvas.fitHeight = true;
+        canvas.fitWidth = false;
+    }
+
+    /**
+     * 适配宽度模式
+     */
+    protected setFitWidth() {
+        const canvas = cc.Canvas.instance;
+        canvas.fitHeight = false;
+        canvas.fitWidth = true;
+    }
+
     /** 下载游戏zip */
     downLoadZip(zipName) {
         window['luojigou']['downloadAssets'](zipName);

+ 1 - 0
assets/res/luojigouFinish/src/LuojigouFinish.ts

@@ -32,6 +32,7 @@ export default class LuojigouFinish extends cc.Component {
      * @description 星星数
      */
     setAnimation(props: PropsFinish): void {
+        common.httpReportdata();
         common.setGameToLocal();
         this.node.active = true;
         const spine = this.SuccessSpine;

+ 2 - 6
assets/res/luojigouStart/src/LuojigouStart.ts

@@ -2,6 +2,7 @@ import Tools from "../../../src/common/Tools";
 import CConst from "../../../src/common/CConst";
 import NotifierCenter from "../../../src/webtcp/NotifierCenter";
 import AudioManager from "../../../src/audioUitls/AudioManager";
+import common from "../../../src/common/common";
 
 /**
  * @description 逻辑狗card的脚本
@@ -40,6 +41,7 @@ export default class LuojigouStart extends cc.Component {
      * @description 设置逻辑狗卡片
      */
     setLuojigouCard(props: PropsStart) {
+        common.setTimeStart();
         this.node.active = true;
         for (let i = 0; i < 4; i++) {
             this.StarList[i].active = i < props.star;
@@ -47,12 +49,7 @@ export default class LuojigouStart extends cc.Component {
         this.ChallengeType.getComponent(cc.Label).string = props.type;
         this.ChallengeTitle.getComponent(cc.Label).string = props.title;
 
-        // if (props.audios && props.audios.length > 0) {
-        //     Tools.playEffectArr(props.audios, this.closeLJGCard.bind(this));
-        // }
-        // else {
         this.scheduleOnce(this.closeLJGCard.bind(this), 4);
-        // }
     }
 
     eventClose() {
@@ -64,7 +61,6 @@ export default class LuojigouStart extends cc.Component {
      * @description 关闭逻辑狗弹窗
      */
     closeLJGCard() {
-        // Tools.stopEffects();
         this.node.active = false;
         this.unscheduleAllCallbacks();
         NotifierCenter.trigger(CConst.EVENT_GAME_START);

+ 2 - 0
assets/res/videoPlayer/src/VideoPlayer.ts

@@ -38,6 +38,7 @@ export default class VideoPlayer extends GameBase {
         }
         if (this.currentTime > 0 && this.video.isPlaying()) {
             this.nodeStart.active = false;
+            common.setTimeStart();
             common.log('视频 播放');
         }
     }
@@ -69,6 +70,7 @@ export default class VideoPlayer extends GameBase {
         }
         else if (event === cc.VideoPlayer.EventType.COMPLETED) {
             common.setGameToLocal();
+            common.httpReportdata();
             NotifierCenter.trigger(CConst.EVENT_ENTER_GAME, {});
         }
         else if (event === cc.VideoPlayer.EventType.CLICKED) {

+ 63 - 4
assets/src/common/common.ts

@@ -37,7 +37,7 @@ class common {
         return this._instance;
     };
 
-    isDebug: boolean = true;
+    isDebug: boolean = false;
     project: string = "luojigou_yinliu";
     itemId: string = '';
     unitCur: number = 1;// 第几集,从1开始
@@ -45,15 +45,24 @@ class common {
     isCanPlayVideo = false;
     attributeMap: any = {};// 内部形式为 { "0" : AttributeUtil } 
     urlToken: string = 'https://open.api.luojigou.vip/mall/mobile/common/verify/token';// 验证token的地址;
-    urlOver: string = 'https://open.api.luojigou.vip/app/app/gameCourse/record/';// 当前集游戏完结,上报地址;
+    urlOver: string = 'https://open.api.luojigou.vip/app/app/gameCourse/record';// 当前集游戏完结,上报地址;
     urlClass: string = 'https://luojigou.vip/ac/#/courseDetail?id=1568062284888866817&mode=luojigou';// 课程页
     objRes: any = {};//通用音频
+    timeStart: number = 0;
 
-    getItemId(){
+    getTimeStart() {
+        return this.timeStart;
+    };
+
+    setTimeStart() {
+        this.timeStart = new Date().getTime();
+    };
+    
+    getItemId() {
         return this.itemId;
     };
 
-    setItemId(id){
+    setItemId(id) {
         this.itemId = id;
     };
 
@@ -122,6 +131,56 @@ class common {
         Tools.setLocalStorage(this.project, JSON.stringify(objLocal));
     };
 
+    /** token验证 */
+    httpCheckToken(): Promise<boolean> {
+        return new Promise((resolve) => {
+            let xhr = new XMLHttpRequest();
+            xhr.open("GET", this.urlToken, true);
+            xhr.setRequestHeader('token', Tools.getLocalStorage('token', null));
+            xhr.onreadystatechange = function () {
+                if (xhr.readyState === 4 && (xhr.status >= 200 && xhr.status < 300)) {
+                    let json = JSON.parse(xhr.responseText);
+                    resolve(json.status == 200);
+                }
+            };
+            xhr.onerror = function () {
+                resolve(false);
+            }
+            xhr.send();
+        });
+    }
+
+    /** 数据上报 */
+    httpReportdata(): Promise<boolean> {
+        let util = this.getUnitNum();
+        let page = this.getPageNum();
+        let attribute: AttributeUnit = this.attributeMap[util];
+        let length = attribute.config.length;
+        let isFinish = page < length ? 0 : 1;
+        let timeStart = this.getTimeStart();
+        let playTime = Math.ceil((new Date().getTime() - timeStart)*0.001);
+        // 组合地址
+        let url = this.urlOver;
+        url += '/' + this.getItemId();
+        url += '/' + playTime;
+        url += '/' + page;
+        url += '/' + isFinish;
+        return new Promise((resolve) => {
+            let xhr = new XMLHttpRequest();
+            xhr.open("GET", url, true);
+            xhr.setRequestHeader('token', Tools.getLocalStorage('token', null));
+            xhr.onreadystatechange = function () {
+                if (xhr.readyState === 4 && (xhr.status >= 200 && xhr.status < 300)) {
+                    resolve(true);
+                }
+            };
+            xhr.onerror = function () {
+                resolve(false);
+            }
+            xhr.send();
+        });
+    }
+
     /** cocos日志打印 */
     log(...params: any) {
         // if (!CC_DEBUG) return;