Commit 1ff7245d authored by Rafi Ramadhan's avatar Rafi Ramadhan
Browse files

update server

parent c290cb32
/*
* 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,110 @@ public class JSONParse { ...@@ -25,16 +25,110 @@ public class JSONParse {
return request; return request;
} }
//Respon JSON untuk login
public Map loginJSON() { public Map loginJSON() {
Map request = new LinkedHashMap(); Map response = new LinkedHashMap();
request.put("status", "OK"); response.put("status", "OK");
request.put("token", "bss7d91234fef"); response.put("token", "bss7d91234fef");
request.put("x", 0); response.put("x", 0);
request.put("y", 0); response.put("y", 0);
request.put("time", 1213131311); response.put("time", 1213131311);
return request; return response;
}
//Respon JSON untuk signup
public Map signupJSON() {
Map response = new LinkedHashMap();
response.put("status", "ok");
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) {
Map response = new LinkedHashMap();
response.put("status", "ok");
response.put("item", item_id);
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) {
Map response = new LinkedHashMap();
response.put("status", "ok");
response.put("time", time);
return response;
}
//Respon JSON untuk field
public Map fieldJSON(String token, int item) {
Map response = new LinkedHashMap();
response.put("status", "ok");
response.put("item", item);
return response;
}
//Respon JSON untuk offer
public Map offerJSON() {
Map response = new LinkedHashMap();
response.put("status", "ok");
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
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;
} }
public String parseJSON(String input, String key) throws ParseException { public String parseJSON(String input, String key) throws ParseException {
...@@ -47,4 +141,15 @@ public class JSONParse { ...@@ -47,4 +141,15 @@ public class JSONParse {
return response; 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();
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();
}
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);
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);
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);
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();
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);
}
}
/*
* 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,12 @@ public class Server extends Thread { ...@@ -30,23 +30,12 @@ public class Server extends Thread {
serverSocket.setSoTimeout(10000); serverSocket.setSoTimeout(10000);
} }
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();
}
return JSONValue.toJSONString(response);
}
@Override @Override
public void run() public void run()
{ {
String response; String response;
JSONParse parse = new JSONParse(); JSONParse parse = new JSONParse();
MethodHandler handler = new MethodHandler();
while(true) while(true)
{ {
try try
...@@ -64,13 +53,42 @@ public class Server extends Thread { ...@@ -64,13 +53,42 @@ public class Server extends Thread {
DataOutputStream out = DataOutputStream out =
new DataOutputStream(server.getOutputStream()); new DataOutputStream(server.getOutputStream());
if(parse.parseJSON(response, "method").equals("login")) { if(parse.parseJSON(response, "method").equals("login")) {
out.writeUTF(Login(response)); out.writeUTF(handler.Login(response));
}
else if(parse.parseJSON(response, "method").equals("signup")) {
out.writeUTF(handler.Signup(response));
}
else if(parse.parseJSON(response, "method").equals("inventory")) {
out.writeUTF(handler.Inventory(response));
}
else if(parse.parseJSON(response, "method").equals("mixitem")) {
out.writeUTF(handler.MixItem(response));
}
else if(parse.parseJSON(response, "method").equals("map")) {
out.writeUTF(handler.Map(response));
}
else if(parse.parseJSON(response, "method").equals("move")) {
out.writeUTF(handler.Move(response));
}
else if(parse.parseJSON(response, "method").equals("field")) {
out.writeUTF(handler.Field(response));
}
else if(parse.parseJSON(response, "method").equals("offer")) {
out.writeUTF(handler.Offer(response));
}
else if(parse.parseJSON(response, "method").equals("tradebox")) {
out.writeUTF(handler.TradeBox(response));
}
else if(parse.parseJSON(response, "method").equals("sendfind")) {
out.writeUTF(handler.sendFind(response));
} }
else { else {
out.writeUTF("Thank you for connecting to " out.writeUTF("Thank you for connecting to "
+ server.getLocalSocketAddress() + "\nGoodbye!"); + server.getLocalSocketAddress() + "\nGoodbye!");
} }
} catch (ParseException ex) { } catch (ParseException ex) {
Logger.getLogger(Server.class.getName()).log(Level.SEVERE, null, ex); Logger.getLogger(Server.class.getName()).log(Level.SEVERE, null, ex);
} }
......
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