134 lines
3.5 KiB
Java
Raw Normal View History

2024-06-25 11:55:26 +01:00
package cc.fascinated.bat.model;
2024-06-24 13:56:01 +01:00
import cc.fascinated.bat.BatApplication;
2024-06-25 12:36:40 +01:00
import cc.fascinated.bat.common.ProfileHolder;
import cc.fascinated.bat.common.Serializable;
import cc.fascinated.bat.common.UserUtils;
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;
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;
import org.springframework.data.annotation.Id;
2024-06-25 15:43:36 +01:00
import java.util.Date;
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-27 13:02:55 +01:00
public class BatUser extends ProfileHolder {
/**
* 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
*/
private Date createdAt;
2024-07-03 00:14:00 +01:00
/**
* The discord user associated with this user
*/
private User user;
public BatUser(@NonNull String id, User user, @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 was not passed through
if (user == null) {
user = UserUtils.getUser(id);
2024-07-01 19:40:32 +01:00
}
2024-07-04 03:40:12 +01:00
this.user = user;
this.globalName = user.getGlobalName();
}
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
*/
public User getDiscordUser() {
2024-07-03 00:14:00 +01:00
if (user == null) {
user = DiscordService.JDA.getUserById(id);
}
return user;
2024-06-24 13:56:01 +01:00
}
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);
}
/**
* Saves the user
*/
public void save() {
2024-07-01 21:49:02 +01:00
org.bson.Document document = new org.bson.Document();
document.put("_id", id);
document.put("createdAt", createdAt);
2024-07-04 05:54:13 +01:00
Map<String, org.bson.Document> profileDocuments = this.document.get("profiles", 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,
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
}