Moved to Hibernate
This commit is contained in:
@@ -1,27 +1,49 @@
|
||||
package com.bernard.nodecames.model;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.UUID;
|
||||
|
||||
import org.hibernate.annotations.JdbcTypeCode;
|
||||
import org.hibernate.type.SqlTypes;
|
||||
|
||||
import jakarta.annotation.Nullable;
|
||||
import jakarta.persistence.AttributeConverter;
|
||||
import jakarta.persistence.Column;
|
||||
import jakarta.persistence.Convert;
|
||||
import jakarta.persistence.Embedded;
|
||||
import jakarta.persistence.Entity;
|
||||
import jakarta.persistence.Id;
|
||||
import jakarta.persistence.ManyToOne;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Getter;
|
||||
import lombok.NoArgsConstructor;
|
||||
import lombok.Setter;
|
||||
import tools.jackson.databind.JsonNode;
|
||||
import tools.jackson.databind.node.ArrayNode;
|
||||
import tools.jackson.databind.node.JsonNodeFactory;
|
||||
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
@Getter
|
||||
@Setter
|
||||
@Entity
|
||||
public class Grid {
|
||||
|
||||
@Id
|
||||
UUID id;
|
||||
|
||||
Card[] cards;
|
||||
@ManyToOne(optional = true)
|
||||
World w;
|
||||
|
||||
//@Type(JsonType.class)
|
||||
@Column(columnDefinition = "json")
|
||||
@Convert(converter = GameEventListAdapter.class)
|
||||
@JdbcTypeCode(SqlTypes.JSON)
|
||||
List<GameEvent> gameEvents;
|
||||
|
||||
@Embedded
|
||||
Hint currentHint = null;
|
||||
int currentGuessCount;
|
||||
|
||||
@@ -29,10 +51,18 @@ public class Grid {
|
||||
int usedWhites;
|
||||
|
||||
Phase phase;
|
||||
Optional<Boolean> won;
|
||||
|
||||
//TODO make this parameter a real Optional<Boolean> with Jdbc understanding that optional means nullable
|
||||
@Nullable
|
||||
Boolean won;
|
||||
|
||||
@Embedded
|
||||
GridConfig config;
|
||||
|
||||
public Grid(UUID id, World w, Phase phase, GridConfig config) {
|
||||
this(id, w, List.of(), null, 0, 0, 0, phase, null, config);
|
||||
}
|
||||
|
||||
public void incrementHintCount() {
|
||||
this.setUsedHints(this.getUsedHints()+1);
|
||||
}
|
||||
@@ -43,10 +73,14 @@ public class Grid {
|
||||
this.setCurrentGuessCount(this.getCurrentGuessCount()+1);
|
||||
}
|
||||
public void newGameEvent(GameEvent event) {
|
||||
this.gameEvents = new ArrayList<>(gameEvents);
|
||||
this.gameEvents.add(event);
|
||||
}
|
||||
public int getCardCount() {
|
||||
return this.getCards().length;
|
||||
public Optional<Boolean> isWonOpt() {
|
||||
return Optional.ofNullable(this.won);
|
||||
}
|
||||
public void setWonOpt(Optional<Boolean> won) {
|
||||
this.won = won.orElse(null);
|
||||
}
|
||||
|
||||
@AllArgsConstructor
|
||||
@@ -124,4 +158,24 @@ public class Grid {
|
||||
}
|
||||
}
|
||||
|
||||
@AllArgsConstructor
|
||||
public static class GameEventListAdapter implements AttributeConverter<List<GameEvent>, JsonNode> {
|
||||
|
||||
JsonNodeFactory jsn;
|
||||
|
||||
@Override
|
||||
public JsonNode convertToDatabaseColumn(List<GameEvent> attribute) {
|
||||
ArrayNode out = jsn.arrayNode(attribute.size());
|
||||
for(GameEvent ge : attribute) {
|
||||
out.add(GameEvent.toJsonNode(ge));
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<GameEvent> convertToEntityAttribute(JsonNode dbData) {
|
||||
return dbData.asArray().valueStream().map(GameEvent::fromJsonNode).toList();
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user