From 3ecd433ca65ac0f9a4ab9db3dc10e8819d90279a Mon Sep 17 00:00:00 2001
From: parabolabam <sorokinvladislav1402@gmail.com>
Date: Tue, 1 Oct 2024 18:40:45 +0400
Subject: [PATCH] add timer

---
 index.html               | 16 ++++++++++++++--
 js/CGameBase.js          | 14 ++++++++++----
 js/CGameRace.js          |  8 ++++++--
 js/CLapIndicator.js      | 11 +++--------
 js/CNextLevelPanel.js    | 16 ++++++++--------
 js/lib/CTimeFormatter.js | 12 ++++++++++++
 6 files changed, 53 insertions(+), 24 deletions(-)
 create mode 100644 js/lib/CTimeFormatter.js

diff --git a/index.html b/index.html
index cbc986a..3d23dde 100644
--- a/index.html
+++ b/index.html
@@ -18,6 +18,7 @@
         <script type="text/javascript" src="js/lib/platform.js"></script>
         <script type="text/javascript" src="js/lib/ios_fullscreen.js"></script>
         <script type="text/javascript" src="js/lib/howler.min.js"></script>
+        <script type="text/javascript" src="js/lib/CTimeFormatter.js"></script>
         <script type="text/javascript" src="js/lib/ctl_utils.js"></script>
         <script type="text/javascript" src="js/lib/sprite_lib.js"></script>
         <script type="text/javascript" src="js/lib/CTextButton.js"></script>
@@ -98,6 +99,7 @@
                             if(getParamValue('ctl-arcade') === "true"){
                                 parent.__ctlArcadeStartSession();
                             }
+                            window.parent.postMessage({ type: "START_F1_SESSION" }, "*");
                             //...ADD YOUR CODE HERE EVENTUALLY
                     });
                      
@@ -105,6 +107,7 @@
                            if(getParamValue('ctl-arcade') === "true"){
                                parent.__ctlArcadeEndSession();
                            }
+                           window.parent.postMessage({ type: "END_F1_SESSION" }, "*");
                            //...ADD YOUR CODE HERE EVENTUALLY
                     });
 
@@ -112,13 +115,22 @@
                            if(getParamValue('ctl-arcade') === "true"){
                                parent.__ctlArcadeRestartLevel({level:iLevel});
                            }
+                           window.parent.postMessage({ type: "RESTART_F1_SESSION" }, "*");
                            //...ADD YOUR CODE HERE EVENTUALLY
                     });
 
-                    $(oMain).on("save_score", function(evt,iScore, szMode) {
+                    $(oMain).on("save_score", function(evt,iScore,szMode) {
                            if(getParamValue('ctl-arcade') === "true"){
-                               parent.__ctlArcadeSaveScore({score:iScore, mode: szMode});
+                               parent.__ctlArcadeSaveScore({score:iScore.score, mode: szMode});
                            }
+                         
+                           window.parent.postMessage({
+                                type: "SAVE_F1_SCORE",
+                                score: iScore.score,
+                                raceTime: iScore.raceTime
+                            },
+                            "*",
+                            );
                            //...ADD YOUR CODE HERE EVENTUALLY
                     });
 
diff --git a/js/CGameBase.js b/js/CGameBase.js
index f9254de..02b3ebc 100644
--- a/js/CGameBase.js
+++ b/js/CGameBase.js
@@ -36,7 +36,8 @@ var CGameBase = function (oData, iLevel) {
 
   this._oLevelBuilder;
 
-  this.raceTimer;
+  this.raceTimer = 0;
+  this.raceTime = 0;
 
   s_oGame = this;
 
@@ -301,6 +302,7 @@ CGameBase.prototype.resetParams = function () {
   }
 
   this._iGameState = STATE_GAME_START;
+  this.raceTime = 0;
   this._iScore = 0;
   this._iTimeElaps = LEVEL_INFO[this._iLevel].time;
   this._iStartCountDown = START_COUNTDOWN;
@@ -452,12 +454,13 @@ CGameBase.prototype.update = function () {
       this._oPlayer.update(iDt);
 
       this.updateRace(iDt);
-      console.log({ s_iTimeElaps, s_iCntTime });
       this.raceTimer =
         this.raceTimer ||
         setInterval(() => {
-          this._oInterface.refreshRaceTime(new Date().getTime());
-        }, 1000);
+          requestAnimationFrame(() =>
+            this._oInterface.refreshRaceTime(++this.raceTime)
+          );
+        }, 1);
 
       break;
     }
@@ -466,6 +469,9 @@ CGameBase.prototype.update = function () {
       this._oPlayer.autoPilot();
 
       this.updateOpponents(iDt);
+      clearInterval(this.raceTimer);
+      this.raceTimer = 0;
+      // this.resetParams();
 
       break;
     }
diff --git a/js/CGameRace.js b/js/CGameRace.js
index 8fccd05..7fcca54 100644
--- a/js/CGameRace.js
+++ b/js/CGameRace.js
@@ -153,6 +153,7 @@ CGameRace.prototype.playerLapCompleted = function(iCurLap){
     this._iCurPlayerLap = iCurLap;
     if(this._iCurPlayerLap >= this._iTotLap){
         ///END RACE
+        this._iCurPlayerLap = this._iTotLap;
         this.trackCompleted();
     }else {
 
@@ -218,7 +219,7 @@ CGameRace.prototype.trackCompleted = function(){
     //////NEXTLEVELPANEL
     if(iPlayerRank <= MIN_RANK_FOR_WIN){
         this._iScore = Math.floor(POINTS_PER_RANK[iPlayerRank-1]);
-        var oNextLevelPanel = new CNextLevelPanel(iPlayerRank, this._iScore, this._iLevel); 
+        var oNextLevelPanel = new CNextLevelPanel(iPlayerRank, this._iScore, this._iLevel, this.raceTime); 
         
          
         
@@ -232,7 +233,10 @@ CGameRace.prototype.trackCompleted = function(){
             iTotalScore += s_aLevelScore[i];
 
         }
-        $(s_oMain).trigger("save_score", iTotalScore);     
+        $(s_oMain).trigger("save_score", {
+          score: iTotalScore,
+          raceTime: this.raceTime,
+        });     
         
     }else{
 
diff --git a/js/CLapIndicator.js b/js/CLapIndicator.js
index bd96f11..854968f 100644
--- a/js/CLapIndicator.js
+++ b/js/CLapIndicator.js
@@ -45,7 +45,7 @@ function CLapIndicator(iX, iY, oParentContainer){
 
       _oRaceTime = new CTLText(
         _oContainer,
-        iTextX * 2.5,
+        iTextX * 3.5,
         iTextY - iHeight / 2,
         iWidth,
         iHeight,
@@ -56,7 +56,7 @@ function CLapIndicator(iX, iY, oParentContainer){
         1,
         2,
         2,
-        sprintf("%s:%s", 0, 0),
+        sprintf("%s:%s:%s", "00", "00", "000"),
         true,
         true,
         false,
@@ -81,12 +81,7 @@ function CLapIndicator(iX, iY, oParentContainer){
     };
 
     this.refreshRaceTime = function (iMilliseconds) {
-      var iSeconds = Math.floor(iMilliseconds / 1000);
-      var iMinutes = Math.floor(iSeconds / 60);
-      iSeconds -= iMinutes * 60;
-
-      var szText = sprintf("%s:%s", iMinutes, iSeconds);
-      _oRaceTime.refreshText(szText);
+      _oRaceTime.refreshText(formatTime(iMilliseconds));
     };
     
     this.setVisible = function(bVal){
diff --git a/js/CNextLevelPanel.js b/js/CNextLevelPanel.js
index 156825c..acf7b7f 100644
--- a/js/CNextLevelPanel.js
+++ b/js/CNextLevelPanel.js
@@ -1,4 +1,4 @@
-function CNextLevelPanel(iRank, iScore, iLevel){
+function CNextLevelPanel(iRank, iScore, iLevel, raceTime){
     
     var _oPanelContainer;
     var _oFade;
@@ -73,17 +73,17 @@ function CNextLevelPanel(iRank, iScore, iLevel){
         oScoreContainer.y = -50;
         _oPanelContainer.addChild(oScoreContainer);
         
-        var oSprite = s_oSpriteLibrary.getSprite('star');
-        var oStar = createBitmap(oSprite);
-        oStar.regX = oSprite.width/2;
-        oStar.regY = oSprite.height/2;
-        oScoreContainer.addChild(oStar);
+        // var oSprite = s_oSpriteLibrary.getSprite('star');
+        // var oStar = createBitmap(oSprite);
+        // oStar.regX = oSprite.width/2;
+        // oStar.regY = oSprite.height/2;
+        // oScoreContainer.addChild(oStar);
         
         var oScoreText =  new CTLText(oScoreContainer, 
                     50, -20, 150, 50, 
                     50, "left", "#fff", PRIMARY_FONT, 1,
                     0, 0,
-                    "0",
+                    formatTime(raceTime),
                     true, true, false,
                     false );
                     oScoreText.setStroke(10,"#000");
@@ -95,7 +95,7 @@ function CNextLevelPanel(iRank, iScore, iLevel){
         
         
         new createjs.Tween.get(_oPanelContainer).to({y:CANVAS_HEIGHT/2},500, createjs.Ease.quartIn).call(function(){
-            oScoreText.roll(iScore, 5000,createjs.Ease.cubicOut);
+            // oScoreText.roll(formatTime(raceTime), 5000,createjs.Ease.cubicOut);
         });
                     
 
diff --git a/js/lib/CTimeFormatter.js b/js/lib/CTimeFormatter.js
new file mode 100644
index 0000000..5e854e9
--- /dev/null
+++ b/js/lib/CTimeFormatter.js
@@ -0,0 +1,12 @@
+function formatTime(iMilliseconds) {
+  const iTotalSeconds = Math.floor(iMilliseconds / 1000);
+  const iMinutes = Math.floor(iTotalSeconds / 60);
+  const iSeconds = iTotalSeconds % 60;
+  const iMillis = iMilliseconds % 1000;
+
+  const szMinutes = String(iMinutes).padStart(2, "0");
+  const szSeconds = String(iSeconds).padStart(2, "0");
+  const szMillis = String(iMillis).padStart(3, "0");
+
+  return `${szMinutes}:${szSeconds}:${szMillis}`;
+}