Skip to content

Commit

Permalink
adding edges to the game model (#181)
Browse files Browse the repository at this point in the history
* adding edges to the game model

* remove debug logging

* remove equals and hashcode and toString in records
  • Loading branch information
Til7701 authored Mar 25, 2024
1 parent b535c9c commit 76a2739
Show file tree
Hide file tree
Showing 17 changed files with 278 additions and 161 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,25 @@
import javafx.scene.layout.AnchorPane;
import lombok.extern.slf4j.Slf4j;
import org.schlunzis.kurtama.client.service.IGameService;
import org.schlunzis.kurtama.common.game.model.EdgeDTO;
import org.schlunzis.kurtama.common.game.model.ITileDTO;

import java.util.Arrays;

@Slf4j
public class TilePane extends AnchorPane {

private final IGameService gameService;

private final Label label = new Label();
private ITileDTO tileDTO;
private EdgeDTO[] edges;

public TilePane(ITileDTO tileDTO, IGameService gameService) {
public TilePane(ITileDTO tileDTO, EdgeDTO[] edges, IGameService gameService) {
this.tileDTO = tileDTO;
this.edges = edges;
this.gameService = gameService;
label.setText(String.valueOf(tileDTO.id()));
label.setText(tileDTO.id() + " " + Arrays.stream(edges).reduce("", (acc, edge) -> acc + edge.secondTileIndex() + ", ", String::concat));
getChildren().add(label);
StringBuilder styleBuilder = new StringBuilder();
if (!tileDTO.figures().isEmpty()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@
import net.rgielen.fxweaver.core.FxmlView;
import org.schlunzis.kurtama.client.fx.TilePane;
import org.schlunzis.kurtama.client.service.IGameService;
import org.schlunzis.kurtama.common.game.model.EdgeDTO;
import org.schlunzis.kurtama.common.game.model.IGameStateDTO;
import org.schlunzis.kurtama.common.game.model.ITileDTO;
import org.springframework.stereotype.Component;

@Slf4j
Expand Down Expand Up @@ -56,7 +58,9 @@ private void updateTerrain(IGameStateDTO gameState) {

for (int x = 0; x < gameState.terrain().width(); x++) {
for (int y = 0; y < gameState.terrain().height(); y++) {
terrainGrid.add(new TilePane(gameState.terrain().tiles()[x][y], gameService), x, y);
ITileDTO tile = gameState.terrain().tiles()[x][y];
EdgeDTO[] edges = gameState.terrain().edges().stream().filter(edge -> edge.firstTileIndex() == tile.id()).toArray(EdgeDTO[]::new);
terrainGrid.add(new TilePane(tile, edges, gameService), x, y);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package org.schlunzis.kurtama.common.game.model;

import org.schlunzis.kurtama.common.IUser;

import java.util.List;

/**
* Represents an edge between two tiles. An edge has a unique id and connects two tiles.
*
* @param id the unique id of the edge
* @param firstTileIndex the index of the first tile
* @param secondTileIndex the index of the second tile
* @param streets the list of users that have built a street on this edge
*/
public record EdgeDTO(int id,
int firstTileIndex,
int secondTileIndex,
List<IUser> streets
) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import com.fasterxml.jackson.databind.annotation.JsonTypeIdResolver;
import org.schlunzis.kurtama.common.util.InheritanceTypeIdResolver;

import java.util.List;

@JsonTypeInfo(use = JsonTypeInfo.Id.CUSTOM, property = "discriminator")
@JsonTypeIdResolver(InheritanceTypeIdResolver.class)
public interface ITerrainDTO {
Expand All @@ -14,4 +16,6 @@ public interface ITerrainDTO {

ITileDTO[][] tiles();

List<EdgeDTO> edges();

}
Original file line number Diff line number Diff line change
@@ -1,32 +1,10 @@
package org.schlunzis.kurtama.common.game.model;

import java.util.Arrays;
import java.util.Objects;

public record SquareTerrainDTO(int width, int height, SquareTileDTO[][] tiles) implements ITerrainDTO {

@Override
public String toString() {
return "SquareTerrainDTO{" +
"width=" + width +
", height=" + height +
", tiles=" + Arrays.toString(tiles) +
'}';
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
SquareTerrainDTO that = (SquareTerrainDTO) o;
return width == that.width && height == that.height && Arrays.equals(tiles, that.tiles);
}

@Override
public int hashCode() {
int result = Objects.hash(width, height);
result = 31 * result + Arrays.hashCode(tiles);
return result;
}
import java.util.List;

public record SquareTerrainDTO(int width,
int height,
SquareTileDTO[][] tiles,
List<EdgeDTO> edges
) implements ITerrainDTO {
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,7 @@

import org.schlunzis.kurtama.common.IUser;

import java.util.Arrays;
import java.util.List;
import java.util.Objects;

public record SquareTileDTO(int id, int[] neighbours, List<IUser> figures) implements ITileDTO {

@Override
public String toString() {
return "SquareTileDTO{" +
"id=" + id +
", neighbours=" + Arrays.toString(neighbours) +
'}';
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
SquareTileDTO that = (SquareTileDTO) o;
return id == that.id && Arrays.equals(neighbours, that.neighbours);
}

@Override
public int hashCode() {
int result = Objects.hash(id);
result = 31 * result + Arrays.hashCode(neighbours);
return result;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@

import lombok.Getter;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.schlunzis.kurtama.server.game.model.SquareGameState;
import org.schlunzis.kurtama.server.game.model.SquareTile;
import org.schlunzis.kurtama.server.user.ServerUser;

import java.util.UUID;

@Slf4j
@Getter
@RequiredArgsConstructor
public class Game {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import org.schlunzis.kurtama.common.game.GameSettings;
import org.schlunzis.kurtama.server.game.model.SquareGameState;
import org.schlunzis.kurtama.server.game.model.SquareTerrain;
import org.schlunzis.kurtama.server.game.model.factory.SquareTerrainFactory;
import org.springframework.stereotype.Component;

import java.util.Collections;
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package org.schlunzis.kurtama.server.game.model;


import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import org.springframework.data.util.Pair;

import java.util.NoSuchElementException;

@AllArgsConstructor(access = AccessLevel.PRIVATE)
public class Edge {

private final ITile tile;
private final EdgeData data;

/**
* Create a pair of edges with the given parameters. Both edges will reference the same data object.
*
* @param id the id of the edges
* @param firstTile the firstTileIndex tile
* @param firstDirection the direction, in which the firstTileIndex tile can access the secondTileIndex tile
* @param secondTile the secondTileIndex tile
* @param secondDirection the direction, in which the secondTileIndex tile can access the firstTileIndex tile
* @return a pair of edges
*/
public static Pair<Edge, Edge> create(int id, ITile firstTile, IDirection firstDirection, ITile secondTile, IDirection secondDirection) {
EdgeData data = new EdgeData(id, firstTile, firstDirection, secondTile, secondDirection);
Edge first = new Edge(firstTile, data);
Edge second = new Edge(secondTile, data);
return Pair.of(first, second);
}

public ITile next() throws NoSuchElementException {
if (tile == null)
throw new NoSuchElementException();
return tile;
}

public boolean hasNext() {
return tile != null;
}

public EdgeData data() {
return data;
}

}

Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package org.schlunzis.kurtama.server.game.model;

import lombok.Getter;
import lombok.RequiredArgsConstructor;
import org.schlunzis.kurtama.common.IUser;
import org.schlunzis.kurtama.common.game.model.EdgeDTO;
import org.schlunzis.kurtama.server.user.ServerUser;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@Getter
@RequiredArgsConstructor
public class EdgeData {

private final int id;

// these references are redundant, but are useful to create the DTO
private final ITile first;
private final IDirection firstDirection;
private final ITile second;
private final IDirection secondDirection;

private final List<ServerUser> streets = new ArrayList<>();

public EdgeDTO toDTO() {
List<IUser> streetDTOs = this.streets.stream().<IUser>map(ServerUser::toDTO).toList();
return new EdgeDTO(id, first.getId(), second.getId(), streetDTOs);
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
EdgeData edgeData = (EdgeData) o;
return id == edgeData.id;
}

@Override
public int hashCode() {
return Objects.hash(id);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,10 @@ public interface ITile {

ITile get(IDirection direction);

void put(IDirection direction, ITile tile);
void put(IDirection direction, Edge edge);

List<ServerUser> getFigures();

List<Team> getStreets(IDirection direction);

void addStreet(Team team, IDirection direction);

void clearStreets();

ITileDTO toDTO();

}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,26 @@
package org.schlunzis.kurtama.server.game.model;

import org.schlunzis.kurtama.common.game.model.ITerrainDTO;
import org.schlunzis.kurtama.common.game.model.ITileDTO;
import org.schlunzis.kurtama.common.game.model.SquareTerrainDTO;
import org.schlunzis.kurtama.common.game.model.SquareTileDTO;
import org.schlunzis.kurtama.common.game.model.*;
import org.schlunzis.kurtama.server.user.ServerUser;

import java.util.List;
import java.util.stream.Collectors;

public class SquareTerrain implements ITerrain {

private final SquareTile[][] tiles;
private final int columns;
private final int rows;
/**
* Unmodifiable List of all edges in the terrain. This list is used to create the DTO.
*/
private final List<EdgeData> edgeDataList;

public SquareTerrain(SquareTile[][] tiles) {
public SquareTerrain(SquareTile[][] tiles, List<EdgeData> edgeDataList) {
this.tiles = tiles;
this.columns = tiles.length;
this.rows = tiles[0].length;
this.edgeDataList = edgeDataList;
}

public ITile get(int index) {
Expand All @@ -30,7 +35,8 @@ public ITerrainDTO toDTO() {
tileDTOs[x][y] = tiles[x][y].toDTO();
}
}
return new SquareTerrainDTO(columns, rows, (SquareTileDTO[][]) tileDTOs);
List<EdgeDTO> edgeDTOs = edgeDataList.stream().map(EdgeData::toDTO).toList();
return new SquareTerrainDTO(columns, rows, (SquareTileDTO[][]) tileDTOs, edgeDTOs);
}

public SquareTile findTileWithFigureOfUser(ServerUser user) {
Expand Down
Loading

0 comments on commit 76a2739

Please sign in to comment.