Commit fe84012c authored by Rafi Ramadhan's avatar Rafi Ramadhan
Browse files

update server logic

parent d632ae2f
...@@ -79,13 +79,19 @@ public class ClientHandler extends Thread { ...@@ -79,13 +79,19 @@ public class ClientHandler extends Thread {
out.writeUTF(handler.sendFind(response)); out.writeUTF(handler.sendFind(response));
break; break;
case "findoffer": case "findoffer":
out.writeUTF(handler.sendFind(response)); out.writeUTF(handler.findOffer(response));
break; break;
case "sendaccept": case "sendaccept":
out.writeUTF(handler.sendFind(response)); out.writeUTF(handler.sendAccept(response));
break; break;
case "accept": case "accept":
out.writeUTF(handler.sendFind(response)); out.writeUTF(handler.Accept(response));
break;
case "fetchitem":
out.writeUTF(handler.fetchItem(response));
break;
case "canceloffer":
out.writeUTF(handler.cancelOffer(response));
break; break;
default: default:
out.writeUTF("Thank you for connecting to " out.writeUTF("Thank you for connecting to "
......
...@@ -127,8 +127,12 @@ public class DBHandler { ...@@ -127,8 +127,12 @@ public class DBHandler {
} }
} }
public void removefromInventory() { public void removefromInventory(String username, int item, int amount) {
try {
statement.executeUpdate("DELETE FROM inventory where username='"+username+"' and item="+item+" limit "+amount);
} catch (SQLException ex) {
Logger.getLogger(DBHandler.class.getName()).log(Level.SEVERE, null, ex);
}
} }
public int getItem(String username, int item) throws SQLException { public int getItem(String username, int item) throws SQLException {
...@@ -158,6 +162,29 @@ public class DBHandler { ...@@ -158,6 +162,29 @@ public class DBHandler {
} }
} }
public void takeOffer(String token) {
try {
statement.executeUpdate("DELETE FROM tradebox where offer_token='"+token+"'");
} catch (SQLException ex) {
Logger.getLogger(DBHandler.class.getName()).log(Level.SEVERE, null, ex);
}
}
public Offer pullOffer(String token) throws SQLException {
Offer offer = new Offer();
ResultSet result = statement.executeQuery("select * from tradebox where offer_token='"+token+"'");
while(result.next()) {
offer.setToken(result.getString("offer_token"));
offer.setOffered(result.getLong("item_offered"));
offer.setOffered_num(result.getLong("num_offered"));
offer.setDemanded(result.getLong("item_demanded"));
offer.setDemanded_num(result.getLong("num_demanded"));
offer.setAvailability(result.getBoolean("availability"));
}
return offer;
}
public List<Offer> getOffer(String username) throws SQLException { public List<Offer> getOffer(String username) throws SQLException {
Offer offer = new Offer(); Offer offer = new Offer();
...@@ -173,20 +200,65 @@ public class DBHandler { ...@@ -173,20 +200,65 @@ public class DBHandler {
offer.setAvailability(result.getBoolean("availability")); offer.setAvailability(result.getBoolean("availability"));
temp = offer.Copy(offer); temp = offer.Copy(offer);
list.add(temp); list.add(temp);
}
return list;
}
public List<Offer> findOffer(int item) throws SQLException {
Offer offer = new Offer();
Offer temp = new Offer();
List<Offer> list = new ArrayList<>();
ResultSet result = statement.executeQuery("select * from tradebox where item_offered="+item);
while(result.next()) {
offer.setToken(result.getString("offer_token"));
offer.setOffered(result.getLong("item_offered"));
offer.setOffered_num(result.getLong("num_offered"));
offer.setDemanded(result.getLong("item_demanded"));
offer.setDemanded_num(result.getLong("num_demanded"));
offer.setAvailability(result.getBoolean("availability"));
temp = offer.Copy(offer);
list.add(temp);
} }
return list; return list;
} }
public List<Offer> copyList(List<Offer> target) { public void finishingOffer(String token) {
try {
List<Offer> result = new ArrayList<>(); statement.executeUpdate("UPDATE tradebox SET availability=false where offer_token='"+token+"'");
for (int i=0; i<target.size(); i++) { } catch (SQLException ex) {
result.add(target.get(i)); Logger.getLogger(DBHandler.class.getName()).log(Level.SEVERE, null, ex);
}
return result;
} }
}
public boolean checkTradebox(String token) throws SQLException {
boolean exist = false;
String offer = "";
ResultSet result = statement.executeQuery("select offer_token from tradebox where offer_token='"+token+"'");
while(result.next()) {
offer = result.getString("offer_token");
}
if(!offer.isEmpty()) {
exist = true;
}
return exist;
}
public boolean checkStatus(String token) throws SQLException {
boolean available = false;
ResultSet result = statement.executeQuery("select availability from tradebox where offer_token='"+token+"'");
while(result.next()) {
available = result.getBoolean("availability");
}
return available;
}
} }
...@@ -192,16 +192,29 @@ public class MethodHandler { ...@@ -192,16 +192,29 @@ public class MethodHandler {
return JSONValue.toJSONString(response); return JSONValue.toJSONString(response);
} }
public String MixItem(String input) throws ParseException { public String MixItem(String input) throws ParseException, SQLException {
JSONParse parse = new JSONParse(); JSONParse parse = new JSONParse();
Map response = new LinkedHashMap(); Map response = new LinkedHashMap();
DBHandler db = new DBHandler();
String token = parse.parseJSON(input, "token");
String username = session.get(searchToken(token)).getUsername();
Long item1 = parse.parseIntJSON(input, "item1"); Long item1 = parse.parseIntJSON(input, "item1");
Long item2 = parse.parseIntJSON(input, "item2"); Long item2 = parse.parseIntJSON(input, "item2");
int item_id = 0;
int item_id = Combine(item1,item2);
if((db.getItem(username, item1.intValue()) >=3) && (db.getItem(username, item2.intValue()) >=3)) {
response = parse.mixItemJSON(item_id, true); item_id = Combine(item1,item2);
db.addInventory(username, item_id);
db.removefromInventory(username, item1.intValue(), 3);
db.removefromInventory(username, item2.intValue(), 3);
response = parse.mixItemJSON(item_id, true);
}
else {
response = parse.mixItemJSON(item_id, false);
}
System.out.println(JSONValue.toJSONString(response)); System.out.println(JSONValue.toJSONString(response));
return JSONValue.toJSONString(response); return JSONValue.toJSONString(response);
} }
...@@ -371,14 +384,15 @@ public class MethodHandler { ...@@ -371,14 +384,15 @@ public class MethodHandler {
Long num_offered = parse.parseIntJSON(input, "n1"); Long num_offered = parse.parseIntJSON(input, "n1");
Long item_demanded = parse.parseIntJSON(input, "demanded_item"); Long item_demanded = parse.parseIntJSON(input, "demanded_item");
Long num_demanded = parse.parseIntJSON(input, "n2"); Long num_demanded = parse.parseIntJSON(input, "n2");
System.out.println(item_offered.intValue());
System.out.println(num_offered.intValue());
if(checkInventory(item_offered.intValue(), num_offered.intValue(), token)){ if(checkInventory(item_offered.intValue(), num_offered.intValue(), token)){
trade.setToken(offer_token); trade.setOffered(item_offered); trade.setOffered_num(num_offered); trade.setToken(offer_token); trade.setOffered(item_offered); trade.setOffered_num(num_offered);
trade.setDemanded(item_demanded); trade.setDemanded_num(num_demanded); trade.setAvailability(true); trade.setDemanded(item_demanded); trade.setDemanded_num(num_demanded); trade.setAvailability(true);
db.putOffer(username, trade); db.putOffer(username, trade);
db.removefromInventory(username, item_offered.intValue(), num_offered.intValue());
response = parse.offerJSON(true); response = parse.offerJSON(true);
//System.out.println();
} }
else { else {
response = parse.offerJSON(false); response = parse.offerJSON(false);
...@@ -386,7 +400,7 @@ public class MethodHandler { ...@@ -386,7 +400,7 @@ public class MethodHandler {
System.out.println(JSONValue.toJSONString(response)); System.out.println(JSONValue.toJSONString(response));
return JSONValue.toJSONString(response); return JSONValue.toJSONString(response);
} }
public String TradeBox(String input) throws ParseException, SQLException { public String TradeBox(String input) throws ParseException, SQLException {
List<Object> offer = new ArrayList<>(); List<Object> offer = new ArrayList<>();
...@@ -455,27 +469,35 @@ public class MethodHandler { ...@@ -455,27 +469,35 @@ public class MethodHandler {
return JSONValue.toJSONString(response); return JSONValue.toJSONString(response);
} }
public String findOffer(String input) throws ParseException { public String findOffer(String input) throws ParseException, SQLException {
List<Object> offer = new ArrayList<>(); List<Object> offer = new ArrayList<>();
List<Object> temp = new ArrayList<>(); List<Object> temp = new ArrayList<>();
List<Object> offers = new ArrayList<>(); List<Object> result = new ArrayList<>();
List<Offer> offers = new ArrayList<>();
JSONParse parse = new JSONParse(); JSONParse parse = new JSONParse();
Map response = new LinkedHashMap(); Map response = new LinkedHashMap();
DBHandler db = new DBHandler();
Long item_id = parse.parseIntJSON(input, "item"); Long item_id = parse.parseIntJSON(input, "item");
offers = db.findOffer(item_id.intValue());
if (item_id==2) {
offer.add(5); offer.add(3); offer.add(6); if(!offers.isEmpty()) {
offer.add(1); offer.add(false); offer.add("we23klf9vv4"); for(int i=0; i<offers.size(); i++) {
temp = copyList(offer); offer.add(offers.get(i).getToken());
offer.clear(); offer.add(offers.get(i).getOffered());
offers.add(temp); offer.add(offers.get(i).getOffered_num());
offer.add(offers.get(i).getDemanded());
offer.add(offers.get(i).getDemanded_num());
offer.add(offers.get(i).isAvailability());
temp = copyList(offer);
offer.clear();
result.add(temp);
}
} }
else {
} response = parse.sendfindJSON(result);
response = parse.sendfindJSON(offers);
System.out.println(JSONValue.toJSONString(response)); System.out.println(JSONValue.toJSONString(response));
return JSONValue.toJSONString(response); return JSONValue.toJSONString(response);
} }
...@@ -490,32 +512,75 @@ public class MethodHandler { ...@@ -490,32 +512,75 @@ public class MethodHandler {
return JSONValue.toJSONString(response); return JSONValue.toJSONString(response);
} }
public String Accept(String input) { public String Accept(String input) throws ParseException, SQLException {
JSONParse parse = new JSONParse(); JSONParse parse = new JSONParse();
Map response = new LinkedHashMap(); Map response = new LinkedHashMap();
DBHandler db = new DBHandler();
response = parse.acceptJSON(true);
String token = parse.parseJSON(input, "offer_token");
if(db.checkStatus(token) && db.checkTradebox(token)) {
db.finishingOffer(token);
response = parse.acceptJSON(true);
}
else {
response = parse.acceptJSON(false);
}
return JSONValue.toJSONString(response); return JSONValue.toJSONString(response);
} }
public String fetchItem(String input) { public void pullItem(String username, int item, int amount) throws SQLException {
DBHandler db = new DBHandler();
for(int i=0; i<amount; i++) {
db.addInventory(username, item);
}
}
public String fetchItem(String input) throws ParseException, SQLException {
JSONParse parse = new JSONParse(); JSONParse parse = new JSONParse();
Map response = new LinkedHashMap(); Map response = new LinkedHashMap();
DBHandler db = new DBHandler();
Offer item = new Offer();
response = parse.fetchitemJSON(true); String token = parse.parseJSON(input, "token");
String offer_token = parse.parseJSON(input, "offer_token");
String username = session.get(searchToken(token)).getUsername();
if(db.checkTradebox(offer_token)) {
item = db.pullOffer(offer_token);
db.takeOffer(offer_token);
pullItem(username, item.getDemanded().intValue(), item.getDemanded_num().intValue());
response = parse.fetchitemJSON(true);
}
else {
response = parse.fetchitemJSON(false);
}
return JSONValue.toJSONString(response); return JSONValue.toJSONString(response);
} }
public String cancelOffer(String input) { public String cancelOffer(String input) throws SQLException, ParseException {
JSONParse parse = new JSONParse(); JSONParse parse = new JSONParse();
Map response = new LinkedHashMap(); Map response = new LinkedHashMap();
DBHandler db = new DBHandler();
response = parse.cancelofferJSON(true); Offer item = new Offer();
String token = parse.parseJSON(input, "token");
String offer_token = parse.parseJSON(input, "offer_token");
String username = session.get(searchToken(token)).getUsername();
if(db.checkTradebox(offer_token)) {
item = db.pullOffer(offer_token);
db.takeOffer(offer_token);
pullItem(username, item.getOffered().intValue(), item.getOffered_num().intValue());
response = parse.cancelofferJSON(true);
}
else {
response = parse.cancelofferJSON(false);
}
return JSONValue.toJSONString(response); return JSONValue.toJSONString(response);
} }
......
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