Commit 47ff722d authored by Akhmad Fakhoni Listiyan Dede's avatar Akhmad Fakhoni Listiyan Dede
Browse files

Get tradebox by token ok

parent df9e8f7f
......@@ -8,6 +8,7 @@ package server.helper;
import com.mongodb.MongoClient;
import com.mongodb.MongoException;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoCursor;
import com.mongodb.client.MongoDatabase;
import static com.mongodb.client.model.Filters.eq;
import java.security.MessageDigest;
......@@ -82,8 +83,18 @@ public class Database {
// addItemToInventory("553b64d23c892b2d5b772c74", 4);
// addItemToInventory("553b64d23c892b2d5b772c74", 4);
System.out.println(saveOfferToTradeBox("553b64d23c892b2d5b772c74", 4, 5, 6, 2));
// System.out.println(saveOfferToTradeBox("553b64d23c892b2d5b772c74", 4, 5, 6, 2));
// addItemToInventory("553b6adc3c892b2edffa877c", 3);
// addItemToInventory("553b6adc3c892b2edffa877c", 2);
// addItemToInventory("553b6adc3c892b2edffa877c", 3);
// addItemToInventory("553b6adc3c892b2edffa877c", 4);
// addItemToInventory("553b6adc3c892b2edffa877c", 4);
// addItemToInventory("553b6adc3c892b2edffa877c", 4);
//
// System.out.println(saveOfferToTradeBox("553b6adc3c892b2edffa877c", 4, 1, 5, 3));
getTradebox("553b64d23c892b2d5b772c74");
}
/**
......@@ -329,6 +340,42 @@ public class Database {
return result;
}
public static ArrayList<ArrayList<Object>> getTradebox(String usertoken)
{
MongoCollection<Document> collection = database.getCollection("tradebox");
MongoCursor<Document> cursor = collection.find().iterator();
ArrayList<ArrayList<Object>> allOffer;
ArrayList<Object> oneoffer;
try{
allOffer = new ArrayList<>();
while(cursor.hasNext())
{
String oneOfferString = cursor.next().toJson();
JSONObject jsonObj = new JSONObject(oneOfferString);
if(jsonObj.getString("usertoken").equals(usertoken))
{
oneoffer = new ArrayList<>();
oneoffer.add(jsonObj.getInt("offered_item"));
oneoffer.add(jsonObj.getInt("n1"));
oneoffer.add(jsonObj.getInt("demanded_item"));
oneoffer.add(jsonObj.getInt("n2"));
oneoffer.add(jsonObj.getBoolean("availability"));
oneoffer.add(getJsonObjId(jsonObj.toString()));
allOffer.add(oneoffer);
}
}
if(allOffer.isEmpty())
return null;
} catch (JSONException | NullPointerException ex) {
return null;
}
finally
{
cursor.close();
}
return allOffer;
}
/**
* Mengembalikan token, username, dan password dari username yang dimasukkan
* @param username
......@@ -389,6 +436,17 @@ public class Database {
return bsonString.substring(bsonString.indexOf("_id=")+4, bsonString.indexOf(","));
}
private static String getJsonObjId(String allJsonString)
{
try {
JSONObject allJson = new JSONObject(allJsonString);
JSONObject objId = new JSONObject(allJson.get("_id").toString());
return objId.getString("$oid");
} catch (JSONException ex) {
return null;
}
}
/**
* Mendapatkan waktu
* @return
......
......@@ -11,6 +11,7 @@ import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import objek.Person;
......@@ -59,13 +60,15 @@ public class Message {
// } catch (JSONException ex) {
// Logger.getLogger(Message.class.getName()).log(Level.SEVERE, null, ex);
// }
JSONObject coba = new JSONObject();
coba.put("method", "login");
coba.put("username", "abcdef");
coba.put("password", Database.encryptToMD5("akhfa"));
System.out.println(receive(coba.toString()));
// JSONObject coba = new JSONObject();
// coba.put("method", "login");
// coba.put("username", "abcdef");
// coba.put("password", Database.encryptToMD5("akhfa"));
// System.out.println(receive(coba.toString()));
// System.out.println(move("553b17283c892b1a8d00961f", 2, 5));
// System.out.println(getTradeBox("553b64d23c892b2d5b772c74"));
}
public static String callURL(String myURL) {
......@@ -164,6 +167,12 @@ public class Message {
response = putOffer(token, Offered_item, n1, demanded_item, n2);
break;
}
case "tradebox":
{
String token = jsonObj.getString("token");
response = getTradeBox(token);
break;
}
case "serverStatus":
response = serverStatus(jsonString);
break;
......@@ -247,7 +256,7 @@ public class Message {
username = jsonObj.getString("username");
Person person = Database.getPerson(username);
if(Database.encryptToMD5(jsonObj.getString("password")).equals(person.password))
if(jsonObj.getString("password").equals(person.password))
{
JSONObject jObj = new JSONObject();
jObj.put("status", "ok");
......@@ -259,7 +268,7 @@ public class Message {
}
else
{
response = fail("username/password combination is not\n" + "found");
response = fail("username/password combination is not found");
}
} catch (JSONException ex) {
response = error();
......@@ -548,23 +557,25 @@ public class Message {
return response;
}
public static String getTradeBox(String token)
public static String getTradeBox(String token) //10
{
JSONObject jsonObj;
String response;
JSONObject jsonAllOffers;
try{
if(/*item di inventory cukup dan token ok*/true)
ArrayList<ArrayList<Object>> allOffers = Database.getTradebox(token);
jsonAllOffers = new JSONObject();
if(allOffers != null)
{
/*simpan offer di database*/
response = statusOKOnly();
jsonAllOffers.put("status", "ok");
jsonAllOffers.put("offers", allOffers);
response = jsonAllOffers.toString();
}
else
else
{
jsonObj.put("status", "fail");
jsonObj.put("description", "insufficient");
response = jsonObj.toString();
response = error();
}
}
catch(JSONException 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