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

This commit is contained in:
nossr50 2021-04-15 11:53:23 -07:00
parent 4f5f3aff80
commit e42eeb1dc4
7 changed files with 47 additions and 22 deletions

View File

@ -1,6 +1,8 @@
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
(Unit Tests) Added a test for initializing the leaderboard on FlatFile
Version 2.1.190
Fixed a null error in BitSetChunkStore
Version 2.1.189

View File

@ -1,6 +1,7 @@
package com.gmail.nossr50.database;
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.PlayerStat;
import com.gmail.nossr50.datatypes.player.PlayerProfile;
@ -89,6 +90,10 @@ public final class FlatFileDatabaseManager implements DatabaseManager {
this.startingLevel = startingLevel;
this.testing = testing;
if(!usersFile.exists()) {
initEmptyDB();
}
if(!testing) {
List<FlatFileDataFlag> flatFileDataFlags = checkFileHealthAndStructure();
@ -882,10 +887,10 @@ public final class FlatFileDatabaseManager implements DatabaseManager {
/**
* 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
if (System.currentTimeMillis() < lastUpdate + UPDATE_WAIT_TIME) {
return;
return LeaderboardStatus.TOO_SOON_TO_UPDATE;
}
lastUpdate = System.currentTimeMillis(); // Log when the last update was run
@ -915,6 +920,10 @@ public final class FlatFileDatabaseManager implements DatabaseManager {
String line;
while ((line = in.readLine()) != null) {
if(line.startsWith("#"))
continue;
String[] data = line.split(":");
playerName = data[USERNAME_INDEX];
int powerLevel = 0;
@ -940,8 +949,8 @@ public final class FlatFileDatabaseManager implements DatabaseManager {
}
catch (Exception 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) {
try {
in.close();
@ -951,6 +960,7 @@ public final class FlatFileDatabaseManager implements DatabaseManager {
}
}
}
}
SkillComparator c = new SkillComparator();
@ -983,6 +993,8 @@ public final class FlatFileDatabaseManager implements DatabaseManager {
playerStatHash.put(PrimarySkillType.TAMING, taming);
playerStatHash.put(PrimarySkillType.FISHING, fishing);
playerStatHash.put(PrimarySkillType.ALCHEMY, alchemy);
return LeaderboardStatus.UPDATED;
}
private void initEmptyDB() {
@ -1014,10 +1026,6 @@ public final class FlatFileDatabaseManager implements DatabaseManager {
logger.info("(" + usersFile.getPath() + ") Validating database file..");
FlatFileDataProcessor dataProcessor = null;
if(!usersFile.exists()) {
initEmptyDB();
}
if (usersFile.exists()) {
BufferedReader bufferedReader = null;
FileWriter fileWriter = null;

View File

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

View File

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

View File

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

View File

@ -1,6 +1,7 @@
package com.gmail.nossr50.database;
import com.gmail.nossr50.TestUtil;
import com.gmail.nossr50.database.flatfile.LeaderboardStatus;
import com.gmail.nossr50.datatypes.database.DatabaseType;
import com.gmail.nossr50.datatypes.player.PlayerProfile;
import com.gmail.nossr50.datatypes.player.UniqueDataType;
@ -24,7 +25,6 @@ import java.util.logging.Logger;
import static org.junit.jupiter.api.Assertions.*;
//TODO: Test update leaderboards
//This class uses JUnit5/Jupiter
public class FlatFileDatabaseManagerTest {
@ -65,7 +65,11 @@ public class FlatFileDatabaseManagerTest {
assertNull(db);
//noinspection UnstableApiUsage
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
@ -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:"
};
@Test
public void testDefaultInit() {
db = new FlatFileDatabaseManager(getTemporaryUserFilePath(), logger, PURGE_TIME, 0);
}
@Test
public void testUpdateLeaderboards() {
assertNotNull(db);
assertEquals(LeaderboardStatus.UPDATED, db.updateLeaderboards());
}
@Test
public void testSaveUser() {
//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
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
assertNotNull(db.getUsersFile());
@ -530,9 +543,7 @@ public class FlatFileDatabaseManagerTest {
public void testDataNotFound() {
//Save the zero version and see if it looks correct
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());
//Check for the "unloaded" profile

View File

@ -2,8 +2,8 @@ package com.gmail.nossr50.util.text;
import net.kyori.adventure.text.TextComponent;
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.
@ -26,7 +26,6 @@ public class TextUtilsTest {
*/
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.");
}
}