From 16bb80ed7a88a5e27380d0d5740e9f2671bf924b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Melaine=20G=C3=A9rard?= Date: Sun, 24 Nov 2024 18:35:52 +0100 Subject: [PATCH] :sparkles: Ajout commande Fiche de personnage --- .../personnage/FichePersoCommand.java | 88 +++++++++++++++++++ .../listeners/GuildMemberJoinListener.java | 8 +- .../gachamelia/listeners/ReadyListener.java | 22 ++++- .../studio/gachamelia/models/Role.java | 13 +++ .../studio/gachamelia/models/User.java | 8 ++ .../repositories/ElementRepository.java | 31 +++++++ .../RankRepository.java | 2 +- .../repositories/RoleRepository.java | 31 +++++++ .../UserRepository.java | 2 +- .../gachamelia/services/ElementService.java | 36 ++++++++ .../gachamelia/services/RankService.java | 2 +- .../gachamelia/services/RoleService.java | 36 ++++++++ .../gachamelia/services/UserService.java | 11 ++- 13 files changed, 278 insertions(+), 12 deletions(-) create mode 100644 src/main/java/org/camelia/studio/gachamelia/commands/personnage/FichePersoCommand.java create mode 100644 src/main/java/org/camelia/studio/gachamelia/repositories/ElementRepository.java rename src/main/java/org/camelia/studio/gachamelia/{repossitories => repositories}/RankRepository.java (98%) create mode 100644 src/main/java/org/camelia/studio/gachamelia/repositories/RoleRepository.java rename src/main/java/org/camelia/studio/gachamelia/{repossitories => repositories}/UserRepository.java (96%) create mode 100644 src/main/java/org/camelia/studio/gachamelia/services/ElementService.java create mode 100644 src/main/java/org/camelia/studio/gachamelia/services/RoleService.java diff --git a/src/main/java/org/camelia/studio/gachamelia/commands/personnage/FichePersoCommand.java b/src/main/java/org/camelia/studio/gachamelia/commands/personnage/FichePersoCommand.java new file mode 100644 index 0000000..fa283ae --- /dev/null +++ b/src/main/java/org/camelia/studio/gachamelia/commands/personnage/FichePersoCommand.java @@ -0,0 +1,88 @@ +package org.camelia.studio.gachamelia.commands.personnage; + +import net.dv8tion.jda.api.EmbedBuilder; +import net.dv8tion.jda.api.entities.Member; +import net.dv8tion.jda.api.entities.Role; +import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent; +import net.dv8tion.jda.api.interactions.commands.OptionMapping; +import net.dv8tion.jda.api.interactions.commands.OptionType; +import net.dv8tion.jda.api.interactions.commands.build.OptionData; +import org.camelia.studio.gachamelia.interfaces.ISlashCommand; +import org.camelia.studio.gachamelia.models.User; +import org.camelia.studio.gachamelia.services.UserService; + +import java.awt.*; +import java.util.List; + +public class FichePersoCommand implements ISlashCommand { + @Override + public String getName() { + return "ficheperso"; + } + + @Override + public String getDescription() { + return "Permet d'afficher la fiche de personnage de l'utilisateur"; + } + + @Override + public List getOptions() { + return List.of( + new OptionData(OptionType.USER, "utilisateur", "L'utilisateur dont vous souhaitez afficher la fiche de personnage").setRequired(false) + ); + } + + @Override + public void execute(SlashCommandInteractionEvent event) { + OptionMapping utilisateur = event.getOption("utilisateur"); + Member member = utilisateur != null ? utilisateur.getAsMember() : event.getMember(); + + if (member == null) { + event.getHook().editOriginal("L'utilisateur n'a pas été trouvé").queue(); + return; + } + EmbedBuilder embedGeneralite = new EmbedBuilder(); + User user = UserService.getInstance().getOrCreateUser(member.getId()); + Role role = event.getGuild().getRoleById(user.getRank().getDiscordId()); + Color color = role != null ? role.getColor() : Color.WHITE; + + embedGeneralite.setAuthor(member.getEffectiveName(), null, user.getRole().getImageUrl()); + embedGeneralite.setTitle("Fiche de personnage"); + embedGeneralite.setColor(color); + embedGeneralite.setDescription(""" + __Caractéristiques principales__ : + - Nom : **%s** + - Rareté : **%s** + - Rôle : **%s** + - Éléments : *%s* + - Xp : **%d** + - Emblème : **%s** + __Statistiques de combat__ : + - Éther : **%d** (%d + 0) + - Astral : **%d** (%d + 0) + - Impact : **%d** (%d + 0) + - Aura : **%d** (%d + 0) + - Égide : **%d** (%d + 0) + - Oracle : **%d** (%d + 0) + """.formatted( + member.getEffectiveName(), + user.getRank().getName(), + user.getRole().getName(), + String.join(", ", user.getElement().getName()), + 0, + "Ø", + 0, 0, + 0, 0, + 0, 0, + 0, 0, + 0, 0, + 0, 0 + )); + + event.getChannel().sendMessageEmbeds(List.of( + embedGeneralite.build() + )).queue(); + + event.getHook().editOriginal("Fiche de personnage de %s".formatted(member.getEffectiveName())).queue(); + } +} diff --git a/src/main/java/org/camelia/studio/gachamelia/listeners/GuildMemberJoinListener.java b/src/main/java/org/camelia/studio/gachamelia/listeners/GuildMemberJoinListener.java index 403e8c3..16f8dbd 100644 --- a/src/main/java/org/camelia/studio/gachamelia/listeners/GuildMemberJoinListener.java +++ b/src/main/java/org/camelia/studio/gachamelia/listeners/GuildMemberJoinListener.java @@ -1,5 +1,6 @@ package org.camelia.studio.gachamelia.listeners; +import jakarta.annotation.Nonnull; import net.dv8tion.jda.api.EmbedBuilder; import net.dv8tion.jda.api.entities.Member; import net.dv8tion.jda.api.entities.Role; @@ -11,8 +12,8 @@ import org.camelia.studio.gachamelia.models.WelcomeMessage; import org.camelia.studio.gachamelia.services.RankService; import org.camelia.studio.gachamelia.services.UserService; import org.camelia.studio.gachamelia.utils.Configuration; + import java.awt.*; -import jakarta.annotation.Nonnull; public class GuildMemberJoinListener extends ListenerAdapter { @@ -25,7 +26,6 @@ public class GuildMemberJoinListener extends ListenerAdapter { TextChannel channel = event.getGuild().getTextChannelById(Configuration.getInstance().getDotenv().get("WELCOME_CHANNEL", "0")); - Role role = event.getGuild().getRoleById(user.getRank().getDiscordId()); Color color = new Color(0, 0, 0); @@ -44,8 +44,8 @@ public class GuildMemberJoinListener extends ListenerAdapter { description.append("\n\n") .append("__Caractéristiques principales__ :\n") - .append("• Rôle « ").append("N/A").append(" ».").append("\n") - .append("• Élément « ").append("N/A").append(" ».").append("\n") + .append("• Rôle « ").append(user.getRole().getName()).append(" ».").append("\n") + .append("• Élément « ").append(user.getElement().getName()).append(" ».").append("\n") ; EmbedBuilder embedBuilder = new EmbedBuilder() diff --git a/src/main/java/org/camelia/studio/gachamelia/listeners/ReadyListener.java b/src/main/java/org/camelia/studio/gachamelia/listeners/ReadyListener.java index 3aaec57..5c79dc9 100644 --- a/src/main/java/org/camelia/studio/gachamelia/listeners/ReadyListener.java +++ b/src/main/java/org/camelia/studio/gachamelia/listeners/ReadyListener.java @@ -1,14 +1,18 @@ package org.camelia.studio.gachamelia.listeners; +import jakarta.annotation.Nonnull; import net.dv8tion.jda.api.JDA; import net.dv8tion.jda.api.entities.Guild; import net.dv8tion.jda.api.entities.Member; import net.dv8tion.jda.api.events.session.ReadyEvent; import net.dv8tion.jda.api.hooks.ListenerAdapter; -import jakarta.annotation.Nonnull; import org.camelia.studio.gachamelia.models.User; -import org.camelia.studio.gachamelia.repossitories.RankRepository; +import org.camelia.studio.gachamelia.repositories.ElementRepository; +import org.camelia.studio.gachamelia.repositories.RankRepository; +import org.camelia.studio.gachamelia.repositories.RoleRepository; +import org.camelia.studio.gachamelia.services.ElementService; import org.camelia.studio.gachamelia.services.RankService; +import org.camelia.studio.gachamelia.services.RoleService; import org.camelia.studio.gachamelia.services.UserService; import org.camelia.studio.gachamelia.utils.Configuration; import org.slf4j.Logger; @@ -25,7 +29,7 @@ public class ReadyListener extends ListenerAdapter { } private void initDatabase(JDA jda) { - if (!RankRepository.getInstance().findAll().isEmpty()) { + if (!RankRepository.getInstance().findAll().isEmpty() || !RoleRepository.getInstance().findAll().isEmpty() || !ElementRepository.getInstance().findAll().isEmpty()) { Guild guild = jda.getGuildById(Configuration.getInstance().getDotenv().get("GUILD_ID")); if (guild != null) { guild.loadMembers().onSuccess(members -> { @@ -37,12 +41,22 @@ public class ReadyListener extends ListenerAdapter { UserService.getInstance().updateUser(user); } + if (user.getRole() == null) { + user.setRole(RoleService.getInstance().getRandomRole()); + UserService.getInstance().updateUser(user); + } + + if (user.getElement() == null) { + user.setElement(ElementService.getInstance().getRandomElement()); + UserService.getInstance().updateUser(user); + } + logger.info("Utilisateur {} initialisé", member.getUser().getAsTag()); } }); } } else { - logger.error("Aucun rang n'a été trouvé dans la base de données"); + logger.error("Aucun rang ou rôle n'a été trouvé dans la base de données"); System.exit(1); } } diff --git a/src/main/java/org/camelia/studio/gachamelia/models/Role.java b/src/main/java/org/camelia/studio/gachamelia/models/Role.java index 263fa87..adcd315 100644 --- a/src/main/java/org/camelia/studio/gachamelia/models/Role.java +++ b/src/main/java/org/camelia/studio/gachamelia/models/Role.java @@ -2,6 +2,7 @@ package org.camelia.studio.gachamelia.models; import jakarta.persistence.*; import org.camelia.studio.gachamelia.interfaces.IEntity; +import org.hibernate.annotations.ColumnDefault; import java.util.List; @@ -18,9 +19,21 @@ public class Role implements IEntity { @Column(nullable = false) private int percentage; + @Column(nullable = false, name = "image_url") + @ColumnDefault(value = "'https://placehold.co/400'") + private String imageUrl; + @OneToMany(mappedBy = "role") private List users; + public String getImageUrl() { + return imageUrl; + } + + public void setImageUrl(String imageUrl) { + this.imageUrl = imageUrl; + } + public List getUsers() { return users; } diff --git a/src/main/java/org/camelia/studio/gachamelia/models/User.java b/src/main/java/org/camelia/studio/gachamelia/models/User.java index 2251486..e54cc8a 100644 --- a/src/main/java/org/camelia/studio/gachamelia/models/User.java +++ b/src/main/java/org/camelia/studio/gachamelia/models/User.java @@ -42,6 +42,14 @@ public class User implements IEntity { return role; } + public void setRole(Role role) { + this.role = role; + } + + public void setElement(Element element) { + this.element = element; + } + public User() { } diff --git a/src/main/java/org/camelia/studio/gachamelia/repositories/ElementRepository.java b/src/main/java/org/camelia/studio/gachamelia/repositories/ElementRepository.java new file mode 100644 index 0000000..560fcc0 --- /dev/null +++ b/src/main/java/org/camelia/studio/gachamelia/repositories/ElementRepository.java @@ -0,0 +1,31 @@ +package org.camelia.studio.gachamelia.repositories; + +import org.camelia.studio.gachamelia.db.HibernateConfig; +import org.camelia.studio.gachamelia.models.Element; +import org.hibernate.Session; +import org.hibernate.SessionFactory; + +import java.util.List; + +public class ElementRepository { + private static ElementRepository instance; + private final SessionFactory sessionFactory; + + public ElementRepository() { + this.sessionFactory = HibernateConfig.getSessionFactory(); + } + + public static ElementRepository getInstance() { + if (instance == null) { + instance = new ElementRepository(); + } + + return instance; + } + + public List findAll() { + try (Session session = sessionFactory.openSession()) { + return session.createQuery("FROM Element", Element.class).list(); + } + } +} diff --git a/src/main/java/org/camelia/studio/gachamelia/repossitories/RankRepository.java b/src/main/java/org/camelia/studio/gachamelia/repositories/RankRepository.java similarity index 98% rename from src/main/java/org/camelia/studio/gachamelia/repossitories/RankRepository.java rename to src/main/java/org/camelia/studio/gachamelia/repositories/RankRepository.java index 6533311..487695e 100644 --- a/src/main/java/org/camelia/studio/gachamelia/repossitories/RankRepository.java +++ b/src/main/java/org/camelia/studio/gachamelia/repositories/RankRepository.java @@ -1,4 +1,4 @@ -package org.camelia.studio.gachamelia.repossitories; +package org.camelia.studio.gachamelia.repositories; import org.camelia.studio.gachamelia.db.HibernateConfig; diff --git a/src/main/java/org/camelia/studio/gachamelia/repositories/RoleRepository.java b/src/main/java/org/camelia/studio/gachamelia/repositories/RoleRepository.java new file mode 100644 index 0000000..70b46da --- /dev/null +++ b/src/main/java/org/camelia/studio/gachamelia/repositories/RoleRepository.java @@ -0,0 +1,31 @@ +package org.camelia.studio.gachamelia.repositories; + +import org.camelia.studio.gachamelia.db.HibernateConfig; +import org.camelia.studio.gachamelia.models.Role; +import org.hibernate.Session; +import org.hibernate.SessionFactory; + +import java.util.List; + +public class RoleRepository { + private static RoleRepository instance; + private final SessionFactory sessionFactory; + + public RoleRepository() { + this.sessionFactory = HibernateConfig.getSessionFactory(); + } + + public static RoleRepository getInstance() { + if (instance == null) { + instance = new RoleRepository(); + } + + return instance; + } + + public List findAll() { + try (Session session = sessionFactory.openSession()) { + return session.createQuery("FROM Role", Role.class).list(); + } + } +} diff --git a/src/main/java/org/camelia/studio/gachamelia/repossitories/UserRepository.java b/src/main/java/org/camelia/studio/gachamelia/repositories/UserRepository.java similarity index 96% rename from src/main/java/org/camelia/studio/gachamelia/repossitories/UserRepository.java rename to src/main/java/org/camelia/studio/gachamelia/repositories/UserRepository.java index 7912bc6..bda2401 100644 --- a/src/main/java/org/camelia/studio/gachamelia/repossitories/UserRepository.java +++ b/src/main/java/org/camelia/studio/gachamelia/repositories/UserRepository.java @@ -1,4 +1,4 @@ -package org.camelia.studio.gachamelia.repossitories; +package org.camelia.studio.gachamelia.repositories; import org.camelia.studio.gachamelia.db.HibernateConfig; diff --git a/src/main/java/org/camelia/studio/gachamelia/services/ElementService.java b/src/main/java/org/camelia/studio/gachamelia/services/ElementService.java new file mode 100644 index 0000000..0e1541b --- /dev/null +++ b/src/main/java/org/camelia/studio/gachamelia/services/ElementService.java @@ -0,0 +1,36 @@ +package org.camelia.studio.gachamelia.services; + +import org.camelia.studio.gachamelia.models.Element; +import org.camelia.studio.gachamelia.repositories.ElementRepository; + +import java.util.List; +import java.util.concurrent.ThreadLocalRandom; + +public class ElementService { + private static ElementService instance; + + public static ElementService getInstance() { + if (instance == null) { + instance = new ElementService(); + } + + return instance; + } + + public Element getRandomElement() { + List elements = ElementRepository.getInstance().findAll(); + + int percentage = ThreadLocalRandom.current().nextInt(elements.size()) + 1; + int cumulativePercentage = 0; + + for (Element element : elements) { + cumulativePercentage++; + if (percentage <= cumulativePercentage) { + return element; + } + } + + // Ne devrait jamais arriver + return null; + } +} diff --git a/src/main/java/org/camelia/studio/gachamelia/services/RankService.java b/src/main/java/org/camelia/studio/gachamelia/services/RankService.java index 8df195d..5199092 100644 --- a/src/main/java/org/camelia/studio/gachamelia/services/RankService.java +++ b/src/main/java/org/camelia/studio/gachamelia/services/RankService.java @@ -3,7 +3,7 @@ package org.camelia.studio.gachamelia.services; import org.camelia.studio.gachamelia.models.ByeMessage; import org.camelia.studio.gachamelia.models.Rank; import org.camelia.studio.gachamelia.models.WelcomeMessage; -import org.camelia.studio.gachamelia.repossitories.RankRepository; +import org.camelia.studio.gachamelia.repositories.RankRepository; import java.util.List; import java.util.concurrent.ThreadLocalRandom; diff --git a/src/main/java/org/camelia/studio/gachamelia/services/RoleService.java b/src/main/java/org/camelia/studio/gachamelia/services/RoleService.java new file mode 100644 index 0000000..42b50f1 --- /dev/null +++ b/src/main/java/org/camelia/studio/gachamelia/services/RoleService.java @@ -0,0 +1,36 @@ +package org.camelia.studio.gachamelia.services; + +import org.camelia.studio.gachamelia.models.Role; +import org.camelia.studio.gachamelia.repositories.RoleRepository; + +import java.util.List; +import java.util.concurrent.ThreadLocalRandom; + +public class RoleService { + private static RoleService instance; + + public static RoleService getInstance() { + if (instance == null) { + instance = new RoleService(); + } + + return instance; + } + + public Role getRandomRole() { + List roles = RoleRepository.getInstance().findAll(); + + int percentage = ThreadLocalRandom.current().nextInt(100); + int cumulativePercentage = 0; + + for (Role role : roles) { + cumulativePercentage += role.getPercentage(); + if (percentage <= cumulativePercentage) { + return role; + } + } + + // Ne devrait jamais arriver + return null; + } +} diff --git a/src/main/java/org/camelia/studio/gachamelia/services/UserService.java b/src/main/java/org/camelia/studio/gachamelia/services/UserService.java index 1c13d66..0bbf218 100644 --- a/src/main/java/org/camelia/studio/gachamelia/services/UserService.java +++ b/src/main/java/org/camelia/studio/gachamelia/services/UserService.java @@ -1,8 +1,10 @@ package org.camelia.studio.gachamelia.services; +import org.camelia.studio.gachamelia.models.Element; import org.camelia.studio.gachamelia.models.Rank; +import org.camelia.studio.gachamelia.models.Role; import org.camelia.studio.gachamelia.models.User; -import org.camelia.studio.gachamelia.repossitories.UserRepository; +import org.camelia.studio.gachamelia.repositories.UserRepository; import java.util.List; @@ -23,6 +25,13 @@ public class UserService { if (user == null) { Rank rank = RankService.getInstance().getRandomRank(); user = new User(discordId, rank); + + Role role = RoleService.getInstance().getRandomRole(); + user.setRole(role); + + Element element = ElementService.getInstance().getRandomElement(); + user.setElement(element); + UserRepository.getInstance().save(user); }