|
| 1 | +package com.juegodefinitivo.autobook.persistence.game; |
| 2 | + |
| 3 | +import com.fasterxml.jackson.core.JsonProcessingException; |
| 4 | +import com.fasterxml.jackson.core.type.TypeReference; |
| 5 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 6 | +import com.juegodefinitivo.autobook.domain.GameSession; |
| 7 | +import org.springframework.jdbc.core.JdbcTemplate; |
| 8 | +import org.springframework.stereotype.Repository; |
| 9 | + |
| 10 | +import java.sql.Timestamp; |
| 11 | +import java.time.Instant; |
| 12 | +import java.util.LinkedHashMap; |
| 13 | +import java.util.List; |
| 14 | +import java.util.Map; |
| 15 | +import java.util.Optional; |
| 16 | + |
| 17 | +@Repository |
| 18 | +public class GameSessionRuntimeRepository { |
| 19 | + |
| 20 | + private static final TypeReference<LinkedHashMap<String, Integer>> MAP_TYPE = new TypeReference<>() { |
| 21 | + }; |
| 22 | + |
| 23 | + private final JdbcTemplate jdbcTemplate; |
| 24 | + private final ObjectMapper objectMapper; |
| 25 | + |
| 26 | + public GameSessionRuntimeRepository(JdbcTemplate jdbcTemplate, ObjectMapper objectMapper) { |
| 27 | + this.jdbcTemplate = jdbcTemplate; |
| 28 | + this.objectMapper = objectMapper; |
| 29 | + } |
| 30 | + |
| 31 | + public void save(String sessionId, GameSession session, Map<String, Integer> narrativeMemory, int challengeAttempts, int challengeCorrect, String lastMessage) { |
| 32 | + String inventoryJson = toJson(session.getInventory()); |
| 33 | + String memoryJson = toJson(narrativeMemory); |
| 34 | + Instant now = Instant.now(); |
| 35 | + |
| 36 | + int updated = jdbcTemplate.update( |
| 37 | + """ |
| 38 | + UPDATE game_sessions |
| 39 | + SET player_name = ?, |
| 40 | + book_path = ?, |
| 41 | + book_title = ?, |
| 42 | + current_scene = ?, |
| 43 | + life = ?, |
| 44 | + knowledge = ?, |
| 45 | + courage = ?, |
| 46 | + focus = ?, |
| 47 | + score = ?, |
| 48 | + correct_answers = ?, |
| 49 | + discoveries = ?, |
| 50 | + completed = ?, |
| 51 | + inventory_json = ?, |
| 52 | + narrative_memory_json = ?, |
| 53 | + challenge_attempts = ?, |
| 54 | + challenge_correct = ?, |
| 55 | + last_message = ?, |
| 56 | + updated_at = ? |
| 57 | + WHERE session_id = ? |
| 58 | + """, |
| 59 | + session.getPlayerName(), |
| 60 | + session.getBookPath(), |
| 61 | + session.getBookTitle(), |
| 62 | + session.getCurrentScene(), |
| 63 | + session.getLife(), |
| 64 | + session.getKnowledge(), |
| 65 | + session.getCourage(), |
| 66 | + session.getFocus(), |
| 67 | + session.getScore(), |
| 68 | + session.getCorrectAnswers(), |
| 69 | + session.getDiscoveries(), |
| 70 | + session.isCompleted(), |
| 71 | + inventoryJson, |
| 72 | + memoryJson, |
| 73 | + challengeAttempts, |
| 74 | + challengeCorrect, |
| 75 | + safeMessage(lastMessage), |
| 76 | + Timestamp.from(now), |
| 77 | + sessionId |
| 78 | + ); |
| 79 | + |
| 80 | + if (updated == 0) { |
| 81 | + jdbcTemplate.update( |
| 82 | + """ |
| 83 | + INSERT INTO game_sessions ( |
| 84 | + session_id, |
| 85 | + player_name, |
| 86 | + book_path, |
| 87 | + book_title, |
| 88 | + current_scene, |
| 89 | + life, |
| 90 | + knowledge, |
| 91 | + courage, |
| 92 | + focus, |
| 93 | + score, |
| 94 | + correct_answers, |
| 95 | + discoveries, |
| 96 | + completed, |
| 97 | + inventory_json, |
| 98 | + narrative_memory_json, |
| 99 | + challenge_attempts, |
| 100 | + challenge_correct, |
| 101 | + last_message, |
| 102 | + updated_at |
| 103 | + ) |
| 104 | + VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) |
| 105 | + """, |
| 106 | + sessionId, |
| 107 | + session.getPlayerName(), |
| 108 | + session.getBookPath(), |
| 109 | + session.getBookTitle(), |
| 110 | + session.getCurrentScene(), |
| 111 | + session.getLife(), |
| 112 | + session.getKnowledge(), |
| 113 | + session.getCourage(), |
| 114 | + session.getFocus(), |
| 115 | + session.getScore(), |
| 116 | + session.getCorrectAnswers(), |
| 117 | + session.getDiscoveries(), |
| 118 | + session.isCompleted(), |
| 119 | + inventoryJson, |
| 120 | + memoryJson, |
| 121 | + challengeAttempts, |
| 122 | + challengeCorrect, |
| 123 | + safeMessage(lastMessage), |
| 124 | + Timestamp.from(now) |
| 125 | + ); |
| 126 | + } |
| 127 | + } |
| 128 | + |
| 129 | + public Optional<StoredSession> load(String sessionId) { |
| 130 | + List<StoredSession> rows = jdbcTemplate.query( |
| 131 | + """ |
| 132 | + SELECT session_id, player_name, book_path, book_title, current_scene, |
| 133 | + life, knowledge, courage, focus, score, correct_answers, |
| 134 | + discoveries, completed, inventory_json, narrative_memory_json, |
| 135 | + challenge_attempts, challenge_correct, last_message |
| 136 | + FROM game_sessions |
| 137 | + WHERE session_id = ? |
| 138 | + """, |
| 139 | + (rs, rowNum) -> { |
| 140 | + GameSession session = new GameSession(); |
| 141 | + session.setPlayerName(rs.getString("player_name")); |
| 142 | + session.setBookPath(rs.getString("book_path")); |
| 143 | + session.setBookTitle(rs.getString("book_title")); |
| 144 | + session.setCurrentScene(rs.getInt("current_scene")); |
| 145 | + session.setLife(rs.getInt("life")); |
| 146 | + session.setKnowledge(rs.getInt("knowledge")); |
| 147 | + session.setCourage(rs.getInt("courage")); |
| 148 | + session.setFocus(rs.getInt("focus")); |
| 149 | + session.setScore(rs.getInt("score")); |
| 150 | + session.setCorrectAnswers(rs.getInt("correct_answers")); |
| 151 | + session.setDiscoveries(rs.getInt("discoveries")); |
| 152 | + session.setCompleted(rs.getBoolean("completed")); |
| 153 | + session.replaceInventory(fromJson(rs.getString("inventory_json"))); |
| 154 | + return new StoredSession( |
| 155 | + rs.getString("session_id"), |
| 156 | + session, |
| 157 | + fromJson(rs.getString("narrative_memory_json")), |
| 158 | + rs.getInt("challenge_attempts"), |
| 159 | + rs.getInt("challenge_correct"), |
| 160 | + rs.getString("last_message") |
| 161 | + ); |
| 162 | + }, |
| 163 | + sessionId |
| 164 | + ); |
| 165 | + return rows.stream().findFirst(); |
| 166 | + } |
| 167 | + |
| 168 | + private String toJson(Map<String, Integer> value) { |
| 169 | + try { |
| 170 | + return objectMapper.writeValueAsString(value == null ? Map.of() : value); |
| 171 | + } catch (JsonProcessingException ex) { |
| 172 | + throw new IllegalStateException("No se pudo serializar sesion runtime.", ex); |
| 173 | + } |
| 174 | + } |
| 175 | + |
| 176 | + private Map<String, Integer> fromJson(String value) { |
| 177 | + try { |
| 178 | + if (value == null || value.isBlank()) { |
| 179 | + return new LinkedHashMap<>(); |
| 180 | + } |
| 181 | + return objectMapper.readValue(value, MAP_TYPE); |
| 182 | + } catch (JsonProcessingException ex) { |
| 183 | + throw new IllegalStateException("No se pudo deserializar sesion runtime.", ex); |
| 184 | + } |
| 185 | + } |
| 186 | + |
| 187 | + private String safeMessage(String value) { |
| 188 | + if (value == null || value.isBlank()) { |
| 189 | + return "Sesion restaurada."; |
| 190 | + } |
| 191 | + return value.length() > 1990 ? value.substring(0, 1990) : value; |
| 192 | + } |
| 193 | + |
| 194 | + public record StoredSession( |
| 195 | + String sessionId, |
| 196 | + GameSession session, |
| 197 | + Map<String, Integer> narrativeMemory, |
| 198 | + int challengeAttempts, |
| 199 | + int challengeCorrect, |
| 200 | + String lastMessage |
| 201 | + ) { |
| 202 | + } |
| 203 | +} |
0 commit comments