From 4e9728cbd39cfb9ff46bb5a69b35b38fb92c53ca Mon Sep 17 00:00:00 2001 From: Sebastien Date: Wed, 2 Jan 2019 00:05:40 +0100 Subject: [PATCH] Correct duplicate guild --- .../UserManager/Stats/UserStatsUtils.java | 28 +++++++++++++++---- 1 file changed, 23 insertions(+), 5 deletions(-) diff --git a/src/main/java/net/Broken/Tools/UserManager/Stats/UserStatsUtils.java b/src/main/java/net/Broken/Tools/UserManager/Stats/UserStatsUtils.java index 79fd099..1313979 100644 --- a/src/main/java/net/Broken/Tools/UserManager/Stats/UserStatsUtils.java +++ b/src/main/java/net/Broken/Tools/UserManager/Stats/UserStatsUtils.java @@ -56,16 +56,34 @@ public class UserStatsUtils { public List getUserStats(UserEntity userEntity){ User jdaUser = MainBot.jda.getUserById(userEntity.getJdaId()); - if(userEntity.getUserStats() == null || userEntity.getUserStats().size() == 0 || userEntity.getUserStats().size() != jdaUser.getMutualGuilds().size()){ + if(userEntity.getUserStats() == null || userEntity.getUserStats().size() == 0 || userEntity.getUserStats().size() < jdaUser.getMutualGuilds().size()){ logger.debug("Stats not found for " + userEntity.getName()); User user = MainBot.jda.getUserById(userEntity.getJdaId()); if(user == null) return null; - List stats = new ArrayList<>(); - for(Guild guid : user.getMutualGuilds()){ - logger.debug(guid.getName()); - stats.add(new UserStats(guid.getId(), userEntity)); + List stats; + if(userEntity.getUserStats() == null || userEntity.getUserStats().size() == 0){ + stats = new ArrayList<>(); + for(Guild guid : user.getMutualGuilds()){ + logger.debug(guid.getName()); + stats.add(new UserStats(guid.getId(), userEntity)); + } } + else{ + stats = userEntity.getUserStats(); + ArrayList guildStat = new ArrayList<>(); + for(UserStats stat : stats){ + guildStat.add(stat.getGuildId()); + } + for(Guild guid : user.getMutualGuilds()){ + logger.debug(guid.getName()); + if(!guildStat.contains(guid.getId())){ + logger.debug("Guild " + guid.getName() + " stat don't exist"); + stats.add(new UserStats(guid.getId(), userEntity)); + } + } + } + stats = (List) userStatsRepository.save(stats); userEntity.setUserStats(stats); userEntity = userRepository.save(userEntity);