112 lines
4.1 KiB
Java
112 lines
4.1 KiB
Java
|
package de.towerdefence.server.match.confirmation;
|
||
|
|
||
|
import de.towerdefence.server.player.Player;
|
||
|
import org.springframework.stereotype.Service;
|
||
|
|
||
|
import java.util.HashMap;
|
||
|
import java.util.Map;
|
||
|
import java.util.Optional;
|
||
|
|
||
|
@Service
|
||
|
public class MatchConfirmationService {
|
||
|
|
||
|
private final Map<Player, UnconfirmedMatch> unconfirmedMatch = new HashMap<>();
|
||
|
|
||
|
public UnconfirmedMatch createMatch(Player player1,
|
||
|
Player player2,
|
||
|
ConfirmationCallbacks player1Callbacks,
|
||
|
ConfirmationCallbacks player2callbacks) {
|
||
|
UnconfirmedMatch match = new UnconfirmedMatch(
|
||
|
player1,
|
||
|
player2,
|
||
|
player1Callbacks,
|
||
|
player2callbacks);
|
||
|
unconfirmedMatch.put(player1, match);
|
||
|
unconfirmedMatch.put(player2, match);
|
||
|
return match;
|
||
|
}
|
||
|
|
||
|
public void accept(Player player, String matchId) {
|
||
|
setPlayerAcceptState(player, matchId, PlayerMatchConfirmState.CONFIRMED);
|
||
|
}
|
||
|
|
||
|
public void decline(Player player, String matchId) {
|
||
|
setPlayerAcceptState(player, matchId, PlayerMatchConfirmState.ABORTED);
|
||
|
}
|
||
|
|
||
|
private void setPlayerAcceptState(Player player, String matchId, PlayerMatchConfirmState state) {
|
||
|
Optional<UnconfirmedMatch> optionalMatch = getPlayerMatch(player, matchId);
|
||
|
if (optionalMatch.isEmpty()) {
|
||
|
return;
|
||
|
}
|
||
|
UnconfirmedMatch match = optionalMatch.get();
|
||
|
|
||
|
Optional<PlayerMatchConfirmState> optionalPlayerState = match.getPlayerState(player);
|
||
|
if (optionalPlayerState.isEmpty()) {
|
||
|
unconfirmedMatch.remove(player);
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
if (optionalPlayerState.get() != PlayerMatchConfirmState.UNKNOWN) {
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
Optional<UnconfirmedMatchState> matchState = match.setPlayerConfirmState(player, state);
|
||
|
if (matchState.isEmpty()) {
|
||
|
unconfirmedMatch.remove(player);
|
||
|
return;
|
||
|
}
|
||
|
handleMatchConfirmation(match, matchState.get());
|
||
|
}
|
||
|
|
||
|
private void handleMatchConfirmation(UnconfirmedMatch match, UnconfirmedMatchState state) {
|
||
|
if (state == UnconfirmedMatchState.WAITING) {
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
unconfirmedMatch.remove(match.getPlayer1());
|
||
|
unconfirmedMatch.remove(match.getPlayer2());
|
||
|
|
||
|
ConfirmationCallbacks player1Callbacks = match.getPlayer1Callbacks();
|
||
|
ConfirmationCallbacks player2Callbacks = match.getPlayer2Callbacks();
|
||
|
|
||
|
switch (state) {
|
||
|
case ABORTED -> {
|
||
|
if (match.getPlayer1State() == PlayerMatchConfirmState.CONFIRMED) {
|
||
|
player1Callbacks.getRequeueCallback().call(
|
||
|
match.getPlayer1(),
|
||
|
match.getMatchId(),
|
||
|
player1Callbacks.getFoundCallback(),
|
||
|
player1Callbacks.getQueuedCallback(),
|
||
|
player1Callbacks.getAbortCallback(),
|
||
|
player1Callbacks.getEstablishedCallback());
|
||
|
}
|
||
|
if (match.getPlayer2State() == PlayerMatchConfirmState.CONFIRMED) {
|
||
|
player2Callbacks.getRequeueCallback().call(
|
||
|
match.getPlayer2(),
|
||
|
match.getMatchId(),
|
||
|
player2Callbacks.getFoundCallback(),
|
||
|
player2Callbacks.getQueuedCallback(),
|
||
|
player2Callbacks.getAbortCallback(),
|
||
|
player2Callbacks.getEstablishedCallback());
|
||
|
}
|
||
|
}
|
||
|
case CONFIRMED -> {
|
||
|
// TODO: Create Match and Send Players the info that the Match is created
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
private Optional<UnconfirmedMatch> getPlayerMatch(Player player, String matchId) {
|
||
|
UnconfirmedMatch match = unconfirmedMatch.get(player);
|
||
|
if (match == null) {
|
||
|
return Optional.empty();
|
||
|
}
|
||
|
if (!match.getMatchId().equals(matchId)) {
|
||
|
unconfirmedMatch.remove(player);
|
||
|
return Optional.empty();
|
||
|
}
|
||
|
return Optional.of(match);
|
||
|
}
|
||
|
}
|