Commit ae1696bb authored by Rakhmatullah Yoga Sutrisna's avatar Rakhmatullah Yoga Sutrisna
Browse files

clientHandler

parents ee2d88ff 90afc6fb
......@@ -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);
}
}
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package grandquestserver;
/**
*
* @author Rakhmatullah Yoga S
*/
public class JSONHandler {
}
......@@ -25,16 +25,199 @@ public class JSONParse {
return request;
}
public Map loginJSON() {
Map request = new LinkedHashMap();
//Respon JSON untuk login
public Map loginJSON(boolean valid) {
Map response = new LinkedHashMap();
request.put("status", "OK");
request.put("token", "bss7d91234fef");
request.put("x", 0);
request.put("y", 0);
request.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(boolean valid) {
Map response = new LinkedHashMap();
return request;
if (valid) {
response.put("status", "ok");
}
else {
response.put("status", "fail");
response.put("description", "username exist");
}
return response;
}
//Respon JSON untuk inventory
public Map invJSON(List<Integer> inv) {
Map response = new LinkedHashMap();
response.put("status", "ok");
response.put("inventory", inv);
return response;
}
//Respon JSON untuk mixitem
public Map mixItemJSON(int item_id, boolean valid) {
Map response = new LinkedHashMap();
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;
}
//Respon JSON untuk map
public Map mapJSON(String location, Long width, Long height) {
Map response = new LinkedHashMap();
response.put("status", "ok");
response.put("name", location);
response.put("width", width);
response.put("height", height);
return response;
}
//Respon JSON untuk move
public Map moveJSON(String token, Long time, boolean valid) {
Map response = new LinkedHashMap();
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, boolean valid) {
Map response = new LinkedHashMap();
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(boolean valid) {
Map response = new LinkedHashMap();
if(valid) {
response.put("status", "ok");
}
else {
response.put("status", "fail");
response.put("description", "insufficient offer");
}
return response;
}
//Respon JSON untuk offer
public Map tradeboxJSON(List<Object> offers) {
Map response = new LinkedHashMap();
List<String> result = new ArrayList<>();
response.put("status", "ok");
response.put("offers", offers);
return response;
}
//Respon JSON untuk sendfind / findoffer
public Map sendfindJSON(List<Object> target) {
Map response = new LinkedHashMap();
List<String> result = new ArrayList<>();
response.put("status", "ok");
response.put("offers", target);
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 {
......@@ -47,4 +230,15 @@ public class JSONParse {
return response;
}
public Long parseIntJSON(String input, String key) throws ParseException {
JSONParser parser = new JSONParser();
Object respond = parser.parse(input);
JSONObject obj = (JSONObject) respond;
Long response = (Long) obj.get(key);
return response;
}
}
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package grandquestserver;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import org.json.simple.JSONValue;
import org.json.simple.parser.ParseException;
/**
*
* @author Rakhmatullah Yoga S
*/
public class MethodHandler {
public String Signup(String input) {
JSONParse parse = new JSONParse();
Map response = new LinkedHashMap();
response = parse.signupJSON(true);
return JSONValue.toJSONString(response);
}
public String Login(String input) throws ParseException {
JSONParse parse = new JSONParse();
Map response = new LinkedHashMap();
if(parse.parseJSON(input, "username").equals("user")) {
response = parse.loginJSON(false);
}
return JSONValue.toJSONString(response);
}
public String Inventory(String input) {
List<Integer> inv = new ArrayList<>();
JSONParse parse = new JSONParse();
Map response = new LinkedHashMap();
inv.add(1); inv.add(2); inv.add(3); inv.add(4); inv.add(5);
inv.add(1); inv.add(2); inv.add(3); inv.add(4); inv.add(5);
response = parse.invJSON(inv);
return JSONValue.toJSONString(response);
}
public String MixItem(String input) throws ParseException {
JSONParse parse = new JSONParse();
Map response = new LinkedHashMap();
Long item1 = parse.parseIntJSON(input, "item1");
Long item2 = parse.parseIntJSON(input, "item2");
int item_id = Combine(item1,item2);
response = parse.mixItemJSON(item_id, true);
System.out.println(JSONValue.toJSONString(response));
return JSONValue.toJSONString(response);
}
public int Combine(Long item1, Long item2) {
int id = 0;
return id;
}
public String Map(String input) throws ParseException {
JSONParse parse = new JSONParse();
Map response = new LinkedHashMap();
String location = parse.parseJSON(input, "name");
Long width = parse.parseIntJSON(input, "width");
Long height = parse.parseIntJSON(input, "height");
response = parse.mapJSON(location, width, height);
System.out.println(JSONValue.toJSONString(response));
return JSONValue.toJSONString(response);
}
public String Move(String input) throws ParseException {
JSONParse parse = new JSONParse();
Map response = new LinkedHashMap();
String token = parse.parseJSON(input, "token");
Long x = parse.parseIntJSON(input, "x");
Long y = parse.parseIntJSON(input, "y");
Long time = 6876781679l;
response = parse.moveJSON(token, time, true);
System.out.println(JSONValue.toJSONString(response));
return JSONValue.toJSONString(response);
}
public String Field(String input) throws ParseException {
JSONParse parse = new JSONParse();
Map response = new LinkedHashMap();
String token = parse.parseJSON(input, "token");
int item = 4;
response = parse.fieldJSON(token, item, true);
System.out.println(JSONValue.toJSONString(response));
return JSONValue.toJSONString(response);
}
public String Offer(String input) throws ParseException {
JSONParse parse = new JSONParse();
Map response = new LinkedHashMap();
response = parse.offerJSON(true);
System.out.println(JSONValue.toJSONString(response));
return JSONValue.toJSONString(response);
}
public String TradeBox(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();
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);
//offers.add(temp);
response = parse.tradeboxJSON(offers);
System.out.println(JSONValue.toJSONString(response));
return JSONValue.toJSONString(response);
}
public List<Object> copyList(List<Object> target) {
List<Object> result = new ArrayList<>();
for (int i=0; i<target.size(); i++) {
result.add(target.get(i));
}
return result;
}
public String sendFind(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");
System.out.println(item_id);
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 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);
}
}
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package grandquestserver;
/**
*
* @author Lenovo
*/
public class Offer {
public int offered;
public int offered_num;
public int demanded;
public int demanded_num;
public boolean availability;
public String token;
public int getOffered() {
return offered;
}
public void setOffered(int offered) {
this.offered = offered;
}
public int getOffered_num() {
return offered_num;
}
public void setOffered_num(int offered_num) {
this.offered_num = offered_num;
}
public int getDemanded() {
return demanded;
}
public void setDemanded(int demanded) {
this.demanded = demanded;
}
public int getDemanded_num() {
return demanded_num;
}
public void setDemanded_num(int demanded_num) {
this.demanded_num = demanded_num;
}
public boolean isAvailability() {
return availability;
}
public void setAvailability(boolean availability) {
this.availability = availability;
}
public String getToken() {
return token;
}
public void setToken(String token) {
this.token = token;
}
}
......@@ -30,23 +30,17 @@ public class Server extends Thread {
//serverSocket.setSoTimeout(10000);
}
public String Login(String input) throws ParseException {
JSONParse parse = new JSONParse();
Map response = new LinkedHashMap();