Skip to content

Commit

Permalink
linked accounts stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
kr45732 committed May 17, 2024
1 parent b5e9741 commit 0465d4e
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ public ResponseEntity<?> getSkyblockEventSettings(@RequestParam(value = "guildId

@GetMapping("/private/linked/all")
public List<LinkedAccount> getAllLinkedAccounts() {
return database.getAllLinkedAccounts();
return database.getAllLinkedAccountsCached();
}

@GetMapping("/private/linked/by")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -502,7 +502,7 @@ public boolean updateGuild() {
if (roleOrRankEnabled || verifyEnabled || rolesEnabled) {
discordToUuid.putAll(
database
.getAllLinkedAccounts()
.getAllLinkedAccountsCached()
.stream()
.filter(o -> !isBlacklistFeatureEnabled("verify") || !blacklist.contains(o.uuid()))
.collect(Collectors.toMap(LinkedAccount::discord, Function.identity()))
Expand Down Expand Up @@ -553,7 +553,6 @@ public boolean updateGuild() {
while (matcher.find()) {
String category = matcher.group(1).toUpperCase();
String type = matcher.group(2).toUpperCase();

if (
category.equals("PLAYER") &&
(type.equals("SKILLS") ||
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public static EmbedBuilder getServerLb(String lbTypeParam, String username, Play
}

Map<String, String> discordToUuid = database
.getAllLinkedAccounts()
.getAllLinkedAccountsCached()
.stream()
.collect(Collectors.toMap(LinkedAccount::discord, LinkedAccount::uuid));
List<String> uuids = new ArrayList<>();
Expand Down
27 changes: 22 additions & 5 deletions src/main/java/com/skyblockplus/utils/database/Database.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@

package com.skyblockplus.utils.database;

import static com.skyblockplus.utils.ApiHandler.getHypixelApiUrl;
import static com.skyblockplus.utils.ApiHandler.leaderboardDatabase;
import static com.skyblockplus.utils.utils.HttpUtils.getJsonObject;
import static com.skyblockplus.utils.utils.Utils.gson;

import com.google.gson.JsonArray;
Expand All @@ -44,6 +46,8 @@
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.time.Duration;
import java.time.Instant;
import java.util.ArrayList;
import java.util.List;
import net.dv8tion.jda.api.entities.Member;
Expand All @@ -57,6 +61,8 @@ public class Database {

public final ServerSettingsService settingsService;
public final HikariDataSource dataSource;
private final List<LinkedAccount> linkedAccountsCache = new ArrayList<>();
private Instant linkedAccountsCacheLastUpdated = null;

@Autowired
public Database(ServerSettingsService settingsService, HikariDataSource dataSource) {
Expand Down Expand Up @@ -292,17 +298,28 @@ public LinkedAccount getByDiscord(String discord) {
return getBy("discord", discord);
}

public List<LinkedAccount> getAllLinkedAccounts() {
public List<LinkedAccount> getAllLinkedAccountsCached() {
if (linkedAccountsCacheLastUpdated == null || Duration.between(linkedAccountsCacheLastUpdated, Instant.now()).toMinutes() >= 1) {
linkedAccountsCacheLastUpdated = Instant.now();
getAllLinkedAccounts();
}

return linkedAccountsCache;
}

private List<LinkedAccount> getAllLinkedAccounts() {
try (
Connection connection = getConnection();
PreparedStatement statement = connection.prepareStatement("SELECT * FROM linked_account")
) {
try (ResultSet response = statement.executeQuery()) {
List<LinkedAccount> linkedAccounts = new ArrayList<>();
while (response.next()) {
linkedAccounts.add(responseToRecord(response));
synchronized (linkedAccountsCache) {
linkedAccountsCache.clear();
while (response.next()) {
linkedAccountsCache.add(responseToRecord(response));
}
}
return linkedAccounts;
return linkedAccountsCache;
}
} catch (Exception ignored) {}
return null;
Expand Down

0 comments on commit 0465d4e

Please sign in to comment.