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