moved calculating first entry on high score screen to separate function
[rocksndiamonds.git] / src / screens.c
index 464840b6e078a7575f84d36c1958fcb963b4a55d..fb3aabc2cc8476e5f385859f4ecfc851fc6df572 100644 (file)
@@ -1739,15 +1739,7 @@ void DrawMainMenu(void)
 
   OpenDoor(DOOR_CLOSE_1 | DOOR_OPEN_2);
 
-#if defined(PLATFORM_EMSCRIPTEN)
-  EM_ASM
-  (
-    FS.syncfs(function(err)
-    {
-      assert(!err);
-    });
-  );
-#endif
+  SyncEmscriptenFilesystem();
 }
 
 static void gotoTopLevelDir(void)
@@ -5059,6 +5051,12 @@ void DrawHallOfFame(int level_nr, int highlight_position)
   else
     SetAnimStatus(GAME_MODE_PSEUDO_SCORESNEW);
 
+  LoadServerScore(level_nr);
+
+  // correct highlight position after adding server scores
+  if (highlight_position >= 0)
+    highlight_position = scores.last_added;
+
   FadeSetEnterScreen();
 
   FadeOut(fade_mask);
@@ -5077,6 +5075,39 @@ void DrawHallOfFame(int level_nr, int highlight_position)
   FadeIn(fade_mask);
 }
 
+static int getHallOfFameFirstEntry(int first_entry, int step)
+{
+  if (step == 0)
+    first_entry = scores.last_added - (NUM_MENU_ENTRIES_ON_SCREEN + 1) / 2 + 1;
+  else
+    first_entry += step;
+
+  if (first_entry < 0)
+    first_entry = 0;
+  else if (first_entry > MAX_SCORE_ENTRIES - NUM_MENU_ENTRIES_ON_SCREEN)
+    first_entry = MAX(0, MAX_SCORE_ENTRIES - NUM_MENU_ENTRIES_ON_SCREEN);
+
+  return first_entry;
+}
+
+static char *getHallOfFameScoreText(int nr)
+{
+  if (!level.rate_time_over_score)
+    return int2str(scores.entry[nr].score, 5); // show normal score
+
+  if (level.use_step_counter)
+    return int2str(scores.entry[nr].time, 5);  // show number of steps
+
+  static char score_text[10];
+  int time_seconds = scores.entry[nr].time / FRAMES_PER_SECOND;
+  int mm = (time_seconds / 60) % 60;
+  int ss = (time_seconds % 60);
+
+  sprintf(score_text, "%02d:%02d", mm, ss);    // show playing time
+
+  return score_text;
+}
+
 static void drawHallOfFameList(int level_nr, int first_entry,
                               int highlight_position)
 {
@@ -5110,10 +5141,10 @@ static void drawHallOfFameList(int level_nr, int first_entry,
     for (j = 0; j < num_dots; j++)
       DrawText(mSX + dx2 + j * getFontWidth(font_nr3), sy, ".", font_nr3);
 
-    if (!strEqual(highscore[entry].Name, EMPTY_PLAYER_NAME))
-      DrawText(mSX + dx2, sy, highscore[entry].Name, font_nr2);
+    if (!strEqual(scores.entry[entry].name, EMPTY_PLAYER_NAME))
+      DrawText(mSX + dx2, sy, scores.entry[entry].name, font_nr2);
 
-    DrawText(mSX + dx3, sy, int2str(highscore[entry].Score, 5), font_nr4);
+    DrawText(mSX + dx3, sy, getHallOfFameScoreText(entry), font_nr4);
   }
 
   redraw_mask |= REDRAW_FIELD;
@@ -5131,12 +5162,7 @@ void HandleHallOfFame(int mx, int my, int dx, int dy, int button)
     level_nr = mx;
     highlight_position = my;
 
-    first_entry = highlight_position - (NUM_MENU_ENTRIES_ON_SCREEN + 1) / 2 + 1;
-
-    if (first_entry < 0)
-      first_entry = 0;
-    else if (first_entry + NUM_MENU_ENTRIES_ON_SCREEN > MAX_SCORE_ENTRIES)
-      first_entry = MAX(0, MAX_SCORE_ENTRIES - NUM_MENU_ENTRIES_ON_SCREEN);
+    first_entry = getHallOfFameFirstEntry(0, 0);
 
     drawHallOfFameList(level_nr, first_entry, highlight_position);
 
@@ -5148,25 +5174,15 @@ void HandleHallOfFame(int mx, int my, int dx, int dy, int button)
 
   if (dy < 0)
   {
-    if (first_entry > 0)
-    {
-      first_entry -= step;
-      if (first_entry < 0)
-       first_entry = 0;
+    first_entry = getHallOfFameFirstEntry(first_entry, -step);
 
-      drawHallOfFameList(level_nr, first_entry, highlight_position);
-    }
+    drawHallOfFameList(level_nr, first_entry, highlight_position);
   }
   else if (dy > 0)
   {
-    if (first_entry + NUM_MENU_ENTRIES_ON_SCREEN < MAX_SCORE_ENTRIES)
-    {
-      first_entry += step;
-      if (first_entry + NUM_MENU_ENTRIES_ON_SCREEN > MAX_SCORE_ENTRIES)
-       first_entry = MAX(0, MAX_SCORE_ENTRIES - NUM_MENU_ENTRIES_ON_SCREEN);
+    first_entry = getHallOfFameFirstEntry(first_entry, step);
 
-      drawHallOfFameList(level_nr, first_entry, highlight_position);
-    }
+    drawHallOfFameList(level_nr, first_entry, highlight_position);
   }
   else if (button == MB_MENU_LEAVE || button == MB_MENU_CHOICE)
   {