Skip to content

Commit

Permalink
enhance format
Browse files Browse the repository at this point in the history
  • Loading branch information
akmsw committed Jan 8, 2024
1 parent 17bec57 commit 86c3032
Show file tree
Hide file tree
Showing 8 changed files with 82 additions and 100 deletions.
28 changes: 12 additions & 16 deletions src/main/java/armameeldoparti/controllers/AnchoragesController.java
Original file line number Diff line number Diff line change
Expand Up @@ -262,23 +262,19 @@ private void updateTextArea() {
view.getTextArea()
.append("ANCLAJE " + (anchorageNumber + 1) + System.lineSeparator());

List<Player> anchoredPlayers = CommonFields.getPlayersSets()
.entrySet()
.stream()
.flatMap(
playersSet -> playersSet.getValue()
.stream()
.filter(
player -> player.getAnchorageNumber() == (anchorageNumber + 1)
)
)
.sorted(Comparator.comparing(player -> player.getPosition()
.ordinal()))
.toList();

for (Player player : anchoredPlayers) {
List<Player> anchorage = CommonFields.getPlayersSets()
.entrySet()
.stream()
.flatMap(players -> players.getValue()
.stream()
.filter(player -> player.getAnchorageNumber() == anchorageNumber + 1))
.sorted(Comparator.comparing(player -> player.getPosition()
.ordinal()))
.toList();

for (Player player : anchorage) {
view.getTextArea()
.append((anchoredPlayers.indexOf(player) + 1) + ". " + player.getName() + System.lineSeparator());
.append((anchorage.indexOf(player) + 1) + ". " + player.getName() + System.lineSeparator());
}

if ((anchorageNumber + 1) != anchoragesAmount) {
Expand Down
10 changes: 5 additions & 5 deletions src/main/java/armameeldoparti/controllers/ResultsController.java
Original file line number Diff line number Diff line change
Expand Up @@ -233,24 +233,24 @@ private void fillTableFields() {
table.setValueAt("EQUIPO " + (teamIndex + 1), 0, teamIndex + 1);
}

for (int rowIndex = 1; rowIndex < rowCount; rowIndex++) {
for (int row = 1; row < rowCount; row++) {
table.setValueAt(
positionsMap.get(
switch (rowIndex) {
switch (row) {
case 1 -> Position.CENTRAL_DEFENDER;
case 2, 3 -> Position.LATERAL_DEFENDER;
case 4, 5 -> Position.MIDFIELDER;
default -> Position.FORWARD;
}
),
rowIndex,
row,
0
);
}

if (CommonFields.getDistribution() == Constants.MIX_BY_SKILL_POINTS) {
for (int colIndex = 0; colIndex < teams.size(); colIndex++) {
table.setValueAt(colIndex == 0 ? positionsMap.get(Position.GOALKEEPER) : "Puntuación del equipo", table.getRowCount() + colIndex - 2, 0);
for (int column = 0; column < teams.size(); column++) {
table.setValueAt(column == 0 ? positionsMap.get(Position.GOALKEEPER) : "Puntuación del equipo", table.getRowCount() + column - 2, 0);
}
} else {
table.setValueAt(positionsMap.get(Position.GOALKEEPER), table.getRowCount() - 1, 0);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package armameeldoparti.controllers;

import java.util.Arrays;
import armameeldoparti.models.Position;
import armameeldoparti.models.ProgramView;
import armameeldoparti.utils.common.CommonFields;
Expand Down Expand Up @@ -70,13 +69,14 @@ public void backButtonEvent() {
* Updates the players name labels.
*/
public void updateNameLabels() {
Arrays.asList(Position.values())
.forEach(position -> CommonFields.getPlayersSets()
.get(position)
.forEach(player -> view.getLabelsMap()
.get(view.getSpinnersMap()
.get(player))
.setText(player.getName())));
for (Position position : Position.values()) {
CommonFields.getPlayersSets()
.get(position)
.forEach(player -> view.getLabelsMap()
.get(view.getSpinnersMap()
.get(player))
.setText(player.getName()));
}

view.pack();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import javax.swing.SwingConstants;
import javax.swing.border.EmptyBorder;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.TableCellRenderer;

/**
* Custom table class.
Expand Down Expand Up @@ -44,30 +43,23 @@ public CustomTable(int rows, int columns) {
* Adjusts the cells size to fit the biggest content shown in the table.
*/
public void adjustCells() {
int totalColumns = getColumnCount();
int totalRows = getRowCount();
int maxCellWidth = 0;
int maxCellHeight = 0;

for (int row = 0; row < totalRows; row++) {
for (int column = 0; column < totalColumns; column++) {
TableCellRenderer cellRenderer = getCellRenderer(row, column);
for (int row = 0; row < getRowCount(); row++) {
for (int column = 0; column < getColumnCount(); column++) {
Component cellComponent = prepareRenderer(getCellRenderer(row, column), row, column);

Component cellComponent = prepareRenderer(cellRenderer, row, column);

int cellWidth = cellComponent.getPreferredSize().width + getIntercellSpacing().width;
int cellHeight = cellComponent.getPreferredSize().height + getIntercellSpacing().width;

maxCellWidth = Math.max(maxCellWidth, cellWidth);
maxCellHeight = Math.max(maxCellHeight, cellHeight);
maxCellWidth = Math.max(maxCellWidth, cellComponent.getPreferredSize().width + getIntercellSpacing().width);
maxCellHeight = Math.max(maxCellHeight, cellComponent.getPreferredSize().height + getIntercellSpacing().height);
}
}

for (int row = 0; row < totalRows; row++) {
for (int row = 0; row < getRowCount(); row++) {
setRowHeight(row, maxCellHeight);
}

for (int column = 0; column < totalColumns; column++) {
for (int column = 0; column < getColumnCount(); column++) {
getColumnModel().getColumn(column)
.setPreferredWidth(maxCellWidth);
}
Expand Down
20 changes: 8 additions & 12 deletions src/main/java/armameeldoparti/utils/mixers/BySkillPointsMixer.java
Original file line number Diff line number Diff line change
Expand Up @@ -97,12 +97,10 @@ public List<Team> withoutAnchorages(List<Team> teams) {
*/
@Override
public List<Team> withAnchorages(List<Team> teams) {
List<List<Player>> anchoredPlayers = CommonFunctions.getAnchoredPlayers();

for (List<Player> anchoredPlayersSet : anchoredPlayers) {
for (List<Player> anchorage : CommonFunctions.getAnchoredPlayers()) {
teams.sort(comparingInt(Team::getTeamSkill));

for (Player player : anchoredPlayersSet) {
for (Player player : anchorage) {
player.setTeamNumber(teams.get(0)
.getTeamNumber());

Expand All @@ -123,16 +121,16 @@ public List<Team> withAnchorages(List<Team> teams) {

remainingPlayers.sort(comparingInt(List::size));

for (List<Player> playersSet : remainingPlayers) {
playersSet.sort(comparingInt(Player::getSkillPoints).reversed());
for (List<Player> players : remainingPlayers) {
players.sort(comparingInt(Player::getSkillPoints).reversed());

if (playersSet.size() == 4) {
if (players.size() == 4) {
teams.sort(comparingInt(Team::getTeamSkill));

distributeSubsets(teams, playersSet, playersSet.get(0)
distributeSubsets(teams, players, players.get(0)
.getPosition());
} else {
for (Player player : playersSet) {
for (Player player : players) {
teams.sort(comparingInt(Team::getTeamSkill));

int teamNumber = 0;
Expand Down Expand Up @@ -169,9 +167,7 @@ public List<Team> withAnchorages(List<Team> teams) {
private void distributeSubsets(List<Team> teams, List<Player> playersSet, Position position) {
List<List<Player>> playersSubsets = new ArrayList<>();

int currentPositionPlayersPerTeam = playersSet.size() / 2;

for (int playerIndex = 0; playerIndex < currentPositionPlayersPerTeam; playerIndex++) {
for (int playerIndex = 0; playerIndex < playersSet.size() / 2; playerIndex++) {
playersSubsets.add(Arrays.asList(playersSet.get(playerIndex), playersSet.get(playersSet.size() - 1 - playerIndex)));
}

Expand Down
60 changes: 29 additions & 31 deletions src/main/java/armameeldoparti/utils/mixers/RandomMixer.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.function.Predicate;

Expand Down Expand Up @@ -56,36 +55,38 @@ public RandomMixer() {
public List<Team> withoutAnchorages(List<Team> teams) {
shuffleTeamNumbers(teams.size());

Map<Position, List<Player>> playersSets = CommonFields.getPlayersSets();
Map<Position, List<Player>> randomTeam1PlayersMap = teams.get(randomTeam1)
.getTeamPlayers();
Map<Position, List<Player>> randomTeam2PlayersMap = teams.get(randomTeam2)
.getTeamPlayers();

for (Position position : Position.values()) {
List<Player> playersAtPosition = new ArrayList<>(playersSets.get(position));
List<Player> playersAtPosition = new ArrayList<>(CommonFields.getPlayersSets()
.get(position));

Collections.shuffle(playersAtPosition);

randomTeam1PlayersMap.get(position)
.addAll(playersAtPosition.subList(0, playersAtPosition.size() / teams.size()));
teams.get(randomTeam1)
.getTeamPlayers()
.get(position)
.addAll(playersAtPosition.subList(0, playersAtPosition.size() / teams.size()));
}

randomTeam1PlayersMap.values()
.stream()
.flatMap(List::stream)
.forEach(player -> player.setTeamNumber(randomTeam1 + 1));
teams.get(randomTeam1)
.getTeamPlayers()
.values()
.stream()
.flatMap(List::stream)
.forEach(player -> player.setTeamNumber(randomTeam1 + 1));

playersSets.values()
.stream()
.flatMap(List::stream)
.filter(player -> player.getTeamNumber() == 0)
.forEach(player -> {
randomTeam2PlayersMap.get(player.getPosition())
.add(player);
CommonFields.getPlayersSets()
.values()
.stream()
.flatMap(List::stream)
.filter(player -> player.getTeamNumber() == 0)
.forEach(player -> {
teams.get(randomTeam2)
.getTeamPlayers()
.get(player.getPosition())
.add(player);

player.setTeamNumber(randomTeam2 + 1);
});
player.setTeamNumber(randomTeam2 + 1);
});

return teams;
}
Expand All @@ -104,23 +105,20 @@ public List<Team> withoutAnchorages(List<Team> teams) {
*/
@Override
public List<Team> withAnchorages(List<Team> teams) {
List<List<Player>> anchorages = CommonFunctions.getAnchoredPlayers();

for (List<Player> anchorage : anchorages) {
for (List<Player> anchorage : CommonFunctions.getAnchoredPlayers()) {
int teamNumber = getAvailableTeam(teams, team -> anchorageCanBeAdded(team, anchorage));

if (teamNumber == -1) {
CommonFunctions.exitProgram(Error.ERROR_INTERNAL);
}

Map<Position, List<Player>> randomTeamPlayersMap = teams.get(teamNumber)
.getTeamPlayers();

for (Player player : anchorage) {
player.setTeamNumber(teamNumber + 1);

randomTeamPlayersMap.get(player.getPosition())
.add(player);
teams.get(teamNumber)
.getTeamPlayers()
.get(player.getPosition())
.add(player);
}
}

Expand Down
14 changes: 7 additions & 7 deletions src/main/java/armameeldoparti/views/NamesInputView.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
import armameeldoparti.utils.common.custom.graphical.CustomTextArea;
import armameeldoparti.utils.common.custom.graphical.CustomTextField;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.EnumMap;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -148,12 +147,13 @@ private void addAnchoragesCheckbox() {
* Builds, stores and configures each position text fields.
*/
private void addTextFields() {
Arrays.asList(Position.values())
.forEach(position -> textFieldsMap.get(position)
.addAll(IntStream.range(0, CommonFields.getPlayersAmountMap()
.get(position) * 2)
.mapToObj(i -> new CustomTextField())
.toList()));
for (Position position : Position.values()) {
textFieldsMap.get(position)
.addAll(IntStream.range(0, CommonFields.getPlayersAmountMap()
.get(position) * 2)
.mapToObj(i -> new CustomTextField())
.toList());
}
}

/**
Expand Down
12 changes: 6 additions & 6 deletions src/main/java/armameeldoparti/views/SkillPointsInputView.java
Original file line number Diff line number Diff line change
Expand Up @@ -87,13 +87,13 @@ private void addSpinners() {
.get(position)), SwingConstants.CENTER),
CommonFunctions.buildMigLayoutConstraints(Constants.MIG_LAYOUT_GROW, Constants.MIG_LAYOUT_SPAN));

List<Player> playersSet = new ArrayList<>(CommonFields.getPlayersSets()
.get(position));
List<Player> players = new ArrayList<>(CommonFields.getPlayersSets()
.get(position));

playersSet.sort(Comparator.comparing(player -> player.getPosition()
.ordinal()));
players.sort(Comparator.comparing(player -> player.getPosition()
.ordinal()));

for (Player player : playersSet) {
for (Player player : players) {
spinnersMap.put(player, new CustomSpinner(new SpinnerNumberModel(Constants.SKILL_INI,
Constants.SKILL_MIN,
Constants.SKILL_MAX,
Expand All @@ -102,7 +102,7 @@ private void addSpinners() {
labelsMap.put(spinnersMap.get(player), new JLabel(player.getName()));

masterPanel.add(labelsMap.get(spinnersMap.get(player)), Constants.MIG_LAYOUT_PUSHX);
masterPanel.add(spinnersMap.get(player), playersSet.indexOf(player) % 2 != 0 ? Constants.MIG_LAYOUT_WRAP : null);
masterPanel.add(spinnersMap.get(player), players.indexOf(player) % 2 != 0 ? Constants.MIG_LAYOUT_WRAP : null);
}
}
}
Expand Down

0 comments on commit 86c3032

Please sign in to comment.