Преглед изворни кода

一键加速发送服务器数据合并优化

kaka пре 4 година
родитељ
комит
7f278b952d

+ 4 - 4
assets/script/before/main/FactoryIcon.ts

@@ -157,15 +157,15 @@ export class FactoryIcon extends cc.Component {
         mk.ui.openPanel('module/speedUpUI/speedUp');
     }
 
-    onRipe() {
+    onRipe(sendToServer) {
         this.data.state = FactroyState.Ripe;
-        gData.gameData.setFactoryDataMap(this.configID, this.data);
+        gData.gameData.setFactoryDataMap(this.configID, this.data, sendToServer);
         gData.gameData.addProductMakeTimesById(this.data.productID);
     }
 
-    onSick() {
+    onSick(sendToServer) {
         this.data.state = FactroyState.Sick;
-        gData.gameData.setFactoryDataMap(this.configID, this.data);
+        gData.gameData.setFactoryDataMap(this.configID, this.data, sendToServer);
     }
 
     public onHarvest(): void {

+ 8 - 2
assets/script/before/main/FactorySystem.ts

@@ -1,6 +1,6 @@
 /** 工厂数据类 */
 
-import { FactroyState } from "../../game/data/GameData";
+import { FactroyState, GameProp } from "../../game/data/GameData";
 import { FactoryIcon } from "./FactoryIcon";
 
 const { ccclass } = cc._decorator;
@@ -43,11 +43,17 @@ export default class FactorySystem {
 
     setHarvest() {
         let len = this.factoryIcons.length;
+        let sendToServer = false;
         for (var i = 0; i < len; i++) {
             if (this.factoryIcons[i].data.state == FactroyState.Producting) {
-                this.factoryIcons[i].countDown.riped();
+                this.factoryIcons[i].countDown.riped(false);
+                sendToServer = true;
             }
         }
+
+        if (sendToServer) {
+            gData.gameData.freshSendToServer(3);
+        }
     }
 
     canSpeedUp() {

+ 8 - 8
assets/script/before/main/FarmCountDown.ts

@@ -100,36 +100,36 @@ export class FarmCountDown extends cc.Component {
         }
     }
 
-    public riped(): void {
+    public riped(sendToServer = true): void {
         this.counting = false;
         this.normalGroup.active = false;
-        let can = false;
+        let sick = false;
         if (gData.gameData.playerProp.completeFarmTaskTimes >= 1 && Math.random() < 0.2) {
             if (this.productConfig.tab == ProductType.nzw) {
                 if (gData.gameData.RawInsectCurArr[0] < parseInt(gData.gameData.RawInsectArr[0])) {
-                    can = true;
+                    sick = true;
                     gData.gameData.RawInsectCurArr[0]++;
                 }
             }
             else if (this.productConfig.tab == ProductType.dw) {
                 if (gData.gameData.RawInsectCurArr[1] < parseInt(gData.gameData.RawInsectArr[1])) {
-                    can = true;
+                    sick = true;
                     gData.gameData.RawInsectCurArr[1]++;
                 }
             }
             else if (gData.gameData.RawInsectCurArr[2] < parseInt(gData.gameData.RawInsectArr[2])) {
-                can = true;
+                sick = true;
                 gData.gameData.RawInsectCurArr[2]++;
             }
         }
 
-        if (can) {
+        if (sick) {
             this.sickIcon.active = true;
-            this.onSick && this.onSick.emit(null);
+            this.onSick && this.onSick.emit([sendToServer]);
         }
         else {
             this.ripedIcon.active = true;
-            this.onRiped && this.onRiped.emit(null);
+            this.onRiped && this.onRiped.emit([sendToServer]);
         }
     }
 }

+ 4 - 4
assets/script/before/main/FarmIcon.ts

@@ -189,15 +189,15 @@ export class FarmIcon extends cc.Component {
     }
 
     /** 成熟 */
-    public async onRiped() {
+    public async onRiped(sendToServer) {
         this.data.state = FarmState.Ripe;
-        gData.gameData.setFarmDataMap(this.configID, this.data);
+        gData.gameData.setFarmDataMap(this.configID, this.data, sendToServer);
     }
 
     /** 生虫 */
-    public onSick() {
+    public onSick(sendToServer) {
         this.data.state = FarmState.Sick;
-        gData.gameData.setFarmDataMap(this.configID, this.data);
+        gData.gameData.setFarmDataMap(this.configID, this.data, sendToServer);
     }
 
     private async onPlantAnimationFinish() {

+ 7 - 1
assets/script/before/main/FarmSystem.ts

@@ -69,12 +69,18 @@ export class FarmSystem {
 
     public setHarvest() {
         let len = this.farms.length;
+        let sendToServer = false;
         for (let i = 0; i < len; i++) {
             if (this.farms[i].data.state == FarmState.Growing) {
                 this.farms[i].process.onSpeed();
-                this.farms[i].countDown.riped();
+                this.farms[i].countDown.riped(false);
+                sendToServer = true;
             }
         }
+
+        if (sendToServer) {
+            gData.gameData.freshSendToServer(1);
+        }
     }
 
     public canSpeedUp() {

+ 4 - 4
assets/script/before/main/PastureIcon.ts

@@ -214,15 +214,15 @@ export default class PastureIcon extends cc.Component {
         mk.ui.openPanel('module/speedUpUI/speedUp');
     }
 
-    onRipe() {
+    onRipe(sendToServer) {
         this.data.state = PastureState.Ripe;
-        gData.gameData.setPastureDataMap(this.configID, this.data);
+        gData.gameData.setPastureDataMap(this.configID, this.data, sendToServer);
         gData.gameData.addProductMakeTimesById(this.data.productID);
     }
 
-    onSick() {
+    onSick(sendToServer) {
         this.data.state = PastureState.Sick;
-        gData.gameData.setPastureDataMap(this.configID, this.data);
+        gData.gameData.setPastureDataMap(this.configID, this.data, sendToServer);
     }
 
     public onHarvest(): void {

+ 7 - 1
assets/script/before/main/PastureSystem.ts

@@ -46,11 +46,17 @@ export default class PastureSystem {
 
     setHarvest() {
         let len = this.pastureIcons.length;
+        let sendToServer = false;
         for (var i = 0; i < len; i++) {
             if (this.pastureIcons[i].data.state == PastureState.Growing) {
-                this.pastureIcons[i].countDown.riped();
+                this.pastureIcons[i].countDown.riped(false);
+                sendToServer = true;
             }
         }
+
+        if (sendToServer) {
+            gData.gameData.freshSendToServer(2);
+        }
     }
 
     canSpeedUp() {

+ 17 - 3
assets/script/game/data/GameData.ts

@@ -562,7 +562,7 @@ export class GameData {
         this.needFreshArr.push(id);
         //更新到服务器
         if (sendToServer) {
-            this.setProp(GameProp.farmData, this._farmData);
+            this.freshSendToServer(1);
         }
     }
 
@@ -734,7 +734,7 @@ export class GameData {
         this.needFreshArr.push(id);
         //更新到服务器
         if (sendToServer) {
-            this.setProp(GameProp.pastureData, this._pastureData);
+            this.freshSendToServer(2);
         }
     }
 
@@ -804,7 +804,7 @@ export class GameData {
         this.needFreshArr.push(id);
         //更新到服务器
         if (sendToServer) {
-            this.setProp(GameProp.factoryData, this._factoryData);
+            this.freshSendToServer(3);
         }
     }
 
@@ -910,6 +910,20 @@ export class GameData {
         return configID;
     }
 
+    /** 发送数据到服务器 */
+    freshSendToServer(type) {
+        switch (type) {
+            case 1:
+                this.setProp(GameProp.farmData, this._farmData);
+                break;
+            case 2:
+                this.setProp(GameProp.pastureData, this._pastureData);
+                break;
+            case 3:
+                this.setProp(GameProp.factoryData, this._factoryData);
+                break;
+        }
+    }
 
     /** 最大可种植/生产商品数组 */
     private _maxProduct = [];