Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion src/main/java/org/example/TravelGameController.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ public class TravelGameController {
private List<PossibleMoves> shownMoves = List.of();
private final Map<PossibleMoves, Button> moveButtons = new HashMap<>();

private Map<Long, Integer> lastTurnByTravelerId = new HashMap<>();

private Location lastClickedDestination = null;
private int cycleIndex = 0;

Expand Down Expand Up @@ -329,10 +331,24 @@ private int screenToGridY(double y, double totalHeight) {
}

private void syncHudAndMap() {
refreshTurnCache();
updateHud();
updateGraphics();
}

private void refreshTurnCache() {
if (players.isEmpty()) {
lastTurnByTravelerId.clear();
return;
}

List<Long> ids = players.stream()
.map(Traveler::getId)
.toList();

lastTurnByTravelerId = journeyService.getLastTurnNumbersForPlayers(ids);
}

private void updateHud() {
if (players.isEmpty()) return;

Expand All @@ -348,7 +364,9 @@ private void updateHud() {
nextPlayerLabel.setText(next != null ? next.getPlayerName() : nextRef.getPlayerName());

currentCreditsLabel.setText(current.getMoney() != null ? current.getMoney().toPlainString() : "-");
currentTurnLabel.setText(String.valueOf(current.getTurnCount()));

int lastTurn = lastTurnByTravelerId.getOrDefault(current.getId(), 0);
currentTurnLabel.setText(String.valueOf(lastTurn + 1));
currentPointLabel.setText(String.valueOf(current.getPlayerScore()));

currentLocationLabel.setText("[" + clampToGrid(current.getPlayerPosX()) + "," + clampToGrid(current.getPlayerPosY()) + "]");
Expand Down
30 changes: 30 additions & 0 deletions src/main/java/org/example/service/JourneyService.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class JourneyService {

Expand Down Expand Up @@ -121,4 +123,32 @@ private Journey doTravelStep(Traveler traveler, LocationLink route, Transport tr

return journey;
}

public Map<Long, Integer> getLastTurnNumbersForPlayers(List<Long> travelerIds) {
if (travelerIds == null || travelerIds.isEmpty()) return Map.of();

List<Object[]> rows = em.createQuery("""
select j.traveler.id, max(j.turnNumber)
from Journey j
where j.traveler.id in :ids
group by j.traveler.id
""", Object[].class)
.setParameter("ids", travelerIds)
.getResultList();

Map<Long, Integer> out = new HashMap<>();
for (Object[] r : rows) {
Long id = (Long) r[0];
Integer maxTurn = (r[1] == null) ? 0 : ((Number) r[1]).intValue();
out.put(id, maxTurn);
}

// spelare utan journeys hamnar inte i rows → sätt 0 på dem
for (Long id : travelerIds) {
out.putIfAbsent(id, 0);
}

return out;
}

}