Add some debug logs

This commit is contained in:
Sebastien 2020-04-12 15:01:39 +02:00
parent 5e1feded79
commit 9419169f4e

View File

@ -31,7 +31,6 @@ public class UserStatsUtils {
static double XP_PER_API_COUNT = 1; static double XP_PER_API_COUNT = 1;
private static UserStatsUtils INSTANCE = new UserStatsUtils(); private static UserStatsUtils INSTANCE = new UserStatsUtils();
public static UserStatsUtils getINSTANCE() { public static UserStatsUtils getINSTANCE() {
@ -50,7 +49,6 @@ public class UserStatsUtils {
private Logger logger = LogManager.getLogger(); private Logger logger = LogManager.getLogger();
private UserStatsUtils() { private UserStatsUtils() {
ApplicationContext context = SpringContext.getAppContext(); ApplicationContext context = SpringContext.getAppContext();
userStatsRepository = (UserStatsRepository) context.getBean("userStatsRepository"); userStatsRepository = (UserStatsRepository) context.getBean("userStatsRepository");
@ -63,30 +61,32 @@ public class UserStatsUtils {
User jdaUser = MainBot.jda.getUserById(userEntity.getJdaId()); User jdaUser = MainBot.jda.getUserById(userEntity.getJdaId());
//TODO clean database for deleted users //TODO clean database for deleted users
if(jdaUser == null) if (jdaUser == null){
logger.warn("jdaUser is null, can't find discord user ????");
return null; return null;
}
logger.debug(userEntity); logger.debug(userEntity);
logger.debug(userEntity.getUserStats()); logger.debug(userEntity.getUserStats());
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());
if(user == null)
return null;
List<UserStats> stats; List<UserStats> stats;
if (userEntity.getUserStats() == null || userEntity.getUserStats().size() == 0) { if (userEntity.getUserStats() == null || userEntity.getUserStats().size() == 0) {
logger.debug("No stats found for user " + jdaUser.getName() + ", use blank.");
logger.debug("Creating stats for guilds: ");
stats = new ArrayList<>(); stats = new ArrayList<>();
for(Guild guid : user.getMutualGuilds()){ for (Guild guid : jdaUser.getMutualGuilds()) {
logger.debug(guid.getName()); logger.debug("..." + guid.getName());
stats.add(new UserStats(guid.getId(), userEntity)); stats.add(new UserStats(guid.getId(), userEntity));
} }
} } else {
else{
stats = userEntity.getUserStats(); stats = userEntity.getUserStats();
ArrayList<String> guildStat = new ArrayList<>(); ArrayList<String> guildStat = new ArrayList<>();
for (UserStats stat : stats) { for (UserStats stat : stats) {
guildStat.add(stat.getGuildId()); guildStat.add(stat.getGuildId());
} }
for(Guild guid : user.getMutualGuilds()){ for (Guild guid : jdaUser.getMutualGuilds()) {
logger.debug(guid.getName()); logger.debug(guid.getName());
if (!guildStat.contains(guid.getId())) { if (!guildStat.contains(guid.getId())) {
logger.debug("Guild " + guid.getName() + " stats don't exist"); logger.debug("Guild " + guid.getName() + " stats don't exist");
@ -114,8 +114,7 @@ public class UserStatsUtils {
if (userList.size() == 0) { if (userList.size() == 0) {
logger.debug("User not registered, generate it. User: " + user.getName() + " " + user.getDiscriminator()); logger.debug("User not registered, generate it. User: " + user.getName() + " " + user.getDiscriminator());
userEntity = genUserEntity(user); userEntity = genUserEntity(user);
} } else
else
userEntity = userList.get(0); userEntity = userList.get(0);
return getUserStats(userEntity); return getUserStats(userEntity);
@ -123,17 +122,18 @@ public class UserStatsUtils {
} }
public UserStats getGuildUserStats(Member member) { public UserStats getGuildUserStats(Member member) {
List<UserEntity> userEntityList = userRepository.findByJdaId(member.getUser().getId()); List<UserEntity> userEntityList = userRepository.findByJdaId(member.getUser().getId());
UserEntity userEntity; UserEntity userEntity;
if( userEntityList.size() == 0) if (userEntityList.size() == 0) {
logger.debug("UserEntity not found for user " + member.getNickname());
userEntity = genUserEntity(member.getUser()); userEntity = genUserEntity(member.getUser());
else } else
userEntity = userEntityList.get(0); userEntity = userEntityList.get(0);
List<UserStats> userStatsList = userStatsRepository.findByUserAndGuildId(userEntity, member.getGuild().getId()); List<UserStats> userStatsList = userStatsRepository.findByUserAndGuildId(userEntity, member.getGuild().getId());
if (userStatsList.size() == 0) { if (userStatsList.size() == 0) {
logger.debug("User stats not found for user " + userEntity.getName() + " guild: " + member.getGuild().getId());
getUserStats(userEntity); getUserStats(userEntity);
userStatsList = userStatsRepository.findByUserAndGuildId(userEntity, member.getGuild().getId()); userStatsList = userStatsRepository.findByUserAndGuildId(userEntity, member.getGuild().getId());
} }
@ -144,6 +144,7 @@ public class UserStatsUtils {
public UserStats getGuildUserStats(UserEntity userEntity, String guildId) { public UserStats getGuildUserStats(UserEntity userEntity, String guildId) {
List<UserStats> userStatsList = userStatsRepository.findByUserAndGuildId(userEntity, guildId); List<UserStats> userStatsList = userStatsRepository.findByUserAndGuildId(userEntity, guildId);
if (userStatsRepository.findByUserAndGuildId(userEntity, guildId).size() == 0) { if (userStatsRepository.findByUserAndGuildId(userEntity, guildId).size() == 0) {
logger.debug("User stats not found for user " + userEntity.getName() + " guild: " + guildId);
getUserStats(userEntity); getUserStats(userEntity);
userStatsList = userStatsRepository.findByUserAndGuildId(userEntity, guildId); userStatsList = userStatsRepository.findByUserAndGuildId(userEntity, guildId);
} }
@ -179,15 +180,12 @@ public class UserStatsUtils {
} }
private UserEntity genUserEntity(User user) { private UserEntity genUserEntity(User user) {
UserEntity userEntity = new UserEntity(user, passwordEncoder); UserEntity userEntity = new UserEntity(user, passwordEncoder);
return userRepository.save(userEntity); return userRepository.save(userEntity);
} }
public GuildStatsPack getStatPack(UserEntity userEntity, String guildId) { public GuildStatsPack getStatPack(UserEntity userEntity, String guildId) {
UserStats userStats = getGuildUserStats(userEntity, guildId); UserStats userStats = getGuildUserStats(userEntity, guildId);
GuildStats selfGuildStats = null; GuildStats selfGuildStats = null;
@ -206,7 +204,6 @@ public class UserStatsUtils {
} }
} }
ranked.sort((guildStats, t1) -> (int) (t1.total - guildStats.total)); ranked.sort((guildStats, t1) -> (int) (t1.total - guildStats.total));
@ -264,6 +261,7 @@ public class UserStatsUtils {
public static class VoicePresenceCounter extends Thread { public static class VoicePresenceCounter extends Thread {
private Member member; private Member member;
public VoicePresenceCounter(Member member) { public VoicePresenceCounter(Member member) {
this.member = member; this.member = member;
} }
@ -287,5 +285,4 @@ public class UserStatsUtils {
} }
} }