Browse Source

1.添加网络端口,
2.修改部分相关js

slambb 3 years ago
parent
commit
e89be6d6ac

+ 43 - 17
assets/Script/Network/dapp.js

@@ -20,8 +20,12 @@ cc.Class({
             //     console.log("finish toast!");
             // },_self);
             try {
-                console.log('开始初始化dapp!')
                 const isTestNet = window['isTestNet'];
+                console.log('-----------开始初始化dapp!-----------');
+                console.log('isTestNet:' + window['isTestNet']);
+                console.log('host:     ' + window['dappHost']);
+                console.log('dappWss:   ' + window['dappWss']);
+                console.log('-----------以上是相关设置-----------');
                 await dapp.init(isTestNet)
                 console.log("用户当前钱包地址:" + dapp.getAddress()) // 初始化成功,得到用户当前钱包地址
                 dapp.onAccountChanged((account) => {
@@ -56,16 +60,16 @@ cc.Class({
                     // }, _self);
 
                     const dappLoginData = await dapp.getLoginSign();
-                    GlobalD.Dapp.signData = JSON.stringify(dappLoginData)
-                    
+                    GlobalD.Dapp.signData = dappLoginData;
+
                     // console.log("dappLoginData:"+JSON.stringify(dappLoginData));
 
-                   let _temp = {
-                        "address": dappLoginData.address, // 签名地址
-                        "id": dappLoginData.id, // 所属账户ID
-                        "signature": dappLoginData.signature, // 签名文本
-                        "timestamp": dappLoginData.timestamp // 签名时间戳,需要在1分钟内完成验签,否则本次签名失效
-                    };
+                    //    let _temp = {
+                    //         "address": dappLoginData.address, // 签名地址
+                    //         "id": dappLoginData.id, // 所属账户ID
+                    //         "signature": dappLoginData.signature, // 签名文本
+                    //         "timestamp": dappLoginData.timestamp // 签名时间戳,需要在1分钟内完成验签,否则本次签名失效
+                    //     };
                     // let _temp = {
                     //     "address": "TDw6xsVnDJWsdRBLkWAwXbv4hE2X2JQs5z", // 签名地址
                     //     "id": 1, // 所属账户ID
@@ -73,15 +77,14 @@ cc.Class({
                     //     "timestamp": 1641279989 // 签名时间戳,需要在1分钟内完成验签,否则本次签名失效
                     // };
 
-                    utils.post(utils.api.loginTokenAndVerification, _temp, (res, value) => {
-                        console.log("loginTokenAndVerification========>" + JSON.stringify(_temp));
+                    utils.post(utils.api.loginTokenAndVerification, dappLoginData, (res, value) => {
+                        // console.log("loginTokenAndVerification========>", _temp);
                         console.log(res, value);
-                    })
-
-                    utils.get(utils.api.loginToken, { loginId: GlobalD.Dapp.UserInfo.id }, (res, value) => {
                         if (value.code == 0) {
                             /** 登录成功获取token */
                             utils.setToken(value.data.token);
+                            /** 登录时候会获取一次游戏端的 userInfo ,后续可以通过userInfo接口获取 */
+                            GlobalD.UserInfo = value.data.userInfo;
                             /** 推送信息 */
                             utils.get(utils.api.playerPullInfo, {}, (res, playerPullInfoTemp) => {
                                 let _playerData = JSON.parse(playerPullInfoTemp.data.playerData);
@@ -91,17 +94,40 @@ cc.Class({
                                 //跳转游戏
                                 cc.find("Canvas").getComponent("Login").loginButton();
 
-                                // GameNet.getInstance().init(GlobalD.Dapp.UserInfo.id, (data) => {
-                                //     console.log('GameNet:' + data);
-                                // }, _self);
                                 utils.init(GlobalD.Dapp.UserInfo.id, (data) => {
                                     console.log('utils ws:' + data);
                                 }, _self);
                             })
+
                         } else {
                             console.log(res, value);
                         }
                     })
+
+                    // utils.get(utils.api.loginToken, { loginId: GlobalD.Dapp.UserInfo.id }, (res, value) => {
+                    //     if (value.code == 0) {
+                    //         /** 登录成功获取token */
+                    //         utils.setToken(value.data.token);
+                    //         /** 推送信息 */
+                    //         utils.get(utils.api.playerPullInfo, {}, (res, playerPullInfoTemp) => {
+                    //             let _playerData = JSON.parse(playerPullInfoTemp.data.playerData);
+                    //             //记录读取的数据到内存里面
+                    //             // 正常游戏
+                    //             userData.readData = _playerData;
+                    //             //跳转游戏
+                    //             cc.find("Canvas").getComponent("Login").loginButton();
+
+                    //             // GameNet.getInstance().init(GlobalD.Dapp.UserInfo.id, (data) => {
+                    //             //     console.log('GameNet:' + data);
+                    //             // }, _self);
+                    //             utils.init(GlobalD.Dapp.UserInfo.id, (data) => {
+                    //                 console.log('utils ws:' + data);
+                    //             }, _self);
+                    //         })
+                    //     } else {
+                    //         console.log(res, value);
+                    //     }
+                    // })
                 } else {
                     //TODO 服务错误
                     console.log("dappInit userInfo 错误");

+ 12 - 10
assets/Script/Network/netUtils.ts

@@ -10,9 +10,9 @@ var utils = {
     version: "0.15",
 
     // baseUrl: "https://www.yuyekeji.cn/game/",
-    baseUrl: "http://127.0.0.1:26001/api_dapp/game/",
+    // baseUrl: "http://127.0.0.1:26001/api_dapp/game/",
     // baseUrl: "https://www.yuyekeji.cn/api_dapp/game/",
-    // baseUrl: window['dappHost']+'/api_dapp/game/',
+    baseUrl: window['dappHost']+'/api_dapp/game/',
     
     api: {
         //获取public 和 系统设置
@@ -30,14 +30,16 @@ var utils = {
         playerPushInfo:'comPlayers/playerPushInfo',
         playerPullInfo:'comPlayers/playerPullInfo',
         
-       
-        //获取宝箱
-        getChest: 'comPlayerGoods/getChest',
-        //开启宝箱,获取一个装备
-        openChest: 'comPlayerGoods/openChest',
-        //穿戴装备
-        wearableEquip: 'comPlayerGoods/wearableEquip',
-
+        //获取游戏config土地列表
+        landConfig:'configLand/getList',
+        //获取用户已经租赁的土地
+        userLandList: 'comPlayerLand/getList',
+        //获取土地信息
+        landState: 'comPlayerLand/getState',
+        //种子
+        mallSeed: 'comMallSeed/getMallSeed',
+        //仓库种子和果实
+        getSeedAndFruit :'comPlayerGoods/getSeedAndFruit', 
     },
     /** 登录获取的token */
     token: null,

+ 192 - 25
assets/Script/public/GameData.js

@@ -45,7 +45,18 @@ cc.Class({
             visible: false,
         },
 
-
+        /**
+         * 接口
+         * todo cnt和snb 由钱包读取
+         */
+        CNT: {
+            default: 0,
+            visible: false,
+        },
+        SNB: {
+            default: 0,
+            visible: false,
+        },
 
     },
     onLoad() {
@@ -76,14 +87,18 @@ cc.Class({
             //读取网络数据
             try {
                 GlobalD.dapp.cntBalance().then((cntBalance) => {
-                    console.log(cntBalance) // string, 精度18,需要自行处理省略几位小数
+                    //会延迟返回
+                    console.log("获取cnt:" + cntBalance) // string, 精度18,需要自行处理省略几位小数
                     GlobalD.GameData.SetCNT(cntBalance);
                 });
             } catch (err) {
                 console.error(err) // 初始化失败,运行环境不是钱包环境
             }
+            //登录时候已经初始化好snb了
+            this.SNB = GlobalD.UserInfo.snb;
+
             this.readData = userData.readData;
-            cc.log('playerPullInfo 读取到数据?:', this.readData)
+            // cc.log('playerPullInfo 读取到数据?:', this.readData)
             this.InitNextworkData();
             this.InitSceneInfo();
 
@@ -92,7 +107,7 @@ cc.Class({
              * todo 如果退出游戏,触发一次存储。
              */
             this.AutoSaveData = function () {
-                this.pushData();
+                this.pushData(true);
             };
             this.schedule(this.AutoSaveData, 20);
         } else {
@@ -101,6 +116,10 @@ cc.Class({
             // this.onClearAllData();
             this.Init();
             this.InitSceneInfo();
+            this.AutoSaveData = function () {
+                this.pushData(false);
+            };
+            this.schedule(this.AutoSaveData, 20);
         }
     },
 
@@ -129,12 +148,14 @@ cc.Class({
         this.onSpawnHighway();
         //初始化时间ui
         cc.find("GameNode/ManageTimer").getComponent('ManageTimer').Init();
-        //初始化金钱ui
+        //初始化金钱ui,gold,diamond, cnt,snb,
         cc.find("GameNode/ManageGolden").getComponent('ManageGolden').InitManageGlodenUI();
         // //初始化地图物件
         // cc.find("GameNode/ManageMap").getComponent('ManageMap').InitManageMap();
         GlobalD.TiledMap.onInitSolid();
 
+        //tudo 初始化DApp固定土地
+
         //初始化生成房屋
         this.ManageUI.getComponent('ManageBuildings').InitBuildings();
         //初始化生成人物
@@ -164,12 +185,6 @@ cc.Class({
         this.GameYear = this.readData.GameYear;
         this.GameMonth = this.readData.GameMonth;
         this.GameDay = this.readData.GameDay;
-        /**
-         * 接口
-         * todo cnt和snb 由钱包读取
-         */
-        this.CNT = 0;
-        this.SNB = 0;
 
         //读取金币
         this.Golden = this.readData.Golden;
@@ -499,14 +514,14 @@ cc.Class({
      * 游戏里面的cnt 和神农呗
      */
     GetCNT: function () {
-        return this.CNT;
+        return parseFloat(this.CNT);
     },
     SetCNT: function (Num) {
         this.CNT = Num;
         this.ManageUI.getComponent('ManageUI').CNTChangeCallBack(this.CNT);
     },
     GetSNB: function () {
-        return this.SNB;
+        return parseFloat(this.SNB);
     },
     SetSNB: function (Num) {
         this.SNB = Num;
@@ -791,8 +806,10 @@ cc.Class({
      * 现在转服务器存储,原本所有相关操作通过服务器计算。
      * 一、目前涉及到的是 SNT 和 SNB 两个货币相关,和买田地操作 (可以理解为租聘,三种类型)
      * 
+     * isNetwork:true,上传到网络
+     * 
      */
-    pushData: function () {
+    pushData: function (isNetwork) {
         // cc.log(this.EveryDayRewardsArray);
         // console.log(this.readData.BFirstLoadGame);
         var datas = {
@@ -857,18 +874,168 @@ cc.Class({
             //任务
             TaskIconCountClick: task.TaskIconCountClick
         }
-        var data = [];
-        // data["openid"] = userData.openId;
-        // data["userdata"] = JSON.stringify(datas);
-        data["playerData"] = JSON.stringify(datas);
-
-        /** 推送信息 */
-        utils.post(utils.api.playerPushInfo, data, (res, playerPushInfoTemp) => {
-            // console.log('playerPushInfoTemp', playerPushInfoTemp);
+
+        if (isNetwork) {
+            var data = [];
+            // data["openid"] = userData.openId;
+            // data["userdata"] = JSON.stringify(datas);
+            data["playerData"] = JSON.stringify(datas);
+
+            /** 推送信息 */
+            utils.post(utils.api.playerPushInfo, data, (res, playerPushInfoTemp) => {
+                // console.log('playerPushInfoTemp', playerPushInfoTemp);
+            })
+        } else {
+            console.log("存储本地:" + isNetwork);
+            //保存用户数据到本地
+            cc.sys.localStorage.setItem('userdata', JSON.stringify(datas));
+        }
+
+
+    },
+
+
+    getLandConfig() {
+        // "data": [
+        //     {
+        //         "id": 1,
+        //         "configLandId": 1,
+        //         "userId": "4",
+        //         "name": "1",
+        //         "isLease": 1,
+        //         "leaseTime": "2022-01-10 21:25:17",
+        //         "leaseMultiple": null,
+        //         "isPlant": 1,
+        //         "plantStart": "2022-01-10 21:25:22",
+        //         "plantMature": 1,
+        //         "landDescribe": "1",
+        //         "createTime": "2022-01-10 21:25:30",
+        //         "updateTime": "2022-01-10 21:25:33"
+        //     }
+        // ],
+        utils.get(utils.api.landConfig, {}, (res, vaule) => {
+
         })
-        return;
-        //保存用户数据到本地
-        cc.sys.localStorage.setItem('userdata', JSON.stringify(datas));
     },
+    //获取用户全部租赁土地
+    getUserLandList() {
+        // "data": [
+        //     {
+        //         "id": 1,
+        //         "configLandId": 1,
+        //         "userId": "4",
+        //         "name": "1",
+        //         "isLease": 1,
+        //         "leaseTime": "2022-01-10 21:25:17",
+        //         "leaseMultiple": null,
+        //         "isPlant": 1,
+        //         "plantStart": "2022-01-10 21:25:22",
+        //         "plantMature": 1,
+        //         "landDescribe": "1",
+        //         "createTime": "2022-01-10 21:25:30",
+        //         "updateTime": "2022-01-10 21:25:33"
+        //     }
+        // ],
+        utils.get(utils.api.userLandList, {}, (res, vaule) => {
 
+        })
+    },
+    //获取用户已租赁土地的状态
+    getLandState() {
+        // {
+        //     "id": 1,
+        //     "configLandId": 1,
+        //     "userId": "4",
+        //     "name": "1",
+        //     "isLease": 1,
+        //     "leaseTime": "2022-01-10 21:25:17",
+        //     "leaseMultiple": null,
+        //     "isPlant": 1,
+        //     "plantStart": "2022-01-10 21:25:22",
+        //     "plantMature": 1,
+        //     "landDescribe": "1",
+        //     "createTime": "2022-01-10 21:25:30",
+        //     "updateTime": "2022-01-10 21:25:33"
+        // }
+        utils.get(utils.api.landState, { landId: 1 }, (res, vaule) => {
+
+        })
+    },
+    //获取商城种子
+    getMallSeed() {
+        // "data": [
+        //     {
+        //         "id": 1,
+        //         "mallType": "0",
+        //         "picture": null,
+        //         "name": "白菜种子",
+        //         "maturity": 100,
+        //         "planting": 100,
+        //         "harvestQuantity": 2500,
+        //         "harvestCount": 2500,
+        //         "harvestName": "大白菜",
+        //         "price": 99,
+        //         "amount": 0,
+        //         "withered": 200,
+        //         "createTime": "2022-01-10 16:45:18",
+        //         "updateTime": "2022-01-10 16:45:26"
+        //     },
+        //     {
+        //         "id": 2,
+        //         "mallType": "0",
+        //         "picture": null,
+        //         "name": "辣椒种子",
+        //         "maturity": 100,
+        //         "planting": 100,
+        //         "harvestQuantity": 2500,
+        //         "harvestCount": 2500,
+        //         "harvestName": "辣椒",
+        //         "price": 99,
+        //         "amount": 0,
+        //         "withered": 200,
+        //         "createTime": "2022-01-10 16:45:23",
+        //         "updateTime": "2022-01-10 16:45:28"
+        //     }
+        // ],
+        utils.get(utils.api.mallSeed, {}, (res, vaule) => {
+
+        })
+    },
+
+    getWarehouseSeedAndFruit(){
+        // "data": {
+        //     "seed": [
+        //         {
+        //             "id": 1,
+        //             "mallType": "0",
+        //             "picture": null,
+        //             "name": "白菜种子",
+        //             "maturity": 100,
+        //             "planting": 100,
+        //             "harvestQuantity": 2500,
+        //             "harvestCount": 2500,
+        //             "harvestName": "大白菜",
+        //             "price": 99,
+        //             "amount": 0,
+        //             "withered": 200,
+        //             "createTime": "2022-01-10 16:45:18",
+        //             "updateTime": "2022-01-10 16:45:26"
+        //         }
+        //     ],
+        //     "fruit": [
+        //         {
+        //             "id": 1,
+        //             "picture": null,
+        //             "name": "白菜",
+        //             "priceSnb": 99,
+        //             "priceCnt": 9.9,
+        //             "createTime": "2022-01-10 16:45:18",
+        //             "updateTime": "2022-01-10 16:45:26"
+        //         }
+        //     ]
+        // },
+        utils.get(utils.api.getSeedAndFruit, {}, (res, vaule) => {
+
+        })
+    }
 });

+ 0 - 1
assets/Script/public/Globals.js

@@ -32,7 +32,6 @@ window.GlobalD = {
     //用户信息
     Dapp: {
         UserInfo: null,
-        CNT: 0,
         address: '',
         signData: null,
     }

+ 4 - 4
assets/Script/public/ManageGolden.js

@@ -21,11 +21,11 @@ cc.Class({
         },
     },
     InitManageGlodenUI(){
-        this.GoldenLabel.getComponent(cc.Label).string =this.FormatMoney(GlobalD.GameData.GetGolden().toString());
-        this.Diamondlabel.getComponent(cc.Label).string =this.FormatMoney(GlobalD.GameData.GetDiamond().toString());
+        this.GoldenLabel.getComponent(cc.Label).string = this.FormatMoney(GlobalD.GameData.GetGolden().toString());
+        this.Diamondlabel.getComponent(cc.Label).string = this.FormatMoney(GlobalD.GameData.GetDiamond().toString());
    
-        this.CNTNode.getComponent(cc.Label).string = GlobalD.GameData.CNT.toString();
-        this.SNBNode.getComponent(cc.Label).string = GlobalD.GameData.SNB.toString();
+        this.CNTNode.getComponent(cc.Label).string = this.FormatMoney(GlobalD.GameData.GetCNT().toString());
+        this.SNBNode.getComponent(cc.Label).string = this.FormatMoney(GlobalD.GameData.GetSNB().toString());
    
     },
     FormatMoney(s, n) {

+ 2 - 2
assets/Script/public/ManageUI.js

@@ -908,10 +908,10 @@ cc.Class({
     },
 
     CNTChangeCallBack: function (Num) {
-        this.CNTlabel.getComponent(cc.Label).string = Num;
+        this.CNTlabel.getComponent(cc.Label).string = this.FormatMoney(Num);
     },
     SNBChangeCallBack: function (Num) {
-        this.SNBlabel.getComponent(cc.Label).string = Num;
+        this.SNBlabel.getComponent(cc.Label).string = this.FormatMoney(Num);
     },
 
     FormatMoney(s, n) {