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.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,
"Ø"
));

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

View File

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

View File

@ -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<User> users;
@ManyToMany(fetch = FetchType.EAGER)
@JoinTable(
name = "users_elements",
joinColumns = @JoinColumn(name = "element_id"),
inverseJoinColumns = @JoinColumn(name = "user_id")
)
private final Set<User> users = new HashSet<>();
public Long getId() {
return id;
@ -30,7 +36,7 @@ public class Element implements IEntity {
this.name = name;
}
public List<User> getUsers() {
public Set<User> getUsers() {
return users;
}

View File

@ -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<Element> elements = new HashSet<>();
@ManyToOne(fetch = FetchType.EAGER)
private Role role;
public Element getElement() {
return element;
public Set<Element> 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() {

View File

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