Pārlūkot izejas kodu

历史游戏记录

panhui 2 gadi atpakaļ
vecāks
revīzija
66afd192e8

+ 27 - 22
assets/scripts/FloatObj.ts

@@ -20,8 +20,7 @@
 * floatObj.multiply(19.9, 100) === 1990
 *
 */
-const floatObj = function () {
-
+const floatObj = (function () {
     /*
      * 判断obj是否为一个整数 整数取整后还是等于自己。利用这个特性来判断是否是整数
      */
@@ -38,15 +37,16 @@ const floatObj = function () {
     function toInteger(floatNum) {
         // 初始化数字与精度 times精度倍数  num转化后的整数
         var ret = { times: 1, num: 0 }
-        var isNegative = floatNum < 0  //是否是小数
-        if (isInteger(floatNum)) {  // 是否是整数
+        var isNegative = floatNum < 0 //是否是小数
+        if (isInteger(floatNum)) {
+            // 是否是整数
             ret.num = floatNum
-            return ret  //是整数直接返回
+            return ret //是整数直接返回
         }
-        var strfi = floatNum + ''  // 转换为字符串
-        var dotPos = strfi.indexOf('.')
+        var strfi = floatNum + "" // 转换为字符串
+        var dotPos = strfi.indexOf(".")
         var len = strfi.substr(dotPos + 1).length // 拿到小数点之后的位数
-        var times = Math.pow(10, len)  // 精度倍数
+        var times = Math.pow(10, len) // 精度倍数
         /* 为什么加0.5?
             前面讲过乘法也会出现精度问题
             假设传入0.16344556此时倍数为100000000
@@ -73,7 +73,7 @@ const floatObj = function () {
     function operation(a, b, op) {
         var o1 = toInteger(a)
         var o2 = toInteger(b)
-        var n1 = o1.num  // 3.25+3.153
+        var n1 = o1.num // 3.25+3.153
         var n2 = o2.num
         var t1 = o1.times
         var t2 = o2.times
@@ -81,17 +81,19 @@ const floatObj = function () {
         var result = null
         switch (op) {
             // 加减需要根据倍数关系来处理
-            case 'add':
-                if (t1 === t2) { // 两个小数倍数相同
+            case "add":
+                if (t1 === t2) {
+                    // 两个小数倍数相同
                     result = n1 + n2
                 } else if (t1 > t2) {
                     // o1 小数位 大于 o2
                     result = n1 + n2 * (t1 / t2)
-                } else {  // o1小数位小于 o2
+                } else {
+                    // o1小数位小于 o2
                     result = n1 * (t2 / t1) + n2
                 }
                 return result / max
-            case 'subtract':
+            case "subtract":
                 if (t1 === t2) {
                     result = n1 - n2
                 } else if (t1 > t2) {
@@ -100,11 +102,11 @@ const floatObj = function () {
                     result = n1 * (t2 / t1) - n2
                 }
                 return result / max
-            case 'multiply':
+            case "multiply":
                 // 325*3153/(100*1000) 扩大100倍 ==>缩小100倍
                 result = (n1 * n2) / (t1 * t2)
                 return result
-            case 'divide':
+            case "divide":
                 // (325/3153)*(1000/100)  缩小100倍 ==>扩大100倍
                 result = (n1 / n2) * (t2 / t1)
                 return result
@@ -112,17 +114,20 @@ const floatObj = function () {
     }
 
     // 加减乘除的四个接口
-    function add(a, b) {
-        return operation(a, b, 'add')
+    function add(num1, num2) {
+        const num1Digits = (num1.toString().split(".")[1] || "").length
+        const num2Digits = (num2.toString().split(".")[1] || "").length
+        const baseNum = Math.pow(10, Math.max(num1Digits, num2Digits))
+        return (num1 * baseNum + num2 * baseNum) / baseNum
     }
     function subtract(a, b) {
-        return operation(a, b, 'subtract')
+        return operation(a, b, "subtract")
     }
     function multiply(a, b) {
-        return operation(a, b, 'multiply')
+        return operation(a, b, "multiply")
     }
     function divide(a, b) {
-        return operation(a, b, 'divide')
+        return operation(a, b, "divide")
     }
     return {
         add: add,
@@ -130,6 +135,6 @@ const floatObj = function () {
         multiply: multiply,
         divide: divide
     }
-}();
+})()
 
-export default floatObj
+export default floatObj

+ 10 - 4
assets/scripts/StoryPannelCtr.ts

@@ -42,10 +42,15 @@ export class StoryPannelCtr extends Component {
         console.log("开始更新故事!!!!!!!!!!!!!")
 
         //this.fullText = str.replace(/[\r\n]+/g, ' ').trim();
-        this.fullText = str
-            .replace(/[\r\n]+/g, " ")
-            .replace(/\s+/g, " ")
-            .trim()
+        if (str) {
+            this.fullText = str
+                .replace(/[\r\n]+/g, " ")
+                .replace(/\s+/g, " ")
+                .trim()
+        } else {
+            return
+        }
+
         if (!this.node) {
             console.warn("RichText component is not assigned!")
             return
@@ -91,6 +96,7 @@ export class StoryPannelCtr extends Component {
                 this.typeWrite()
             }, this.typeSpeed)
         } else {
+            console.log(this.isOnProcess)
             //初始化中间值
             this.processedCharsCount = 0
             this.currentLine = ""

+ 99 - 9
assets/scripts/gameCtr.ts

@@ -78,6 +78,8 @@ export class GameCtr extends Component {
     public roomId: number = 1
     public gameId: number = 1
     public resetNum: number = 1
+    public gameStatus: string = ""
+    public gameLine: number = 0
 
     private gameInfo: any = null
     private historyInfo: any[] = []
@@ -182,9 +184,14 @@ export class GameCtr extends Component {
             .then((data) => {
                 //获取游戏ID,房间信息
                 console.log(data.currentGameId + "   " + data.active)
-                this.gameId = data.currentGameId
+                let gameId = new URLSearchParams(location.search).get("gameId")
+                if (gameId != null) {
+                    this.gameId = Number(gameId)
+                    console.log("get gameId from URL:" + gameId)
+                } else {
+                    this.gameId = data.currentGameId
+                }
                 this.noticeStr = data.notice
-
                 if (this.socketClient.socket) {
                     this.socketClient.socket.disconnect()
                 } else {
@@ -205,6 +212,12 @@ export class GameCtr extends Component {
                         const historyUrl = `${this.originUrl}/api/game/${this.gameId}/history`
                         this.gameInfo = data
                         this.resetNum = data.resetNum
+                        if (new URLSearchParams(location.search).get("gameId") !== null) {
+                            this.gameStatus = data.status
+                        }
+                        if (this.gameStatus === "finished") {
+                            this.gameLine = 0
+                        }
 
                         fetch(historyUrl)
                             .then((response) => {
@@ -220,6 +233,10 @@ export class GameCtr extends Component {
                                 this.historyInfo = data
                                 //根据游戏信息与历史信息更新房间组件
                                 this.updateGame(this.gameInfo, this.historyInfo)
+                                if (this.gameStatus === "finished") {
+                                    this.addHistoryQueue(this.historyInfo)
+                                } else {
+                                }
                             })
                             .catch((error) => {
                                 console.error("Fetch error:", error)
@@ -320,7 +337,8 @@ export class GameCtr extends Component {
 
                 const differenceInMilliseconds = flatObj.subtract(lastHistoryDate.getTime(), firstHistoryDate.getTime())
                 // 将毫秒转为天数
-                const differenceInDays = flatObj.add(flatObj.divide(differenceInMilliseconds, 1000 * 60 * 60 * 24), 1)
+                let _differenceInMilliseconds = flatObj.divide(differenceInMilliseconds, 1000 * 60 * 60 * 24)
+                const differenceInDays = flatObj.add(differenceInMilliseconds, 1)
 
                 console.log("校验游戏进程是否同步:当前天数:" + this.currentDay + "  后台进行天数" + differenceInDays)
 
@@ -685,22 +703,92 @@ export class GameCtr extends Component {
         }
     }
 
+    addHistoryQueue(historyInfo) {
+        if (this.gameLine < historyInfo.length) {
+            let nowHistory = historyInfo[this.gameLine]
+            if (this.gameLine > 0) {
+                let date1 = new Date(historyInfo[this.gameLine].date).getDate()
+                let date2 = new Date(historyInfo[this.gameLine - 1].date).getDate()
+                console.log(date1)
+                console.log(date2)
+                let evetsimgs = ["evening"]
+                if (
+                    date1 !== date2 ||
+                    evetsimgs.indexOf(historyInfo[this.gameLine].time) !==
+                        evetsimgs.indexOf(historyInfo[this.gameLine - 1].time)
+                ) {
+                    this.processCtr.messageQueue.push({
+                        type: "timeChange",
+                        data: {
+                            time: nowHistory.time,
+                            date: nowHistory.date
+                        }
+                    })
+                }
+
+                if (historyInfo[this.gameLine].charactors.length > historyInfo[this.gameLine - 1].charactors.length) {
+                    let playerNames = this.player.map((item) => {
+                        return item.name
+                    })
+                    let newCharactor = historyInfo[this.gameLine].charactors.forEach((item) => {
+                        if (playerNames.indexOf(item.name) === -1) {
+                            this.processCtr.messageQueue.push({
+                                type: "newCharactor",
+                                data: item
+                            })
+                        }
+                    })
+                }
+            }
+            if (nowHistory.plot) {
+                this.processCtr.messageQueue.push({
+                    type: "plot",
+                    data: nowHistory.plot
+                })
+            }
+
+            if (nowHistory.options && nowHistory.options.length > 0) {
+                this.processCtr.messageQueue.push({
+                    type: "options",
+                    data: nowHistory.options
+                })
+            }
+            if (nowHistory.choice) {
+                this.processCtr.messageQueue.push({
+                    type: "voteResult",
+                    data: nowHistory.choice
+                })
+            }
+            this.gameLine++
+            this.addHistoryQueue(historyInfo)
+        } else {
+            this.processCtr.doneProcessing()
+        }
+    }
+
     updateGame(gameInfo: any, historyInfo: any[]) {
         if (gameInfo && history) {
-            this.updateDateByHistory(historyInfo[0], historyInfo[historyInfo.length - 1])
-
             const firstHistory = historyInfo[0]
+            const nowHistory = historyInfo[this.gameLine]
             const lastHistory = historyInfo[historyInfo.length - 1]
-            const charactors: any[] = lastHistory.charactors
+            let charactors: any[] = lastHistory.charactors
 
             //初始化剧情!!!
             //触发剧情框文字
-            if (this.storyPannelCtr) {
+
+            if (this.storyPannelCtr && this.gameStatus !== "finished") {
                 this.storyPannelCtr.initStory(lastHistory.plot)
             } else {
                 console.log("plot error")
             }
 
+            if (this.gameStatus === "finished") {
+                charactors = firstHistory.charactors
+                this.updateDateByHistory(historyInfo[0], firstHistory)
+            } else {
+                this.updateDateByHistory(historyInfo[0], historyInfo[historyInfo.length - 1])
+            }
+
             //初始化角色!!!
             this.player = []
             this.npc = []
@@ -771,7 +859,8 @@ export class GameCtr extends Component {
         // 计算日期差(结果单位为毫秒)
         const lastHistoryDate = new Date(data.data.date)
         const firstHistoryDate = new Date(this.historyInfo[0].date)
-
+        console.log(lastHistoryDate)
+        console.log(firstHistoryDate)
         const differenceInMilliseconds = lastHistoryDate.getTime() - firstHistoryDate.getTime()
         // 将毫秒转为天数
         const differenceInDays = differenceInMilliseconds / (1000 * 60 * 60 * 24) + 1
@@ -818,7 +907,8 @@ export class GameCtr extends Component {
                     continue
                 }
 
-                let newHp = flatObj.add(orgHp, flatObj.divide(this.modifyHp[i].changeValue, 100))
+                let _newHp = flatObj.divide(this.modifyHp[i].changeValue, 100)
+                let newHp = flatObj.add(orgHp, _newHp)
 
                 if (newHp <= 0) {
                     newHp = 0

+ 1 - 0
assets/scripts/socket/SocketClient.ts

@@ -51,6 +51,7 @@ export class SocketClient extends Component {
             }
         })
     }
+
 }
 
 // private  SERVER_URL:string = "wss://airpg.izouma.com/";

+ 2 - 0
assets/scripts/socket/processCtr.ts

@@ -25,6 +25,8 @@ export class processCtr extends Component {
 
 
     processNextMessage = function () {
+        console.log('加入队列')
+        console.log(this.messageQueue)
         if (this.isProcessing || this.messageQueue.length === 0) {
             return;  // If a message is currently being processed or there's no message in the queue, exit
         }