From bc48da810926b269202c030519cb6be8a33294f6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20Autin?= Date: Thu, 10 Jun 2021 13:30:07 +0200 Subject: [PATCH 1/4] Refactored configuration package --- src/configuration/Config.java | 240 ++++++++++-------------------- src/configuration/JsonToJava.java | 75 ---------- src/configuration/config.json | 55 ------- 3 files changed, 75 insertions(+), 295 deletions(-) delete mode 100644 src/configuration/JsonToJava.java delete mode 100644 src/configuration/config.json diff --git a/src/configuration/Config.java b/src/configuration/Config.java index 2048aea..94478bc 100644 --- a/src/configuration/Config.java +++ b/src/configuration/Config.java @@ -1,165 +1,75 @@ -package configuration; - -import java.util.Random; - -public class Config { - - // tester a la main (okay normalement mais ajouter des test) - public static void main(String[] args) { - int[] tempfortest = { 0, 2, 1, 1 }; // {arene/nb_joueur/perso1/perso2} 0=alatoire - config(tempfortest); - } - - // les variable a configurer - // sel1 pour savoir si on a deja selectionner le joueur 1 - - public static String arene, perso1, perso2; - public static int nb_joueur; - private static boolean sel1 = false; - - /* - * fonction config qui prend en entre un tableau d'entier chaque case - * correspondant a une variable, et ne retourne rien mais modifie la valeur des - * variables par des fonctions annexes - */ - - private static void config(int[] tab) { - int i = 0; - while (i < tab.length) { - - switch (i) { - case 0: - SelArene(tab[i]); - i++; - break; - - case 1: - NbJoueur(tab[i]); - i++; - break; - - case 2: - SelPerso(tab[i]); - i++; - break; - - case 3: - SelPerso(tab[i]); - i++; - break; - /* - * case 4: ? - */ - - default: - System.out.println("ERROR OUT OF BOUNDS CONFIG ARRAY"); - i = tab.length; - break; - } - } - System.out.println(arene + " " + nb_joueur + " " + perso1 + " " + perso2 + " " + sel1); - } - - /* - * fonction SelArene prend un entier en parametre et permet de choisir l'arene - * du jeu - */ - - private static void SelArene(int s) { - switch (s) { - case 0: - SelArene(random(1, 2)); - break; - case 1: - arene = "arene1.png"; - break; - - case 2: - arene = "arene2.png"; - break; - - default: - System.out.println("ERROR ARENE INEXISTANTE"); - } - } - - /* - * fonction NbJoueur prend un entier en parametre et permet de determiner si un - * bot sera necessaire - */ - - private static void NbJoueur(int s) { - switch (s) { - case 1: - nb_joueur = 1; - break; - - case 2: - nb_joueur = 2; - break; - default: - System.out.println("ERROR NUMBER OF PLAYER"); - } - - } - - /* - * fonction SelArene prend un entier en parametre et permet de choisir le - * personnage en fonction du joueur - */ - - private static void SelPerso(int s) { - if (sel1 == false) { - switch (s) { - case 0: - SelPerso(random(1, 2)); - break; - case 1: - perso1 = "perso1.png"; - sel1 = true; - break; - case 2: - perso1 = "perso2.png"; - sel1 = true; - break; - default: - System.out.println("ERROR PERSO INEXISTANT"); - } - } else if (sel1 == true) { - - switch (s) { - case 0: - SelPerso(random(1, 2)); - break; - - case 1: - perso2 = "perso1.png"; - sel1 = false; - if (perso1 == perso2) { - perso2 = "perso1_swapcolor.png"; - } - break; - - case 2: - perso2 = "perso2.png"; - sel1 = false; - if (perso1 == perso2) { - perso2 = "perso2_swapcolor.png"; - } - break; - default: - System.out.println("ERROR PERSO INEXISTANT"); - } - } else { - System.out.println("ERROR SELECTION PLAYER"); - } - } - - // fonction nombre aleatoire entre deux borne - private static int random(int min, int max) { - Random random = new Random(); - int value = random.nextInt(max - 1 + min) + min; - return value; - } - -} +package configuration; + +import java.io.*; +import org.json.simple.*; +import org.json.simple.parser.*; + +public class JsonToJava { + + public static void main(String args[]) { + + JsonRecover(); + + } + + private static void JsonRecover() { + // initialize the parser + JSONParser jsonP = new JSONParser(); + try { + // read the json document + JSONObject jsonO = (JSONObject) jsonP.parse(new FileReader("src/configuration/config.json")); + + // to print all values + // System.out.println(jsonO.values()); + + // isolate the "test" part and print it + // String test = (String) jsonO.get("test"); + // System.out.println("ceci est un test :" + test); + + // arena selection + // select an element on the list + JSONArray arena = (JSONArray) jsonO.get("arena"); + // print a case of this element + System.out.println("arena : " + arena.get(1)); + + // nb players selection + JSONArray nb_players = (JSONArray) jsonO.get("nb_players"); + System.out.println("nb_player : " + nb_players.get(1)); + + // character selection + JSONArray character1 = (JSONArray) jsonO.get("character1"); + System.out.println("players 1 : " + character1.get(1)); + + JSONArray character2 = (JSONArray) jsonO.get("character2"); + System.out.println("players 2 : " + character2.get(1)); + + // resolution + JSONArray resolution = (JSONArray) jsonO.get("resolution"); + // resolution string " width x heigth" + JSONObject reso = (JSONObject) resolution.get(1); + + String heightStr = (String) reso.get("height"); + int height = Integer.parseInt(heightStr); // String to int + + String widthStr = (String) reso.get("width"); + int width = Integer.parseInt(widthStr); + + System.out.println("heigth : " + height + " width : " + width); + + // button selection + JSONArray allButton = (JSONArray) jsonO.get("button"); + System.out.println(allButton); + + String up = (String) allButton.get(0); + System.out.println("button for up is : " + up); + + } catch (FileNotFoundException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + } catch (ParseException e) { + e.printStackTrace(); + + } + } +} diff --git a/src/configuration/JsonToJava.java b/src/configuration/JsonToJava.java deleted file mode 100644 index 94478bc..0000000 --- a/src/configuration/JsonToJava.java +++ /dev/null @@ -1,75 +0,0 @@ -package configuration; - -import java.io.*; -import org.json.simple.*; -import org.json.simple.parser.*; - -public class JsonToJava { - - public static void main(String args[]) { - - JsonRecover(); - - } - - private static void JsonRecover() { - // initialize the parser - JSONParser jsonP = new JSONParser(); - try { - // read the json document - JSONObject jsonO = (JSONObject) jsonP.parse(new FileReader("src/configuration/config.json")); - - // to print all values - // System.out.println(jsonO.values()); - - // isolate the "test" part and print it - // String test = (String) jsonO.get("test"); - // System.out.println("ceci est un test :" + test); - - // arena selection - // select an element on the list - JSONArray arena = (JSONArray) jsonO.get("arena"); - // print a case of this element - System.out.println("arena : " + arena.get(1)); - - // nb players selection - JSONArray nb_players = (JSONArray) jsonO.get("nb_players"); - System.out.println("nb_player : " + nb_players.get(1)); - - // character selection - JSONArray character1 = (JSONArray) jsonO.get("character1"); - System.out.println("players 1 : " + character1.get(1)); - - JSONArray character2 = (JSONArray) jsonO.get("character2"); - System.out.println("players 2 : " + character2.get(1)); - - // resolution - JSONArray resolution = (JSONArray) jsonO.get("resolution"); - // resolution string " width x heigth" - JSONObject reso = (JSONObject) resolution.get(1); - - String heightStr = (String) reso.get("height"); - int height = Integer.parseInt(heightStr); // String to int - - String widthStr = (String) reso.get("width"); - int width = Integer.parseInt(widthStr); - - System.out.println("heigth : " + height + " width : " + width); - - // button selection - JSONArray allButton = (JSONArray) jsonO.get("button"); - System.out.println(allButton); - - String up = (String) allButton.get(0); - System.out.println("button for up is : " + up); - - } catch (FileNotFoundException e) { - e.printStackTrace(); - } catch (IOException e) { - e.printStackTrace(); - } catch (ParseException e) { - e.printStackTrace(); - - } - } -} diff --git a/src/configuration/config.json b/src/configuration/config.json deleted file mode 100644 index fbd5c52..0000000 --- a/src/configuration/config.json +++ /dev/null @@ -1,55 +0,0 @@ -{ - "arena": [ - "random", - "arena1.png", - "arena2.png" - ], - "nb_players": [ - "1", - "2" - ], - "character1": [ - "random", - "character1.png", - "character2.png" - ], - "character2": [ - "random", - "character1.png", - "character1_swapcolor.png", - "character2.png", - "character2_swapcolor.png" - ], - "resolution": [ - { - "width": "800", - "height": "600" - }, - { - "width": "1280", - "height": "1024" - }, - { - "width": "1680", - "height": "1050" - }, - { - "width": "1920", - "height": "1080" - }, - { - "persoWidth": "800", - "persoHeight": "600" - } - ], - "button": [ - "UP", - "DOWN", - "RIGTH", - "LEFT", - "A", - "B", - "X", - "Y" - ] -} From bacf117aa56a4c569e03eabf8815970acd7f76ad Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20Autin?= Date: Thu, 10 Jun 2021 13:30:49 +0200 Subject: [PATCH 2/4] Fully rewrote configuration.Config Now is capable of parsing configuration file and writing file. --- src/configuration/Config.java | 105 +++++++++++++++++++++++----------- 1 file changed, 71 insertions(+), 34 deletions(-) diff --git a/src/configuration/Config.java b/src/configuration/Config.java index 94478bc..b400758 100644 --- a/src/configuration/Config.java +++ b/src/configuration/Config.java @@ -4,20 +4,24 @@ import java.io.*; import org.json.simple.*; import org.json.simple.parser.*; -public class JsonToJava { - - public static void main(String args[]) { - - JsonRecover(); - +public class Config { + + public int width, height, rounds; + public boolean fullscreen; + public String stage; + public String p1, p2; + + public Config() throws FileNotFoundException { + parse(); } - private static void JsonRecover() { + public void parse() throws FileNotFoundException { + // initialize the parser JSONParser jsonP = new JSONParser(); try { // read the json document - JSONObject jsonO = (JSONObject) jsonP.parse(new FileReader("src/configuration/config.json")); + JSONObject jsonO = (JSONObject) jsonP.parse(new FileReader("game.set")); // to print all values // System.out.println(jsonO.values()); @@ -26,42 +30,45 @@ public class JsonToJava { // String test = (String) jsonO.get("test"); // System.out.println("ceci est un test :" + test); - // arena selection - // select an element on the list - JSONArray arena = (JSONArray) jsonO.get("arena"); + + JSONArray game = (JSONArray) jsonO.get("game"); + JSONObject settings = (JSONObject) game.get(0); + // print a case of this element - System.out.println("arena : " + arena.get(1)); + stage = (String) settings.get("stage"); // nb players selection - JSONArray nb_players = (JSONArray) jsonO.get("nb_players"); - System.out.println("nb_player : " + nb_players.get(1)); - + /* JSONArray nb_players = (JSONArray) jsonO.get("nb_players"); + System.out.println("nb_player : " + nb_players.get(1)); */ + // character selection - JSONArray character1 = (JSONArray) jsonO.get("character1"); - System.out.println("players 1 : " + character1.get(1)); + p1 = (String) settings.get("character1"); + p2 = (String) settings.get("character2"); - JSONArray character2 = (JSONArray) jsonO.get("character2"); - System.out.println("players 2 : " + character2.get(1)); - - // resolution - JSONArray resolution = (JSONArray) jsonO.get("resolution"); - // resolution string " width x heigth" - JSONObject reso = (JSONObject) resolution.get(1); - - String heightStr = (String) reso.get("height"); - int height = Integer.parseInt(heightStr); // String to int - - String widthStr = (String) reso.get("width"); - int width = Integer.parseInt(widthStr); - - System.out.println("heigth : " + height + " width : " + width); + height = Integer.parseInt((String) settings.get("height")); // String to int + width = Integer.parseInt((String) settings.get("width")); + + // fullscreen + String fs = (String) settings.get("fullscreen"); + if (fs.equals("true")) { + fullscreen = true; + } else fullscreen = false; + + // rounds + String temprounds = (String) settings.get("rounds"); + switch (temprounds) { + case "1": rounds = 1; + case "3": rounds = 3; + case "5": rounds = 5; + default: rounds = 1; + } // button selection - JSONArray allButton = (JSONArray) jsonO.get("button"); + /* JSONArray allButton = (JSONArray) jsonO.get("button"); System.out.println(allButton); String up = (String) allButton.get(0); - System.out.println("button for up is : " + up); + System.out.println("button for up is : " + up); */ } catch (FileNotFoundException e) { e.printStackTrace(); @@ -72,4 +79,34 @@ public class JsonToJava { } } + + @SuppressWarnings("unchecked") + public void write(int width, int height, int rounds, boolean fullscreen, String character1, String character2, String stage) throws Exception { + + JSONObject metafile = new JSONObject(); + JSONArray array = new JSONArray(); + + JSONObject set = new JSONObject(); + set.put("width", Integer.toString(width)); + set.put("height", Integer.toString(height)); + set.put("rounds", Integer.toString(rounds)); + set.put("fullscreen", Boolean.toString(fullscreen)); + set.put("character1", character1); + set.put("character2", character2); + set.put("stage", stage); + + array.add(set); + + metafile.put("game", array); + + try (FileWriter file = new FileWriter("game.set", false)) { + file.write(metafile.toJSONString()); + file.close(); + + } catch (FileNotFoundException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + } + } } From 4572a1659f80a91346a2fd899aa539bf964b0a16 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20Autin?= Date: Thu, 10 Jun 2021 13:31:36 +0200 Subject: [PATCH 3/4] Refactored launcher.Settings to use new configuration.Config Simpler, safer design. --- src/launcher/Settings.java | 170 +++++-------------------------------- 1 file changed, 23 insertions(+), 147 deletions(-) diff --git a/src/launcher/Settings.java b/src/launcher/Settings.java index d2e2060..c52bc7a 100644 --- a/src/launcher/Settings.java +++ b/src/launcher/Settings.java @@ -3,162 +3,38 @@ package launcher; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; +import java.util.HashMap; + +import configuration.*; public class Settings { - private class Player { - - private int controller_id; - private String character; - private int color; - - public Player(int cid, String ch, int col) { - controller_id = cid; - character = ch; - color = col; - } - - protected int getController_id() { - return controller_id; - } - protected void setController_id(int controller_id) { - this.controller_id = controller_id; - } - protected String getCharacter() { - return character; - } - protected void setCharacter(String character) { - this.character = character; - } - protected int getColor() { - return color; - } - protected void setColor(int color) { - this.color = color; - } - - } + private Config config; - private FileInputStream f_is; - private int width, height, r_x, r_y, rounds; - private boolean fullscreen; - private String stage; - private Player p1, p2; - - public Settings() throws Exception { + public Settings() { try { - f_is = new FileInputStream("game.set"); - } catch (FileNotFoundException e) { - File f = new File("game.set"); - f.createNewFile(); - } finally { - f_is = new FileInputStream("game.set"); + config = new Config(); + } catch (Exception e) { + e.printStackTrace(); + System.exit(1); } - - try { - parseSettings(); - } finally { - setDefaultSettings(); - } - } - - private void parseSettings() throws Exception { - //TODO: parseSettings() (Needs settings syntax fixed) } public void setSettings() throws Exception { - //TODO: parseSettings() (Needs settings syntax fixed) - } - - private void setDefaultSettings() { - width = 800; - height = 600; - r_x = 4; - r_y = 3; - rounds = 3; - fullscreen = false; - p1 = new Player(0, "base", 0); - p2 = new Player(0, "base", 0); - } - - protected FileInputStream getF_is() { - return f_is; - } - - protected int getWidth() { - return width; - } - - protected int getHeight() { - return height; - } - - protected int getR_x() { - return r_x; - } - - protected int getR_y() { - return r_y; - } - - protected int getRounds() { - return rounds; - } - - protected boolean isFullscreen() { - return fullscreen; - } - - protected String getStage() { - return stage; - } - - protected Player getP1() { - return p1; - } - - protected Player getP2() { - return p2; - } - - protected void setF_is(FileInputStream f_is) { - this.f_is = f_is; - } - - protected void setWidth(int width) { - this.width = width; - } - - protected void setHeight(int height) { - this.height = height; - } - - protected void setR_x(int r_x) { - this.r_x = r_x; - } - - protected void setR_y(int r_y) { - this.r_y = r_y; - } - - protected void setRounds(int rounds) { - this.rounds = rounds; - } - - protected void setFullscreen(boolean fullscreen) { - this.fullscreen = fullscreen; - } - - protected void setStage(String stage) { - this.stage = stage; - } - - protected void setP1(Player p1) { - this.p1 = p1; - } - - protected void setP2(Player p2) { - this.p2 = p2; + HashMap set = Launcher.pointer.getArraysettings(); + int width = (Integer) set.get("width"); + int height = (Integer) set.get("height"); + int rounds = (Integer) set.get("rounds"); + boolean fullscreen = (Boolean) set.get("fullscreen"); + String character1 = (String) set.get("character1"); + String character2 = (String) set.get("character2"); + String stage = (String) set.get("stage"); + + try { + config.write(width, height, rounds, fullscreen, character1, character2, stage); + } catch (Exception e) { + e.printStackTrace(); + } } } From 29470c5e82823217b222565449142344fe624612 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20Autin?= Date: Thu, 10 Jun 2021 13:32:06 +0200 Subject: [PATCH 4/4] Added new configuration file --- game.set | 1 + 1 file changed, 1 insertion(+) create mode 100644 game.set diff --git a/game.set b/game.set new file mode 100644 index 0000000..61921ba --- /dev/null +++ b/game.set @@ -0,0 +1 @@ +{"game":[{"character2":"ken","character1":"ryu","fullscreen":"true","stage":"default","width":"1920","rounds":"5","height":"1080"}]} \ No newline at end of file