✨ Ajout commande Fiche de personnage
This commit is contained in:
parent
4abbb8b0aa
commit
16bb80ed7a
@ -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();
|
||||||
|
}
|
||||||
|
}
|
@ -1,5 +1,6 @@
|
|||||||
package org.camelia.studio.gachamelia.listeners;
|
package org.camelia.studio.gachamelia.listeners;
|
||||||
|
|
||||||
|
import jakarta.annotation.Nonnull;
|
||||||
import net.dv8tion.jda.api.EmbedBuilder;
|
import net.dv8tion.jda.api.EmbedBuilder;
|
||||||
import net.dv8tion.jda.api.entities.Member;
|
import net.dv8tion.jda.api.entities.Member;
|
||||||
import net.dv8tion.jda.api.entities.Role;
|
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.RankService;
|
||||||
import org.camelia.studio.gachamelia.services.UserService;
|
import org.camelia.studio.gachamelia.services.UserService;
|
||||||
import org.camelia.studio.gachamelia.utils.Configuration;
|
import org.camelia.studio.gachamelia.utils.Configuration;
|
||||||
|
|
||||||
import java.awt.*;
|
import java.awt.*;
|
||||||
import jakarta.annotation.Nonnull;
|
|
||||||
|
|
||||||
|
|
||||||
public class GuildMemberJoinListener extends ListenerAdapter {
|
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"));
|
TextChannel channel = event.getGuild().getTextChannelById(Configuration.getInstance().getDotenv().get("WELCOME_CHANNEL", "0"));
|
||||||
|
|
||||||
|
|
||||||
Role role = event.getGuild().getRoleById(user.getRank().getDiscordId());
|
Role role = event.getGuild().getRoleById(user.getRank().getDiscordId());
|
||||||
Color color = new Color(0, 0, 0);
|
Color color = new Color(0, 0, 0);
|
||||||
|
|
||||||
@ -44,8 +44,8 @@ public class GuildMemberJoinListener extends ListenerAdapter {
|
|||||||
|
|
||||||
description.append("\n\n")
|
description.append("\n\n")
|
||||||
.append("__Caractéristiques principales__ :\n")
|
.append("__Caractéristiques principales__ :\n")
|
||||||
.append("• Rôle « ").append("N/A").append(" ».").append("\n")
|
.append("• Rôle « ").append(user.getRole().getName()).append(" ».").append("\n")
|
||||||
.append("• Élément « ").append("N/A").append(" ».").append("\n")
|
.append("• Élément « ").append(user.getElement().getName()).append(" ».").append("\n")
|
||||||
;
|
;
|
||||||
|
|
||||||
EmbedBuilder embedBuilder = new EmbedBuilder()
|
EmbedBuilder embedBuilder = new EmbedBuilder()
|
||||||
|
@ -1,14 +1,18 @@
|
|||||||
package org.camelia.studio.gachamelia.listeners;
|
package org.camelia.studio.gachamelia.listeners;
|
||||||
|
|
||||||
|
import jakarta.annotation.Nonnull;
|
||||||
import net.dv8tion.jda.api.JDA;
|
import net.dv8tion.jda.api.JDA;
|
||||||
import net.dv8tion.jda.api.entities.Guild;
|
import net.dv8tion.jda.api.entities.Guild;
|
||||||
import net.dv8tion.jda.api.entities.Member;
|
import net.dv8tion.jda.api.entities.Member;
|
||||||
import net.dv8tion.jda.api.events.session.ReadyEvent;
|
import net.dv8tion.jda.api.events.session.ReadyEvent;
|
||||||
import net.dv8tion.jda.api.hooks.ListenerAdapter;
|
import net.dv8tion.jda.api.hooks.ListenerAdapter;
|
||||||
import jakarta.annotation.Nonnull;
|
|
||||||
import org.camelia.studio.gachamelia.models.User;
|
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.RankService;
|
||||||
|
import org.camelia.studio.gachamelia.services.RoleService;
|
||||||
import org.camelia.studio.gachamelia.services.UserService;
|
import org.camelia.studio.gachamelia.services.UserService;
|
||||||
import org.camelia.studio.gachamelia.utils.Configuration;
|
import org.camelia.studio.gachamelia.utils.Configuration;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
@ -25,7 +29,7 @@ public class ReadyListener extends ListenerAdapter {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void initDatabase(JDA jda) {
|
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"));
|
Guild guild = jda.getGuildById(Configuration.getInstance().getDotenv().get("GUILD_ID"));
|
||||||
if (guild != null) {
|
if (guild != null) {
|
||||||
guild.loadMembers().onSuccess(members -> {
|
guild.loadMembers().onSuccess(members -> {
|
||||||
@ -37,12 +41,22 @@ public class ReadyListener extends ListenerAdapter {
|
|||||||
UserService.getInstance().updateUser(user);
|
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());
|
logger.info("Utilisateur {} initialisé", member.getUser().getAsTag());
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
} else {
|
} 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);
|
System.exit(1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -2,6 +2,7 @@ package org.camelia.studio.gachamelia.models;
|
|||||||
|
|
||||||
import jakarta.persistence.*;
|
import jakarta.persistence.*;
|
||||||
import org.camelia.studio.gachamelia.interfaces.IEntity;
|
import org.camelia.studio.gachamelia.interfaces.IEntity;
|
||||||
|
import org.hibernate.annotations.ColumnDefault;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
@ -18,9 +19,21 @@ public class Role implements IEntity {
|
|||||||
@Column(nullable = false)
|
@Column(nullable = false)
|
||||||
private int percentage;
|
private int percentage;
|
||||||
|
|
||||||
|
@Column(nullable = false, name = "image_url")
|
||||||
|
@ColumnDefault(value = "'https://placehold.co/400'")
|
||||||
|
private String imageUrl;
|
||||||
|
|
||||||
@OneToMany(mappedBy = "role")
|
@OneToMany(mappedBy = "role")
|
||||||
private List<User> users;
|
private List<User> users;
|
||||||
|
|
||||||
|
public String getImageUrl() {
|
||||||
|
return imageUrl;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setImageUrl(String imageUrl) {
|
||||||
|
this.imageUrl = imageUrl;
|
||||||
|
}
|
||||||
|
|
||||||
public List<User> getUsers() {
|
public List<User> getUsers() {
|
||||||
return users;
|
return users;
|
||||||
}
|
}
|
||||||
|
@ -42,6 +42,14 @@ public class User implements IEntity {
|
|||||||
return role;
|
return role;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void setRole(Role role) {
|
||||||
|
this.role = role;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setElement(Element element) {
|
||||||
|
this.element = element;
|
||||||
|
}
|
||||||
|
|
||||||
public User() {
|
public User() {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -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();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -1,4 +1,4 @@
|
|||||||
package org.camelia.studio.gachamelia.repossitories;
|
package org.camelia.studio.gachamelia.repositories;
|
||||||
|
|
||||||
|
|
||||||
import org.camelia.studio.gachamelia.db.HibernateConfig;
|
import org.camelia.studio.gachamelia.db.HibernateConfig;
|
@ -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();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -1,4 +1,4 @@
|
|||||||
package org.camelia.studio.gachamelia.repossitories;
|
package org.camelia.studio.gachamelia.repositories;
|
||||||
|
|
||||||
|
|
||||||
import org.camelia.studio.gachamelia.db.HibernateConfig;
|
import org.camelia.studio.gachamelia.db.HibernateConfig;
|
@ -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;
|
||||||
|
}
|
||||||
|
}
|
@ -3,7 +3,7 @@ package org.camelia.studio.gachamelia.services;
|
|||||||
import org.camelia.studio.gachamelia.models.ByeMessage;
|
import org.camelia.studio.gachamelia.models.ByeMessage;
|
||||||
import org.camelia.studio.gachamelia.models.Rank;
|
import org.camelia.studio.gachamelia.models.Rank;
|
||||||
import org.camelia.studio.gachamelia.models.WelcomeMessage;
|
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.List;
|
||||||
import java.util.concurrent.ThreadLocalRandom;
|
import java.util.concurrent.ThreadLocalRandom;
|
||||||
|
@ -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;
|
||||||
|
}
|
||||||
|
}
|
@ -1,8 +1,10 @@
|
|||||||
package org.camelia.studio.gachamelia.services;
|
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.Rank;
|
||||||
|
import org.camelia.studio.gachamelia.models.Role;
|
||||||
import org.camelia.studio.gachamelia.models.User;
|
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;
|
import java.util.List;
|
||||||
|
|
||||||
@ -23,6 +25,13 @@ public class UserService {
|
|||||||
if (user == null) {
|
if (user == null) {
|
||||||
Rank rank = RankService.getInstance().getRandomRank();
|
Rank rank = RankService.getInstance().getRandomRank();
|
||||||
user = new User(discordId, rank);
|
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);
|
UserRepository.getInstance().save(user);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user