diff options
| author | sowgro <tpoke.ferrari@gmail.com> | 2023-06-03 21:19:20 -0400 | 
|---|---|---|
| committer | sowgro <tpoke.ferrari@gmail.com> | 2023-06-03 21:19:20 -0400 | 
| commit | 6ed49fb61521000957b9768d059124981ec559cb (patch) | |
| tree | 2773a6265e968de3b916e36beabf9bca5ec69937 /src/main | |
| parent | f48f187cd8707a700ba86d2ef7338ca793a5dd74 (diff) | |
| download | NPEhero-6ed49fb61521000957b9768d059124981ec559cb.tar.gz NPEhero-6ed49fb61521000957b9768d059124981ec559cb.tar.bz2 NPEhero-6ed49fb61521000957b9768d059124981ec559cb.zip  | |
add level validation
Diffstat (limited to '')
| -rw-r--r-- | src/main/Difficulty.java | 65 | ||||
| -rw-r--r-- | src/main/Level.java | 109 | ||||
| -rw-r--r-- | src/main/LevelController.java | 17 | 
3 files changed, 147 insertions, 44 deletions
diff --git a/src/main/Difficulty.java b/src/main/Difficulty.java index abb12f4..7174c33 100644 --- a/src/main/Difficulty.java +++ b/src/main/Difficulty.java @@ -11,7 +11,7 @@ import org.json.simple.parser.JSONParser;  import javafx.collections.FXCollections;  import javafx.collections.ObservableList; -public class Difficulty  +public class Difficulty  {      public File thisDir;      public String title = "Unnamed"; @@ -20,6 +20,7 @@ public class Difficulty      public Double bpm = 0.0;      public int numBeats;      public Level level; +    public boolean isValid = false;      /**       * Creates a new Difficulty and gives it a file path @@ -31,33 +32,54 @@ public class Difficulty          this.level = level;      } -    /** -     * Checks for files in the difficulty folder and runs cooresponding actions -     */      public void readData()      { -        for(File cur: thisDir.listFiles()) //iterates through all files/folders in src/assets/levels/LEVEL/DIFFICULTY +        boolean isValid1 = true; +        if (new File(thisDir, "metadata.json").exists())          { -            if (cur.getName().equals("metadata.json")) -            { -                parseMetadata(); -            } -            if (cur.getName().equals("leaderboard.json")) +            if (!parseMetadata())              { -                parseLeaderboard(); +                isValid1 = false;              } -            if (cur.getName().equals("notes.txt")) +        } +        else +        { +            System.err.println(thisDir+" is missing metadata.json"); +            isValid1 = false; +        } + +        if (new File(thisDir, "leaderboard.json").exists()) +        { +            if (!parseLeaderboard())              { -                notes = cur; +                isValid1 = false;              }          } +        else +        { +            System.err.println(thisDir+" is missing leaderboard.json"); +            isValid1 = false; +        } + +        if (new File(thisDir, "notes.txt").exists()) +        { +            notes = new File(thisDir, "notes.txt"); +        } +        else +        { +            System.err.println(thisDir+" is missing notes.txt"); +            isValid1 = false; +        } + +        isValid = isValid1;      }      /**       * Reads in json metadata and assigns values to variables       */ -    public void parseMetadata() +    public boolean parseMetadata()      { +        boolean isValid = true;          File file = new File(thisDir, "metadata.json");          JSONParser jsonParser = new JSONParser(); //parser to read the file @@ -73,7 +95,9 @@ public class Difficulty  		catch (Exception e)          {              e.printStackTrace(); +            isValid = false;          } +        return isValid;      }      /** @@ -102,8 +126,9 @@ public class Difficulty      /**       * Reads in json leaderboard and assigns populates list with leaderboardEntries       */ -    public void parseLeaderboard() +    public boolean parseLeaderboard()      { +        boolean isValid = true;          File file = new File(thisDir, "leaderboard.json");          JSONParser jsonParser = new JSONParser(); //parser to read the file @@ -125,8 +150,10 @@ public class Difficulty  		}  		catch (Exception e)          { +            isValid = false;              e.printStackTrace();          } +        return isValid;      }      /** @@ -178,4 +205,12 @@ public class Difficulty      {          return title;      } + +    public boolean isValid() { +        return isValid; +    } + +    public String getTitle() { +        return title; +    }  } diff --git a/src/main/Level.java b/src/main/Level.java index cb16489..a173191 100644 --- a/src/main/Level.java +++ b/src/main/Level.java @@ -1,8 +1,10 @@  package main;  import java.io.File; -import java.util.ArrayList;  import java.util.Comparator; + +import javafx.collections.FXCollections; +import javafx.collections.ObservableList;  import javafx.scene.image.Image;  import javafx.scene.paint.Color;  import java.io.FileWriter; @@ -19,7 +21,9 @@ public class Level      public File thisDir;      private String title = "Unnamed";      private String artist = "Unknown"; -    private ArrayList<Difficulty> diffList; +    private ObservableList<Difficulty> diffList; +    private ObservableList<Difficulty> validDiffList; +    private boolean isValid;      public Image preview; //optional      public String desc = "No description"; @@ -36,44 +40,85 @@ public class Level          thisDir = newDir;      } -    /** -     * Checks for files in the level folder and runs cooresponding actions -     */      public void readData()      { -        diffList = new ArrayList<Difficulty>(); +        boolean isValid1 = true; +        if (new File(thisDir, "metadata.json").exists()) +        { +            if (!parseMetadata()) +            { +                System.err.println(new File(thisDir, "metadata.json")+" contains error(s)"); +                isValid1 = false; +            } +        } +        else +        { +            System.err.println(thisDir+" is missing metadata.json"); +            isValid1 = false; +        } + +        if (new File(thisDir, "song.wav").exists()) +        { +            song = new File(thisDir,"song.wav"); +        } +        else +        { +            System.err.println(thisDir+" is missing song.wav"); +            isValid1 = false; +        } + +        if (new File(thisDir, "background.png").exists()) +        { +            background = new Image(new File(thisDir,"background.png").toURI().toString()); +        } +        else +        { +            System.out.println(thisDir+" is missing background.png, though it is not required"); +        } + +        if (! new File(thisDir, "preview.png").exists()) +        { +            preview = new Image(new File(thisDir,"preview.png").toURI().toString()); +        } +        else +        { +            System.out.println(thisDir+" is missing preview.png, though it is not required"); +        } + +        diffList = FXCollections.observableArrayList(); +        validDiffList = FXCollections.observableArrayList();          for(File cur: thisDir.listFiles()) //iterates through all files/folders in src/assets/levels/LEVEL          {              if (cur.isDirectory()) //all subfolders within a level folder are difficulties              {                  Difficulty diff = new Difficulty(cur,this);                  diff.readData(); -                diffList.add(diff); -            } -            if (cur.getName().equals("metadata.json")) -            { -                parseMetadata(); -            } -            if (cur.getName().equals("preview.png")) -            { -                preview = new Image(cur.toURI().toString()); -            } -            if (cur.getName().equals("background.png")) -            { -                background = new Image(cur.toURI().toString()); -            } -            if (cur.getName().equals("song.wav")) -            { -                song = cur; +                if (diff.isValid) +                { +                    diffList.add(diff); +                    validDiffList.add(diff); +                } +                else +                { +                    diffList.add(diff); +                }              }          } +        if (validDiffList.size() == 0) +        { +            System.err.println(thisDir+" contains no valid difficulties"); +            isValid1 = false; +        } + +        isValid = isValid1;      }      /**       * Reads in json metadata and assigns values to variables       */ -    public void parseMetadata()  +    public boolean parseMetadata()       { +        boolean isValid = true;          JSONParser jsonParser = new JSONParser(); //parser to read the file  		try(FileReader reader = new FileReader(new File(thisDir, "metadata.json"))) @@ -104,7 +149,9 @@ public class Level  		catch (Exception e)   		{  			e.printStackTrace(); +            isValid = false;  		}  +        return isValid;      }      /** @@ -165,8 +212,6 @@ public class Level      public void removeDiff(Difficulty diff)      {          File hold = diff.thisDir; -        diffList.remove(diff); -          try {              Files.walk(hold.toPath())              .sorted(Comparator.reverseOrder()) @@ -175,6 +220,7 @@ public class Level          } catch (IOException e) {              e.printStackTrace();          } +        readData();      }      /** @@ -192,11 +238,14 @@ public class Level          readData();      } -    public ArrayList<Difficulty> getDiffList() -    { +    public ObservableList<Difficulty> getDiffList() {          return diffList;      } +    public ObservableList<Difficulty> getValidDiffList() { +        return validDiffList; +    } +      public String getTitle()       {          return title; @@ -214,4 +263,8 @@ public class Level      public void setArtist(String artist) {          this.artist = artist;      } + +    public boolean isValid() { +        return isValid; +    }  } diff --git a/src/main/LevelController.java b/src/main/LevelController.java index 78d638a..07ea3f8 100644 --- a/src/main/LevelController.java +++ b/src/main/LevelController.java @@ -12,7 +12,8 @@ import javafx.collections.ObservableList;  public class LevelController  {      File thisDir = new File("levels"); -    public static ObservableList<Level> levelList; +    private static ObservableList<Level> levelList; +    private static ObservableList<Level> validLevelList;      /**       * Creates a levelController, which holds all the levels @@ -28,11 +29,16 @@ public class LevelController      public void readData()      {          levelList = FXCollections.observableArrayList(); +        validLevelList = FXCollections.observableArrayList();          for (File cur: thisDir.listFiles()) //iterates through all files/folders in levels          {              Level level = new Level(cur);              level.readData();              levelList.add(level); +            if (level.isValid()) +            { +                validLevelList.add(level); +            }          }      } @@ -76,5 +82,14 @@ public class LevelController          } catch (IOException e) {              e.printStackTrace();          } +        readData(); +    } + +    public static ObservableList<Level> getLevelList() { +        return levelList; +    } + +    public static ObservableList<Level> getValidLevelList() { +        return validLevelList;      }  }
\ No newline at end of file  | 
