Initial final refactor (problems noticed by intellij)

This commit is contained in:
2026-06-19 15:38:15 +02:00
parent 338df7748b
commit 36814e3166
32 changed files with 45 additions and 104 deletions
@@ -95,9 +95,8 @@ public class ClientLauncherTUI {
/**
* Starts the TUI client. Creates a JLine terminal, loops through login → game → rematch.
*
* @throws InterruptedException if the thread is interrupted while waiting.
*/
private void start() throws InterruptedException {
private void start() {
try {
System.setOut(new PrintStream(System.out, true, StandardCharsets.UTF_8));
System.setErr(new PrintStream(System.err, true, StandardCharsets.UTF_8));
@@ -28,7 +28,7 @@ public abstract class TribeCard extends PlayableCard implements Serializable {
/**
* The minimum number of players required for this tribe card.
*/
private int nMin=0;
private int nMin;
/**
* Returns the minimum number of players required for this tribe card.
@@ -82,8 +82,8 @@ public class ShamanicRitual extends EventCard {
@Override
public void activateEvent (ArrayList <Player> playerList){
Map<Player, Integer> playerMap = new HashMap<>();
int tmpIcons = 0;
int tmpCount = 0;
int tmpIcons;
int tmpCount;
for (Player player : playerList) {
tmpIcons = player.getShamans().stream().mapToInt(sh -> sh.getIcon()).sum();
@@ -48,7 +48,7 @@ public class MiniModel implements Serializable {
/**
* Map of players indexed by username.
*/
public Map<String, Player> players;
public final Map<String, Player> players;
/**
* List of totems still available for selection.
@@ -60,27 +60,6 @@ public class MiniModel implements Serializable {
*/
public List<Player> standingPlayers;
/**
* Constructs an empty mini model.
*/
public MiniModel() {
}
/**
* Constructs a mini model with only the card lists populated.
*
* @param upperListTribeCards the upper row of tribe cards on the board.
* @param lowerListTribeCards the lower row of tribe cards on the board.
* @param upperListBuildingCards the upper row of building cards on the board.
* @param lowerListBuildingCards the lower row of building cards on the board.
*/
public MiniModel(ArrayList<TribeCard> upperListTribeCards,ArrayList<TribeCard>lowerListTribeCards,ArrayList<BuildingCard> upperListBuildingCards,ArrayList<BuildingCard>lowerListBuildingCards) {
this.upperListTribeCards = upperListTribeCards;
this.lowerListTribeCards = lowerListTribeCards;
this.upperListBuildingCards = upperListBuildingCards;
this.lowerListBuildingCards = lowerListBuildingCards;
this.disconnectedPlayers = new ArrayList<>();
}
/**
* Constructs a complete mini model from the current server-side game data.
@@ -33,8 +33,6 @@ public abstract class OrderLogicCard implements Serializable {
return Collections.unmodifiableList(playerList);
}
/** Total number of players in this game. */
protected final int nPlayers;
/**
@@ -50,7 +48,6 @@ public abstract class OrderLogicCard implements Serializable {
public OrderLogicCard(ArrayList<Player> players) {
Collections.shuffle(players);
this.players = new LinkedList<>(players);
nPlayers = players.size();
this.playerList = new ArrayList<>(players.stream().map(x -> new OrderPlayer(x, false)).toList());
}
@@ -262,7 +262,7 @@ public class Player implements Serializable {
*/
@Override
public String toString() {
int last = -1;
int last;
var table = new AsciiTable(ROUNDED, 1);
@@ -196,8 +196,6 @@ public abstract class NetworkEvent implements Serializable {
* Applies this event to the specified client-side mini model.
*
* @param model the mini model on which the event must be applied.
* @return {@code true} if the event is applied successfully,
* {@code false} otherwise.
*/
public abstract boolean apply(MiniModel model);
public abstract void apply(MiniModel model);
}
@@ -50,19 +50,17 @@ public class AddPlayer extends NetworkEvent {
* turn order, game state, and slot map.
*
* @param miniModel the client-side model to update.
* @return {@code false} if the event is marked as an error; {@code true} otherwise.
*/
@Override
public boolean apply(MiniModel miniModel){
public void apply(MiniModel miniModel){
synchronized (miniModel) {
if (isError)
return false;
return;
miniModel.setPlayers(playerList);
miniModel.setOrderLogicCard(orderLogicCard);
miniModel.setCurrentState(currentState);
miniModel.setSlotPlayerMap(slotPlayerMap);
miniModel.setLastEvent(this);
return true;
}
}
}
@@ -76,10 +76,9 @@ public class ApplyNextRound extends NetworkEvent {
* slot map, turn order, and game state for the new round.
*
* @param miniModel the client-side model to update.
* @return {@code true} always (this event cannot produce an error).
*/
@Override
public boolean apply(MiniModel miniModel) {
public void apply(MiniModel miniModel) {
synchronized (miniModel) {
miniModel.setSlotPlayerMap(slotPlayerMap);
miniModel.setOrderLogicCard(orderLogicCard);
@@ -90,7 +89,6 @@ public class ApplyNextRound extends NetworkEvent {
miniModel.setUpperListBuildingCards(upperListBuildingCards);
miniModel.setLowerListBuildingCards(lowerListBuildingCards);
miniModel.setLastEvent(this);
return true;
}
}
}
@@ -55,13 +55,12 @@ public class DisconnectedPlayer extends NetworkEvent {
* updating player data, turn order, game state, and disconnected player list.
*
* @param miniModel the client-side model to update.
* @return {@code false} if the event is marked as an error; {@code true} otherwise.
*/
@Override
public boolean apply(MiniModel miniModel){
public void apply(MiniModel miniModel){
synchronized (miniModel) {
if (isError)
return false;
return;
miniModel.setPlayers(playerList);
miniModel.setOrderLogicCard(orderLogicCard);
miniModel.setCurrentState(currentState);
@@ -69,7 +68,6 @@ public class DisconnectedPlayer extends NetworkEvent {
miniModel.setDisconnectedPlayers(disconnectedPlayers);
miniModel.setAvailableTotems(availableTotems);
miniModel.setLastEvent(this);
return true;
}
}
@@ -40,13 +40,12 @@ public class DrawLowerBuildingCard extends NetworkEvent{
* from the lower building list and updating players, turn order, game state, and slot map.
*
* @param miniModel the client-side model to update.
* @return {@code false} if the event is marked as an error; {@code true} otherwise.
*/
@Override
public boolean apply(MiniModel miniModel){
public void apply(MiniModel miniModel){
synchronized (miniModel) {
if (isError)
return false;
return;
miniModel.removeLowerBuildingCard(pos);
miniModel.setPlayers(playerList);
@@ -54,7 +53,6 @@ public class DrawLowerBuildingCard extends NetworkEvent{
miniModel.setCurrentState(currentState);
miniModel.setSlotPlayerMap(slotPlayerMap);
miniModel.setLastEvent(this);
return true;
}
}
@@ -39,13 +39,12 @@ public class DrawLowerTribeCard extends NetworkEvent{
* from the lower tribe list and updating players, turn order, game state, and slot map.
*
* @param miniModel the client-side model to update.
* @return {@code false} if the event is marked as an error; {@code true} otherwise.
*/
@Override
public boolean apply(MiniModel miniModel){
public void apply(MiniModel miniModel){
synchronized (miniModel) {
if (isError)
return false;
return;
miniModel.removeLowerTribeCard(pos);
miniModel.setPlayers(playerList);
@@ -53,7 +52,6 @@ public class DrawLowerTribeCard extends NetworkEvent{
miniModel.setCurrentState(currentState);
miniModel.setSlotPlayerMap(slotPlayerMap);
miniModel.setLastEvent(this);
return true;
}
}
@@ -40,13 +40,12 @@ public class DrawUpperBuildingCard extends NetworkEvent{
* from the upper building list and updating players, turn order, game state, and slot map.
*
* @param miniModel the client-side model to update.
* @return {@code false} if the event is marked as an error; {@code true} otherwise.
*/
@Override
public boolean apply(MiniModel miniModel){
public void apply(MiniModel miniModel){
synchronized (miniModel) {
if (isError)
return false;
return;
miniModel.removeUpperBuildingCard(pos);
miniModel.setPlayers(playerList);
@@ -54,7 +53,6 @@ public class DrawUpperBuildingCard extends NetworkEvent{
miniModel.setCurrentState(currentState);
miniModel.setSlotPlayerMap(slotPlayerMap);
miniModel.setLastEvent(this);
return true;
}
}
}
@@ -39,20 +39,18 @@ public class DrawUpperTribeCard extends NetworkEvent{
* from the upper tribe list and updating players, turn order, game state, and slot map.
*
* @param miniModel the client-side model to update.
* @return {@code false} if the event is marked as an error; {@code true} otherwise.
*/
@Override
public boolean apply(MiniModel miniModel){
public void apply(MiniModel miniModel){
synchronized (miniModel) {
if (isError)
return false;
return;
miniModel.removeUpperTribeCard(pos);
miniModel.setPlayers(playerList);
miniModel.setOrderLogicCard(orderLogicCard);
miniModel.setCurrentState(currentState);
miniModel.setSlotPlayerMap(slotPlayerMap);
miniModel.setLastEvent(this);
return true;
}
}
@@ -52,10 +52,9 @@ public class EndedGame extends NetworkEvent {
* game state and setting the standings list for the leaderboard.
*
* @param miniModel the client-side model to update.
* @return {@code true} always (this event cannot produce an error).
*/
@Override
public boolean apply(MiniModel miniModel) {
public void apply(MiniModel miniModel) {
synchronized (miniModel) {
miniModel.setSlotPlayerMap(slotPlayerMap);
miniModel.setOrderLogicCard(orderLogicCard);
@@ -63,7 +62,6 @@ public class EndedGame extends NetworkEvent {
miniModel.setPlayers(playerList);
miniModel.setStandingPlayers(playerList);
miniModel.setLastEvent(this);
return true;
}
}
}
@@ -48,21 +48,18 @@ public class ReconnectPlayer extends NetworkEvent {
* turn order, current game state, and slot assignments are updated.
*
* @param miniModel the mini model on which to apply the event.
* @return {@code true} if the update is applied successfully,
* {@code false} if the event represents an error.
*/
@Override
public boolean apply(MiniModel miniModel) {
public void apply(MiniModel miniModel) {
synchronized (miniModel) {
if (isError)
return false;
return;
miniModel.setPlayers(playerList);
miniModel.setOrderLogicCard(orderLogicCard);
miniModel.setCurrentState(currentState);
miniModel.setSlotPlayerMap(slotPlayerMap);
miniModel.setDisconnectedPlayers(disconnectedPlayers);
miniModel.setLastEvent(this);
return true;
}
}
}
@@ -34,19 +34,17 @@ public class SkipTurn extends NetworkEvent {
* turn order, game state, and slot map after a skip turn action.
*
* @param miniModel the client-side model to update.
* @return {@code false} if the event is marked as an error; {@code true} otherwise.
*/
@Override
public boolean apply(MiniModel miniModel){
public void apply(MiniModel miniModel){
synchronized (miniModel) {
if (isError)
return false;
return;
miniModel.setPlayers(playerList);
miniModel.setOrderLogicCard(orderLogicCard);
miniModel.setCurrentState(currentState);
miniModel.setSlotPlayerMap(slotPlayerMap);
miniModel.setLastEvent(this);
return true;
}
}
@@ -40,19 +40,17 @@ public class SlotChoice extends NetworkEvent {
* turn order, game state, and slot map after a slot selection.
*
* @param miniModel the client-side model to update.
* @return {@code false} if the event is marked as an error; {@code true} otherwise.
*/
@Override
public boolean apply(MiniModel miniModel){
public void apply(MiniModel miniModel){
synchronized (miniModel) {
if (isError)
return false;
return;
miniModel.setPlayers(playerList);
miniModel.setOrderLogicCard(orderLogicCard);
miniModel.setCurrentState(currentState);
miniModel.setSlotPlayerMap(slotPlayerMap);
miniModel.setLastEvent(this);
return true;
}
}
@@ -64,14 +64,12 @@ public class TotemChoice extends NetworkEvent implements Serializable {
* totems are updated.
*
* @param miniModel the mini model on which to apply the event.
* @return {@code true} if the update is applied successfully,
* {@code false} if the event represents an error.
*/
@Override
public boolean apply(MiniModel miniModel) {
public void apply(MiniModel miniModel) {
synchronized (miniModel) {
if (isError)
return false;
return;
miniModel.setPlayers(playerList);
miniModel.setOrderLogicCard(orderLogicCard);
@@ -79,7 +77,6 @@ public class TotemChoice extends NetworkEvent implements Serializable {
miniModel.setSlotPlayerMap(slotPlayerMap);
miniModel.setAvailableTotems(availableTotems);
miniModel.setLastEvent(this);
return true;
}
}
}
@@ -27,7 +27,7 @@ import java.util.concurrent.*;
*/
public class RMIHeartbeat {
private String username = "";
private String username;
private final LimitedMap<String, Boolean> playerList;
private final Map<String, IClientCallback> clients;
private final BlockingQueue<NetworkEvent> actionQueue;
@@ -295,20 +295,15 @@ public class RMIServer extends UnicastRemoteObject implements IGameServer {
* <p>The method configures the server hostname, creates the RMI registry
* on the specified port, and registers this server instance under the
* {@code RMIGameServer} name.
*
* @return {@code true} if the server starts successfully,
* {@code false} otherwise.
*/
public boolean start() {
public void start() {
try {
System.setProperty("java.rmi.server.hostname", host);
Registry registry = LocateRegistry.createRegistry(nPort);
registry.rebind("RMIGameServer", this);
System.out.println("RMI Server started on port: " + nPort);
return true;
} catch (RemoteException e) {
e.printStackTrace();
return false;
}
}
@@ -53,7 +53,7 @@ public class LeaderboardFXMLController {
/** Returns a cached {@link Image} for the given classpath {@code path}, loading it on first access. */
private Image loadImage(String path) {
return imageCache.computeIfAbsent(path,
p -> new Image(getClass().getResourceAsStream(p)));
p -> new Image(Objects.requireNonNull(getClass().getResourceAsStream(p))));
}
@@ -20,6 +20,8 @@ import javafx.scene.paint.Color;
import javafx.stage.Screen;
import javafx.util.Duration;
import java.util.Objects;
/**
* FXML controller for the login scene.
*
@@ -69,7 +71,7 @@ public class LoginFXMLController {
@FXML
public void initialize() {
// Background setup
Image img = new Image(getClass().getResourceAsStream("/GUIImages/BackgroundLogin.png"));
Image img = new Image(Objects.requireNonNull(getClass().getResourceAsStream("/GUIImages/BackgroundLogin.png")));
backgroundImage.setImage(img);
Rectangle2D screenBounds = Screen.getPrimary().getBounds();
backgroundImage.setFitWidth(screenBounds.getWidth());
@@ -91,7 +91,7 @@ public class MainFXMLController {
/** Returns a cached {@link Image} for the given classpath {@code path}, loading it on first access. */
private Image loadImage(String path) {
return imageCache.computeIfAbsent(path,
p -> new Image(getClass().getResourceAsStream(p)));
p -> new Image(Objects.requireNonNull(getClass().getResourceAsStream(p))));
}
/**
@@ -16,6 +16,7 @@ import javafx.stage.Screen;
import javafx.util.Duration;
import java.util.Locale;
import java.util.Objects;
/**
* FXML controller for the totem selection scene.
@@ -52,7 +53,7 @@ public class TotemFXMLController {
/** Initializes the scene: sets the background image to fill the screen. */
@FXML
public void initialize() {
Image img = new Image(getClass().getResourceAsStream("/GUIImages/Background.png"));
Image img = new Image(Objects.requireNonNull(getClass().getResourceAsStream("/GUIImages/Background.png")));
backgroundImage.setImage(img);
Rectangle2D screenBounds = Screen.getPrimary().getBounds();
backgroundImage.setFitWidth(screenBounds.getWidth());
@@ -122,7 +123,7 @@ public class TotemFXMLController {
/** Builds a totem card widget; if {@code interactive}, wires click/hover handlers and selection logic. */
private VBox buildCard(Totems totem, int idx, boolean interactive) {
ImageView img = new ImageView(new Image(getClass().getResourceAsStream("/GUIImages/Totems/totem_" + String.valueOf(totem).toLowerCase(Locale.ROOT) + ".png")));
ImageView img = new ImageView(new Image(Objects.requireNonNull(getClass().getResourceAsStream("/GUIImages/Totems/totem_" + String.valueOf(totem).toLowerCase(Locale.ROOT) + ".png"))));
img.setFitHeight(150);
img.setPreserveRatio(true);
@@ -180,7 +180,7 @@ class GameControllerTest {
fail("Current player has no remaining draw actions.");
}
private Queue<Player> completeSlotChoiceWithSlots(Game game, GameController controller, int... slotIndexes) {
private void completeSlotChoiceWithSlots(Game game, GameController controller, int... slotIndexes) {
assertEquals(game.getNPlayers(), slotIndexes.length);
Queue<Player> order = new LinkedList<>();
@@ -194,7 +194,6 @@ class GameControllerTest {
assertEquals(GameStages.RES_ACTIONS, game.getCurrentState().getGameStage());
return order;
}
@@ -39,7 +39,7 @@ class GameTest {
assertNotNull(game.getCurrentState().getCurrentPlayer());
}
private Queue<Player> completeSlotChoice(Game game) {
private void completeSlotChoice(Game game) {
if (game.getCurrentState().getGameStage() == GameStages.TOTEM_CHOICE) {
completeTotemChoice(game);
}
@@ -64,7 +64,6 @@ class GameTest {
assertEquals(GameStages.RES_ACTIONS, game.getCurrentState().getGameStage());
return order;
}
private int firstNonEventIndex(List<TribeCard> cards) {
@@ -156,7 +156,7 @@ class Order2Test {
}
@Test
void toStringTest() throws NoSuchFieldException, IllegalAccessException {
void toStringTest() {
String usr1 = "wPIshEUhiOpRRnIBFfM89s2$@q$9FGbz";
String usr2 = "L*Ncw1rjjrF2xn%H@4P1iC@&NNIooQQk";
@@ -181,7 +181,7 @@ class Order3Test {
assertEquals(p1, order.getFirst());
}
@Test
void toStringTest() throws NoSuchFieldException, IllegalAccessException {
void toStringTest() {
String usr1 = "wPIshEUhiOpRRnIBFfM89s2$@q$9FGbz";
String usr2 = "L*Ncw1rjjrF2xn%H@4P1iC@&NNIooQQk";
String usr3 = "L*Ncw1rjjrF2xn%H@4d1iC@&NNIooQQk";
@@ -272,7 +272,7 @@ class Order4Test {
}
@Test
void toStringTest() throws NoSuchFieldException, IllegalAccessException {
void toStringTest() {
String usr1 = "wPIshEUhiOpRRnIBFfM89s2$@q$9FGbz";
String usr2 = "L*Ncw1rjjrF2xn%H@4P1iC@&NNIooQQk";
String usr3 = "L*Ncw1rjjrF2xn%H@4d1iC@&NNIooQQk";
@@ -361,7 +361,7 @@ class Order5Test {
assertEquals(p1, order.getFirst());
}
@Test
void toStringTest() throws NoSuchFieldException, IllegalAccessException {
void toStringTest() {
String usr1 = "wPIshEUhiOpRRnIBFfM89s2$@q$9FGbz";
String usr2 = "L*Ncw1rjjrF2xn%H@4P1iC@&NNIooQQk";
String usr3 = "L*Ncw1rjjrF2xn%H@4d1iC@&NNIooQQk";
@@ -1,7 +1,5 @@
package it.polimi.ingsw.gc14.Model;
import it.polimi.ingsw.gc14.Model.Cards.Building.Effects.*;
import it.polimi.ingsw.gc14.Model.Cards.BuildingCard;
import it.polimi.ingsw.gc14.Model.Cards.TribeCards.CharacterType;
import it.polimi.ingsw.gc14.Model.Cards.TribeCards.Characters.*;
import org.junit.jupiter.api.DisplayName;