Fix éléments
Some checks failed
Build and Push Docker Image / build (push) Failing after 3m14s

This commit is contained in:
Melaine Gérard 2024-11-28 09:37:50 +01:00
parent 729d4fc611
commit 5f6384fa32
6 changed files with 31 additions and 17 deletions

View File

@ -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.OptionType;
import net.dv8tion.jda.api.interactions.commands.build.OptionData; import net.dv8tion.jda.api.interactions.commands.build.OptionData;
import org.camelia.studio.gachamelia.interfaces.ISlashCommand; 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.User;
import org.camelia.studio.gachamelia.models.UserStat; import org.camelia.studio.gachamelia.models.UserStat;
import org.camelia.studio.gachamelia.repositories.StatRepository; import org.camelia.studio.gachamelia.repositories.StatRepository;
@ -69,7 +70,7 @@ public class FichePersoCommand implements ISlashCommand {
member.getEffectiveName(), member.getEffectiveName(),
user.getRank().getName(), user.getRank().getName(),
user.getRole().getName(), user.getRole().getName(),
String.join(", ", user.getElement().getName()), user.getElements().stream().map(Element::getName).reduce("", (a, b) -> a + ", " + b).substring(2),
0, 0,
"Ø" "Ø"
)); ));

View File

@ -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.entities.channel.concrete.TextChannel;
import net.dv8tion.jda.api.events.guild.member.GuildMemberJoinEvent; import net.dv8tion.jda.api.events.guild.member.GuildMemberJoinEvent;
import net.dv8tion.jda.api.hooks.ListenerAdapter; 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.User;
import org.camelia.studio.gachamelia.models.WelcomeMessage; import org.camelia.studio.gachamelia.models.WelcomeMessage;
import org.camelia.studio.gachamelia.services.RankService; import org.camelia.studio.gachamelia.services.RankService;
@ -46,7 +47,7 @@ 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(user.getRole().getName()).append(" ».").append("\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()) EmbedBuilder embedBuilder = EmbedUtils.createDefaultEmbed(event.getJDA())

View File

@ -67,8 +67,8 @@ public class ReadyListener extends ListenerAdapter {
UserService.getInstance().updateUser(user); UserService.getInstance().updateUser(user);
} }
if (user.getElement() == null) { if (user.getElements().isEmpty()) {
user.setElement(ElementService.getInstance().getRandomElement()); user.addElement(ElementService.getInstance().getRandomElement());
UserService.getInstance().updateUser(user); UserService.getInstance().updateUser(user);
} }

View File

@ -3,7 +3,8 @@ 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 java.util.List; import java.util.HashSet;
import java.util.Set;
@Entity @Entity
@Table(name = "elements") @Table(name = "elements")
@ -15,8 +16,13 @@ public class Element implements IEntity {
@Column(nullable = false) @Column(nullable = false)
private String name; private String name;
@OneToMany(mappedBy = "element") @ManyToMany(fetch = FetchType.EAGER)
private List<User> users; @JoinTable(
name = "users_elements",
joinColumns = @JoinColumn(name = "element_id"),
inverseJoinColumns = @JoinColumn(name = "user_id")
)
private final Set<User> users = new HashSet<>();
public Long getId() { public Long getId() {
return id; return id;
@ -30,7 +36,7 @@ public class Element implements IEntity {
this.name = name; this.name = name;
} }
public List<User> getUsers() { public Set<User> getUsers() {
return users; return users;
} }

View File

@ -6,6 +6,8 @@ import org.hibernate.annotations.CreationTimestamp;
import org.hibernate.annotations.UpdateTimestamp; import org.hibernate.annotations.UpdateTimestamp;
import java.time.LocalDateTime; import java.time.LocalDateTime;
import java.util.HashSet;
import java.util.Set;
@Entity @Entity
@Table(name = "users") @Table(name = "users")
@ -28,14 +30,19 @@ public class User implements IEntity {
@Column(name = "updatedAt") @Column(name = "updatedAt")
private LocalDateTime updatedAt; private LocalDateTime updatedAt;
@ManyToOne(fetch = FetchType.EAGER) @ManyToMany(fetch = FetchType.EAGER)
private Element element; @JoinTable(
name = "users_elements",
joinColumns = @JoinColumn(name = "user_id"),
inverseJoinColumns = @JoinColumn(name = "element_id")
)
private final Set<Element> elements = new HashSet<>();
@ManyToOne(fetch = FetchType.EAGER) @ManyToOne(fetch = FetchType.EAGER)
private Role role; private Role role;
public Element getElement() { public Set<Element> getElements() {
return element; return elements;
} }
public Role getRole() { public Role getRole() {
@ -46,8 +53,8 @@ public class User implements IEntity {
this.role = role; this.role = role;
} }
public void setElement(Element element) { public void addElement(Element elements) {
this.element = element; this.elements.add(elements);
} }
public User() { public User() {

View File

@ -28,7 +28,8 @@ public class UserService {
user.setRole(role); user.setRole(role);
Element element = ElementService.getInstance().getRandomElement(); Element element = ElementService.getInstance().getRandomElement();
user.setElement(element); user.addElement(element);
UserRepository.getInstance().save(user);
List<Stat> stats = StatRepository.getInstance().findAll(); List<Stat> stats = StatRepository.getInstance().findAll();
for (Stat stat : stats) { for (Stat stat : stats) {
@ -38,8 +39,6 @@ public class UserService {
userStat.setValue(0); userStat.setValue(0);
StatRepository.getInstance().saveUserStat(userStat); StatRepository.getInstance().saveUserStat(userStat);
} }
UserRepository.getInstance().save(user);
} }
return user; return user;