✨ Ajout stockage des stats de l'utilisateur
This commit is contained in:
parent
ba23aea0dc
commit
f323417322
@ -9,6 +9,8 @@ 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.User;
|
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 org.camelia.studio.gachamelia.services.UserService;
|
||||||
|
|
||||||
import java.awt.*;
|
import java.awt.*;
|
||||||
@ -51,12 +53,9 @@ public class FichePersoCommand implements ISlashCommand {
|
|||||||
User user = UserService.getInstance().getOrCreateUser(member.getId());
|
User user = UserService.getInstance().getOrCreateUser(member.getId());
|
||||||
Role role = event.getGuild().getRoleById(user.getRank().getDiscordId());
|
Role role = event.getGuild().getRoleById(user.getRank().getDiscordId());
|
||||||
Color color = role != null ? role.getColor() : Color.WHITE;
|
Color color = role != null ? role.getColor() : Color.WHITE;
|
||||||
|
List<UserStat> stats = StatRepository.getInstance().getUserStats(user);
|
||||||
|
|
||||||
embedGeneralite.setAuthor(member.getEffectiveName(), null, user.getRole().getImageUrl());
|
StringBuilder description = new StringBuilder("""
|
||||||
embedGeneralite.setTitle("Fiche de personnage");
|
|
||||||
embedGeneralite.setColor(color);
|
|
||||||
embedGeneralite.setThumbnail(member.getUser().getEffectiveAvatarUrl());
|
|
||||||
embedGeneralite.setDescription("""
|
|
||||||
__Caractéristiques principales__ :
|
__Caractéristiques principales__ :
|
||||||
- Nom : **%s**
|
- Nom : **%s**
|
||||||
- Rareté : **%s**
|
- Rareté : **%s**
|
||||||
@ -65,27 +64,27 @@ public class FichePersoCommand implements ISlashCommand {
|
|||||||
- Xp : **%d**
|
- Xp : **%d**
|
||||||
- Emblème : **%s**
|
- Emblème : **%s**
|
||||||
__Statistiques de combat__ :
|
__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(
|
""".formatted(
|
||||||
member.getEffectiveName(),
|
member.getEffectiveName(),
|
||||||
user.getRank().getName(),
|
user.getRank().getName(),
|
||||||
user.getRole().getName(),
|
user.getRole().getName(),
|
||||||
String.join(", ", user.getElement().getName()),
|
String.join(", ", user.getElement().getName()),
|
||||||
0,
|
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(
|
event.getChannel().sendMessageEmbeds(List.of(
|
||||||
embedGeneralite.build()
|
embedGeneralite.build()
|
||||||
)).queue();
|
)).queue();
|
||||||
|
@ -6,10 +6,13 @@ 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 org.camelia.studio.gachamelia.models.Stat;
|
||||||
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.repositories.ElementRepository;
|
import org.camelia.studio.gachamelia.repositories.ElementRepository;
|
||||||
import org.camelia.studio.gachamelia.repositories.RankRepository;
|
import org.camelia.studio.gachamelia.repositories.RankRepository;
|
||||||
import org.camelia.studio.gachamelia.repositories.RoleRepository;
|
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.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.RoleService;
|
||||||
@ -18,6 +21,8 @@ import org.camelia.studio.gachamelia.utils.Configuration;
|
|||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
public class ReadyListener extends ListenerAdapter {
|
public class ReadyListener extends ListenerAdapter {
|
||||||
private static final Logger logger = LoggerFactory.getLogger(ReadyListener.class);
|
private static final Logger logger = LoggerFactory.getLogger(ReadyListener.class);
|
||||||
|
|
||||||
@ -29,7 +34,7 @@ public class ReadyListener extends ListenerAdapter {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void initDatabase(JDA jda) {
|
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"));
|
Guild guild = jda.getGuildById(Configuration.getInstance().getDotenv().get("GUILD_ID"));
|
||||||
if (guild != null) {
|
if (guild != null) {
|
||||||
guild.loadMembers().onSuccess(members -> {
|
guild.loadMembers().onSuccess(members -> {
|
||||||
@ -51,6 +56,19 @@ public class ReadyListener extends ListenerAdapter {
|
|||||||
UserService.getInstance().updateUser(user);
|
UserService.getInstance().updateUser(user);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
List<UserStat> stats = StatRepository.getInstance().getUserStats(user);
|
||||||
|
|
||||||
|
if (stats.isEmpty()) {
|
||||||
|
List<Stat> 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());
|
logger.info("Utilisateur {} initialisé", member.getUser().getAsTag());
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
@ -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);
|
||||||
|
}
|
||||||
|
}
|
@ -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);
|
||||||
|
}
|
||||||
|
}
|
@ -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<Stat> findAll() {
|
||||||
|
try (Session session = sessionFactory.openSession()) {
|
||||||
|
return session.createQuery("FROM Stat", Stat.class).list();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<UserStat> 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();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -1,9 +1,7 @@
|
|||||||
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.*;
|
||||||
import org.camelia.studio.gachamelia.models.Rank;
|
import org.camelia.studio.gachamelia.repositories.StatRepository;
|
||||||
import org.camelia.studio.gachamelia.models.Role;
|
|
||||||
import org.camelia.studio.gachamelia.models.User;
|
|
||||||
import org.camelia.studio.gachamelia.repositories.UserRepository;
|
import org.camelia.studio.gachamelia.repositories.UserRepository;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
@ -32,6 +30,15 @@ public class UserService {
|
|||||||
Element element = ElementService.getInstance().getRandomElement();
|
Element element = ElementService.getInstance().getRandomElement();
|
||||||
user.setElement(element);
|
user.setElement(element);
|
||||||
|
|
||||||
|
List<Stat> 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);
|
UserRepository.getInstance().save(user);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user