2024-06-25 11:55:26 +01:00
|
|
|
package cc.fascinated.bat.model;
|
2024-06-24 13:56:01 +01:00
|
|
|
|
2024-07-01 01:12:32 +01:00
|
|
|
import cc.fascinated.bat.BatApplication;
|
2024-06-25 12:36:40 +01:00
|
|
|
import cc.fascinated.bat.common.ProfileHolder;
|
2024-07-01 01:12:32 +01:00
|
|
|
import cc.fascinated.bat.common.Serializable;
|
2024-06-30 03:36:00 +01:00
|
|
|
import cc.fascinated.bat.features.namehistory.profile.user.NameHistoryProfile;
|
2024-06-30 02:36:17 +01:00
|
|
|
import cc.fascinated.bat.features.scoresaber.profile.user.ScoreSaberProfile;
|
2024-06-24 13:56:01 +01:00
|
|
|
import cc.fascinated.bat.service.DiscordService;
|
2024-07-01 01:12:32 +01:00
|
|
|
import cc.fascinated.bat.service.MongoService;
|
|
|
|
import com.mongodb.client.model.ReplaceOptions;
|
2024-06-24 13:56:01 +01:00
|
|
|
import lombok.Getter;
|
|
|
|
import lombok.NonNull;
|
|
|
|
import lombok.RequiredArgsConstructor;
|
|
|
|
import lombok.Setter;
|
|
|
|
import net.dv8tion.jda.api.entities.User;
|
2024-07-01 01:12:32 +01:00
|
|
|
import org.slf4j.Logger;
|
|
|
|
import org.slf4j.LoggerFactory;
|
2024-06-24 13:56:01 +01:00
|
|
|
import org.springframework.data.annotation.Id;
|
|
|
|
import org.springframework.data.mongodb.core.mapping.Document;
|
|
|
|
|
2024-06-25 15:43:36 +01:00
|
|
|
import java.util.Date;
|
2024-07-01 01:12:32 +01:00
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.Map;
|
2024-06-24 13:56:01 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @author Fascinated (fascinated7)
|
|
|
|
*/
|
|
|
|
@RequiredArgsConstructor
|
2024-06-28 03:01:21 +01:00
|
|
|
@Getter
|
|
|
|
@Setter
|
2024-06-24 13:56:01 +01:00
|
|
|
@Document(collection = "users")
|
2024-06-27 13:02:55 +01:00
|
|
|
public class BatUser extends ProfileHolder {
|
2024-07-01 01:12:32 +01:00
|
|
|
private static final Logger log = LoggerFactory.getLogger(BatUser.class);
|
|
|
|
/**
|
|
|
|
* The document that belongs to this user
|
|
|
|
*/
|
|
|
|
private final org.bson.Document document;
|
|
|
|
|
2024-06-24 13:56:01 +01:00
|
|
|
/**
|
|
|
|
* The ID of the user
|
|
|
|
*/
|
2024-06-28 03:01:21 +01:00
|
|
|
@NonNull
|
|
|
|
@Id
|
|
|
|
private final String id;
|
2024-06-24 13:56:01 +01:00
|
|
|
|
2024-07-01 19:40:32 +01:00
|
|
|
/**
|
|
|
|
* The global name of the user
|
|
|
|
*/
|
|
|
|
private String globalName;
|
|
|
|
|
2024-06-25 15:43:36 +01:00
|
|
|
/**
|
|
|
|
* The time this user was created
|
|
|
|
*/
|
2024-07-01 01:12:32 +01:00
|
|
|
private Date createdAt;
|
|
|
|
|
|
|
|
public BatUser(@NonNull String id, @NonNull org.bson.Document document) {
|
|
|
|
this.id = id;
|
|
|
|
this.document = document;
|
|
|
|
boolean newAccount = this.document.isEmpty();
|
|
|
|
this.createdAt = newAccount ? new Date() : document.getDate("createdAt");
|
2024-07-01 19:40:32 +01:00
|
|
|
|
|
|
|
User user = DiscordService.JDA.getUserById(id);
|
|
|
|
if (user != null) {
|
|
|
|
this.globalName = user.getGlobalName();
|
|
|
|
}
|
2024-07-01 01:12:32 +01:00
|
|
|
}
|
2024-06-25 15:43:36 +01:00
|
|
|
|
2024-06-28 03:01:21 +01:00
|
|
|
/**
|
|
|
|
* The name of the user
|
|
|
|
*/
|
|
|
|
public String getName() {
|
2024-07-01 19:40:32 +01:00
|
|
|
return this.getGlobalName();
|
2024-06-28 03:01:21 +01:00
|
|
|
}
|
|
|
|
|
2024-06-24 13:56:01 +01:00
|
|
|
/**
|
|
|
|
* Gets the guild as the JDA Guild
|
|
|
|
*
|
|
|
|
* @return the guild
|
|
|
|
*/
|
2024-06-24 14:58:57 +01:00
|
|
|
public User getDiscordUser() {
|
2024-06-24 13:56:01 +01:00
|
|
|
return DiscordService.JDA.getUserById(id);
|
|
|
|
}
|
2024-06-30 02:36:17 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the user's ScoreSaber profile
|
|
|
|
*
|
|
|
|
* @return the user's ScoreSaber profile
|
|
|
|
*/
|
|
|
|
public ScoreSaberProfile getScoreSaberProfile() {
|
|
|
|
return getProfile(ScoreSaberProfile.class);
|
|
|
|
}
|
2024-06-30 03:36:00 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the user's name history profile
|
|
|
|
*
|
|
|
|
* @return the user's name history profile
|
|
|
|
*/
|
|
|
|
public NameHistoryProfile getNameHistoryProfile() {
|
|
|
|
return getProfile(NameHistoryProfile.class);
|
|
|
|
}
|
2024-07-01 01:12:32 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Saves the user
|
|
|
|
*/
|
|
|
|
public void save() {
|
2024-07-01 21:49:02 +01:00
|
|
|
org.bson.Document document = new org.bson.Document();
|
2024-07-01 01:12:32 +01:00
|
|
|
document.put("_id", id);
|
|
|
|
document.put("createdAt", createdAt);
|
|
|
|
|
|
|
|
Map<String, org.bson.Document> profileDocuments = new HashMap<>();
|
|
|
|
for (Serializable profile : getProfiles().values()) {
|
|
|
|
profileDocuments.put(profile.getClass().getSimpleName(), profile.serialize(BatApplication.GSON));
|
|
|
|
}
|
|
|
|
document.put("profiles", profileDocuments);
|
|
|
|
|
|
|
|
MongoService.INSTANCE.getUsersCollection().replaceOne(
|
|
|
|
new org.bson.Document("_id", id),
|
2024-07-01 21:49:02 +01:00
|
|
|
document,
|
2024-07-01 01:12:32 +01:00
|
|
|
new ReplaceOptions().upsert(true)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public <T extends Serializable> T getProfile(Class<T> clazz) {
|
|
|
|
return getProfileFromDocument(clazz, document);
|
|
|
|
}
|
2024-06-24 13:56:01 +01:00
|
|
|
}
|