package uulm.teamname.marvelous.gamelibrary.gamelogic; import uulm.teamname.marvelous.gamelibrary.Tuple; import uulm.teamname.marvelous.gamelibrary.events.*; import uulm.teamname.marvelous.gamelibrary.requests.CharacterRequest; import uulm.teamname.marvelous.gamelibrary.requests.Request; import uulm.teamname.marvelous.gamelibrary.requests.RequestType; import java.util.ArrayList; /** Contains game logic handling. */ class GameLogic { /** * Produces resulting {@link Event}s from a given {@link Request} independently of any {@link GameState}. * @return The list of resulting events */ public static ArrayList executeRequest(Request request) { ArrayList result = new ArrayList<>(); switch(request.type) { case MeleeAttackRequest, RangedAttackRequest -> { CharacterRequest data = (CharacterRequest)request; result.add(new CharacterEvent() .setOriginEntity(data.originEntity) .setTargetEntity(data.targetEntity) .setOriginField(data.originField) .setTargetField(data.targetField) .setAmount(data.value) .type(request.type == RequestType.MeleeAttackRequest ? EventType.MeleeAttackEvent : EventType.RangedAttackEvent)); result.add(new EntityEvent() .setTargetEntity(data.originEntity) .setTargetField(data.originField) .setAmount(1) .type(EventType.ConsumedAPEvent)); result.add(new EntityEvent() .setTargetEntity(data.targetEntity) .setTargetField(data.targetField) .setAmount(data.value) .type(EventType.TakenDamageEvent)); } case MoveRequest -> { CharacterRequest data = (CharacterRequest)request; result.add(new CharacterEvent() .setOriginEntity(data.originEntity) .setOriginField(data.originField) .setTargetField(data.targetField) .type(EventType.MoveEvent)); result.add(new EntityEvent() .setTargetEntity(data.originEntity) .setTargetField(data.targetField) //when this event gets handled, the character already moved to the target field .setAmount(1) .type(EventType.ConsumedMPEvent)); //TODO: execute character swap in GameLogic.executeRequest } case ExchangeInfinityStoneRequest, UseInfinityStoneRequest -> { CharacterRequest data = (CharacterRequest)request; result.add(new CharacterEvent() .setOriginEntity(data.originEntity) .setTargetEntity(data.targetEntity) .setOriginField(data.originField) .setTargetField(data.targetField) .setStone(data.stoneType) .type(request.type == RequestType.ExchangeInfinityStoneRequest ? EventType.ExchangeInfinityStoneEvent : EventType.UseInfinityStoneEvent)); result.add(new EntityEvent() .setTargetEntity(data.originEntity) .setTargetField(data.originField) .setAmount(1) .type(EventType.ConsumedAPEvent)); } case DisconnectRequest -> { result.add(new GameEvent() .type(EventType.DisconnectEvent)); } } return result; } /** * Checks a {@link Request} for validity for a {@link GameState}. * @param state The game state to check on * @param request The request to validate * @return Whether or not the request is valid */ public static boolean checkRequest(GameState state, Request request) { //TODO: implement GameLogic.checkRequest return false; } /** * Applies an {@link Event} to a {@link GameState}. * @param state The game state to apply to * @param event The event to apply */ public static void applyEvent(GameState state, Event event) { //TODO: implement GameLogic.applyEvent } /** * Checks a {@link GameState} for the current overtime win condition. * @param state The game state to check * @return The {@link ParticipantType} that is currently winning the game according to overtime ruling */ public static ParticipantType checkWinConditions(GameState state) { //TODO: GameLogic.checkWinConditions is kind of ugly Tuple player1; Tuple player2; int value1; int value2; for(WinCondition condition: WinCondition.values()) { player1 = new Tuple(ParticipantType.Player1, condition); player2 = new Tuple(ParticipantType.Player2, condition); value1 = 0; value2 = 0; if(state.winConditions.containsKey(player1)) { value1 = state.winConditions.get(player1); } if(state.winConditions.containsKey(player2)) { value2 = state.winConditions.get(player2); } if(value1 > value2) { return ParticipantType.Player1; } if(value2 > value1) { return ParticipantType.Player2; } } return ParticipantType.None; } }