Commit 95df3b25 authored by Rakhmatullah Yoga Sutrisna's avatar Rakhmatullah Yoga Sutrisna
Browse files

revisi sendaccept

parent d320e03a
......@@ -6,6 +6,7 @@
package grandquestserver;
import static grandquestserver.MethodHandler.session;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.DataOutputStream;
......@@ -117,6 +118,11 @@ public class ClientHandler extends Thread {
resultlocal.append("offers", apa[i]);
}
System.out.println("hasil sendfind: "+JSONValue.toJSONString(resultlocal));
//simpan di var
DBHandler db = new DBHandler();
String token = parse.parseJSON(response, "token");
String username = session.get(handler.searchToken(token)).getUsername();
db.saveOfferFound(username, resultlocal.toString());
out.writeUTF(JSONValue.toJSONString(resultlocal));
break;
case "findoffer":
......@@ -124,8 +130,12 @@ public class ClientHandler extends Thread {
break;
case "sendaccept":
String offer_token = parse.parseJSON(response, "offer_token");
token = parse.parseJSON(response, "token");
JSONObject ceklocal = new JSONObject(handler.Accept(response));
System.out.println("respon dari lokal: " + ceklocal.toString());
boolean offer_found = ceklocal.getString("status").equals("ok");
db = new DBHandler();
Offer item = new Offer();
if(!offer_found) {
JSONObject requestAccept = new JSONObject();
requestAccept.put("method", "accept");
......@@ -153,6 +163,35 @@ public class ClientHandler extends Thread {
JSONObject acceptok = new JSONObject();
acceptok.put("status","ok");
out.writeUTF(acceptok.toString());
// nanganin perpindahan item di database;
username = session.get(handler.searchToken(token)).getUsername();
JSONObject offersfound_whole = new JSONObject(db.loadOfferFound(username));
JSONArray offersfound = offersfound_whole.getJSONArray("offers");
boolean found = false;
int j = 0;
while(!found && j<offersfound.length()){
if(offersfound.getJSONArray(j).get(5).equals(token)){
found = true;
}
else{
j++;
}
}
if(found){
//kurangin item dari offer yang ketemu punyanya si username yang ada di token
JSONArray offermatched = offersfound.getJSONArray(j);
int offer_itemid = offermatched.getInt(0);
int offer_item_qty = offermatched.getInt(1);
int demand_itemid = offermatched.getInt(2);
int demand_item_qty = offermatched.getInt(3);
boolean availability = offermatched.getBoolean(4);
//TODO: cekdemand item kuantitinya si user yang ada di token, punya ga dia, kalo punya kurangin demand, tambahin offer
}
System.out.println(username);
System.out.println(item.getOffered().intValue());
handler.pullItem(username, item.getOffered().intValue(), item.getOffered_num().intValue());
}
else{
JSONObject acceptok = new JSONObject();
......@@ -162,7 +201,16 @@ public class ClientHandler extends Thread {
}
}
else {
username = session.get(handler.searchToken(token)).getUsername();
item = db.pullOffer(offer_token);
System.out.println(username);
System.out.println(item.getOffered().intValue());
//TODO: cek demand quantity dan kurangin kalo >= stok dia punya di inventory
handler.pullItem(username, item.getOffered().intValue(), item.getOffered_num().intValue());
out.writeUTF(handler.sendAccept(response));
// nanganin perpindahan item di database
}
break;
case "accept":
......
......@@ -200,6 +200,27 @@ public class DBHandler {
return offer;
}
public void saveOfferFound(String user_name, String offers_found) {
try {
statement.executeUpdate("update user set offers_found='"+offers_found+"' where username='"+user_name+"'");
} catch (SQLException ex) {
Logger.getLogger(DBHandler.class.getName()).log(Level.SEVERE, null, ex);
}
}
public String loadOfferFound(String user_name) {
String offers_found = null;
try {
ResultSet result = statement.executeQuery("select offers_found from user where username='"+user_name+"'");
while(result.next()) {
offers_found = result.getString("offers_found");
}
} catch (SQLException ex) {
Logger.getLogger(DBHandler.class.getName()).log(Level.SEVERE, null, ex);
}
return offers_found;
}
//menampilkan seluruh isi tradebox
public List<Offer> getOffer(String username) throws SQLException {
......
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