Fully rewrote configuration.Config
Now is capable of parsing configuration file and writing file.
This commit is contained in:
parent
bc48da8109
commit
bacf117aa5
@ -4,20 +4,24 @@ import java.io.*;
|
|||||||
import org.json.simple.*;
|
import org.json.simple.*;
|
||||||
import org.json.simple.parser.*;
|
import org.json.simple.parser.*;
|
||||||
|
|
||||||
public class JsonToJava {
|
public class Config {
|
||||||
|
|
||||||
public static void main(String args[]) {
|
public int width, height, rounds;
|
||||||
|
public boolean fullscreen;
|
||||||
JsonRecover();
|
public String stage;
|
||||||
|
public String p1, p2;
|
||||||
|
|
||||||
|
public Config() throws FileNotFoundException {
|
||||||
|
parse();
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void JsonRecover() {
|
public void parse() throws FileNotFoundException {
|
||||||
|
|
||||||
// initialize the parser
|
// initialize the parser
|
||||||
JSONParser jsonP = new JSONParser();
|
JSONParser jsonP = new JSONParser();
|
||||||
try {
|
try {
|
||||||
// read the json document
|
// 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
|
// to print all values
|
||||||
// System.out.println(jsonO.values());
|
// System.out.println(jsonO.values());
|
||||||
@ -26,42 +30,45 @@ public class JsonToJava {
|
|||||||
// String test = (String) jsonO.get("test");
|
// String test = (String) jsonO.get("test");
|
||||||
// System.out.println("ceci est un test :" + test);
|
// System.out.println("ceci est un test :" + test);
|
||||||
|
|
||||||
// arena selection
|
|
||||||
// select an element on the list
|
JSONArray game = (JSONArray) jsonO.get("game");
|
||||||
JSONArray arena = (JSONArray) jsonO.get("arena");
|
JSONObject settings = (JSONObject) game.get(0);
|
||||||
|
|
||||||
// print a case of this element
|
// print a case of this element
|
||||||
System.out.println("arena : " + arena.get(1));
|
stage = (String) settings.get("stage");
|
||||||
|
|
||||||
// nb players selection
|
// nb players selection
|
||||||
JSONArray nb_players = (JSONArray) jsonO.get("nb_players");
|
/* JSONArray nb_players = (JSONArray) jsonO.get("nb_players");
|
||||||
System.out.println("nb_player : " + nb_players.get(1));
|
System.out.println("nb_player : " + nb_players.get(1)); */
|
||||||
|
|
||||||
// character selection
|
// character selection
|
||||||
JSONArray character1 = (JSONArray) jsonO.get("character1");
|
p1 = (String) settings.get("character1");
|
||||||
System.out.println("players 1 : " + character1.get(1));
|
p2 = (String) settings.get("character2");
|
||||||
|
|
||||||
JSONArray character2 = (JSONArray) jsonO.get("character2");
|
height = Integer.parseInt((String) settings.get("height")); // String to int
|
||||||
System.out.println("players 2 : " + character2.get(1));
|
width = Integer.parseInt((String) settings.get("width"));
|
||||||
|
|
||||||
// resolution
|
// fullscreen
|
||||||
JSONArray resolution = (JSONArray) jsonO.get("resolution");
|
String fs = (String) settings.get("fullscreen");
|
||||||
// resolution string " width x heigth"
|
if (fs.equals("true")) {
|
||||||
JSONObject reso = (JSONObject) resolution.get(1);
|
fullscreen = true;
|
||||||
|
} else fullscreen = false;
|
||||||
|
|
||||||
String heightStr = (String) reso.get("height");
|
// rounds
|
||||||
int height = Integer.parseInt(heightStr); // String to int
|
String temprounds = (String) settings.get("rounds");
|
||||||
|
switch (temprounds) {
|
||||||
String widthStr = (String) reso.get("width");
|
case "1": rounds = 1;
|
||||||
int width = Integer.parseInt(widthStr);
|
case "3": rounds = 3;
|
||||||
|
case "5": rounds = 5;
|
||||||
System.out.println("heigth : " + height + " width : " + width);
|
default: rounds = 1;
|
||||||
|
}
|
||||||
|
|
||||||
// button selection
|
// button selection
|
||||||
JSONArray allButton = (JSONArray) jsonO.get("button");
|
/* JSONArray allButton = (JSONArray) jsonO.get("button");
|
||||||
System.out.println(allButton);
|
System.out.println(allButton);
|
||||||
|
|
||||||
String up = (String) allButton.get(0);
|
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) {
|
} catch (FileNotFoundException e) {
|
||||||
e.printStackTrace();
|
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();
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user