Skip to content

Commit

Permalink
game management and factory
Browse files Browse the repository at this point in the history
  • Loading branch information
Til7701 committed Mar 15, 2024
1 parent 0328812 commit b71b65c
Show file tree
Hide file tree
Showing 7 changed files with 131 additions and 72 deletions.
16 changes: 16 additions & 0 deletions server/src/main/java/org/schlunzis/kurtama/server/game/Game.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package org.schlunzis.kurtama.server.game;

import lombok.Getter;
import lombok.RequiredArgsConstructor;
import org.schlunzis.kurtama.server.game.model.SquareGameState;

import java.util.UUID;

@Getter
@RequiredArgsConstructor
public class Game {

private final UUID id;
private final SquareGameState gameState;

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

import lombok.RequiredArgsConstructor;
import org.schlunzis.kurtama.common.game.GameSettings;
import org.springframework.stereotype.Component;

import java.util.UUID;

@Component
@RequiredArgsConstructor
public class GameManagement {

private final GameStore gameStore;

public Game createGame(GameSettings gameSettings) {
return gameStore.create(gameSettings);
}

public void removeGame(Game game) {
gameStore.remove(game.getId());
}

public void removeGame(UUID id) {
gameStore.remove(id);
}

public Game getGame(UUID id) {
return gameStore.get(id).orElseThrow();
}

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

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.schlunzis.kurtama.common.game.GameSettings;
import org.schlunzis.kurtama.common.messages.game.client.StartGameRequest;
import org.schlunzis.kurtama.common.messages.game.server.GameStartedMessage;
import org.schlunzis.kurtama.server.game.model.SquareGameState;
import org.schlunzis.kurtama.server.game.model.SquareTerrain;
import org.schlunzis.kurtama.server.service.ClientMessageContext;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;

import java.util.Collections;

@Slf4j
@Component
@RequiredArgsConstructor
public class GameService {

private final GameManagement gameManagement;

@EventListener
public void onStartGameRequest(ClientMessageContext<StartGameRequest> cmc) {
StartGameRequest request = cmc.getClientMessage();
GameSettings gameSettings = request.gameSettings();
log.info("Starting game with settings: {}", gameSettings);

SquareTerrainFactory squareTerrainFactory = new SquareTerrainFactory(gameSettings);
SquareTerrain terrain = (SquareTerrain) squareTerrainFactory.create();
SquareGameState gameState = new SquareGameState(terrain, Collections.emptyList());
Game game = gameManagement.createGame(gameSettings);
SquareGameState gameState = game.getGameState();

cmc.respond(new GameStartedMessage(gameState.toDTO()));
cmc.close();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package org.schlunzis.kurtama.server.game;

import lombok.RequiredArgsConstructor;
import org.schlunzis.kurtama.common.game.GameSettings;
import org.springframework.stereotype.Component;

import java.util.Collection;
import java.util.Map;
import java.util.Optional;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;


@Component
@RequiredArgsConstructor
public class GameStore {

private final Map<UUID, Game> gameMap = new ConcurrentHashMap<>();
private final IGameFactory gameFactory;

public Game create(GameSettings gameSettings) {
UUID uuid;
do {
uuid = UUID.randomUUID();
} while (gameMap.containsKey(uuid));

Game game = gameFactory.create(uuid, gameSettings);
gameMap.put(uuid, game);
return game;
}

public boolean remove(UUID uuid) {
return gameMap.remove(uuid) != null;
}

public Optional<Game> get(UUID uuid) {
return Optional.ofNullable(gameMap.get(uuid));
}

public Collection<Game> getAll() {
return gameMap.values();
}

}

This file was deleted.

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

import org.schlunzis.kurtama.common.game.GameSettings;

import java.util.UUID;

public interface IGameFactory {
Game create(UUID uuid, GameSettings gameSettings);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package org.schlunzis.kurtama.server.game;

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.springframework.stereotype.Component;

import java.util.Collections;
import java.util.UUID;

@Component
public class SquareGameFactory implements IGameFactory {

@Override
public Game create(UUID id, GameSettings gameSettings) {
SquareGameState gameState = createGameState(gameSettings);
return new Game(id, gameState);
}

private SquareGameState createGameState(GameSettings gameSettings) {
SquareTerrainFactory terrainFactory = new SquareTerrainFactory(gameSettings);
return new SquareGameState((SquareTerrain) terrainFactory.create(), Collections.emptyList());
}

}

0 comments on commit b71b65c

Please sign in to comment.