put player in a player object in the return json and update the cache information in json responses
Some checks failed
Deploy App / docker (ubuntu-latest, 2.44.0, 17, 3.8.5) (push) Failing after 22s

This commit is contained in:
Lee 2024-04-13 17:10:40 +01:00
parent 2895525412
commit c198339acc
9 changed files with 92 additions and 37 deletions

@ -2,6 +2,7 @@ package cc.fascinated.controller;
import cc.fascinated.model.cache.CachedPlayer; import cc.fascinated.model.cache.CachedPlayer;
import cc.fascinated.model.cache.CachedPlayerName; import cc.fascinated.model.cache.CachedPlayerName;
import cc.fascinated.model.player.Player;
import cc.fascinated.service.PlayerService; import cc.fascinated.service.PlayerService;
import io.swagger.v3.oas.annotations.Parameter; import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag; 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 = "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 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) { @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"; String dispositionHeader = download ? "attachment; filename=%s.png" : "inline; filename=%s.png";
// Return the part image // Return the part image

@ -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.
* <p>
* The default values are:
* <br>
* <ul>
* <li>cached: true</li>
* <li>cachedAt: {@link System#currentTimeMillis()}</li>
* </ul>
* <br>
* </p>
*
* @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;
}
}
}

@ -24,10 +24,9 @@ public final class CachedEndpointStatus implements Serializable {
private final Map<String, Status> endpoints; private final Map<String, Status> endpoints;
/** /**
* The unix timestamp of when this * The cache information about the request.
* server was cached, -1 if not cached.
*/ */
private long cached; private CacheInformation cache;
public enum Status { public enum Status {
/** /**
@ -36,10 +35,8 @@ public final class CachedEndpointStatus implements Serializable {
ONLINE, ONLINE,
/** /**
* The service is degraded and may not be fully operational. * The service is online, but may be experiencing issues.
* <p>
* This could be due to high load or other issues. * This could be due to high load or other issues.
* </p>
*/ */
DEGRADED, DEGRADED,

@ -27,8 +27,7 @@ public final class CachedMinecraftServer implements Serializable {
private final MinecraftServer server; private final MinecraftServer server;
/** /**
* The unix timestamp of when this * The cache information about the request.
* server was cached, -1 if not cached.
*/ */
private long cached; private CacheInformation cache;
} }

@ -1,12 +1,11 @@
package cc.fascinated.model.cache; 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.player.Player;
import cc.fascinated.model.skin.Skin; import com.fasterxml.jackson.annotation.JsonIgnore;
import lombok.AllArgsConstructor;
import lombok.Getter; import lombok.Getter;
import lombok.Setter; import lombok.Setter;
import lombok.ToString; import org.springframework.data.annotation.Id;
import org.springframework.data.redis.core.RedisHash; import org.springframework.data.redis.core.RedisHash;
import java.io.Serializable; import java.io.Serializable;
@ -18,17 +17,22 @@ import java.util.UUID;
* @author Braydon * @author Braydon
*/ */
@Setter @Getter @Setter @Getter
@ToString(callSuper = true) @AllArgsConstructor
@RedisHash(value = "player", timeToLive = 60L * 60L) // 1 hour (in seconds) @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 * The unique id of the player.
* player was cached, -1 if not cached.
*/ */
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); * The player to cache.
this.cached = cached; */
} private Player player;
/**
* The cache information about the request.
*/
private CacheInformation cache;
} }

@ -6,7 +6,6 @@ import cc.fascinated.model.mojang.MojangProfile;
import cc.fascinated.model.skin.Skin; import cc.fascinated.model.skin.Skin;
import lombok.AllArgsConstructor; import lombok.AllArgsConstructor;
import lombok.Getter; import lombok.Getter;
import org.springframework.data.annotation.Id;
import java.util.UUID; import java.util.UUID;
@ -16,7 +15,7 @@ public class Player {
/** /**
* The UUID of the player * The UUID of the player
*/ */
@Id private final UUID uniqueId; private final UUID uniqueId;
/** /**
* The trimmed UUID of the player * The trimmed UUID of the player

@ -5,6 +5,7 @@ import cc.fascinated.common.EndpointStatus;
import cc.fascinated.common.ExpiringSet; import cc.fascinated.common.ExpiringSet;
import cc.fascinated.common.WebRequest; import cc.fascinated.common.WebRequest;
import cc.fascinated.config.Config; import cc.fascinated.config.Config;
import cc.fascinated.model.cache.CacheInformation;
import cc.fascinated.model.cache.CachedEndpointStatus; import cc.fascinated.model.cache.CachedEndpointStatus;
import cc.fascinated.model.mojang.MojangProfile; import cc.fascinated.model.mojang.MojangProfile;
import cc.fascinated.model.mojang.MojangUsernameToUuid; import cc.fascinated.model.mojang.MojangUsernameToUuid;
@ -231,10 +232,10 @@ public class MojangService {
CachedEndpointStatus status = new CachedEndpointStatus( CachedEndpointStatus status = new CachedEndpointStatus(
MOJANG_ENDPOINT_STATUS_KEY, MOJANG_ENDPOINT_STATUS_KEY,
endpoints, endpoints,
System.currentTimeMillis() CacheInformation.defaultCache()
); );
mojangEndpointStatusRepository.save(status); mojangEndpointStatusRepository.save(status);
status.setCached(-1L); // Indicate that the status is not cached status.getCache().setCached(false);
return status; return status;
} }

@ -9,6 +9,7 @@ import cc.fascinated.exception.impl.BadRequestException;
import cc.fascinated.exception.impl.MojangAPIRateLimitException; import cc.fascinated.exception.impl.MojangAPIRateLimitException;
import cc.fascinated.exception.impl.RateLimitException; import cc.fascinated.exception.impl.RateLimitException;
import cc.fascinated.exception.impl.ResourceNotFoundException; import cc.fascinated.exception.impl.ResourceNotFoundException;
import cc.fascinated.model.cache.CacheInformation;
import cc.fascinated.model.cache.CachedPlayer; import cc.fascinated.model.cache.CachedPlayer;
import cc.fascinated.model.cache.CachedPlayerName; import cc.fascinated.model.cache.CachedPlayerName;
import cc.fascinated.model.cache.CachedPlayerSkinPart; import cc.fascinated.model.cache.CachedPlayerSkinPart;
@ -73,17 +74,20 @@ public class PlayerService {
log.info("Got player profile from Mojang: {}", id); log.info("Got player profile from Mojang: {}", id);
Tuple<Skin, Cape> skinAndCape = mojangProfile.getSkinAndCape(); Tuple<Skin, Cape> skinAndCape = mojangProfile.getSkinAndCape();
CachedPlayer player = new CachedPlayer( CachedPlayer player = new CachedPlayer(
uuid, // Player UUID
new Player(
uuid, // Player UUID uuid, // Player UUID
UUIDUtils.removeDashes(uuid), // Trimmed UUID UUIDUtils.removeDashes(uuid), // Trimmed UUID
mojangProfile.getName(), // Player Name mojangProfile.getName(), // Player Name
skinAndCape.getLeft(), // Skin skinAndCape.getLeft(), // Skin
skinAndCape.getRight(), // Cape skinAndCape.getRight(), // Cape
mojangProfile.getProperties(), // Raw properties mojangProfile.getProperties() // Raw properties
System.currentTimeMillis() // Cache time ),
CacheInformation.defaultCache() // Cache time
); );
playerCacheRepository.save(player); playerCacheRepository.save(player);
player.setCached(-1); // Indicate that the player is not cached player.getCache().setCached(false);
return player; return player;
} catch (RateLimitException exception) { } catch (RateLimitException exception) {
throw new MojangAPIRateLimitException(); throw new MojangAPIRateLimitException();

@ -5,6 +5,7 @@ import cc.fascinated.common.EnumUtils;
import cc.fascinated.config.Config; import cc.fascinated.config.Config;
import cc.fascinated.exception.impl.BadRequestException; import cc.fascinated.exception.impl.BadRequestException;
import cc.fascinated.exception.impl.ResourceNotFoundException; import cc.fascinated.exception.impl.ResourceNotFoundException;
import cc.fascinated.model.cache.CacheInformation;
import cc.fascinated.model.cache.CachedMinecraftServer; import cc.fascinated.model.cache.CachedMinecraftServer;
import cc.fascinated.model.dns.DNSRecord; import cc.fascinated.model.dns.DNSRecord;
import cc.fascinated.model.dns.impl.ARecord; import cc.fascinated.model.dns.impl.ARecord;
@ -89,7 +90,7 @@ public class ServerService {
CachedMinecraftServer server = new CachedMinecraftServer( CachedMinecraftServer server = new CachedMinecraftServer(
key, key,
platform.getPinger().ping(hostname, ip, port, records.toArray(new DNSRecord[0])), platform.getPinger().ping(hostname, ip, port, records.toArray(new DNSRecord[0])),
System.currentTimeMillis() CacheInformation.defaultCache()
); );
// Check if the server is blocked by Mojang // Check if the server is blocked by Mojang
@ -99,7 +100,7 @@ public class ServerService {
log.info("Found server: {}:{}", hostname, port); log.info("Found server: {}:{}", hostname, port);
serverCacheRepository.save(server); serverCacheRepository.save(server);
server.setCached(-1); // Indicate that the server is not cached server.getCache().setCached(false);
return server; return server;
} }