Ajout commande Fiche de personnage

This commit is contained in:
Melaine Gérard 2024-11-24 18:35:52 +01:00
parent 4abbb8b0aa
commit 16bb80ed7a
13 changed files with 278 additions and 12 deletions

View File

@ -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<OptionData> 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();
}
}

View File

@ -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()

View File

@ -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);
}
}

View File

@ -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<User> users;
public String getImageUrl() {
return imageUrl;
}
public void setImageUrl(String imageUrl) {
this.imageUrl = imageUrl;
}
public List<User> getUsers() {
return users;
}

View File

@ -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() {
}

View File

@ -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<Element> findAll() {
try (Session session = sessionFactory.openSession()) {
return session.createQuery("FROM Element", Element.class).list();
}
}
}

View File

@ -1,4 +1,4 @@
package org.camelia.studio.gachamelia.repossitories;
package org.camelia.studio.gachamelia.repositories;
import org.camelia.studio.gachamelia.db.HibernateConfig;

View File

@ -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<Role> findAll() {
try (Session session = sessionFactory.openSession()) {
return session.createQuery("FROM Role", Role.class).list();
}
}
}

View File

@ -1,4 +1,4 @@
package org.camelia.studio.gachamelia.repossitories;
package org.camelia.studio.gachamelia.repositories;
import org.camelia.studio.gachamelia.db.HibernateConfig;

View File

@ -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<Element> 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;
}
}

View File

@ -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;

View File

@ -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<Role> 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;
}
}

View File

@ -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);
}