if(currentLine.contains("MOVE")){
//opponent has made a move
String line = currentLine;
line = line.replaceAll("(SVR GAME MOVE )", ""); //remove SVR GAME MATCH
System.out.println("Na filter:" + line);
line = line.replaceAll("(\"|-|\s)", "");//remove quotations and - and spaces (=\s)
System.out.println("Na filter:" + line);
line = line.replaceAll("(\w+)", "\"$1\""); //add quotations to every word
System.out.println("Na filter:" + line);
JSONParser parser = new JSONParser();
try {
JSONObject json = (JSONObject) parser.parse(line);
//@todo bug when details is empty causes
//@todo example string: SVR GAME MOVE {PLAYER: "b", MOVE: "1", DETAILS: ""}
//@todo string that (line) that causes an error when parsing to json {"PLAYER":"b","MOVE":"1","DETAILS":}
//@todo Unexpected token RIGHT BRACE(}), i think because "details" has no value
System.out.println(json.get("PLAYER"));
System.out.println(json.get("MOVE"));
System.out.println(json.get("DETAILS"));
int index = Integer.valueOf(json.get("MOVE").toString());
for(GameView v : views){
v.serverMove(index);//dummy data is index 1
}
} catch (ParseException e) {
e.printStackTrace();
}
}
if(currentLine.contains("MOVE")){ //opponent has made a move String line = currentLine; line = line.replaceAll("(SVR GAME MOVE )", ""); //remove SVR GAME MATCH System.out.println("Na filter:" + line); line = line.replaceAll("(\"|-|\s)", "");//remove quotations and - and spaces (=\s) System.out.println("Na filter:" + line); line = line.replaceAll("(\w+)", "\"$1\""); //add quotations to every word System.out.println("Na filter:" + line); JSONParser parser = new JSONParser(); try { JSONObject json = (JSONObject) parser.parse(line); //@todo bug when details is empty causes //@todo example string: SVR GAME MOVE {PLAYER: "b", MOVE: "1", DETAILS: ""} //@todo string that (line) that causes an error when parsing to json {"PLAYER":"b","MOVE":"1","DETAILS":} //@todo Unexpected token RIGHT BRACE(}), i think because "details" has no value System.out.println(json.get("PLAYER")); System.out.println(json.get("MOVE")); System.out.println(json.get("DETAILS")); int index = Integer.valueOf(json.get("MOVE").toString()); for(GameView v : views){ v.serverMove(index);//dummy data is index 1 } } catch (ParseException e) { e.printStackTrace(); } }
See @todo