-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
adding edges to the game model (#181)
* adding edges to the game model * remove debug logging * remove equals and hashcode and toString in records
- Loading branch information
Showing
17 changed files
with
278 additions
and
161 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 20 additions & 0 deletions
20
common/src/main/java/org/schlunzis/kurtama/common/game/model/EdgeDTO.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
) { | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 6 additions & 28 deletions
34
common/src/main/java/org/schlunzis/kurtama/common/game/model/SquareTerrainDTO.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 { | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 0 additions & 57 deletions
57
server/src/main/java/org/schlunzis/kurtama/server/game/SquareTerrainFactory.java
This file was deleted.
Oops, something went wrong.
48 changes: 48 additions & 0 deletions
48
server/src/main/java/org/schlunzis/kurtama/server/game/model/Edge.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
} | ||
|
44 changes: 44 additions & 0 deletions
44
server/src/main/java/org/schlunzis/kurtama/server/game/model/EdgeData.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.