From 5f6384fa321bbc6c1e1eb36dcdbd2b3e92ba9469 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Melaine=20G=C3=A9rard?= Date: Thu, 28 Nov 2024 09:37:50 +0100 Subject: [PATCH] =?UTF-8?q?:sparkles:=20Fix=20=C3=A9l=C3=A9ments?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../personnage/FichePersoCommand.java | 3 ++- .../listeners/GuildMemberJoinListener.java | 3 ++- .../gachamelia/listeners/ReadyListener.java | 4 ++-- .../studio/gachamelia/models/Element.java | 14 ++++++++++---- .../studio/gachamelia/models/User.java | 19 +++++++++++++------ .../gachamelia/services/UserService.java | 5 ++--- 6 files changed, 31 insertions(+), 17 deletions(-) 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 9cae1ce..39b9065 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 @@ -8,6 +8,7 @@ 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.Element; import org.camelia.studio.gachamelia.models.User; import org.camelia.studio.gachamelia.models.UserStat; import org.camelia.studio.gachamelia.repositories.StatRepository; @@ -69,7 +70,7 @@ public class FichePersoCommand implements ISlashCommand { member.getEffectiveName(), user.getRank().getName(), user.getRole().getName(), - String.join(", ", user.getElement().getName()), + user.getElements().stream().map(Element::getName).reduce("", (a, b) -> a + ", " + b).substring(2), 0, "Ø" )); 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 9b6ac05..0d9eba9 100644 --- a/src/main/java/org/camelia/studio/gachamelia/listeners/GuildMemberJoinListener.java +++ b/src/main/java/org/camelia/studio/gachamelia/listeners/GuildMemberJoinListener.java @@ -7,6 +7,7 @@ import net.dv8tion.jda.api.entities.Role; import net.dv8tion.jda.api.entities.channel.concrete.TextChannel; import net.dv8tion.jda.api.events.guild.member.GuildMemberJoinEvent; import net.dv8tion.jda.api.hooks.ListenerAdapter; +import org.camelia.studio.gachamelia.models.Element; import org.camelia.studio.gachamelia.models.User; import org.camelia.studio.gachamelia.models.WelcomeMessage; import org.camelia.studio.gachamelia.services.RankService; @@ -46,7 +47,7 @@ public class GuildMemberJoinListener extends ListenerAdapter { description.append("\n\n") .append("__Caractéristiques principales__ :\n") .append("• Rôle « ").append(user.getRole().getName()).append(" ».").append("\n") - .append("• Élément « ").append(user.getElement().getName()).append(" ».").append("\n") + .append("• Élément « ").append(user.getElements().stream().map(Element::getName).reduce("", (a, b) -> a + ", " + b).substring(2)).append(" ».").append("\n") ; EmbedBuilder embedBuilder = EmbedUtils.createDefaultEmbed(event.getJDA()) 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 6ceeb5f..8504b58 100644 --- a/src/main/java/org/camelia/studio/gachamelia/listeners/ReadyListener.java +++ b/src/main/java/org/camelia/studio/gachamelia/listeners/ReadyListener.java @@ -67,8 +67,8 @@ public class ReadyListener extends ListenerAdapter { UserService.getInstance().updateUser(user); } - if (user.getElement() == null) { - user.setElement(ElementService.getInstance().getRandomElement()); + if (user.getElements().isEmpty()) { + user.addElement(ElementService.getInstance().getRandomElement()); UserService.getInstance().updateUser(user); } diff --git a/src/main/java/org/camelia/studio/gachamelia/models/Element.java b/src/main/java/org/camelia/studio/gachamelia/models/Element.java index 3a9c774..3ec0e4d 100644 --- a/src/main/java/org/camelia/studio/gachamelia/models/Element.java +++ b/src/main/java/org/camelia/studio/gachamelia/models/Element.java @@ -3,7 +3,8 @@ package org.camelia.studio.gachamelia.models; import jakarta.persistence.*; import org.camelia.studio.gachamelia.interfaces.IEntity; -import java.util.List; +import java.util.HashSet; +import java.util.Set; @Entity @Table(name = "elements") @@ -15,8 +16,13 @@ public class Element implements IEntity { @Column(nullable = false) private String name; - @OneToMany(mappedBy = "element") - private List users; + @ManyToMany(fetch = FetchType.EAGER) + @JoinTable( + name = "users_elements", + joinColumns = @JoinColumn(name = "element_id"), + inverseJoinColumns = @JoinColumn(name = "user_id") + ) + private final Set users = new HashSet<>(); public Long getId() { return id; @@ -30,7 +36,7 @@ public class Element implements IEntity { this.name = name; } - public List getUsers() { + public Set 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 e54cc8a..5532012 100644 --- a/src/main/java/org/camelia/studio/gachamelia/models/User.java +++ b/src/main/java/org/camelia/studio/gachamelia/models/User.java @@ -6,6 +6,8 @@ import org.hibernate.annotations.CreationTimestamp; import org.hibernate.annotations.UpdateTimestamp; import java.time.LocalDateTime; +import java.util.HashSet; +import java.util.Set; @Entity @Table(name = "users") @@ -28,14 +30,19 @@ public class User implements IEntity { @Column(name = "updatedAt") private LocalDateTime updatedAt; - @ManyToOne(fetch = FetchType.EAGER) - private Element element; + @ManyToMany(fetch = FetchType.EAGER) + @JoinTable( + name = "users_elements", + joinColumns = @JoinColumn(name = "user_id"), + inverseJoinColumns = @JoinColumn(name = "element_id") + ) + private final Set elements = new HashSet<>(); @ManyToOne(fetch = FetchType.EAGER) private Role role; - public Element getElement() { - return element; + public Set getElements() { + return elements; } public Role getRole() { @@ -46,8 +53,8 @@ public class User implements IEntity { this.role = role; } - public void setElement(Element element) { - this.element = element; + public void addElement(Element elements) { + this.elements.add(elements); } public User() { 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 cc6f5db..6363688 100644 --- a/src/main/java/org/camelia/studio/gachamelia/services/UserService.java +++ b/src/main/java/org/camelia/studio/gachamelia/services/UserService.java @@ -28,7 +28,8 @@ public class UserService { user.setRole(role); Element element = ElementService.getInstance().getRandomElement(); - user.setElement(element); + user.addElement(element); + UserRepository.getInstance().save(user); List stats = StatRepository.getInstance().findAll(); for (Stat stat : stats) { @@ -38,8 +39,6 @@ public class UserService { userStat.setValue(0); StatRepository.getInstance().saveUserStat(userStat); } - - UserRepository.getInstance().save(user); } return user;