private PlayerWithCard findSamePlayer(List<PlayerWithCard> reserve, Player player) { GlobalPosition[] priority = new GlobalPosition[0]; switch (player.getCurrentPosition().getPositionOnField()) { case FORWARD: priority = FORWARD_PRIORITY; break; case MIDFIELDER: priority = MIDFIELDIER_PRORITY; break; case DEFENDER: priority = DEFENDER_PRIORITY; break; case GOALKEEPER: priority = GOALKEEPER_PRIORITY; break; } Set<PlayerWithCard> candidates = new TreeSet<>(); for (GlobalPosition position : priority) { for (PlayerWithCard playerWithCard : reserve) { if (playerWithCard.player.getCurrentPosition().getPositionOnField() == position) { candidates.add(playerWithCard); } } } return candidates.iterator().next(); }
private void removePlayer(int minute, int playerIndex) { PlayerWithCard playerFrom = startPlayers.get(playerIndex); Player playerFromField = playerFrom.player; events.add(new Event(EventType.RED_CARD, minute, playerFromField, team)); playerFromField.addRedCard(); if (playerFromField.getCurrentPosition().getPositionOnField() == GlobalPosition.GOALKEEPER) { if (substitutesCount < MAX_SUBSTITUTIONS_COUNT) { PlayerWithCard reserveGoalkeeper = null; for (PlayerWithCard reservePlayer : reservePlayers) { if (reservePlayer.player.getCurrentPositionOnField() == GlobalPosition.GOALKEEPER) { reserveGoalkeeper = reservePlayer; break; } } if (reserveGoalkeeper != null) { changePlayer(minute, playerFrom); } } } startPlayers.remove(playerIndex); }
private List<Event> createEvents(int minute) { for (PlayerWithCard playerWithCard : startPlayers) { Player player = playerWithCard.player; player.addFatifue(Randomization.RANDOM.nextDouble()); player.addExperience(DEFAULT_EXPERIENCE_VALUE * experienceCoeff); } int chance = Randomization.RANDOM.nextInt(THOUSAND); if (chance < YELLOW_CARD_CHANCE) { int playerIndex = Randomization.RANDOM.nextInt(startPlayers.size()); PlayerWithCard playerWithCard = startPlayers.get(playerIndex); Player player = playerWithCard.player; player.addYellowCard(); events.add(new Event(EventType.YELLOW_CARD, minute, player, team)); if (playerWithCard.yellowCardsCount == 1) { removePlayer(minute, playerIndex); } else { ++playerWithCard.yellowCardsCount; } } else if (chance < RED_CARD_CHANCE) { int playerIndex = Randomization.RANDOM.nextInt(startPlayers.size()); removePlayer(minute, playerIndex); } else if (chance < INJURE_CHANCE) { int playerIndex = Randomization.RANDOM.nextInt(startPlayers.size()); PlayerWithCard injured = startPlayers.get(playerIndex); Player injuredPlayer = injured.player; events.add(new Event(EventType.INJURE, minute, injuredPlayer, team)); injuredPlayer.setInjured(InjureType.getInjure(Randomization.RANDOM.nextInt(HUNDRED))); if (substitutesCount < MAX_SUBSTITUTIONS_COUNT) { changePlayer(minute, injured); } } if (minute > PREFERRED_TIME_TO_CHANGE_PLAYER) { if (chance < SUBSTITUTE_CHANCE && substitutesCount < MAX_SUBSTITUTIONS_COUNT) { PlayerWithCard mostTired = findMostTired(startPlayers); changePlayer(minute, mostTired); } } if (hasGoals && minutesForGoal[minuteForGoalIndex] == minute) { scoreGoal(minute); } return events; }