2025-03-05 11:39:47 +01:00
|
|
|
package de.towerdefence.server.match;
|
|
|
|
|
2025-03-13 12:08:07 +01:00
|
|
|
import de.towerdefence.server.match.callbacks.PlayerHitpointsCallback;
|
2025-03-12 14:34:55 +01:00
|
|
|
import de.towerdefence.server.match.callbacks.PlayerMoneyCallback;
|
|
|
|
import de.towerdefence.server.match.exeptions.InvalidPlacementException;
|
|
|
|
import de.towerdefence.server.match.exeptions.InvalidPlacementReason;
|
|
|
|
import de.towerdefence.server.match.exeptions.NotInMatchException;
|
2025-03-05 11:39:47 +01:00
|
|
|
import de.towerdefence.server.player.Player;
|
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.Map;
|
2025-03-12 12:15:25 +01:00
|
|
|
import java.util.Optional;
|
2025-03-12 15:10:57 +01:00
|
|
|
import java.util.concurrent.Executors;
|
|
|
|
import java.util.concurrent.ScheduledExecutorService;
|
|
|
|
import java.util.concurrent.ScheduledFuture;
|
|
|
|
import java.util.concurrent.TimeUnit;
|
|
|
|
|
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
|
|
import de.towerdefence.server.game.GameSession;
|
|
|
|
import de.towerdefence.server.game.Tower;
|
|
|
|
import de.towerdefence.server.game.callbacks.PlayerMoneyCallback;
|
|
|
|
import de.towerdefence.server.game.exeptions.InvalidPlacementException;
|
|
|
|
import de.towerdefence.server.game.exeptions.InvalidPlacementReason;
|
|
|
|
import de.towerdefence.server.player.Player;
|
2025-03-05 11:39:47 +01:00
|
|
|
|
|
|
|
@Service
|
|
|
|
public class MatchService {
|
|
|
|
private final Map<Player, Match> playerMatches = new HashMap<>();
|
2025-03-12 12:15:25 +01:00
|
|
|
private final Map<Match, ScheduledFuture<?>> moneyTasks = new HashMap<>();
|
|
|
|
private static final ScheduledExecutorService scheduler = Executors.newSingleThreadScheduledExecutor();
|
2025-03-05 11:39:47 +01:00
|
|
|
|
|
|
|
public void createMatch(String matchId, Player player1, Player player2) {
|
|
|
|
Match match = new Match(matchId, player1, player2);
|
|
|
|
playerMatches.put(player1, match);
|
|
|
|
playerMatches.put(player2, match);
|
|
|
|
}
|
|
|
|
|
|
|
|
public Match get(Player player) {
|
|
|
|
return playerMatches.get(player);
|
|
|
|
}
|
2025-03-10 13:39:14 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @return opponent
|
|
|
|
*/
|
2025-03-12 14:34:55 +01:00
|
|
|
public Player placeTower(Player player, int x, int y) throws NotInMatchException, InvalidPlacementException {
|
|
|
|
Match match = playerMatches.get(player);
|
|
|
|
if (!match.hasMatchStarted()) {
|
|
|
|
throw new InvalidPlacementException(InvalidPlacementReason.MATCH_NOT_STARTED);
|
|
|
|
}
|
|
|
|
Optional<GameSession> playerSession = match.getPlayerGameSession(player);
|
|
|
|
if (playerSession.isEmpty()) {
|
|
|
|
throw new NotInMatchException();
|
|
|
|
}
|
|
|
|
Optional<Player> opponent = match.getOpponent(player);
|
|
|
|
if (opponent.isEmpty()) {
|
|
|
|
throw new NotInMatchException();
|
|
|
|
}
|
|
|
|
playerSession.get().placeTower(new Tower(), x, y);
|
|
|
|
return opponent.get();
|
2025-03-12 12:15:25 +01:00
|
|
|
}
|
|
|
|
|
2025-03-13 12:08:07 +01:00
|
|
|
public void playerConnected(
|
|
|
|
Player player,
|
|
|
|
PlayerMoneyCallback moneyCallback,
|
|
|
|
PlayerHitpointsCallback hitpointsCallback
|
|
|
|
) {
|
2025-03-12 12:15:25 +01:00
|
|
|
Match match = playerMatches.get(player);
|
2025-03-13 12:08:07 +01:00
|
|
|
match.connectPlayer(player, moneyCallback, hitpointsCallback);
|
2025-03-12 14:34:55 +01:00
|
|
|
Optional<GameSession> optionalPlayerSession = match.getPlayerGameSession(player);
|
|
|
|
if (optionalPlayerSession.isEmpty()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
GameSession playerSession = optionalPlayerSession.get();
|
|
|
|
moneyCallback.call(player, playerSession.getMoney());
|
2025-03-13 12:08:07 +01:00
|
|
|
hitpointsCallback.call(player, playerSession.getPlayerHitpoints() );
|
2025-03-12 14:34:55 +01:00
|
|
|
if (!match.hasMatchStarted()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
Optional<Player> optionalOpponent = match.getOpponent(player);
|
|
|
|
if (optionalOpponent.isEmpty()) {
|
2025-03-12 12:15:25 +01:00
|
|
|
return;
|
|
|
|
}
|
2025-03-12 14:34:55 +01:00
|
|
|
Player opponent = optionalOpponent.get();
|
|
|
|
Optional<GameSession> optionalOpponentSession = match.getPlayerGameSession(opponent);
|
|
|
|
if (optionalOpponentSession.isEmpty()) {
|
2025-03-12 12:15:25 +01:00
|
|
|
return;
|
|
|
|
}
|
2025-03-12 14:34:55 +01:00
|
|
|
GameSession opponentSession = optionalOpponentSession.get();
|
2025-03-12 12:15:25 +01:00
|
|
|
ScheduledFuture<?> moneyTask = scheduler.scheduleAtFixedRate(
|
2025-03-12 14:34:55 +01:00
|
|
|
() -> {
|
|
|
|
playerSession.addMoney(3);
|
|
|
|
opponentSession.addMoney(3);
|
|
|
|
},
|
|
|
|
5,
|
|
|
|
5,
|
|
|
|
TimeUnit.SECONDS);
|
2025-03-12 12:15:25 +01:00
|
|
|
moneyTasks.put(match, moneyTask);
|
2025-03-10 13:39:14 +01:00
|
|
|
}
|
2025-03-05 11:39:47 +01:00
|
|
|
}
|