Skip to content

Commit

Permalink
Optimize leaderboard updating for sql
Browse files Browse the repository at this point in the history
  • Loading branch information
Archy-X committed Jul 8, 2024
1 parent 2b3c52b commit 4a5fc15
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public void onMergeSkills(CommandSender sender, String from, String to) {
int success = 0;
try {
StorageProvider storage = plugin.getStorageProvider();
for (UserState state : storage.loadStates(false)) {
for (UserState state : storage.loadStates(false, false)) {
Entry<Skill, Integer> fromEntry = state.skillLevels().entrySet().stream()
.filter(e -> e.getKey().getId().toString().equals(from))
.findFirst()
Expand Down Expand Up @@ -84,7 +84,7 @@ public void removeAllModifiers(CommandSender sender) {
int skipped = 0;
try {
StorageProvider storage = plugin.getStorageProvider();
for (UserState state : storage.loadStates(false)) {
for (UserState state : storage.loadStates(false, false)) {
if (state.statModifiers().isEmpty() && state.traitModifiers().isEmpty()){
skipped++;
continue;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ private void addLoadedPlayersToLeaderboards(Map<Skill, List<SkillValue>> skillLb
}

private void addOfflinePlayers(Map<Skill, List<SkillValue>> skillLb, List<SkillValue> powerLb, List<SkillValue> averageLb) throws Exception {
List<UserState> offlineStates = plugin.getStorageProvider().loadStates(true);
List<UserState> offlineStates = plugin.getStorageProvider().loadStates(true, true);
for (UserState state : offlineStates) {
int powerLevel = 0;
double powerXp = 0.0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public void load(UUID uuid) throws Exception {

public abstract void delete(UUID uuid) throws Exception;

public abstract List<UserState> loadStates(boolean ignoreOnline) throws Exception;
public abstract List<UserState> loadStates(boolean ignoreOnline, boolean skipKeyValues) throws Exception;

public void startAutoSaving() {
if (!plugin.configBoolean(Option.AUTO_SAVE_ENABLED)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public File saveBackup(boolean savePlayerData) throws Exception {

config.node("backup_version").set(2);

List<UserState> states = plugin.getStorageProvider().loadStates(false);
List<UserState> states = plugin.getStorageProvider().loadStates(false, false);
if (states.size() > plugin.configInt(Option.AUTOMATIC_BACKUPS_MAX_USERS)) {
plugin.logger().info("Automatic backup saving was skipped due to too many users (" + states.size() + "), use your own backup system.");
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -401,7 +401,7 @@ public void delete(UUID uuid) throws Exception {
}

@Override
public List<UserState> loadStates(boolean ignoreOnline) {
public List<UserState> loadStates(boolean ignoreOnline, boolean skipKeyValues) {
List<UserState> states = new ArrayList<>();
// Get all files in data directory
File[] files = new File(dataDirectory).listFiles();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -642,7 +642,7 @@ public void delete(UUID uuid) throws Exception {
}

@Override
public List<UserState> loadStates(boolean ignoreOnline) throws Exception {
public List<UserState> loadStates(boolean ignoreOnline, boolean skipKeyValues) throws Exception {
List<UserState> states = new ArrayList<>();

Map<Integer, Map<Skill, Integer>> loadedSkillLevels = new HashMap<>();
Expand All @@ -655,7 +655,8 @@ public List<UserState> loadStates(boolean ignoreOnline) throws Exception {
while (resultSet.next()) {
int userId = resultSet.getInt("user_id");
String skillName = resultSet.getString("skill_name");
Skill skill = plugin.getSkillRegistry().get(NamespacedId.fromString(skillName));
Skill skill = plugin.getSkillRegistry().getOrNull(NamespacedId.fromString(skillName));
if (skill == null) continue;

int level = resultSet.getInt("skill_level");
double xp = resultSet.getDouble("skill_xp");
Expand All @@ -665,7 +666,6 @@ public List<UserState> loadStates(boolean ignoreOnline) throws Exception {
}
}
}

String usersQuery = "SELECT user_id, player_uuid, mana FROM " + tablePrefix + "users;";
try (PreparedStatement statement = connection.prepareStatement(usersQuery)) {
try (ResultSet resultSet = statement.executeQuery()) {
Expand All @@ -679,8 +679,15 @@ public List<UserState> loadStates(boolean ignoreOnline) throws Exception {

double mana = resultSet.getDouble("mana");

Map<String, StatModifier> statModifiers = loadStatModifiers(connection, uuid, userId);
Map<String, TraitModifier> traitModifiers = loadTraitModifiers(connection, uuid, userId);
Map<String, StatModifier> statModifiers;
Map<String, TraitModifier> traitModifiers;
if (!skipKeyValues) {
statModifiers = loadStatModifiers(connection, uuid, userId);
traitModifiers = loadTraitModifiers(connection, uuid, userId);
} else {
statModifiers = Collections.emptyMap();
traitModifiers = Collections.emptyMap();
}

Map<Skill, Integer> skillLevelMap = loadedSkillLevels.get(userId);
Map<Skill, Double> skillXpMap = loadedSkillXp.get(userId);
Expand Down

0 comments on commit 4a5fc15

Please sign in to comment.