|
| 1 | +package net.elytrarace.server.cup; |
| 2 | + |
| 3 | +import net.elytrarace.common.cup.CupService; |
| 4 | +import net.elytrarace.common.cup.model.FileCupDTO; |
| 5 | +import net.elytrarace.common.cup.model.ResolvedCupDTO; |
| 6 | +import net.elytrarace.common.map.MapService; |
| 7 | +import net.elytrarace.common.map.model.FileMapDTO; |
| 8 | +import net.elytrarace.common.map.model.LocationDTO; |
| 9 | +import net.elytrarace.common.map.model.PortalDTO; |
| 10 | +import net.elytrarace.server.physics.Ring; |
| 11 | +import net.minestom.server.coordinate.Pos; |
| 12 | +import net.minestom.server.coordinate.Vec; |
| 13 | +import org.jetbrains.annotations.NotNull; |
| 14 | +import org.slf4j.Logger; |
| 15 | +import org.slf4j.LoggerFactory; |
| 16 | + |
| 17 | +import java.nio.file.Path; |
| 18 | +import java.util.ArrayList; |
| 19 | +import java.util.List; |
| 20 | +import java.util.Optional; |
| 21 | +import java.util.concurrent.ExecutionException; |
| 22 | + |
| 23 | +/** |
| 24 | + * Converts shared DTOs (CupDTO, FileMapDTO, PortalDTO) into server-side domain objects |
| 25 | + * (CupDefinition, MapDefinition, Ring) that the game engine can use directly. |
| 26 | + */ |
| 27 | +public final class CupLoader { |
| 28 | + |
| 29 | + private static final Logger LOGGER = LoggerFactory.getLogger(CupLoader.class); |
| 30 | + private static final int DEFAULT_RING_POINTS = 10; |
| 31 | + |
| 32 | + private final CupService cupService; |
| 33 | + private final MapService mapService; |
| 34 | + private final Path worldsPath; |
| 35 | + |
| 36 | + public CupLoader(@NotNull CupService cupService, @NotNull MapService mapService, @NotNull Path worldsPath) { |
| 37 | + this.cupService = cupService; |
| 38 | + this.mapService = mapService; |
| 39 | + this.worldsPath = worldsPath; |
| 40 | + } |
| 41 | + |
| 42 | + /** |
| 43 | + * Loads the first available cup and converts it to a {@link CupDefinition}. |
| 44 | + * Returns empty if no cups are configured. |
| 45 | + */ |
| 46 | + public Optional<CupDefinition> loadFirstCup() { |
| 47 | + var cups = cupService.getCups(); |
| 48 | + if (cups.isEmpty()) { |
| 49 | + LOGGER.warn("No cups configured — server will remain in lobby mode"); |
| 50 | + return Optional.empty(); |
| 51 | + } |
| 52 | + return loadCup(cups.getFirst()); |
| 53 | + } |
| 54 | + |
| 55 | + /** |
| 56 | + * Resolves a {@link FileCupDTO} into a {@link CupDefinition} by loading all referenced maps. |
| 57 | + */ |
| 58 | + public Optional<CupDefinition> loadCup(@NotNull FileCupDTO cupDTO) { |
| 59 | + try { |
| 60 | + var resolved = (ResolvedCupDTO) mapService.getMapByCup(cupDTO).get(); |
| 61 | + var maps = new ArrayList<MapDefinition>(); |
| 62 | + |
| 63 | + for (var mapDTO : resolved.maps()) { |
| 64 | + if (!(mapDTO instanceof FileMapDTO fileMapDTO)) continue; |
| 65 | + var mapDef = convertMap(fileMapDTO); |
| 66 | + mapDef.ifPresent(maps::add); |
| 67 | + } |
| 68 | + |
| 69 | + if (maps.isEmpty()) { |
| 70 | + LOGGER.warn("Cup '{}' resolved to zero loadable maps — skipping", cupDTO.name().asString()); |
| 71 | + return Optional.empty(); |
| 72 | + } |
| 73 | + |
| 74 | + LOGGER.info("Loaded cup '{}' with {} maps", cupDTO.name().asString(), maps.size()); |
| 75 | + return Optional.of(new CupDefinition(cupDTO.name().asString(), maps)); |
| 76 | + |
| 77 | + } catch (InterruptedException e) { |
| 78 | + Thread.currentThread().interrupt(); |
| 79 | + LOGGER.error("Interrupted while loading cup '{}'", cupDTO.name().asString(), e); |
| 80 | + return Optional.empty(); |
| 81 | + } catch (ExecutionException e) { |
| 82 | + LOGGER.error("Failed to resolve maps for cup '{}'", cupDTO.name().asString(), e.getCause()); |
| 83 | + return Optional.empty(); |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + /** |
| 88 | + * Converts a {@link FileMapDTO} to a {@link MapDefinition}. |
| 89 | + * Returns empty if the world directory does not exist. |
| 90 | + */ |
| 91 | + private Optional<MapDefinition> convertMap(@NotNull FileMapDTO dto) { |
| 92 | + var worldDir = worldsPath.resolve(dto.world()); |
| 93 | + if (!worldDir.toFile().exists()) { |
| 94 | + LOGGER.warn("World directory not found for map '{}': {} — skipping map", |
| 95 | + dto.name().asString(), worldDir.toAbsolutePath()); |
| 96 | + return Optional.empty(); |
| 97 | + } |
| 98 | + |
| 99 | + var portals = new ArrayList<>(dto.portals()); |
| 100 | + var rings = portals.stream() |
| 101 | + .map(this::convertPortalToRing) |
| 102 | + .toList(); |
| 103 | + |
| 104 | + var spawnPos = deriveSpawn(portals); |
| 105 | + LOGGER.info(" Map '{}' — {} rings, spawn {}", dto.name().asString(), rings.size(), spawnPos); |
| 106 | + |
| 107 | + return Optional.of(new MapDefinition(dto.name().asString(), worldDir, rings, spawnPos)); |
| 108 | + } |
| 109 | + |
| 110 | + /** |
| 111 | + * Converts a portal checkpoint into a ring. |
| 112 | + * The ring center is derived from the location marked as center=true. |
| 113 | + * The radius is the max distance from center to any edge location. |
| 114 | + * The normal is computed from edge vectors when possible; defaults to (0,0,1). |
| 115 | + */ |
| 116 | + private Ring convertPortalToRing(@NotNull PortalDTO portal) { |
| 117 | + var locations = portal.locations(); |
| 118 | + |
| 119 | + var centerLoc = locations.stream() |
| 120 | + .filter(LocationDTO::center) |
| 121 | + .findFirst() |
| 122 | + .orElse(locations.isEmpty() ? new LocationDTO(0, 64, 0, true) : locations.getFirst()); |
| 123 | + |
| 124 | + var center = new Vec(centerLoc.x(), centerLoc.y(), centerLoc.z()); |
| 125 | + |
| 126 | + var edgePoints = locations.stream() |
| 127 | + .filter(l -> !l.center()) |
| 128 | + .map(l -> new Vec(l.x(), l.y(), l.z())) |
| 129 | + .toList(); |
| 130 | + |
| 131 | + double radius = edgePoints.stream() |
| 132 | + .mapToDouble(e -> center.distance(e)) |
| 133 | + .max() |
| 134 | + .orElse(3.0); |
| 135 | + |
| 136 | + var normal = computeNormal(center, edgePoints); |
| 137 | + |
| 138 | + return new Ring(center, normal, radius, DEFAULT_RING_POINTS); |
| 139 | + } |
| 140 | + |
| 141 | + /** |
| 142 | + * Computes the plane normal from edge points around a center. |
| 143 | + * Uses the cross product of the first two edge vectors when available. |
| 144 | + * Falls back to (0,0,1) if the normal cannot be determined. |
| 145 | + */ |
| 146 | + private Vec computeNormal(@NotNull Vec center, @NotNull List<Vec> edgePoints) { |
| 147 | + if (edgePoints.size() < 2) { |
| 148 | + return new Vec(0, 0, 1); |
| 149 | + } |
| 150 | + var v1 = edgePoints.get(0).sub(center).normalize(); |
| 151 | + var v2 = edgePoints.get(1).sub(center).normalize(); |
| 152 | + var cross = v1.cross(v2); |
| 153 | + var len = cross.length(); |
| 154 | + if (len < 1e-6) { |
| 155 | + return new Vec(0, 0, 1); |
| 156 | + } |
| 157 | + return cross.div(len); |
| 158 | + } |
| 159 | + |
| 160 | + /** |
| 161 | + * Derives a spawn position from the first portal's center location. |
| 162 | + * Adds 2 blocks of Y clearance so players don't spawn inside the ring. |
| 163 | + * Falls back to (0, 64, 0) if no portals are present. |
| 164 | + */ |
| 165 | + private Pos deriveSpawn(@NotNull List<? extends PortalDTO> portals) { |
| 166 | + if (portals.isEmpty()) { |
| 167 | + return new Pos(0, 64, 0); |
| 168 | + } |
| 169 | + var first = portals.getFirst(); |
| 170 | + var centerLoc = first.locations().stream() |
| 171 | + .filter(LocationDTO::center) |
| 172 | + .findFirst() |
| 173 | + .orElse(first.locations().isEmpty() ? new LocationDTO(0, 64, 0, true) : first.locations().getFirst()); |
| 174 | + |
| 175 | + return new Pos(centerLoc.x(), centerLoc.y() + 2, centerLoc.z()); |
| 176 | + } |
| 177 | +} |
0 commit comments