From c198339accc9f939969f428fe15ca55f9e260e65 Mon Sep 17 00:00:00 2001
From: Liam
Date: Sat, 13 Apr 2024 17:10:40 +0100
Subject: [PATCH] put player in a player object in the return json and update
the cache information in json responses
---
.../controller/PlayerController.java | 4 +-
.../model/cache/CacheInformation.java | 48 +++++++++++++++++++
.../model/cache/CachedEndpointStatus.java | 11 ++---
.../model/cache/CachedMinecraftServer.java | 5 +-
.../fascinated/model/cache/CachedPlayer.java | 30 +++++++-----
.../cc/fascinated/model/player/Player.java | 3 +-
.../cc/fascinated/service/MojangService.java | 5 +-
.../cc/fascinated/service/PlayerService.java | 18 ++++---
.../cc/fascinated/service/ServerService.java | 5 +-
9 files changed, 92 insertions(+), 37 deletions(-)
create mode 100644 src/main/java/cc/fascinated/model/cache/CacheInformation.java
diff --git a/src/main/java/cc/fascinated/controller/PlayerController.java b/src/main/java/cc/fascinated/controller/PlayerController.java
index 0c0b0ae..1d2d858 100644
--- a/src/main/java/cc/fascinated/controller/PlayerController.java
+++ b/src/main/java/cc/fascinated/controller/PlayerController.java
@@ -2,6 +2,7 @@ package cc.fascinated.controller;
import cc.fascinated.model.cache.CachedPlayer;
import cc.fascinated.model.cache.CachedPlayerName;
+import cc.fascinated.model.player.Player;
import cc.fascinated.service.PlayerService;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
@@ -50,7 +51,8 @@ public class PlayerController {
@Parameter(description = "The size of the image", example = "256") @RequestParam(required = false, defaultValue = "256") int size,
@Parameter(description = "Whether to render the skin overlay (skin layers)", example = "false") @RequestParam(required = false, defaultValue = "false") boolean overlays,
@Parameter(description = "Whether to download the image") @RequestParam(required = false, defaultValue = "false") boolean download) {
- CachedPlayer player = playerService.getPlayer(id);
+ CachedPlayer cachedPlayer = playerService.getPlayer(id);
+ Player player = cachedPlayer.getPlayer();
String dispositionHeader = download ? "attachment; filename=%s.png" : "inline; filename=%s.png";
// Return the part image
diff --git a/src/main/java/cc/fascinated/model/cache/CacheInformation.java b/src/main/java/cc/fascinated/model/cache/CacheInformation.java
new file mode 100644
index 0000000..c56b490
--- /dev/null
+++ b/src/main/java/cc/fascinated/model/cache/CacheInformation.java
@@ -0,0 +1,48 @@
+package cc.fascinated.model.cache;
+
+import lombok.AllArgsConstructor;
+import lombok.Getter;
+import lombok.Setter;
+
+@AllArgsConstructor @Getter @Setter
+public class CacheInformation {
+ /**
+ * Whether this request is cached.
+ */
+ private boolean cached;
+
+ /**
+ * The unix timestamp of when this was cached.
+ */
+ private long cachedTime;
+
+ /**
+ * Create a new cache information object with the default values.
+ *
+ * The default values are:
+ *
+ *
+ * - cached: true
+ * - cachedAt: {@link System#currentTimeMillis()}
+ *
+ *
+ *
+ *
+ * @return the default cache information object
+ */
+ public static CacheInformation defaultCache() {
+ return new CacheInformation(true, System.currentTimeMillis());
+ }
+
+ /**
+ * Sets if this request is cached.
+ *
+ * @param cached the new value of if this request is cached
+ */
+ public void setCached(boolean cached) {
+ this.cached = cached;
+ if (!cached) {
+ cachedTime = -1;
+ }
+ }
+}
diff --git a/src/main/java/cc/fascinated/model/cache/CachedEndpointStatus.java b/src/main/java/cc/fascinated/model/cache/CachedEndpointStatus.java
index c00c61b..fa4bc08 100644
--- a/src/main/java/cc/fascinated/model/cache/CachedEndpointStatus.java
+++ b/src/main/java/cc/fascinated/model/cache/CachedEndpointStatus.java
@@ -24,10 +24,9 @@ public final class CachedEndpointStatus implements Serializable {
private final Map endpoints;
/**
- * The unix timestamp of when this
- * server was cached, -1 if not cached.
+ * The cache information about the request.
*/
- private long cached;
+ private CacheInformation cache;
public enum Status {
/**
@@ -36,10 +35,8 @@ public final class CachedEndpointStatus implements Serializable {
ONLINE,
/**
- * The service is degraded and may not be fully operational.
- *
- * This could be due to high load or other issues.
- *
+ * The service is online, but may be experiencing issues.
+ * This could be due to high load or other issues.
*/
DEGRADED,
diff --git a/src/main/java/cc/fascinated/model/cache/CachedMinecraftServer.java b/src/main/java/cc/fascinated/model/cache/CachedMinecraftServer.java
index 943ef03..4545452 100644
--- a/src/main/java/cc/fascinated/model/cache/CachedMinecraftServer.java
+++ b/src/main/java/cc/fascinated/model/cache/CachedMinecraftServer.java
@@ -27,8 +27,7 @@ public final class CachedMinecraftServer implements Serializable {
private final MinecraftServer server;
/**
- * The unix timestamp of when this
- * server was cached, -1 if not cached.
+ * The cache information about the request.
*/
- private long cached;
+ private CacheInformation cache;
}
\ No newline at end of file
diff --git a/src/main/java/cc/fascinated/model/cache/CachedPlayer.java b/src/main/java/cc/fascinated/model/cache/CachedPlayer.java
index 5803bb5..955e031 100644
--- a/src/main/java/cc/fascinated/model/cache/CachedPlayer.java
+++ b/src/main/java/cc/fascinated/model/cache/CachedPlayer.java
@@ -1,12 +1,11 @@
package cc.fascinated.model.cache;
-import cc.fascinated.model.mojang.MojangProfile;
-import cc.fascinated.model.player.Cape;
import cc.fascinated.model.player.Player;
-import cc.fascinated.model.skin.Skin;
+import com.fasterxml.jackson.annotation.JsonIgnore;
+import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.Setter;
-import lombok.ToString;
+import org.springframework.data.annotation.Id;
import org.springframework.data.redis.core.RedisHash;
import java.io.Serializable;
@@ -18,17 +17,22 @@ import java.util.UUID;
* @author Braydon
*/
@Setter @Getter
-@ToString(callSuper = true)
+@AllArgsConstructor
@RedisHash(value = "player", timeToLive = 60L * 60L) // 1 hour (in seconds)
-public final class CachedPlayer extends Player implements Serializable {
+public final class CachedPlayer implements Serializable {
/**
- * The unix timestamp of when this
- * player was cached, -1 if not cached.
+ * The unique id of the player.
*/
- private long cached;
+ @JsonIgnore
+ @Id private UUID uniqueId;
- public CachedPlayer(UUID uniqueId, String trimmedUniqueId, String username, Skin skin, Cape cape, MojangProfile.ProfileProperty[] rawProperties, long cached) {
- super(uniqueId, trimmedUniqueId, username, skin, cape, rawProperties);
- this.cached = cached;
- }
+ /**
+ * The player to cache.
+ */
+ private Player player;
+
+ /**
+ * The cache information about the request.
+ */
+ private CacheInformation cache;
}
\ No newline at end of file
diff --git a/src/main/java/cc/fascinated/model/player/Player.java b/src/main/java/cc/fascinated/model/player/Player.java
index 5ac567e..a5dec85 100644
--- a/src/main/java/cc/fascinated/model/player/Player.java
+++ b/src/main/java/cc/fascinated/model/player/Player.java
@@ -6,7 +6,6 @@ import cc.fascinated.model.mojang.MojangProfile;
import cc.fascinated.model.skin.Skin;
import lombok.AllArgsConstructor;
import lombok.Getter;
-import org.springframework.data.annotation.Id;
import java.util.UUID;
@@ -16,7 +15,7 @@ public class Player {
/**
* The UUID of the player
*/
- @Id private final UUID uniqueId;
+ private final UUID uniqueId;
/**
* The trimmed UUID of the player
diff --git a/src/main/java/cc/fascinated/service/MojangService.java b/src/main/java/cc/fascinated/service/MojangService.java
index 518fab8..20ffef3 100644
--- a/src/main/java/cc/fascinated/service/MojangService.java
+++ b/src/main/java/cc/fascinated/service/MojangService.java
@@ -5,6 +5,7 @@ import cc.fascinated.common.EndpointStatus;
import cc.fascinated.common.ExpiringSet;
import cc.fascinated.common.WebRequest;
import cc.fascinated.config.Config;
+import cc.fascinated.model.cache.CacheInformation;
import cc.fascinated.model.cache.CachedEndpointStatus;
import cc.fascinated.model.mojang.MojangProfile;
import cc.fascinated.model.mojang.MojangUsernameToUuid;
@@ -231,10 +232,10 @@ public class MojangService {
CachedEndpointStatus status = new CachedEndpointStatus(
MOJANG_ENDPOINT_STATUS_KEY,
endpoints,
- System.currentTimeMillis()
+ CacheInformation.defaultCache()
);
mojangEndpointStatusRepository.save(status);
- status.setCached(-1L); // Indicate that the status is not cached
+ status.getCache().setCached(false);
return status;
}
diff --git a/src/main/java/cc/fascinated/service/PlayerService.java b/src/main/java/cc/fascinated/service/PlayerService.java
index 51fca2e..3231036 100644
--- a/src/main/java/cc/fascinated/service/PlayerService.java
+++ b/src/main/java/cc/fascinated/service/PlayerService.java
@@ -9,6 +9,7 @@ import cc.fascinated.exception.impl.BadRequestException;
import cc.fascinated.exception.impl.MojangAPIRateLimitException;
import cc.fascinated.exception.impl.RateLimitException;
import cc.fascinated.exception.impl.ResourceNotFoundException;
+import cc.fascinated.model.cache.CacheInformation;
import cc.fascinated.model.cache.CachedPlayer;
import cc.fascinated.model.cache.CachedPlayerName;
import cc.fascinated.model.cache.CachedPlayerSkinPart;
@@ -74,16 +75,19 @@ public class PlayerService {
Tuple skinAndCape = mojangProfile.getSkinAndCape();
CachedPlayer player = new CachedPlayer(
uuid, // Player UUID
- UUIDUtils.removeDashes(uuid), // Trimmed UUID
- mojangProfile.getName(), // Player Name
- skinAndCape.getLeft(), // Skin
- skinAndCape.getRight(), // Cape
- mojangProfile.getProperties(), // Raw properties
- System.currentTimeMillis() // Cache time
+ new Player(
+ uuid, // Player UUID
+ UUIDUtils.removeDashes(uuid), // Trimmed UUID
+ mojangProfile.getName(), // Player Name
+ skinAndCape.getLeft(), // Skin
+ skinAndCape.getRight(), // Cape
+ mojangProfile.getProperties() // Raw properties
+ ),
+ CacheInformation.defaultCache() // Cache time
);
playerCacheRepository.save(player);
- player.setCached(-1); // Indicate that the player is not cached
+ player.getCache().setCached(false);
return player;
} catch (RateLimitException exception) {
throw new MojangAPIRateLimitException();
diff --git a/src/main/java/cc/fascinated/service/ServerService.java b/src/main/java/cc/fascinated/service/ServerService.java
index 3dd7ff7..346ad85 100644
--- a/src/main/java/cc/fascinated/service/ServerService.java
+++ b/src/main/java/cc/fascinated/service/ServerService.java
@@ -5,6 +5,7 @@ import cc.fascinated.common.EnumUtils;
import cc.fascinated.config.Config;
import cc.fascinated.exception.impl.BadRequestException;
import cc.fascinated.exception.impl.ResourceNotFoundException;
+import cc.fascinated.model.cache.CacheInformation;
import cc.fascinated.model.cache.CachedMinecraftServer;
import cc.fascinated.model.dns.DNSRecord;
import cc.fascinated.model.dns.impl.ARecord;
@@ -89,7 +90,7 @@ public class ServerService {
CachedMinecraftServer server = new CachedMinecraftServer(
key,
platform.getPinger().ping(hostname, ip, port, records.toArray(new DNSRecord[0])),
- System.currentTimeMillis()
+ CacheInformation.defaultCache()
);
// Check if the server is blocked by Mojang
@@ -99,7 +100,7 @@ public class ServerService {
log.info("Found server: {}:{}", hostname, port);
serverCacheRepository.save(server);
- server.setCached(-1); // Indicate that the server is not cached
+ server.getCache().setCached(false);
return server;
}