Correct duplicate guild

This commit is contained in:
Sebastien 2019-01-02 00:05:40 +01:00
parent 905f34cac2
commit 4e9728cbd3

View File

@ -56,16 +56,34 @@ public class UserStatsUtils {
public List<UserStats> getUserStats(UserEntity userEntity){ public List<UserStats> getUserStats(UserEntity userEntity){
User jdaUser = MainBot.jda.getUserById(userEntity.getJdaId()); 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()); logger.debug("Stats not found for " + userEntity.getName());
User user = MainBot.jda.getUserById(userEntity.getJdaId()); User user = MainBot.jda.getUserById(userEntity.getJdaId());
if(user == null) if(user == null)
return null; return null;
List<UserStats> stats = new ArrayList<>(); List<UserStats> stats;
for(Guild guid : user.getMutualGuilds()){ if(userEntity.getUserStats() == null || userEntity.getUserStats().size() == 0){
logger.debug(guid.getName()); stats = new ArrayList<>();
stats.add(new UserStats(guid.getId(), userEntity)); for(Guild guid : user.getMutualGuilds()){
logger.debug(guid.getName());
stats.add(new UserStats(guid.getId(), userEntity));
}
} }
else{
stats = userEntity.getUserStats();
ArrayList<String> 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<UserStats>) userStatsRepository.save(stats); stats = (List<UserStats>) userStatsRepository.save(stats);
userEntity.setUserStats(stats); userEntity.setUserStats(stats);
userEntity = userRepository.save(userEntity); userEntity = userRepository.save(userEntity);