Commit 0519cfe5 authored by Darwin's avatar Darwin
Browse files

send Find done

parent e7eca896
......@@ -2290,30 +2290,28 @@ public class MainPage extends javax.swing.JFrame {
private void generateFindOfferTable() {
try {
JSONObject response = client.tradebox();
JSONObject response = client.sendFind(client.token,FindId);
if(response.getString("status").equals("ok")) {
JSONArray offer = response.getJSONArray("offers");
int dimX=offer.length();
int dimY=5;
tradeboxModel=new TradeboxTableModel(dimX, dimY);
FindOfferBoxModel =new TradeboxTableModel(dimX, dimY);
for(int i=0;i<offer.length();i++)
{
ImageIcon newPlayerIcon = getImage(offer.getJSONArray(i).getInt(0), 100, 100);
tradeboxModel.setValueAt(newPlayerIcon, i, 0);
tradeboxModel.setValueAt(offer.getJSONArray(i).get(1), i, 1);
FindOfferBoxModel.setValueAt(newPlayerIcon, i, 0);
FindOfferBoxModel.setValueAt(offer.getJSONArray(i).get(1), i, 1);
newPlayerIcon = getImage(offer.getJSONArray(i).getInt(2), 100, 100);
tradeboxModel.setValueAt(newPlayerIcon, i, 2);
tradeboxModel.setValueAt(offer.getJSONArray(i).get(3), i, 3);
FindOfferBoxModel.setValueAt(newPlayerIcon, i, 2);
FindOfferBoxModel.setValueAt(offer.getJSONArray(i).get(3), i, 3);
if(offer.getJSONArray(i).get(4).toString().equals("true")){ //avalaible
tradeboxModel.setValueAt("Cancel", i, 4);
}else{
tradeboxModel.setValueAt("Fetch", i, 4);
FindOfferBoxModel.setValueAt("Buy", i, 4);
}
}
tradeboxTable.setModel(tradeboxModel);
tradeboxTable.setRowHeight(100);
FindOfferItemTable.setModel(FindOfferBoxModel);
FindOfferItemTable.setRowHeight(100);
Action delete = new AbstractAction()
{
......@@ -2325,10 +2323,7 @@ public class MainPage extends javax.swing.JFrame {
if(offer.getJSONArray(modelRow).get(4).toString().equals("true"))
{
String offerToken=offer.getJSONArray(modelRow).get(5).toString();
cancelOffer(offerToken);
}else{
String offerToken=offer.getJSONArray(modelRow).get(5).toString();
fetchItem(offerToken);
sendAccept(offerToken);
}
} catch (JSONException ex) {
System.out.println("mouse click json Exception");
......@@ -2336,14 +2331,14 @@ public class MainPage extends javax.swing.JFrame {
}
}
};
ButtonColumn buttonColumn = new ButtonColumn(tradeboxTable, delete, 4);
ButtonColumn buttonColumn = new ButtonColumn(FindOfferItemTable, delete, 4);
buttonColumn.setMnemonic(KeyEvent.VK_D);
} else {
showMessage("Tradebox Error. Try again later..");
showMessage("Cannot find item. Try again later..");
}
} catch (Exception ex) {
showMessage("Server does not respond. Please try again later..");
System.out.println("Tradebox Exception");
System.out.println("FindItem Exception");
Logger.getLogger(MainPage.class.getName()).log(Level.SEVERE, null, ex);
}
}
......@@ -2433,6 +2428,25 @@ public class MainPage extends javax.swing.JFrame {
return newIcon;
}
private void sendAccept(String offerToken) {
try {
JSONObject response = client.sendAccept(offerToken);
if(response.getString("status").equals("ok")){
showMessage("Item will be traded");
generateFindOfferTable();
}else if(response.getString("status").equals("fail")){
showMessage(response.getString("description"));
}else{
showMessage(response.getString("status"));
}
} catch (Exception ex) {
showMessage("Server does not respond. Please try again later..");
System.out.println("Send Accept Item Exception");
Logger.getLogger(MainPage.class.getName()).log(Level.SEVERE, null, ex);
}
}
private void fetchItem(String offerToken)
{
......
......@@ -256,6 +256,7 @@ public class TCPClient {
return obj;
}
public JSONObject sendFind(String token,int FindId) throws Exception {
openConnection();
......
......@@ -348,16 +348,7 @@ public class TCPServer implements Runnable {
inFromServer.read(b);
String response = new String(b, "UTF-8");
<<<<<<< HEAD
System.out.println(response);
JSONObject tempResponse = new JSONObject(response);
System.out.println("hahaha");
if(tempResponse.getString("status").equalsIgnoreCase("ok")) {
if(tempResponse.getJSONArray("offers").length()>0) {
for(int i=0; i<tempResponse.getJSONArray("offers").length(); i++) {
offers.put(tempResponse.getJSONArray("offers").get(i));
offerHost.add(new Host(server.ipAddress, server.port));
=======
System.out.println(response);
JSONObject tempResponse = new JSONObject(response);
if(tempResponse.getString("status").equalsIgnoreCase("ok")) {
......@@ -366,7 +357,7 @@ public class TCPServer implements Runnable {
offers.put(tempResponse.getJSONArray("offers").get(i));
offerHost.add(new Host(server.ipAddress, server.port));
}
>>>>>>> 805d57339903105b67427132e4fc49fb767c36b1
}
}
......
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