Commit 0c8c324f authored by Akhmad Fakhoni Listiyan Dede's avatar Akhmad Fakhoni Listiyan Dede
Browse files

FindOffer ok

parent 53a4c96e
......@@ -415,6 +415,44 @@ public class Database {
return allOffer;
}
public static ArrayList<ArrayList<Object>> getTradebox(int itemIdx)
{
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.getInt("offered_item") == itemIdx)
{
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
......
......@@ -69,7 +69,7 @@ public class Message {
// System.out.println(move("553b17283c892b1a8d00961f", 2, 5));
getTradeBox("553b64d23c892b2d5b772c74");
sendFine("553b64d23c892b2d5b772c74", 4);
sendFind("553b64d23c892b2d5b772c74", 4);
}
public static String callURL(String myURL) {
......@@ -159,21 +159,28 @@ public class Message {
break;
}
case "offer":
{
String token = jsonObj.getString("token");
int Offered_item = jsonObj.getInt("offered_item");
int n1 = jsonObj.getInt("n1");
int demanded_item = jsonObj.getInt("demanded_item");
int n2 = jsonObj.getInt("n2");
response = putOffer(token, Offered_item, n1, demanded_item, n2);
break;
}
{
String token = jsonObj.getString("token");
int Offered_item = jsonObj.getInt("offered_item");
int n1 = jsonObj.getInt("n1");
int demanded_item = jsonObj.getInt("demanded_item");
int n2 = jsonObj.getInt("n2");
response = putOffer(token, Offered_item, n1, demanded_item, n2);
break;
}
case "tradebox":
{
String token = jsonObj.getString("token");
response = getTradeBox(token);
break;
}
{
String token = jsonObj.getString("token");
response = getTradeBox(token);
break;
}
case "sendfind":
{
String token = jsonObj.getString("token");
int item = jsonObj.getInt("item");
response = sendFind(token, item);
break;
}
case "serverStatus":
response = serverStatus(jsonString);
break;
......@@ -586,7 +593,13 @@ public class Message {
return response;
}
public static String sendFine(String token, int itemId) //11
/**
* Find offer sesuai dengan item id yang diinginkan. Dikirim oleh client.
* @param token
* @param itemId
* @return
*/
public static String sendFind(String token, int itemId) //11
{
String response;
JSONObject jsonAllOffers;
......@@ -619,7 +632,12 @@ public class Message {
//tradebox
//sendfind
public static String sendFindOffer(int item)
/**
* Send find offer to another server. Untuk menemukan semua offer yang tersedia dari semua server
* @param item
* @return
*/
public static String sendFindOffer(int item) //12
{
JSONObject jsonObj = new JSONObject();
String response;
......@@ -640,9 +658,26 @@ public class Message {
return response;
}
public static String getFidOffer()
/**
* Fungsi untuk catch method find offer dari server lain
* @param itemIdx
* @return
*/
public static String getFindOffer(int itemIdx) //12
{
return "";
String response;
JSONObject jsonObj = new JSONObject();
try
{
jsonObj.put("status", "ok");
jsonObj.put("offers", Database.getTradebox(itemIdx));
response = jsonObj.toString();
}catch(JSONException ex)
{
System.out.println("getFindOffer() Error: " + jsonObj.toString());
response = error();
}
return response;
}
/**
......
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