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 index 1b997a7..57b7348 100644 --- a/src/main/java/org/camelia/studio/gachamelia/commands/personnage/FichePersoCommand.java +++ b/src/main/java/org/camelia/studio/gachamelia/commands/personnage/FichePersoCommand.java @@ -9,6 +9,8 @@ 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.models.UserStat; +import org.camelia.studio.gachamelia.repositories.StatRepository; import org.camelia.studio.gachamelia.services.UserService; import java.awt.*; @@ -51,12 +53,9 @@ public class FichePersoCommand implements ISlashCommand { User user = UserService.getInstance().getOrCreateUser(member.getId()); Role role = event.getGuild().getRoleById(user.getRank().getDiscordId()); Color color = role != null ? role.getColor() : Color.WHITE; + List stats = StatRepository.getInstance().getUserStats(user); - embedGeneralite.setAuthor(member.getEffectiveName(), null, user.getRole().getImageUrl()); - embedGeneralite.setTitle("Fiche de personnage"); - embedGeneralite.setColor(color); - embedGeneralite.setThumbnail(member.getUser().getEffectiveAvatarUrl()); - embedGeneralite.setDescription(""" + StringBuilder description = new StringBuilder(""" __Caractéristiques principales__ : - Nom : **%s** - Rareté : **%s** @@ -65,27 +64,27 @@ public class FichePersoCommand implements ISlashCommand { - 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 + "Ø" )); + for (UserStat stat : stats) { + int userStat = stat.getValue(); + int equipmentStat = 0; + description.append("- %s : **%d** (%d + %d)\n".formatted(stat.getStat().getName(), userStat + equipmentStat, userStat, equipmentStat)); + } + + embedGeneralite.setAuthor(member.getEffectiveName(), null, user.getRole().getImageUrl()); + embedGeneralite.setTitle("Fiche de personnage"); + embedGeneralite.setColor(color); + embedGeneralite.setThumbnail(member.getUser().getEffectiveAvatarUrl()); + embedGeneralite.setDescription(description.toString()); + event.getChannel().sendMessageEmbeds(List.of( embedGeneralite.build() )).queue(); 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 5c79dc9..8f43818 100644 --- a/src/main/java/org/camelia/studio/gachamelia/listeners/ReadyListener.java +++ b/src/main/java/org/camelia/studio/gachamelia/listeners/ReadyListener.java @@ -6,10 +6,13 @@ 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 org.camelia.studio.gachamelia.models.Stat; import org.camelia.studio.gachamelia.models.User; +import org.camelia.studio.gachamelia.models.UserStat; 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.repositories.StatRepository; import org.camelia.studio.gachamelia.services.ElementService; import org.camelia.studio.gachamelia.services.RankService; import org.camelia.studio.gachamelia.services.RoleService; @@ -18,6 +21,8 @@ import org.camelia.studio.gachamelia.utils.Configuration; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import java.util.List; + public class ReadyListener extends ListenerAdapter { private static final Logger logger = LoggerFactory.getLogger(ReadyListener.class); @@ -29,7 +34,7 @@ public class ReadyListener extends ListenerAdapter { } private void initDatabase(JDA jda) { - if (!RankRepository.getInstance().findAll().isEmpty() || !RoleRepository.getInstance().findAll().isEmpty() || !ElementRepository.getInstance().findAll().isEmpty()) { + if (!RankRepository.getInstance().findAll().isEmpty() || !RoleRepository.getInstance().findAll().isEmpty() || !ElementRepository.getInstance().findAll().isEmpty() || !StatRepository.getInstance().findAll().isEmpty()) { Guild guild = jda.getGuildById(Configuration.getInstance().getDotenv().get("GUILD_ID")); if (guild != null) { guild.loadMembers().onSuccess(members -> { @@ -51,6 +56,19 @@ public class ReadyListener extends ListenerAdapter { UserService.getInstance().updateUser(user); } + List stats = StatRepository.getInstance().getUserStats(user); + + if (stats.isEmpty()) { + List statsList = StatRepository.getInstance().findAll(); + for (Stat stat : statsList) { + UserStat userStat = new UserStat(); + userStat.setUser(user); + userStat.setStat(stat); + userStat.setValue(0); + StatRepository.getInstance().saveUserStat(userStat); + } + } + logger.info("Utilisateur {} initialisé", member.getUser().getAsTag()); } }); diff --git a/src/main/java/org/camelia/studio/gachamelia/models/UserStat.java b/src/main/java/org/camelia/studio/gachamelia/models/UserStat.java new file mode 100644 index 0000000..317e956 --- /dev/null +++ b/src/main/java/org/camelia/studio/gachamelia/models/UserStat.java @@ -0,0 +1,73 @@ +package org.camelia.studio.gachamelia.models; + +import jakarta.persistence.Column; +import jakarta.persistence.EmbeddedId; +import jakarta.persistence.Entity; +import jakarta.persistence.Table; +import org.camelia.studio.gachamelia.interfaces.IEntity; + +import java.util.Objects; + +@Entity +@Table(name = "user_stats") +public class UserStat implements IEntity { + @EmbeddedId + private UserStatId id; + + @Column(nullable = false) + private int value; + + public UserStat() { + } + + public UserStat(User user, Stat stat, int value) { + this.id = new UserStatId(user, stat); + this.value = value; + } + + public UserStatId getId() { + return id; + } + + public User getUser() { + return id.getUser(); + } + + public void setUser(User user) { + if (this.id == null) { + this.id = new UserStatId(); + } + this.id.setUser(user); + } + + public Stat getStat() { + return id.getStat(); + } + + public void setStat(Stat stat) { + if (this.id == null) { + this.id = new UserStatId(); + } + this.id.setStat(stat); + } + + public int getValue() { + return value; + } + + public void setValue(int value) { + this.value = value; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (!(o instanceof UserStat userStat)) return false; + return Objects.equals(id, userStat.id); + } + + @Override + public int hashCode() { + return Objects.hash(id); + } +} diff --git a/src/main/java/org/camelia/studio/gachamelia/models/UserStatId.java b/src/main/java/org/camelia/studio/gachamelia/models/UserStatId.java new file mode 100644 index 0000000..362dc29 --- /dev/null +++ b/src/main/java/org/camelia/studio/gachamelia/models/UserStatId.java @@ -0,0 +1,54 @@ +package org.camelia.studio.gachamelia.models; + +import jakarta.persistence.FetchType; +import jakarta.persistence.JoinColumn; +import jakarta.persistence.ManyToOne; + +import java.io.Serializable; +import java.util.Objects; + +public class UserStatId implements Serializable { + @ManyToOne(fetch = FetchType.EAGER) + @JoinColumn(name = "stat_id", nullable = false) + private Stat stat; + + @ManyToOne(fetch = FetchType.EAGER) + @JoinColumn(name = "user_id", nullable = false) + private User user; + + public UserStatId() { + } + + public UserStatId(User user, Stat stat) { + this.stat = stat; + this.user = user; + } + + public Stat getStat() { + return stat; + } + + public void setStat(Stat stat) { + this.stat = stat; + } + + public User getUser() { + return user; + } + + public void setUser(User user) { + this.user = user; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (!(o instanceof UserStatId that)) return false; + return stat.equals(that.stat) && user.equals(that.user); + } + + @Override + public int hashCode() { + return Objects.hash(stat, user); + } +} diff --git a/src/main/java/org/camelia/studio/gachamelia/repositories/StatRepository.java b/src/main/java/org/camelia/studio/gachamelia/repositories/StatRepository.java new file mode 100644 index 0000000..2b4a15d --- /dev/null +++ b/src/main/java/org/camelia/studio/gachamelia/repositories/StatRepository.java @@ -0,0 +1,50 @@ +package org.camelia.studio.gachamelia.repositories; + +import org.camelia.studio.gachamelia.db.HibernateConfig; +import org.camelia.studio.gachamelia.models.Stat; +import org.camelia.studio.gachamelia.models.User; +import org.camelia.studio.gachamelia.models.UserStat; +import org.hibernate.Session; +import org.hibernate.SessionFactory; + +import java.util.List; + +public class StatRepository { + private static StatRepository instance; + private final SessionFactory sessionFactory; + + public StatRepository() { + this.sessionFactory = HibernateConfig.getSessionFactory(); + } + + public static StatRepository getInstance() { + if (instance == null) { + instance = new StatRepository(); + } + + return instance; + } + + public List findAll() { + try (Session session = sessionFactory.openSession()) { + return session.createQuery("FROM Stat", Stat.class).list(); + } + } + + public List getUserStats(User user) { + try (Session session = sessionFactory.openSession()) { + + return session.createQuery("FROM UserStat us WHERE us.id.user.id = :userId", UserStat.class) + .setParameter("userId", user.getId()) + .getResultList(); + } + } + + public void saveUserStat(UserStat userStat) { + try (Session session = sessionFactory.openSession()) { + session.beginTransaction(); + session.persist(userStat); + session.getTransaction().commit(); + } + } +} 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 0bbf218..cc6f5db 100644 --- a/src/main/java/org/camelia/studio/gachamelia/services/UserService.java +++ b/src/main/java/org/camelia/studio/gachamelia/services/UserService.java @@ -1,9 +1,7 @@ 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.models.*; +import org.camelia.studio.gachamelia.repositories.StatRepository; import org.camelia.studio.gachamelia.repositories.UserRepository; import java.util.List; @@ -32,6 +30,15 @@ public class UserService { Element element = ElementService.getInstance().getRandomElement(); user.setElement(element); + List stats = StatRepository.getInstance().findAll(); + for (Stat stat : stats) { + UserStat userStat = new UserStat(); + userStat.setUser(user); + userStat.setStat(stat); + userStat.setValue(0); + StatRepository.getInstance().saveUserStat(userStat); + } + UserRepository.getInstance().save(user); }