Skip to content

Commit

Permalink
figure movement wip
Browse files Browse the repository at this point in the history
  • Loading branch information
Til7701 committed Mar 15, 2024
1 parent b71b65c commit 73f0fc8
Show file tree
Hide file tree
Showing 18 changed files with 168 additions and 10 deletions.
15 changes: 13 additions & 2 deletions client/src/main/java/org/schlunzis/kurtama/client/fx/TilePane.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,29 @@

import javafx.scene.control.Label;
import javafx.scene.layout.AnchorPane;
import lombok.extern.slf4j.Slf4j;
import org.schlunzis.kurtama.client.service.IGameService;
import org.schlunzis.kurtama.common.game.model.ITileDTO;


@Slf4j
public class TilePane extends AnchorPane {

private final IGameService gameService;

private final Label label = new Label();
private ITileDTO tileDTO;

public TilePane(ITileDTO tileDTO) {
public TilePane(ITileDTO tileDTO, IGameService gameService) {
this.tileDTO = tileDTO;
this.gameService = gameService;
label.setText(String.valueOf(tileDTO.id()));
getChildren().add(label);
if (!tileDTO.figures().isEmpty())
this.setStyle("-fx-background-color: red;");
this.setOnMouseClicked(event -> {
log.info("Tile " + tileDTO.id() + " clicked");
gameService.sendMoveRequest(tileDTO.id());
});
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ 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]), x, y);
terrainGrid.add(new TilePane(gameState.terrain().tiles()[x][y], gameService), x, y);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@ public interface IGameService {

ObjectProperty<IGameStateDTO> getGameState();

void sendMoveRequest(int id);
}

Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,42 @@
import javafx.beans.property.ObjectProperty;
import javafx.beans.property.SimpleObjectProperty;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import org.schlunzis.kurtama.client.service.IGameService;
import org.schlunzis.kurtama.common.game.model.IGameStateDTO;
import org.schlunzis.kurtama.common.messages.game.client.MoveRequest;
import org.schlunzis.kurtama.common.messages.game.server.GameStartedMessage;
import org.schlunzis.kurtama.common.messages.game.server.UpdateGameStateMessage;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Service;

import java.util.UUID;

@Getter
@Service
@RequiredArgsConstructor
class GameService implements IGameService {

@Getter
private final ApplicationEventPublisher eventBus;

private final ObjectProperty<IGameStateDTO> gameState = new SimpleObjectProperty<>();
private UUID gameID;

@EventListener
public void onGameStartedMessage(GameStartedMessage gsm) {
gameState.set(gsm.gameState());
gameID = gsm.gameID();
}

@EventListener
public void onUpdateGameStateMessage(UpdateGameStateMessage gsm) {
gameState.set(gsm.getGameState());
}

@Override
public void sendMoveRequest(int id) {
eventBus.publishEvent(new MoveRequest(gameID, id));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@

import com.fasterxml.jackson.annotation.JsonTypeInfo;
import com.fasterxml.jackson.databind.annotation.JsonTypeIdResolver;
import org.schlunzis.kurtama.common.IUser;
import org.schlunzis.kurtama.common.util.InheritanceTypeIdResolver;

import java.util.List;

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

int[] neighbours();

List<IUser> figures();

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

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) implements ITileDTO {
public record SquareTileDTO(int id, int[] neighbours, List<IUser> figures) implements ITileDTO {

@Override
public String toString() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package org.schlunzis.kurtama.common.messages.game.client;

import lombok.Getter;
import lombok.RequiredArgsConstructor;
import org.schlunzis.kurtama.common.messages.IClientMessage;

import java.util.UUID;

@Getter
@RequiredArgsConstructor
public abstract class AbstractGameRequest implements IClientMessage {

private final UUID gameID;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package org.schlunzis.kurtama.common.messages.game.client;

import lombok.Getter;

import java.util.UUID;

@Getter
public class MoveRequest extends AbstractGameRequest {

private final int fieldIndex;

public MoveRequest(UUID gameID, int fieldIndex) {
super(gameID);
this.fieldIndex = fieldIndex;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package org.schlunzis.kurtama.common.messages.game.server;

import lombok.RequiredArgsConstructor;
import org.schlunzis.kurtama.common.messages.IServerMessage;

import java.util.UUID;

@RequiredArgsConstructor
public abstract class AbstractGameMessage implements IServerMessage {

private final UUID gameID;

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import org.schlunzis.kurtama.common.game.model.IGameStateDTO;
import org.schlunzis.kurtama.common.messages.IServerMessage;

public record GameStartedMessage(IGameStateDTO gameState) implements IServerMessage {
import java.util.UUID;

public record GameStartedMessage(UUID gameID, IGameStateDTO gameState) implements IServerMessage {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package org.schlunzis.kurtama.common.messages.game.server;

import lombok.Getter;
import org.schlunzis.kurtama.common.game.model.IGameStateDTO;

import java.util.UUID;

@Getter
public class UpdateGameStateMessage extends AbstractGameMessage {

private final IGameStateDTO gameState;

public UpdateGameStateMessage(UUID gameID, IGameStateDTO gameState) {
super(gameID);
this.gameState = gameState;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import lombok.Getter;
import lombok.RequiredArgsConstructor;
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;

Expand All @@ -13,4 +15,10 @@ public class Game {
private final UUID id;
private final SquareGameState gameState;

public void move(ServerUser user, int fieldIndex) {
SquareTile currentTile = gameState.findTileWithFigureOfUser(user);
currentTile.getFigures().removeIf(figure -> figure.equals(user));
gameState.getTerrain().get(fieldIndex).getFigures().add(user);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.schlunzis.kurtama.common.game.GameSettings;
import org.schlunzis.kurtama.common.messages.game.client.MoveRequest;
import org.schlunzis.kurtama.common.messages.game.client.StartGameRequest;
import org.schlunzis.kurtama.common.messages.game.server.GameStartedMessage;
import org.schlunzis.kurtama.common.messages.game.server.UpdateGameStateMessage;
import org.schlunzis.kurtama.server.game.model.SquareGameState;
import org.schlunzis.kurtama.server.service.ClientMessageContext;
import org.springframework.context.event.EventListener;
Expand All @@ -26,7 +28,19 @@ public void onStartGameRequest(ClientMessageContext<StartGameRequest> cmc) {
Game game = gameManagement.createGame(gameSettings);
SquareGameState gameState = game.getGameState();

cmc.respond(new GameStartedMessage(gameState.toDTO()));
cmc.respond(new GameStartedMessage(game.getId(), gameState.toDTO()));
cmc.close();
}

@EventListener
public void onMoveRequest(ClientMessageContext<MoveRequest> cmc) {
MoveRequest request = cmc.getClientMessage();
log.info("Move request: {}", request);

Game game = gameManagement.getGame(request.getGameID());
game.move(cmc.getUser(), request.getFieldIndex());

cmc.respond(new UpdateGameStateMessage(game.getId(), game.getGameState().toDTO()));
cmc.close();
}

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

import org.schlunzis.kurtama.common.game.model.ITileDTO;
import org.schlunzis.kurtama.server.user.ServerUser;

import java.util.List;

Expand All @@ -12,7 +13,7 @@ public interface ITile {

void put(IDirection direction, ITile tile);

List<Team> getFigures();
List<ServerUser> getFigures();

List<Team> getStreets(IDirection direction);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import lombok.AllArgsConstructor;
import lombok.Getter;
import org.schlunzis.kurtama.common.game.model.SquareGameStateDTO;
import org.schlunzis.kurtama.server.user.ServerUser;

import java.util.List;

Expand All @@ -17,4 +18,8 @@ public SquareGameStateDTO toDTO() {
return new SquareGameStateDTO(terrain.toDTO());
}

public SquareTile findTileWithFigureOfUser(ServerUser user) {
return terrain.findTileWithFigureOfUser(user);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
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.server.user.ServerUser;

public class SquareTerrain implements ITerrain {

Expand Down Expand Up @@ -32,4 +33,15 @@ public ITerrainDTO toDTO() {
return new SquareTerrainDTO(columns, rows, (SquareTileDTO[][]) tileDTOs);
}

public SquareTile findTileWithFigureOfUser(ServerUser user) {
for (int x = 0; x < columns; x++) {
for (int y = 0; y < rows; y++) {
if (tiles[x][y].hasFigureOfUser(user)) {
return tiles[x][y];
}
}
}
return null;
}

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

import lombok.Getter;
import org.schlunzis.kurtama.common.IUser;
import org.schlunzis.kurtama.common.game.model.ITileDTO;
import org.schlunzis.kurtama.common.game.model.SquareTileDTO;
import org.schlunzis.kurtama.server.user.ServerUser;

import java.lang.reflect.Array;
import java.util.ArrayList;
Expand All @@ -15,7 +17,7 @@ public class SquareTile implements ITile {

private final ITile[] neighbours = new ITile[SquareDirection.values().length];
@Getter
private final List<Team> figures = new ArrayList<>();
private final List<ServerUser> figures = new ArrayList<>();
@Getter
private final List<Team> villages = new ArrayList<>();
@Getter
Expand Down Expand Up @@ -66,7 +68,15 @@ public ITileDTO toDTO() {
for (int i = 0; i < neighbours.length; i++) {
neighbourIds[i] = neighbours[i] == null ? -1 : neighbours[i].getId();
}
return new SquareTileDTO(id, neighbourIds);
List<IUser> figureDTOs = new ArrayList<>();
for (ServerUser figure : figures) {
figureDTOs.add(figure.toDTO());
}
return new SquareTileDTO(id, neighbourIds, figureDTOs);
}

public boolean hasFigureOfUser(ServerUser user) {
return figures.contains(user);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
import org.schlunzis.kurtama.common.IUser;
import org.schlunzis.kurtama.common.UserDTO;
Expand All @@ -11,8 +12,10 @@
@Data
@NoArgsConstructor
@AllArgsConstructor
@EqualsAndHashCode(onlyExplicitlyIncluded = true)
public class ServerUser implements IUser {

@EqualsAndHashCode.Include
private UUID id;
private String email;
private String username;
Expand Down

0 comments on commit 73f0fc8

Please sign in to comment.