Commit d7747515 authored by Diah Fauziah's avatar Diah Fauziah
Browse files
parents 6c79e982 78597edf
......@@ -115,7 +115,9 @@ public class Client extends Thread{
case "move":
cmdMove(JSONtoString("token",cmd),JSONtoString("x",cmd),JSONtoString("y",cmd));
break;
case "8": System.out.println(cmdField("ece63df163ce1948008064c006f4900d"));
/* 8 */
case "field":
cmdField(JSONtoString("token",cmd));
break;
case "offer" :
cmdOffer(JSONtoString("token",cmd), Integer.parseInt(JSONtoString("offered_item",cmd)), Integer.parseInt(JSONtoString("n1",cmd)), Integer.parseInt(JSONtoString("demanded_item",cmd)), Integer.parseInt(JSONtoString("n2",cmd)));
......@@ -281,25 +283,14 @@ public class Client extends Thread{
sendToClient(obj.toString());
}
/* Sukses 0.7 */
public String cmdField(String token) throws IOException, NoSuchAlgorithmException, JSONException{
String response = "";
String request= "";
Socket miniSocket = new Socket(URL,dbPort);
public void cmdField(String token) throws IOException, NoSuchAlgorithmException, JSONException{
JSONObject obj = new JSONObject();
obj.put("method", "field");
obj.put("token", token);
PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(miniSocket.getOutputStream())));
out.println(obj);
out.println();
out.flush();
BufferedReader in = new BufferedReader(new InputStreamReader(miniSocket.getInputStream()));
response = org.apache.commons.io.IOUtils.toString(in);
in.close();
return response;
try {
obj = database.isThereItem(token);
} catch (ClassNotFoundException ex) {
Logger.getLogger(Client.class.getName()).log(Level.SEVERE, null, ex);
}
sendToClient(obj.toString());
}
public void cmdOffer(String token,int offered_item,int n1, int n2, int demanded_item) throws IOException, NoSuchAlgorithmException, JSONException{
JSONObject obj = new JSONObject();
......
......@@ -181,6 +181,45 @@ public class Database {
}
return jml;
}
public JSONObject isThereItem(String token) throws ClassNotFoundException, JSONException{
Connection conn = null;
Statement statement = null;
ResultSet rs;
JSONObject objToSent = new JSONObject();
JSONObject obj;
int x = -9;
int y = -9;
String itemid;
try{
Class.forName(driver);
conn = DriverManager.getConnection(url+dbName,username,password);
statement = conn.createStatement();
rs = statement.executeQuery("SELECT x,y from users WHERE token ='"+token+"'");
while(rs.next()){
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");
} 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){
objToSent.put("status", "error");
e.printStackTrace();
}
return objToSent;
}
public JSONObject mixItems(String token, String itemid1, String itemid2) throws ClassNotFoundException, InstantiationException, IllegalAccessException, SQLException, NoSuchAlgorithmException{
Connection conn = null;
Statement statement = null;
......@@ -203,7 +242,7 @@ public class Database {
conn = DriverManager.getConnection(url+dbName,username,password);
statement = conn.createStatement();
try {
rs = statement.executeQuery("SELECT item0,item1,item2,item3,item4,item5,item6,item7,item8,item9 from `inventory` WHERE `token`='76aafe92b629d489996f7a1357005d33'");
rs = statement.executeQuery("SELECT item0,item1,item2,item3,item4,item5,item6,item7,item8,item9 from `inventory` WHERE `token`='"+token+"'");
while(rs.next()){
item0 = rs.getInt("item0");
item1 = rs.getInt("item1");
......
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