Commit 90afc6fb authored by Rafi Ramadhan's avatar Rafi Ramadhan
Browse files

request status updated

parent 84a32bce
......@@ -25,10 +25,10 @@ public class GrandQuestServer {
*/
public static void main(String[] args) throws IOException, SQLException, ClassNotFoundException, InstantiationException, IllegalAccessException {
int port = 8000;
//Thread t = new Server(port);
//t.start();
DBHandler database = new DBHandler();
database.addInventory("ardi", 2);
Thread t = new Server(port);
t.start();
//DBHandler database = new DBHandler();
//database.addInventory("ardi", 2);
}
}
......@@ -26,24 +26,34 @@ public class JSONParse {
}
//Respon JSON untuk login
public Map loginJSON() {
public Map loginJSON(boolean valid) {
Map response = new LinkedHashMap();
response.put("status", "OK");
response.put("token", "bss7d91234fef");
response.put("x", 0);
response.put("y", 0);
response.put("time", 1213131311);
if (valid) {
response.put("status", "OK");
response.put("token", "bss7d91234fef");
response.put("x", 0);
response.put("y", 0);
response.put("time", 1213131311);
}
else {
response.put("status", "fail");
response.put("description", "username atau password salah");
}
return response;
}
//Respon JSON untuk signup
public Map signupJSON() {
public Map signupJSON(boolean valid) {
Map response = new LinkedHashMap();
response.put("status", "ok");
if (valid) {
response.put("status", "ok");
}
else {
response.put("status", "fail");
response.put("description", "username exist");
}
return response;
}
......@@ -58,12 +68,17 @@ public class JSONParse {
}
//Respon JSON untuk mixitem
public Map mixItemJSON(int item_id) {
public Map mixItemJSON(int item_id, boolean valid) {
Map response = new LinkedHashMap();
response.put("status", "ok");
response.put("item", item_id);
if (valid) {
response.put("status", "ok");
response.put("item", item_id);
}
else {
response.put("status", "fail");
response.put("description", "item cannot be mixed");
}
return response;
}
......@@ -80,31 +95,46 @@ public class JSONParse {
}
//Respon JSON untuk move
public Map moveJSON(String token, Long time) {
public Map moveJSON(String token, Long time, boolean valid) {
Map response = new LinkedHashMap();
response.put("status", "ok");
response.put("time", time);
if(valid) {
response.put("status", "ok");
response.put("time", time);
}
else {
response.put("status", "fail");
response.put("description", "invalid movement");
}
return response;
}
//Respon JSON untuk field
public Map fieldJSON(String token, int item) {
public Map fieldJSON(String token, int item, boolean valid) {
Map response = new LinkedHashMap();
response.put("status", "ok");
response.put("item", item);
if(valid) {
response.put("status", "ok");
response.put("item", item);
}
else {
response.put("status", "fail");
response.put("description", "no item here");
}
return response;
}
//Respon JSON untuk offer
public Map offerJSON() {
public Map offerJSON(boolean valid) {
Map response = new LinkedHashMap();
response.put("status", "ok");
if(valid) {
response.put("status", "ok");
}
else {
response.put("status", "fail");
response.put("description", "insufficient offer");
}
return response;
}
......@@ -119,7 +149,7 @@ public class JSONParse {
return response;
}
//Respon JSON untuk sendfind
//Respon JSON untuk sendfind / findoffer
public Map sendfindJSON(List<Object> target) {
Map response = new LinkedHashMap();
......@@ -131,6 +161,65 @@ public class JSONParse {
return response;
}
//Respon JSON untuk sendaccept
public Map sendacceptJSON(boolean valid) {
Map response = new LinkedHashMap();
if(valid) {
response.put("status", "ok");
}
else {
response.put("status", "fail");
response.put("description", "cannot accept this item");
}
return response;
}
//Respon JSON untuk accept
public Map acceptJSON(boolean valid) {
Map response = new LinkedHashMap();
if (valid) {
response.put("status", "ok");
}
else {
response.put("status", "fail");
response.put("description", "item is not available");
}
return response;
}
//Respon JSON untuk fetchitem
public Map fetchitemJSON(boolean valid) {
Map response = new LinkedHashMap();
if(valid) {
response.put("status", "ok");
}
else {
response.put("status", "fail");
response.put("description", "no item to be fetched");
}
return response;
}
//Respon JSON untuk canceloffer
public Map cancelofferJSON(boolean valid) {
Map response = new LinkedHashMap();
if(valid) {
response.put("status", "ok");
}
else {
response.put("status", "fail");
response.put("description", "offer cannot be cancelled");
}
return response;
}
public String parseJSON(String input, String key) throws ParseException {
JSONParser parser = new JSONParser();
......
......@@ -24,7 +24,7 @@ public class MethodHandler {
JSONParse parse = new JSONParse();
Map response = new LinkedHashMap();
response = parse.signupJSON();
response = parse.signupJSON(true);
return JSONValue.toJSONString(response);
}
......@@ -36,7 +36,7 @@ public class MethodHandler {
if(parse.parseJSON(input, "username").equals("user")) {
response = parse.loginJSON();
response = parse.loginJSON(false);
}
return JSONValue.toJSONString(response);
}
......@@ -63,7 +63,7 @@ public class MethodHandler {
int item_id = Combine(item1,item2);
response = parse.mixItemJSON(item_id);
response = parse.mixItemJSON(item_id, true);
System.out.println(JSONValue.toJSONString(response));
return JSONValue.toJSONString(response);
}
......@@ -97,7 +97,7 @@ public class MethodHandler {
Long y = parse.parseIntJSON(input, "y");
Long time = 6876781679l;
response = parse.moveJSON(token, time);
response = parse.moveJSON(token, time, true);
System.out.println(JSONValue.toJSONString(response));
return JSONValue.toJSONString(response);
}
......@@ -109,7 +109,7 @@ public class MethodHandler {
String token = parse.parseJSON(input, "token");
int item = 4;
response = parse.fieldJSON(token, item);
response = parse.fieldJSON(token, item, true);
System.out.println(JSONValue.toJSONString(response));
return JSONValue.toJSONString(response);
}
......@@ -119,7 +119,7 @@ public class MethodHandler {
JSONParse parse = new JSONParse();
Map response = new LinkedHashMap();
response = parse.offerJSON();
response = parse.offerJSON(true);
System.out.println(JSONValue.toJSONString(response));
return JSONValue.toJSONString(response);
}
......@@ -179,4 +179,69 @@ public class MethodHandler {
System.out.println(JSONValue.toJSONString(response));
return JSONValue.toJSONString(response);
}
public String findOffer(String input) throws ParseException {
List<Object> offer = new ArrayList<>();
List<Object> temp = new ArrayList<>();
List<Object> offers = new ArrayList<>();
JSONParse parse = new JSONParse();
Map response = new LinkedHashMap();
Long item_id = parse.parseIntJSON(input, "item");
if (item_id==2) {
offer.add(5); offer.add(3); offer.add(6);
offer.add(1); offer.add(false); offer.add("we23klf9vv4");
temp = copyList(offer);
offer.clear();
offers.add(temp);
}
else {
}
response = parse.sendfindJSON(offers);
System.out.println(JSONValue.toJSONString(response));
return JSONValue.toJSONString(response);
}
public String sendAccept(String input) {
JSONParse parse = new JSONParse();
Map response = new LinkedHashMap();
response = parse.sendacceptJSON(true);
return JSONValue.toJSONString(response);
}
public String Accept(String input) {
JSONParse parse = new JSONParse();
Map response = new LinkedHashMap();
response = parse.acceptJSON(true);
return JSONValue.toJSONString(response);
}
public String fetchItem(String input) {
JSONParse parse = new JSONParse();
Map response = new LinkedHashMap();
response = parse.fetchitemJSON(true);
return JSONValue.toJSONString(response);
}
public String cancelOffer(String input) {
JSONParse parse = new JSONParse();
Map response = new LinkedHashMap();
response = parse.cancelofferJSON(true);
return JSONValue.toJSONString(response);
}
}
......@@ -84,6 +84,16 @@ public class Server extends Thread {
else if(parse.parseJSON(response, "method").equals("sendfind")) {
out.writeUTF(handler.sendFind(response));
}
else if(parse.parseJSON(response, "method").equals("findoffer")) {
out.writeUTF(handler.sendFind(response));
}
else if(parse.parseJSON(response, "method").equals("sendaccept")) {
out.writeUTF(handler.sendFind(response));
}
else if(parse.parseJSON(response, "method").equals("accept")) {
out.writeUTF(handler.sendFind(response));
}
else {
out.writeUTF("Thank you for connecting to "
+ server.getLocalSocketAddress() + "\nGoodbye!");
......
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