Commit eae10bf9 authored by Jonathan Sudibya's avatar Jonathan Sudibya
Browse files

update some new method

parent 7102dd8a
......@@ -55,6 +55,14 @@ public class DBHandler {
WriteResult result = users.update(query, update);
}
public static void setOfferAttribute(String username, String key, String value) {
DBObject query = new BasicDBObject("username",username);
DBObject update = new BasicDBObject();
query.put("username",username);
update.put("$set", new BasicDBObject(key,value));
WriteResult result = offers.update(query, update);
}
public static void signUp (String username, String password) {
BasicDBObject document = new BasicDBObject();
document.put("username", username);
......
package Method;
import static Controller.ConnectionHandler.serverList;
import Controller.DBHandler;
import Controller.ConnectionHandler;
import com.mongodb.BasicDBObject;
......@@ -33,17 +34,115 @@ public final class EndpointMethod {
return token;
}
public static String getInventory(JSONObject obj) {
JSONObject res = new JSONObject();
res.put("status", "error");
String token = (String) obj.get("token");
String username = ConnectionHandler.tokenMap.get(token);
System.out.println("Usernameny adalah" + username);
if (username != null) {
JSONArray arr = new JSONArray();
int[] pos = DBHandler.getInventory(username);
for (int i = 0; i < pos.length; i++) {
arr.add(pos[i]);
}
res.put("status", "ok");
res.put("inventory", arr);
}
System.out.println(res.toString());
return res.toString();
}
public static String mixItem(JSONObject obj) {
JSONObject res = new JSONObject();
res.put("status", "error");
String token = (String) obj.get("token");
String username = ConnectionHandler.tokenMap.get(token);
if (username != null) {
int[] inv = DBHandler.getInventory(username);
int id1 = (int) (long) obj.get("item1");
int id2 = (int) (long) obj.get("item2");
if (inv[id1] < 3 && inv[id2] < 3) {
res.put("status", "fail");
res.put("description", "You don't have enough items to do that");
} else {
String response = DBHandler.mixItem(username, id1, id2);
System.out.println(response);
if ("error".equals(response)) {
res.put("status", "fail");
res.put("description", "You cannot mix those items");
} else {
res.put("status", "ok");
int result = 0;
if (id1 == 0 && id2 == 1) {
result = 4;
} else if (id1 == 1 && id2 == 2) {
result = 5;
} else if (id1 == 2 && id2 == 3) {
result = 6;
} else if (id1 == 4 && id2 == 5) {
result = 7;
} else if (id1 == 5 && id2 == 6) {
result = 8;
} else if (id1 == 7 && id2 == 8) {
result = 9;
}
res.put("item", result);
}
}
}
System.out.println(res.toString());
return res.toString();
}
public static String offer(JSONObject obj) throws NoSuchAlgorithmException {
JSONObject res = new JSONObject();
res.put("status", "error");
String token = (String) obj.get("token");
String username = ConnectionHandler.tokenMap.get(token);
if (username != null) {
String offered = Long.toString((long) obj.get("offered_item"));
String demanded = Long.toString((long) obj.get("demanded_item"));
int n1 = (int) (long) obj.get("n1");
int n2 = (int) (long) obj.get("n2");
//int sum1 = (int)(DBHandler.getUserAttribute(username,offered));
//int sum2 = (int)(DBHandler.getUserAttribute(username,demanded));
String att1 = DBHandler.getUserAttribute(username, offered);
String att2 = DBHandler.getUserAttribute(username, demanded);
int sum1 = Integer.parseInt(att1);
int sum2 = Integer.parseInt(att2);
if (n1 <= sum1) {
String timestamp = Long.toString(System.currentTimeMillis());
String offer_token = EndpointMethod.generateToken(username + timestamp);
BasicDBObject queryObj = new BasicDBObject();
queryObj.put("username", username);
DBCursor cursorUser = DBHandler.users.find(queryObj);
if (cursorUser.size() > 0) {
BasicDBObject userObj = new BasicDBObject();
userObj = (BasicDBObject) cursorUser.next();
DBHandler.setUserAttribute(username, offered, String.valueOf(sum1 - n1));
DBHandler.addOffer(username, offer_token, Integer.parseInt(offered), n1, Integer.parseInt(demanded), n2);
res.put("status", "ok");
}
} else {
res.put("status", "fail");
res.put("description", "You don't have enough items to do that");
}
}
return res.toString();
}
public static String tradebox(JSONObject obj) throws IOException, ParseException {
JSONObject res = new JSONObject();
res.put("status", "error");
BasicDBObject queryObj = new BasicDBObject();
String token=obj.get("token").toString();
String username=ConnectionHandler.tokenMap.get(token);
String token = obj.get("token").toString();
String username = ConnectionHandler.tokenMap.get(token);
queryObj.put("username", username);
DBCursor cursor = DBHandler.offers.find(queryObj);
JSONArray offer_list = new JSONArray();
res.put("status", "ok");
if (cursor.hasNext()) {
while (cursor.hasNext()) {
BasicDBObject docObj = new BasicDBObject();
docObj = (BasicDBObject) cursor.next();
JSONArray offerJson = new JSONArray();
......@@ -61,8 +160,57 @@ public final class EndpointMethod {
public static String findOffer(JSONObject obj) throws UnknownHostException {
JSONObject res = new JSONObject();
res.put("status", "error");
BasicDBObject queryObj = new BasicDBObject();
queryObj.put("offered_item", obj.get("item").toString());
DBCursor cursor = DBHandler.offers.find(queryObj);
if (cursor.size() > 0) {
JSONArray offer_list = new JSONArray();
while (cursor.hasNext()) {
BasicDBObject docObj = new BasicDBObject();
docObj = (BasicDBObject) cursor.next();
JSONArray offerJson = new JSONArray();
offerJson.add(docObj.get("offered_item"));
offerJson.add(docObj.get("n1"));
offerJson.add(docObj.get("demanded_item"));
offerJson.add(docObj.get("n2"));
offerJson.add(docObj.get("available"));
offerJson.add(docObj.get("offer_token"));
offer_list.add(offerJson);
}
res.put("status", "ok");
res.put("offers", DBHandler.offers);
res.put("offers", offer_list);
}
return res.toString();
}
public static String sendAccept(JSONObject obj) {
return null;
}
public static String accept(JSONObject obj) {
JSONObject res = new JSONObject();
res.put("status", "error");
BasicDBObject queryObj = new BasicDBObject();
queryObj.put("offer_token", obj.get("offer_token").toString());
DBCursor cursor = DBHandler.offers.find(queryObj);
if (cursor.size() > 0) {
JSONArray offer_list = new JSONArray();
while (cursor.hasNext()) {
BasicDBObject docObj = new BasicDBObject();
docObj = (BasicDBObject) cursor.next();
if (docObj.get("available").toString().equalsIgnoreCase("true")) {
BasicDBObject updateObj = new BasicDBObject();
updateObj.put("available", false);
DBHandler.setOfferAttribute(null, null, null);
res.put("status", "ok");
} else {
res.put("status", "fail");
res.put("description", "offer is not available");
}
}
}
return res.toString();
}
......@@ -70,9 +218,10 @@ public final class EndpointMethod {
JSONObject res = new JSONObject();
String token = Obj.get("token").toString();
String offer_token = Obj.get("offer_token").toString();
String username = ConnectionHandler.tokenMap.get(token);
BasicDBObject queryObj = new BasicDBObject();
queryObj.put("offer_token", offer_token);
queryObj.put("username", ConnectionHandler.tokenMap.get(token));
queryObj.put("username", username);
DBCursor cursor = DBHandler.offers.find(queryObj);
while (cursor.hasNext()) {
BasicDBObject curObj = new BasicDBObject();
......@@ -80,12 +229,13 @@ public final class EndpointMethod {
if (curObj.get("available").toString().equalsIgnoreCase("false")) {
res.put("status", "ok");
BasicDBObject queryObj1 = new BasicDBObject();
queryObj1.put("username", ConnectionHandler.tokenMap.get(token));
queryObj1.put("username", username);
DBCursor cursorUser = DBHandler.users.find(queryObj1);
BasicDBObject curObj1 = new BasicDBObject();
curObj1 = (BasicDBObject) cursorUser.next();
Long tmp = Long.valueOf(curObj.get("n2").toString()) + Long.valueOf(curObj1.get(curObj.get("demanded_item").toString()).toString());
curObj1.put(curObj.get("demanded_item").toString(), tmp);
Integer tmp = Integer.valueOf(curObj.get("n2").toString()) + Integer.valueOf(curObj1.get(curObj.get("demanded_item").toString()).toString());
DBHandler.setUserAttribute(username, curObj.get("demanded_item").toString(), tmp.toString());
DBHandler.offers.remove(curObj);
} else {
res.put("status", "fail");
res.put("description", "Unknown fail in fetchItem");
......@@ -98,9 +248,10 @@ public final class EndpointMethod {
JSONObject res = new JSONObject();
String token = Obj.get("token").toString();
String offer_token = Obj.get("offer_token").toString();
String username = ConnectionHandler.tokenMap.get(token);
BasicDBObject queryObj = new BasicDBObject();
queryObj.put("offer_token", offer_token);
queryObj.put("username", ConnectionHandler.tokenMap.get(token));
queryObj.put("username", username);
DBCursor cursor = DBHandler.offers.find(queryObj);
while (cursor.hasNext()) {
BasicDBObject curObj = new BasicDBObject();
......@@ -108,12 +259,13 @@ public final class EndpointMethod {
if (curObj.get("available").toString().equalsIgnoreCase("true")) {
res.put("status", "ok");
BasicDBObject queryObj1 = new BasicDBObject();
queryObj1.put("username", ConnectionHandler.tokenMap.get(token));
queryObj1.put("username", username);
DBCursor cursorUser = DBHandler.users.find(queryObj1);
BasicDBObject curObj1 = new BasicDBObject();
curObj1 = (BasicDBObject) cursorUser.next();
Long tmp = Long.valueOf(curObj.get("n1").toString()) + Long.valueOf(curObj1.get(curObj.get("offered_item").toString()).toString());
curObj1.put(curObj.get("offered_item").toString(), tmp);
DBHandler.setUserAttribute(username, curObj.get("offered_item").toString(), tmp.toString());
DBHandler.offers.remove(curObj);
} else {
res.put("status", "fail");
res.put("description", "Unknown fail in cancelOffer");
......@@ -162,72 +314,6 @@ public final class EndpointMethod {
return res.toString();
}
public static String getInventory(JSONObject obj) {
JSONObject res = new JSONObject();
res.put("status", "error");
String token = (String) obj.get("token");
String username = ConnectionHandler.tokenMap.get(token);
if(username!=null) {
JSONArray arr = new JSONArray();
int[] pos = DBHandler.getInventory(username);
for(int i=0; i<pos.length; i++) {
arr.add(pos[i]);
}
res.put("status","ok");
res.put("inventory",arr);
} System.out.println(res.toString());
return res.toString();
}
public static String mixItem(JSONObject obj) {
JSONObject res = new JSONObject();
res.put("status", "error");
String token = (String) obj.get("token");
String username = ConnectionHandler.tokenMap.get(token);
if(username!=null) {
int[] inv = DBHandler.getInventory(username);
int id1 = (int)(long) obj.get("item1");
int id2 = (int)(long) obj.get("item2");
//System.out.println(inv[id1] + inv[id2]);
if(inv[id1]<3 || inv[id2]<3) {
res.put("status", "fail");
res.put("description", "You don't have enough items to do that");
}
else {
String response = DBHandler.mixItem(username, id1, id2); System.out.println(response);
if("error".equals(response)) {
res.put("status", "fail");
res.put("description","You cannot mix those items");
}
else {
res.put("status","ok");
int result = 0;
if(id1==0 && id2==1) {
result = 4;
}
else if(id1==1 && id2==2) {
result = 5;
}
else if(id1==2 && id2==3) {
result = 6;
}
else if(id1==4 && id2==5) {
result = 7;
}
else if(id1==5 && id2==6) {
result = 8;
}
else if(id1==7 && id2==8) {
result = 9;
}
res.put("item",result);
}
}
}
System.out.println(res.toString());
return res.toString();
}
public static String getMap(JSONObject obj) {
JSONObject res = new JSONObject();
res.put("status", "error");
......@@ -306,39 +392,23 @@ public final class EndpointMethod {
return res.toString();
}
public static String offer(JSONObject obj) throws NoSuchAlgorithmException {
JSONObject res = new JSONObject();
res.put("status", "error");
String token = (String) obj.get("token");
String tokenOffer;
String username = ConnectionHandler.tokenMap.get(token);
if(username!=null) {
String offered = Long.toString((long) obj.get("offered_item"));
String demanded = Long.toString((long) obj.get("demanded_item"));
int n1 = (int)(long) obj.get("n1");
int n2 = (int)(long) obj.get("n2");
//int sum1 = (int)(DBHandler.getUserAttribute(username,offered));
//int sum2 = (int)(DBHandler.getUserAttribute(username,demanded));
int sum1 = Integer.parseInt(DBHandler.getUserAttribute(username,offered));
int sum2 = Integer.parseInt(DBHandler.getUserAttribute(username,demanded));
if(n1<=sum1 && n2<=sum2) {
String timestamp = Long.toString(System.currentTimeMillis());
tokenOffer = EndpointMethod.generateToken(username+timestamp);
DBHandler.addOffer(username, tokenOffer, Integer.parseInt(offered), n1, Integer.parseInt(demanded), n2);
res.put("status", "ok");
}
else {
res.put("status", "fail");
res.put("description","You don't have enough items to do that");
}
}
return res.toString();
}
public static String statusError()
{
JSONObject error = new JSONObject();
error.put("status","error");
return error.toJSONString();
}
public static String updateServerStatus(JSONObject obj)
{
serverList = (JSONArray) obj.get("value");
JSONObject json = new JSONObject();
if (serverList == null){
json.put("status","ok");
}
else{
json.put("status","error");
}
return json.toJSONString();
}
}
......@@ -5,7 +5,6 @@
*/
package Model;
import static Controller.ConnectionHandler.serverList;
import Method.EndpointMethod;
import java.security.NoSuchAlgorithmException;
import java.util.logging.Level;
......@@ -93,7 +92,8 @@ public final class MessageProcessor {
break;
}
case "serverStatus" : {
serverList = (JSONArray) data.get("value");
response = EndpointMethod.updateServerStatus(data);
break;
}
default : {
//senderror
......
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