Browse Source

Fix FlatFile leaderboards not working in certain situations + added leaderboards unit test

nossr50 4 years ago
parent
commit
e42eeb1dc4

+ 3 - 1
Changelog.txt

@@ -1,6 +1,8 @@
 Version 2.1.191
 Version 2.1.191
-    Fixed a critical bug related to our BlockTracker
+    Fixed an exploit
+    Fixed a bug that prevented the leaderboards from working on FlatFile in some circumstances
     Some minor optimizations to our Block events
     Some minor optimizations to our Block events
+    (Unit Tests) Added a test for initializing the leaderboard on FlatFile
 Version 2.1.190
 Version 2.1.190
     Fixed a null error in BitSetChunkStore
     Fixed a null error in BitSetChunkStore
 Version 2.1.189
 Version 2.1.189

+ 16 - 8
src/main/java/com/gmail/nossr50/database/FlatFileDatabaseManager.java

@@ -1,6 +1,7 @@
 package com.gmail.nossr50.database;
 package com.gmail.nossr50.database;
 
 
 import com.gmail.nossr50.api.exceptions.InvalidSkillException;
 import com.gmail.nossr50.api.exceptions.InvalidSkillException;
+import com.gmail.nossr50.database.flatfile.LeaderboardStatus;
 import com.gmail.nossr50.datatypes.database.DatabaseType;
 import com.gmail.nossr50.datatypes.database.DatabaseType;
 import com.gmail.nossr50.datatypes.database.PlayerStat;
 import com.gmail.nossr50.datatypes.database.PlayerStat;
 import com.gmail.nossr50.datatypes.player.PlayerProfile;
 import com.gmail.nossr50.datatypes.player.PlayerProfile;
@@ -89,6 +90,10 @@ public final class FlatFileDatabaseManager implements DatabaseManager {
         this.startingLevel = startingLevel;
         this.startingLevel = startingLevel;
         this.testing = testing;
         this.testing = testing;
 
 
+        if(!usersFile.exists()) {
+            initEmptyDB();
+        }
+
         if(!testing) {
         if(!testing) {
             List<FlatFileDataFlag> flatFileDataFlags = checkFileHealthAndStructure();
             List<FlatFileDataFlag> flatFileDataFlags = checkFileHealthAndStructure();
 
 
@@ -882,10 +887,10 @@ public final class FlatFileDatabaseManager implements DatabaseManager {
     /**
     /**
      * Update the leader boards.
      * Update the leader boards.
      */
      */
-    public void updateLeaderboards() {
+    public @NotNull LeaderboardStatus updateLeaderboards() {
         // Only update FFS leaderboards every 10 minutes.. this puts a lot of strain on the server (depending on the size of the database) and should not be done frequently
         // Only update FFS leaderboards every 10 minutes.. this puts a lot of strain on the server (depending on the size of the database) and should not be done frequently
         if (System.currentTimeMillis() < lastUpdate + UPDATE_WAIT_TIME) {
         if (System.currentTimeMillis() < lastUpdate + UPDATE_WAIT_TIME) {
-            return;
+            return LeaderboardStatus.TOO_SOON_TO_UPDATE;
         }
         }
 
 
         lastUpdate = System.currentTimeMillis(); // Log when the last update was run
         lastUpdate = System.currentTimeMillis(); // Log when the last update was run
@@ -915,6 +920,10 @@ public final class FlatFileDatabaseManager implements DatabaseManager {
                 String line;
                 String line;
 
 
                 while ((line = in.readLine()) != null) {
                 while ((line = in.readLine()) != null) {
+
+                    if(line.startsWith("#"))
+                        continue;
+
                     String[] data = line.split(":");
                     String[] data = line.split(":");
                     playerName = data[USERNAME_INDEX];
                     playerName = data[USERNAME_INDEX];
                     int powerLevel = 0;
                     int powerLevel = 0;
@@ -940,8 +949,8 @@ public final class FlatFileDatabaseManager implements DatabaseManager {
             }
             }
             catch (Exception e) {
             catch (Exception e) {
                 logger.severe("Exception while reading " + usersFilePath + " during user " + playerName + " (Are you sure you formatted it correctly?) " + e);
                 logger.severe("Exception while reading " + usersFilePath + " during user " + playerName + " (Are you sure you formatted it correctly?) " + e);
-            }
-            finally {
+                return LeaderboardStatus.FAILED;
+            } finally {
                 if (in != null) {
                 if (in != null) {
                     try {
                     try {
                         in.close();
                         in.close();
@@ -951,6 +960,7 @@ public final class FlatFileDatabaseManager implements DatabaseManager {
                     }
                     }
                 }
                 }
             }
             }
+
         }
         }
 
 
         SkillComparator c = new SkillComparator();
         SkillComparator c = new SkillComparator();
@@ -983,6 +993,8 @@ public final class FlatFileDatabaseManager implements DatabaseManager {
         playerStatHash.put(PrimarySkillType.TAMING, taming);
         playerStatHash.put(PrimarySkillType.TAMING, taming);
         playerStatHash.put(PrimarySkillType.FISHING, fishing);
         playerStatHash.put(PrimarySkillType.FISHING, fishing);
         playerStatHash.put(PrimarySkillType.ALCHEMY, alchemy);
         playerStatHash.put(PrimarySkillType.ALCHEMY, alchemy);
+
+        return LeaderboardStatus.UPDATED;
     }
     }
 
 
     private void initEmptyDB() {
     private void initEmptyDB() {
@@ -1014,10 +1026,6 @@ public final class FlatFileDatabaseManager implements DatabaseManager {
         logger.info("(" + usersFile.getPath() + ") Validating database file..");
         logger.info("(" + usersFile.getPath() + ") Validating database file..");
         FlatFileDataProcessor dataProcessor = null;
         FlatFileDataProcessor dataProcessor = null;
 
 
-        if(!usersFile.exists()) {
-            initEmptyDB();
-        }
-
         if (usersFile.exists()) {
         if (usersFile.exists()) {
             BufferedReader bufferedReader = null;
             BufferedReader bufferedReader = null;
             FileWriter fileWriter = null;
             FileWriter fileWriter = null;

+ 7 - 0
src/main/java/com/gmail/nossr50/database/flatfile/LeaderboardStatus.java

@@ -0,0 +1,7 @@
+package com.gmail.nossr50.database.flatfile;
+
+public enum LeaderboardStatus {
+    TOO_SOON_TO_UPDATE,
+    UPDATED,
+    FAILED
+}

+ 0 - 1
src/main/java/com/gmail/nossr50/util/Misc.java

@@ -9,7 +9,6 @@ import com.gmail.nossr50.util.player.UserManager;
 import com.google.common.collect.ImmutableSet;
 import com.google.common.collect.ImmutableSet;
 import org.bukkit.Location;
 import org.bukkit.Location;
 import org.bukkit.Material;
 import org.bukkit.Material;
-import org.bukkit.World;
 import org.bukkit.block.BlockState;
 import org.bukkit.block.BlockState;
 import org.bukkit.entity.*;
 import org.bukkit.entity.*;
 import org.bukkit.inventory.ItemStack;
 import org.bukkit.inventory.ItemStack;

+ 0 - 1
src/main/java/com/gmail/nossr50/util/blockmeta/BitSetChunkStore.java

@@ -1,7 +1,6 @@
 package com.gmail.nossr50.util.blockmeta;
 package com.gmail.nossr50.util.blockmeta;
 
 
 import com.gmail.nossr50.mcMMO;
 import com.gmail.nossr50.mcMMO;
-import com.gmail.nossr50.util.Misc;
 import org.bukkit.Bukkit;
 import org.bukkit.Bukkit;
 import org.bukkit.World;
 import org.bukkit.World;
 import org.jetbrains.annotations.NotNull;
 import org.jetbrains.annotations.NotNull;

+ 18 - 7
src/test/java/com/gmail/nossr50/database/FlatFileDatabaseManagerTest.java

@@ -1,6 +1,7 @@
 package com.gmail.nossr50.database;
 package com.gmail.nossr50.database;
 
 
 import com.gmail.nossr50.TestUtil;
 import com.gmail.nossr50.TestUtil;
+import com.gmail.nossr50.database.flatfile.LeaderboardStatus;
 import com.gmail.nossr50.datatypes.database.DatabaseType;
 import com.gmail.nossr50.datatypes.database.DatabaseType;
 import com.gmail.nossr50.datatypes.player.PlayerProfile;
 import com.gmail.nossr50.datatypes.player.PlayerProfile;
 import com.gmail.nossr50.datatypes.player.UniqueDataType;
 import com.gmail.nossr50.datatypes.player.UniqueDataType;
@@ -24,7 +25,6 @@ import java.util.logging.Logger;
 
 
 import static org.junit.jupiter.api.Assertions.*;
 import static org.junit.jupiter.api.Assertions.*;
 
 
-//TODO: Test update leaderboards
 //This class uses JUnit5/Jupiter
 //This class uses JUnit5/Jupiter
 public class FlatFileDatabaseManagerTest {
 public class FlatFileDatabaseManagerTest {
 
 
@@ -65,7 +65,11 @@ public class FlatFileDatabaseManagerTest {
         assertNull(db);
         assertNull(db);
         //noinspection UnstableApiUsage
         //noinspection UnstableApiUsage
         tempDir = Files.createTempDir();
         tempDir = Files.createTempDir();
-        db = new FlatFileDatabaseManager(new File(tempDir.getPath() + File.separator + TEST_FILE_NAME), logger, PURGE_TIME, 0, true);
+        db = new FlatFileDatabaseManager(new File(getTemporaryUserFilePath()), logger, PURGE_TIME, 0, true);
+    }
+
+    private @NotNull String getTemporaryUserFilePath() {
+        return tempDir.getPath() + File.separator + TEST_FILE_NAME;
     }
     }
 
 
     @AfterEach
     @AfterEach
@@ -131,6 +135,17 @@ public class FlatFileDatabaseManagerTest {
             "mrfloris:2420:::0:2452:0:1983:1937:1790:3042:badvalue:3102:2408:3411:0:0:0:0:0:0:0:0::642:0:1617583171:0:1617165043:0:1617583004:1617563189:1616785408::2184:0:0:1617852413:HEARTS:415:0:631e3896-da2a-4077-974b-d047859d76bc:5:1600906906:"
             "mrfloris:2420:::0:2452:0:1983:1937:1790:3042:badvalue:3102:2408:3411:0:0:0:0:0:0:0:0::642:0:1617583171:0:1617165043:0:1617583004:1617563189:1616785408::2184:0:0:1617852413:HEARTS:415:0:631e3896-da2a-4077-974b-d047859d76bc:5:1600906906:"
     };
     };
 
 
+    @Test
+    public void testDefaultInit() {
+        db = new FlatFileDatabaseManager(getTemporaryUserFilePath(), logger, PURGE_TIME, 0);
+    }
+
+    @Test
+    public void testUpdateLeaderboards() {
+        assertNotNull(db);
+        assertEquals(LeaderboardStatus.UPDATED, db.updateLeaderboards());
+    }
+
     @Test
     @Test
     public void testSaveUser() {
     public void testSaveUser() {
         //Make a Profile to save and check to see if it worked
         //Make a Profile to save and check to see if it worked
@@ -141,8 +156,6 @@ public class FlatFileDatabaseManagerTest {
 
 
         //Save the zero version and see if it looks correct
         //Save the zero version and see if it looks correct
         assertNotNull(db);
         assertNotNull(db);
-        assertFalse(db.getUsersFile().exists());
-        db.checkFileHealthAndStructure();
         assertTrue(db.getUsersFile().exists()); //Users file should have been created from the above com.gmail.nossr50.database.FlatFileDatabaseManager.checkFileHealthAndStructure
         assertTrue(db.getUsersFile().exists()); //Users file should have been created from the above com.gmail.nossr50.database.FlatFileDatabaseManager.checkFileHealthAndStructure
         assertNotNull(db.getUsersFile());
         assertNotNull(db.getUsersFile());
 
 
@@ -530,9 +543,7 @@ public class FlatFileDatabaseManagerTest {
     public void testDataNotFound() {
     public void testDataNotFound() {
         //Save the zero version and see if it looks correct
         //Save the zero version and see if it looks correct
         assertNotNull(db);
         assertNotNull(db);
-        assertFalse(db.getUsersFile().exists());
-        db.checkFileHealthAndStructure();
-        assertTrue(db.getUsersFile().exists()); //Users file should have been created from the above com.gmail.nossr50.database.FlatFileDatabaseManager.checkFileHealthAndStructure
+        assertTrue(db.getUsersFile().exists());
         assertNotNull(db.getUsersFile());
         assertNotNull(db.getUsersFile());
 
 
         //Check for the "unloaded" profile
         //Check for the "unloaded" profile

+ 3 - 4
src/test/java/com/gmail/nossr50/util/text/TextUtilsTest.java

@@ -2,8 +2,8 @@ package com.gmail.nossr50.util.text;
 
 
 import net.kyori.adventure.text.TextComponent;
 import net.kyori.adventure.text.TextComponent;
 import net.kyori.adventure.text.format.NamedTextColor;
 import net.kyori.adventure.text.format.NamedTextColor;
-import org.junit.Assert;
-import org.junit.Test;
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.Test;
 
 
 /**
 /**
  * This Unit Test checks if Adventure was set up correctly and works as expected.
  * This Unit Test checks if Adventure was set up correctly and works as expected.
@@ -26,7 +26,6 @@ public class TextUtilsTest {
          */
          */
         TextComponent component = TextUtils.colorizeText(inputText);
         TextComponent component = TextUtils.colorizeText(inputText);
 
 
-        Assert.assertEquals("Looks like Adventure is not working correctly.",
-                NamedTextColor.DARK_RED, component.color());
+        Assertions.assertEquals(NamedTextColor.DARK_RED, component.color(), "Looks like Adventure is not working correctly.");
     }
     }
 }
 }