Commit c9e2d2e6 authored by adwisatya's avatar adwisatya
Browse files

Sukses: load map dari file, ambil file dari field berdasarkan map

parent 4f0ec817
#Mon, 27 Apr 2015 01:02:47 +0700
#Mon, 27 Apr 2015 02:21:39 +0700
C\:\\Users\\adwisatya\\Documents\\Semester\ 6\\Tugas\\IF3230\ Sister\\IF3230-Tugas-Besar-Sister-2015\\src\\server=
{"name" : "Bandar Behari","width" : 4,"height" : 4,"map" :[["R13", "R13", "R13", "R13"],["R13", "R13", "R14", "R14"],["R13", "R14", "R14", "R14"],["R13", "R14", "R14", "R14"]]}
......@@ -36,9 +36,12 @@ import java.util.Scanner;
import java.util.TimeZone;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.json.JSONException;
import org.json.*;
import org.json.JSONObject;
import org.json.simple.parser.JSONParser;
/**
*
* @author adwisatya
......@@ -77,7 +80,6 @@ public class Client extends Thread{
/* Testing database */
clientSentence = "";
char[] buffer = new char[MAX_BUFFER];
loadMap();
while(true){
try{
fromClient = new BufferedReader(new InputStreamReader(connectionSocket.getInputStream()));
......@@ -307,15 +309,14 @@ public class Client extends Thread{
}
/* Sukses */
public void cmdMap(String token) throws IOException, NoSuchAlgorithmException, JSONException{
loadMap();
String response = "";
JSONObject tmpobj = new JSONObject();
JSONObject obj = new JSONObject();
try {
obj = database.getMap();
} catch (ClassNotFoundException ex) {
obj.put("status", "error");
Logger.getLogger(Client.class.getName()).log(Level.SEVERE, null, ex);
}
tmpobj = utility.loadMap();
obj.put("status", "ok");
obj.put("name", tmpobj.getString("name"));
obj.put("width", tmpobj.getInt("width"));
obj.put("height", tmpobj.getInt("height"));
sendToClient(obj.toString());
}
/* Sukses */
......@@ -557,18 +558,6 @@ public class Client extends Thread{
return pass;
}
public void loadMap(){
String fileName = "map.json";
String inputFilePath = null;
File jarFile;
try {
jarFile = new File(Client.class.getProtectionDomain().getCodeSource().getLocation().toURI().getPath());
inputFilePath = jarFile.getParent() + File.separator + fileName;
} catch (URISyntaxException ex) {
Logger.getLogger(Client.class.getName()).log(Level.SEVERE, null, ex);
}
System.out.println(inputFilePath);
}
}
......@@ -204,20 +204,20 @@ public class Database {
x = rs.getInt("x");
y = rs.getInt("y");
}
rs = statement.executeQuery("SELECT description from map");
while(rs.next()){
try {
obj = new JSONObject(rs.getString("description"));
itemid = obj.getString("R"+y+""+x);
objToSent.put("item", new Integer(itemid));
objToSent.put("status", "ok");
updateJmlItem(token,itemid,getJmlItem(token,itemid));
} catch (JSONException ex) {
objToSent.put("description", "tidak ada item");
objToSent.put("status", "fail");
Logger.getLogger(Database.class.getName()).log(Level.SEVERE, null, ex);
}
// rs = statement.executeQuery("SELECT description from map");
// while(rs.next()){
try {
obj = new JSONObject(utility.loadMap().getString("description"));
itemid = obj.getString("R"+(y+1)+""+(x+1));
objToSent.put("item", new Integer(itemid));
objToSent.put("status", "ok");
updateJmlItem(token,itemid,getJmlItem(token,itemid));
} catch (JSONException ex) {
objToSent.put("description", "tidak ada item");
objToSent.put("status", "fail");
Logger.getLogger(Database.class.getName()).log(Level.SEVERE, null, ex);
}
// }
statement.close();
conn.close();
}catch(SQLException e){
......
......@@ -24,6 +24,7 @@ import static thealchemist.Client.myPort;
* @author adwisatya
*/
public class TCPServer {
static int portNumber = 6789;
public static void main(String argv[]) throws Exception{
// ServerSocket mainSocket ;
......@@ -37,9 +38,11 @@ public class TCPServer {
// client.start();
// }
// }
Utility utility = new Utility();
ServerSocket mainSocket ;
mainSocket = new ServerSocket(portNumber);
//utility.loadMap();
System.out.println("Preparing map...");
System.out.println("Start listening...");
while(true){
......
......@@ -5,33 +5,43 @@
*/
package thealchemist;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.net.InetAddress;
import java.net.URISyntaxException;
import java.net.UnknownHostException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.sql.SQLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.json.simple.parser.JSONParser;
/**
*
* @author adwisatya
*/
public class Utility {
public long getTime(){
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
TimeZone utcZone = TimeZone.getTimeZone("UTC");
simpleDateFormat.setTimeZone(utcZone);
simpleDateFormat.setTimeZone(TimeZone.getDefault());
Date now = new Date();
Calendar calendar = Calendar.getInstance();
String strDate = simpleDateFormat.format(now);
try {
......@@ -82,4 +92,77 @@ public class Utility {
}
return sb.toString();
}
public JSONObject loadMap(){
Database database = new Database();
String fileName = "map.json";
String inputFilePath = null;
File jarFile;
try {
jarFile = new File(Client.class.getProtectionDomain().getCodeSource().getLocation().toURI().getPath());
inputFilePath = jarFile.getParent() + File.separator +".."+ File.separator + fileName;
} catch (URISyntaxException ex) {
Logger.getLogger(Client.class.getName()).log(Level.SEVERE, null, ex);
}
//System.out.println(inputFilePath);
JSONObject mapDesc = new JSONObject();
JSONParser parser = new JSONParser();
JSONObject jsonobj = null;
Object obj = null;
try {
obj = parser.parse(new FileReader(inputFilePath));
} catch (IOException ex) {
Logger.getLogger(Client.class.getName()).log(Level.SEVERE, null, ex);
} catch (org.json.simple.parser.ParseException ex) {
Logger.getLogger(Client.class.getName()).log(Level.SEVERE, null, ex);
}
try {
jsonobj = new JSONObject(obj.toString());
Pattern p = Pattern.compile("\"([^\"]*)\"");
Matcher m = p.matcher(jsonobj.getString("map").toString());
int width = jsonobj.getInt("width");
int height = jsonobj.getInt("height");
ArrayList<String> tmpItemLoc = new ArrayList<String>();
while (m.find()) {
//System.out.println(m.group(1));
tmpItemLoc.add(m.group(1));
}
for(int x = 1;x<=width;x++){
for(int y = 1;y <= height; y++){
mapDesc.put("R"+y+x, new Integer(getItemCode(tmpItemLoc.get(x*y-1))));
}
}
jsonobj.put("description", mapDesc.toString());
} catch (JSONException ex) {
Logger.getLogger(Client.class.getName()).log(Level.SEVERE, null, ex);
}
return jsonobj;
}
public int getItemCode(String code) {
int itemCode = -9;
switch(code) {
case "R11": itemCode = 0;
break;
case "R12": itemCode = 1;
break;
case "R13": itemCode = 2;
break;
case "R14": itemCode = 3;
break;
case "R21": itemCode = 4;
break;
case "R22": itemCode = 5;
break;
case "R23": itemCode = 6;
break;
case "R31": itemCode = 7;
break;
case "R32": itemCode = 8;
break;
case "R41": itemCode = 9;
break;
}
return itemCode;
}
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment