Class PlayerFileHandlerCsv

java.lang.Object
edu.ntnu.idi.idatt.filehandler.PlayerFileHandlerCsv
All Implemented Interfaces:
FileHandler<Player>

public class PlayerFileHandlerCsv extends Object implements FileHandler<Player>
PlayerFileHandlerCsv.

Implements the FileHandler interface for Player objects, providing functionality to read player data from and write player data to CSV (Comma Separated Values) formatted files. This handler can distinguish between LadderGamePlayer and LudoPlayer types based on the CSV header and structure.

When reading, it determines the player type from the header line and parses subsequent lines accordingly. For Ludo players, it expects "name, colorHex, isBot". For Ladder Game players, it expects "name, colorHex, playerTokenType, isBot".

When writing, it generates the appropriate header based on the type of the first player in the list and then formats each player object into a CSV line.

See Also:
  • Constructor Details

    • PlayerFileHandlerCsv

      public PlayerFileHandlerCsv()
  • Method Details

    • readFile

      public List<Player> readFile(String path) throws IOException
      Reads a list of Player objects from a CSV file at the specified path. The method automatically detects if the players are LudoPlayers or LadderGamePlayers based on the header row in the CSV file.
      Specified by:
      readFile in interface FileHandler<Player>
      Parameters:
      path - The path to the CSV file.
      Returns:
      A list of Player objects deserialized from the file. Returns an empty list if a player line is malformed or if an I/O error (other than file not found) occurs.
      Throws:
      IOException - if the file cannot be found or if an I/O error occurs during reading that prevents processing (e.g., permissions issues).
    • writeFile

      public void writeFile(String path, List<Player> players) throws IOException
      Writes a list of Player objects to a CSV file at the specified path. The CSV header and line format are determined by the type of the first player in the list (either LudoPlayer or LadderGamePlayer).
      Specified by:
      writeFile in interface FileHandler<Player>
      Parameters:
      path - The path to the CSV file where players will be written.
      players - The list of Player objects to serialize.
      Throws:
      IOException - if an error occurs during file writing (e.g., path not found, permissions).
    • ladderGamePlayerFromCsvLine

      public Player ladderGamePlayerFromCsvLine(String line)
      Parses a line from a CSV file, expecting LadderGamePlayer data format. The expected format is: "name,colorHex,playerTokenType,isBot".
      Parameters:
      line - The CSV line string.
      Returns:
      A LadderGamePlayer object, or null if the line is malformed.
    • ludoPlayerFromCsvLine

      public Player ludoPlayerFromCsvLine(String line)
      Parses a line from a CSV file, expecting LudoPlayer data format. The expected format is: "name,colorHex,isBot". PlayerTokenType.CIRCLE is assumed.
      Parameters:
      line - The CSV line string.
      Returns:
      A LudoPlayer object, or null if the line is malformed.
    • toCsvLine

      public String toCsvLine(Player player)
      Converts a Player object to its CSV string representation. The format depends on whether the player is an instance of LudoPlayer (name,colorHex,isBot) or another Player type, assumed to be LadderGamePlayer (name,colorHex,playerTokenType,isBot).
      Parameters:
      player - The Player object to convert.
      Returns:
      A string representing the player in CSV format.