Add stats API

This commit is contained in:
Sebastien 2018-12-14 17:57:02 +02:00
parent e6f6372d66
commit ae83001360
7 changed files with 133 additions and 39 deletions

View File

@ -8,7 +8,7 @@ import net.Broken.Tools.Command.CommandParser;
import net.Broken.Tools.EmbedMessageUtils; import net.Broken.Tools.EmbedMessageUtils;
import net.Broken.Tools.Moderateur; import net.Broken.Tools.Moderateur;
import net.Broken.Tools.PrivateMessage; import net.Broken.Tools.PrivateMessage;
import net.Broken.Tools.UserManager.UserStatsUtils; import net.Broken.Tools.UserManager.Stats.UserStatsUtils;
import net.Broken.audio.AudioM; import net.Broken.audio.AudioM;
import net.dv8tion.jda.core.EmbedBuilder; import net.dv8tion.jda.core.EmbedBuilder;
import net.dv8tion.jda.core.entities.*; import net.dv8tion.jda.core.entities.*;
@ -78,7 +78,6 @@ public class BotListener extends ListenerAdapter {
String message = guildPref.getWelcomeMessage(); String message = guildPref.getWelcomeMessage();
message = message.replaceAll("@name", event.getMember().getAsMention()); message = message.replaceAll("@name", event.getMember().getAsMention());
logger.debug(message); logger.debug(message);
// "Salut "+event.getUser().getAsMention()+"! Ecris ton nom, prénom, ta promotion et ton groupe ici! Un admin te donnera accées a ton groupe!"
chanel.sendMessage(message).complete(); chanel.sendMessage(message).complete();
} }
@ -124,7 +123,7 @@ public class BotListener extends ListenerAdapter {
public void onGuildVoiceJoin(GuildVoiceJoinEvent event) { public void onGuildVoiceJoin(GuildVoiceJoinEvent event) {
super.onGuildVoiceJoin(event); super.onGuildVoiceJoin(event);
if(!event.getMember().getUser().isBot()) if(!event.getMember().getUser().isBot())
new UserStatsUtils.VoicePresenceCompter(event.getMember()).start(); new UserStatsUtils.VoicePresenceCounter(event.getMember()).start();
} }
@Override @Override

View File

@ -1,32 +1,24 @@
package net.Broken; package net.Broken;
import net.Broken.DB.Entity.UserEntity; import net.Broken.DB.Entity.UserEntity;
import net.Broken.DB.Entity.UserStats;
import net.Broken.DB.Repository.UserRepository; import net.Broken.DB.Repository.UserRepository;
import net.Broken.RestApi.ApiCommandLoader; import net.Broken.RestApi.ApiCommandLoader;
import net.Broken.Tools.Command.CommandLoader; import net.Broken.Tools.Command.CommandLoader;
import net.Broken.Tools.DayListener.DayListener; import net.Broken.Tools.DayListener.DayListener;
import net.Broken.Tools.DayListener.Listeners.DailyMadame; import net.Broken.Tools.DayListener.Listeners.DailyMadame;
import net.Broken.Tools.DayListener.Listeners.ResetSpam; import net.Broken.Tools.DayListener.Listeners.ResetSpam;
import net.Broken.Tools.UserManager.UserStatsUtils; import net.Broken.Tools.UserManager.Stats.UserStatsUtils;
import net.Broken.audio.Youtube.YoutubeTools;
import net.dv8tion.jda.core.AccountType; import net.dv8tion.jda.core.AccountType;
import net.dv8tion.jda.core.JDA; import net.dv8tion.jda.core.JDA;
import net.dv8tion.jda.core.JDABuilder; import net.dv8tion.jda.core.JDABuilder;
import net.dv8tion.jda.core.OnlineStatus; import net.dv8tion.jda.core.OnlineStatus;
import net.dv8tion.jda.core.entities.Game; import net.dv8tion.jda.core.entities.Game;
import net.dv8tion.jda.core.entities.Guild; import net.dv8tion.jda.core.entities.Guild;
import net.dv8tion.jda.core.entities.Member;
import net.dv8tion.jda.core.entities.RichPresence;
import net.dv8tion.jda.core.entities.impl.JDAImpl;
import net.dv8tion.jda.core.exceptions.RateLimitedException;
import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.Logger;
import org.json.JSONObject;
import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContext;
import javax.security.auth.login.LoginException; import javax.security.auth.login.LoginException;
import java.io.IOException;
import java.util.List; import java.util.List;
@ -54,7 +46,8 @@ public class Init {
logger.info("Connecting to Discord api..."); logger.info("Connecting to Discord api...");
//connection au bot //connection au bot
jda = new JDABuilder(AccountType.BOT).setToken(token).setBulkDeleteSplittingEnabled(false).buildBlocking(); jda = new JDABuilder(AccountType.BOT).setToken(token).setBulkDeleteSplittingEnabled(false).build();
jda = jda.awaitReady();
MainBot.jda = jda; MainBot.jda = jda;
jda.setAutoReconnect(true); jda.setAutoReconnect(true);

View File

@ -4,12 +4,11 @@ import com.google.api.client.googleapis.json.GoogleJsonResponseException;
import com.sedmelluq.discord.lavaplayer.player.AudioPlayer; import com.sedmelluq.discord.lavaplayer.player.AudioPlayer;
import com.sedmelluq.discord.lavaplayer.track.AudioTrack; import com.sedmelluq.discord.lavaplayer.track.AudioTrack;
import net.Broken.DB.Entity.UserEntity; import net.Broken.DB.Entity.UserEntity;
import net.Broken.DB.Entity.UserStats;
import net.Broken.DB.Repository.UserRepository; import net.Broken.DB.Repository.UserRepository;
import net.Broken.MainBot; import net.Broken.MainBot;
import net.Broken.RestApi.Data.*; import net.Broken.RestApi.Data.*;
import net.Broken.Tools.UserManager.Exceptions.UnknownTokenException; import net.Broken.Tools.UserManager.Exceptions.UnknownTokenException;
import net.Broken.Tools.UserManager.UserStatsUtils; import net.Broken.Tools.UserManager.Stats.UserStatsUtils;
import net.Broken.Tools.UserManager.UserUtils; import net.Broken.Tools.UserManager.UserUtils;
import net.Broken.audio.AudioM; import net.Broken.audio.AudioM;
import net.Broken.audio.GetVoiceChanels; import net.Broken.audio.GetVoiceChanels;

View File

@ -5,9 +5,12 @@ import net.Broken.DB.Entity.UserEntity;
import net.Broken.DB.Repository.PendingUserRepository; import net.Broken.DB.Repository.PendingUserRepository;
import net.Broken.DB.Repository.UserRepository; import net.Broken.DB.Repository.UserRepository;
import net.Broken.MainBot; import net.Broken.MainBot;
import net.Broken.RestApi.Data.CommandResponseData;
import net.Broken.RestApi.Data.UserManager.*; import net.Broken.RestApi.Data.UserManager.*;
import net.Broken.Tools.UserManager.Exceptions.*; import net.Broken.Tools.UserManager.Exceptions.*;
import net.Broken.Tools.UserManager.Oauth; import net.Broken.Tools.UserManager.Oauth;
import net.Broken.Tools.UserManager.Stats.GuildStatsPack;
import net.Broken.Tools.UserManager.Stats.UserStatsUtils;
import net.Broken.Tools.UserManager.UserUtils; import net.Broken.Tools.UserManager.UserUtils;
import net.dv8tion.jda.core.Permission; import net.dv8tion.jda.core.Permission;
import net.dv8tion.jda.core.entities.Guild; import net.dv8tion.jda.core.entities.Guild;
@ -151,6 +154,27 @@ public class UserManagerAPIController {
} }
} }
@RequestMapping(value = "/getStatsPack", method = RequestMethod.GET)
public ResponseEntity<GuildStatsPack> getStatsPack(@CookieValue(value = "token") String token, @RequestParam(value = "guild") String guildID){
try{
UserEntity user = userUtils.getUserWithApiToken(userRepository, token);
Guild guild = MainBot.jda.getGuildById(guildID);
if(guild == null ){
logger.warn("Request whit no guild!");
return new ResponseEntity(HttpStatus.BAD_REQUEST);
}
return new ResponseEntity<>(UserStatsUtils.getINSTANCE().getStatPack(user, guildID), HttpStatus.OK);
} catch (UnknownTokenException e) {
logger.info("Token check fail");
return new ResponseEntity(HttpStatus.UNAUTHORIZED);
}
}

View File

@ -0,0 +1,34 @@
package net.Broken.Tools.UserManager.Stats;
public class GuildStats{
public String userName;
public long voiceTime;
public long voiceXp;
public long messageCount;
public long messageXp;
public long apiCount;
public long apiXp;
public long total;
public GuildStats() {
}
public GuildStats(String userName, long voiceTime, long messageCount, long apiCount) {
this.userName = userName;
this.voiceTime = voiceTime;
this.messageCount = messageCount;
this.apiCount = apiCount;
voiceXp = (long) (this.voiceTime * UserStatsUtils.XP_PER_VOICE_TIME);
messageXp = (long) (this.messageCount * UserStatsUtils.XP_PER_MESSAGE);
apiXp = (long) (this.apiCount * UserStatsUtils.XP_PER_API_COUNT);
total = voiceXp + messageXp + apiXp;
}
}

View File

@ -0,0 +1,17 @@
package net.Broken.Tools.UserManager.Stats;
import java.util.List;
public class GuildStatsPack {
public int rank;
public GuildStats selfStats;
public List<GuildStats> ranking;
public GuildStatsPack(int rank, GuildStats selfStats, List<GuildStats> ranking) {
this.rank = rank;
this.selfStats = selfStats;
this.ranking = ranking;
}
}

View File

@ -1,4 +1,4 @@
package net.Broken.Tools.UserManager; package net.Broken.Tools.UserManager.Stats;
import net.Broken.DB.Entity.UserEntity; import net.Broken.DB.Entity.UserEntity;
import net.Broken.DB.Entity.UserStats; import net.Broken.DB.Entity.UserStats;
@ -7,6 +7,7 @@ import net.Broken.DB.Repository.UserStatsRepository;
import net.Broken.MainBot; import net.Broken.MainBot;
import net.Broken.SpringContext; import net.Broken.SpringContext;
import net.Broken.Tools.UserManager.Exceptions.UnknownTokenException; import net.Broken.Tools.UserManager.Exceptions.UnknownTokenException;
import net.Broken.Tools.UserManager.UserUtils;
import net.dv8tion.jda.core.entities.Guild; import net.dv8tion.jda.core.entities.Guild;
import net.dv8tion.jda.core.entities.Member; import net.dv8tion.jda.core.entities.Member;
import net.dv8tion.jda.core.entities.User; import net.dv8tion.jda.core.entities.User;
@ -16,10 +17,16 @@ import org.springframework.context.ApplicationContext;
import org.springframework.security.crypto.password.PasswordEncoder; import org.springframework.security.crypto.password.PasswordEncoder;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Comparator;
import java.util.List; import java.util.List;
public class UserStatsUtils { public class UserStatsUtils {
static double XP_PER_VOICE_TIME = 0.1;
static double XP_PER_MESSAGE = 1;
static double XP_PER_API_COUNT = 1;
private static UserStatsUtils INSTANCE = new UserStatsUtils(); private static UserStatsUtils INSTANCE = new UserStatsUtils();
@ -82,7 +89,9 @@ public class UserStatsUtils {
} }
public void addMessageCount(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)
@ -96,7 +105,21 @@ public class UserStatsUtils {
userStatsList = userStatsRepository.findByUserAndGuildId(userEntity, member.getGuild().getId()); userStatsList = userStatsRepository.findByUserAndGuildId(userEntity, member.getGuild().getId());
} }
UserStats userStats = userStatsList.get(0); return userStatsList.get(0);
}
public UserStats getGuildUserStats(UserEntity userEntity, String guildId){
List<UserStats> userStatsList = userStatsRepository.findByUserAndGuildId(userEntity, guildId);
if(userStatsRepository.findByUserAndGuildId(userEntity, guildId).size() == 0){
getUserStats(userEntity);
userStatsList = userStatsRepository.findByUserAndGuildId(userEntity, guildId);
}
return userStatsList.get(0);
}
public void addMessageCount(Member member){
UserStats userStats = getGuildUserStats(member);
userStats.setMessageCount(userStats.getMessageCount() + 1); userStats.setMessageCount(userStats.getMessageCount() + 1);
userStatsRepository.save(userStats); userStatsRepository.save(userStats);
@ -106,12 +129,7 @@ public class UserStatsUtils {
public void addApiCount(UserEntity userEntity, String guildId){ public void addApiCount(UserEntity userEntity, String guildId){
List<UserStats> userStatsList = userStatsRepository.findByUserAndGuildId(userEntity, guildId); UserStats userStats = getGuildUserStats(userEntity, guildId);
if(userStatsRepository.findByUserAndGuildId(userEntity, guildId).size() == 0){
getUserStats(userEntity);
userStatsList = userStatsRepository.findByUserAndGuildId(userEntity, guildId);
}
UserStats userStats = userStatsList.get(0);
userStats.setApiCommandCount(userStats.getApiCommandCount() + 1); userStats.setApiCommandCount(userStats.getApiCommandCount() + 1);
userStatsRepository.save(userStats); userStatsRepository.save(userStats);
@ -120,19 +138,7 @@ public class UserStatsUtils {
private void addVocalCount(Member member){ private void addVocalCount(Member member){
List<UserEntity> userEntityList = userRepository.findByJdaId(member.getUser().getId()); UserStats userStats = getGuildUserStats(member);
UserEntity userEntity;
if( userEntityList.size() == 0)
userEntity = genUserEntity(member.getUser());
else
userEntity = userEntityList.get(0);
List<UserStats> userStatsList = userStatsRepository.findByUserAndGuildId(userEntity, member.getGuild().getId());
if(userStatsList.size() == 0){
getUserStats(userEntity);
userStatsList = userStatsRepository.findByUserAndGuildId(userEntity, member.getGuild().getId());
}
UserStats userStats = userStatsList.get(0);
userStats.setVocalTime(userStats.getVocalTime() + 10); userStats.setVocalTime(userStats.getVocalTime() + 10);
userStatsRepository.save(userStats); userStatsRepository.save(userStats);
@ -149,9 +155,28 @@ public class UserStatsUtils {
public static class VoicePresenceCompter extends Thread{ public GuildStatsPack getStatPack(UserEntity userEntity, String guildId){
UserStats userStats = getGuildUserStats(userEntity, guildId);
GuildStats selfGuildStats = null;
List<UserStats> allStats = userStatsRepository.findByGuildId(guildId);
List<GuildStats> ranked = new ArrayList<>();
for(UserStats stats : allStats){
GuildStats temp = new GuildStats(stats.getUser().getName(), stats.getVocalTime(), stats.getMessageCount(), stats.getApiCommandCount());
if(stats.getUser().getId().equals(userEntity.getId())){
selfGuildStats = temp;
}
ranked.add(temp);
}
ranked.sort((guildStats, t1) -> (int) (guildStats.total - t1.total));
return new GuildStatsPack(ranked.indexOf(selfGuildStats) + 1 , selfGuildStats, ranked);
}
public static class VoicePresenceCounter extends Thread{
private Member member; private Member member;
public VoicePresenceCompter(Member member){ public VoicePresenceCounter(Member member){
this.member = member; this.member = member;
} }
@ -171,4 +196,7 @@ public class UserStatsUtils {
} }
} }
} }
} }