Commit d37cf799 authored by Stephen's avatar Stephen
Browse files

Added tradebox

parents d2ba2d93 4425b3f1
......@@ -13,4 +13,5 @@ src/client/nbbuild/
src/client/nbdist/
src/client/nbactions.xml
src/client/nb-configuration.xml
.nb-gradle/
\ No newline at end of file
.nb-gradle/
/src/client/dist/
\ No newline at end of file
compile.on.save=true
<<<<<<< HEAD
user.properties.file=C:\\Users\\Stephen\\AppData\\Roaming\\NetBeans\\8.0.2\\build.properties
\ No newline at end of file
user.properties.file=C:\\Users\\Stephen\\AppData\\Roaming\\NetBeans\\8.0.2\\build.properties
=======
<<<<<<< HEAD
<<<<<<< HEAD
user.properties.file=C:\\Users\\user\\AppData\\Roaming\\NetBeans\\8.0\\build.properties
=======
<<<<<<< HEAD
=======
>>>>>>> 2e2ad538abfe623e38cc24be02c48f5ad2ee0768
user.properties.file=C:\\Users\\susanti_2\\AppData\\Roaming\\NetBeans\\8.0.2\\build.properties
>>>>>>> 43e41c8c08f84039568ca701a584f38f9911e548
=======
user.properties.file=C:\\Users\\Stephen\\AppData\\Roaming\\NetBeans\\8.0.2\\build.properties
>>>>>>> 9b3bab9e9eefddca873933beff1cff411b869149
>>>>>>> 4425b3f14619792ceebdcdb264cb7c61d1d9d38c
This diff is collapsed.
This diff is collapsed.
......@@ -9,15 +9,11 @@ public class MapTableModel extends AbstractTableModel{
private Object[][] data;
private int dimX;
private int dimY;
private ImageIcon playerIcon;
public MapTableModel() {
dimX=0;
dimY=0;
}
public MapTableModel(int x, int y) {
dimX=x;
......@@ -29,10 +25,6 @@ public class MapTableModel extends AbstractTableModel{
data[i][j] = "";
}
}
// ClassLoader cldr = this.getClass().getClassLoader();
// java.net.URL imageURL = cldr.getResource("images/player.png");
// playerIcon = new ImageIcon(imageURL);
}
@Override
......@@ -51,9 +43,14 @@ public class MapTableModel extends AbstractTableModel{
}
public void setValueAt(Object value, int row, int col) {
data[row][col] = "playerIcon";
data[row][col] = value;
fireTableCellUpdated(row, col);
}
@Override
public Class<?> getColumnClass(int column) {
return ImageIcon.class;
}
}
......@@ -147,6 +147,19 @@ public class TCPClient {
return obj;
}
public JSONObject tradeboxJSON(String token) {
JSONObject obj = new JSONObject();
try {
obj.put("method", "tradebox");
obj.put("token", token);
} catch(Exception e) {
e.printStackTrace();
}
return obj;
}
public JSONObject cancelofferJSON(String token, String offer_token) {
JSONObject obj = new JSONObject();
......@@ -299,7 +312,30 @@ public class TCPClient {
return responseJSON;
}
public JSONObject move(String token, int x, int y) throws Exception
public JSONObject tradebox(String token) throws Exception
{
System.out.println("Tradebox");
String toSend = tradeboxJSON(token).toString();
//throw ioexception
DataOutputStream outToServer;
try {
outToServer = new DataOutputStream(clientSocket.getOutputStream());
outToServer.writeBytes(toSend + '\n');
} catch (IOException ex) {
Logger.getLogger(TCPClient.class.getName()).log(Level.SEVERE, null, ex);
throw new Exception("outtoserver tradebox exception");
}
//ambil balasan server
BufferedReader inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
String response = inFromServer.readLine();
JSONObject responseJSON = new JSONObject(response);
return responseJSON;
}
public JSONObject move(int x, int y) throws Exception
{
System.out.println("Move");
String toSend = moveJSON(token, x, y).toString();
......@@ -322,7 +358,7 @@ public class TCPClient {
return responseJSON;
}
public JSONObject field(String token) throws Exception
public JSONObject field() throws Exception
{
System.out.println("Field");
String toSend = fieldJSON(token).toString();
......@@ -335,6 +371,7 @@ public class TCPClient {
} catch (IOException ex) {
Logger.getLogger(TCPClient.class.getName()).log(Level.SEVERE, null, ex);
throw new Exception("outtoserver field exception");
}
//ambil balasan server
......@@ -358,6 +395,7 @@ public class TCPClient {
} catch (IOException ex) {
Logger.getLogger(TCPClient.class.getName()).log(Level.SEVERE, null, ex);
throw new Exception("outtoserver canceloffer exception");
}
//ambil balasan server
......@@ -372,106 +410,106 @@ public class TCPClient {
// {
// TCPClient client = new TCPClient("localhost", 6789);
//
//// do {
//// BufferedReader inFromUser = new BufferedReader( new InputStreamReader(System.in));
//// DataOutputStream outToServer = new DataOutputStream(clientSocket.getOutputStream());
//// BufferedReader inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
////
//// // Test Register
//// do {
//// System.out.println("Register");
//// System.out.print("Username : "); username = inFromUser.readLine();
//// System.out.print("Password : "); password = inFromUser.readLine();
//// String toSend = registerJSON(username, password).toString();
////
//// outToServer.writeBytes(toSend + '\n');
////
//// String response = inFromServer.readLine();
//// JSONObject responseJSON = new JSONObject(response);
//// responseStatus = responseJSON.getString("status");
//// if(responseStatus.equalsIgnoreCase("ok")) {
//// System.out.println("Registration Success!");
//// } else if(responseStatus.equalsIgnoreCase("fail")) {
//// String reason = responseJSON.getString("description");
//// System.out.println("Registration fail, " + reason);
//// }
//// System.out.println();
//// } while(!responseStatus.equalsIgnoreCase("ok"));
////
//// // Test Login
//// do {
//// System.out.println("Login");
//// System.out.print("Username : "); username = inFromUser.readLine();
//// System.out.print("Password : "); password = inFromUser.readLine();
//// String toSend = loginJSON(username, password).toString();
////
//// outToServer.writeBytes(toSend + '\n');
////
//// String response = inFromServer.readLine();
//// JSONObject responseJSON = new JSONObject(response);
//// responseStatus = responseJSON.getString("status");
//// if(responseStatus.equalsIgnoreCase("ok")) {
//// System.out.println("Login Success!");
//// System.out.println("Token : " + responseJSON.getString("token"));
//// token = responseJSON.getString("token");
//// } else if(responseStatus.equalsIgnoreCase("fail")) {
//// String reason = responseJSON.getString("description");
//// System.out.println("Login fail, " + reason);
//// }
//// System.out.println();
//// } while(!responseStatus.equalsIgnoreCase("ok"));
////
//// // Test Inventory
//// do {
//// do {
//// System.out.print("Command : "); command = inFromUser.readLine();
//// } while(!command.equalsIgnoreCase("inventory") && !command.equalsIgnoreCase("exit"));
////
//// if(command.equalsIgnoreCase("inventory")) {
//// String toSend = inventoryJSON(token).toString();
////
//// outToServer.writeBytes(toSend + '\n');
////
//// String response = inFromServer.readLine();
//// JSONObject responseJSON = new JSONObject(response);
//// responseStatus = responseJSON.getString("status");
//// if(responseStatus.equalsIgnoreCase("ok")) {
//// System.out.println("Your Inventory : ");
//// List inventory = new ArrayList<Integer>();
//// JSONArray jsonarray = responseJSON.getJSONArray("inventory");
//// for(int i=0; i<jsonarray.length(); i++) {
//// inventory.add(jsonarray.getInt(i));
//// }
//// printItemList(inventory);
//// } else if(responseStatus.equalsIgnoreCase("error")) {
//// System.out.println("Error opening inventory");
//// }
//// System.out.println();
//// }
//// } while(!command.equalsIgnoreCase("exit"));
////
//// // Test Mix Item
//// do {
//// System.out.println("Mix Item");
//// System.out.print("Item 1 ID : "); int id1 = Integer.parseInt(inFromUser.readLine());
//// System.out.print("Item 2 ID : "); int id2 = Integer.parseInt(inFromUser.readLine());
//// String toSend = mixitemJSON(token, id1, id2).toString();
////
//// outToServer.writeBytes(toSend + '\n');
////
//// String response = inFromServer.readLine();
//// JSONObject responseJSON = new JSONObject(response);
//// responseStatus = responseJSON.getString("status");
//// if(responseStatus.equalsIgnoreCase("ok")) {
//// System.out.println("Mix item succeded, your new item id : " + responseJSON.getInt("item"));
//// } else if(responseStatus.equalsIgnoreCase("fail")) {
//// System.out.println("Failed to mix item, " + responseJSON.getString("description"));
//// } else if(responseStatus.equalsIgnoreCase("error")) {
//// System.out.println("Error mixing item");
//// }
//// System.out.println();
//// } while(true);
//// } while(!command.equalsIgnoreCase("exit"));
// do {
// BufferedReader inFromUser = new BufferedReader( new InputStreamReader(System.in));
// DataOutputStream outToServer = new DataOutputStream(clientSocket.getOutputStream());
// BufferedReader inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
//
// // Test Register
// do {
// System.out.println("Register");
// System.out.print("Username : "); username = inFromUser.readLine();
// System.out.print("Password : "); password = inFromUser.readLine();
// String toSend = registerJSON(username, password).toString();
//
// outToServer.writeBytes(toSend + '\n');
//
// String response = inFromServer.readLine();
// JSONObject responseJSON = new JSONObject(response);
// responseStatus = responseJSON.getString("status");
// if(responseStatus.equalsIgnoreCase("ok")) {
// System.out.println("Registration Success!");
// } else if(responseStatus.equalsIgnoreCase("fail")) {
// String reason = responseJSON.getString("description");
// System.out.println("Registration fail, " + reason);
// }
// System.out.println();
// } while(!responseStatus.equalsIgnoreCase("ok"));
//
// // Test Login
// do {
// System.out.println("Login");
// System.out.print("Username : "); username = inFromUser.readLine();
// System.out.print("Password : "); password = inFromUser.readLine();
// String toSend = loginJSON(username, password).toString();
//
// outToServer.writeBytes(toSend + '\n');
//
// String response = inFromServer.readLine();
// JSONObject responseJSON = new JSONObject(response);
// responseStatus = responseJSON.getString("status");
// if(responseStatus.equalsIgnoreCase("ok")) {
// System.out.println("Login Success!");
// System.out.println("Token : " + responseJSON.getString("token"));
// token = responseJSON.getString("token");
// } else if(responseStatus.equalsIgnoreCase("fail")) {
// String reason = responseJSON.getString("description");
// System.out.println("Login fail, " + reason);
// }
// System.out.println();
// } while(!responseStatus.equalsIgnoreCase("ok"));
//
// // Test Inventory
// do {
// do {
// System.out.print("Command : "); command = inFromUser.readLine();
// } while(!command.equalsIgnoreCase("inventory") && !command.equalsIgnoreCase("exit"));
//
// if(command.equalsIgnoreCase("inventory")) {
// String toSend = inventoryJSON(token).toString();
//
// outToServer.writeBytes(toSend + '\n');
//
// String response = inFromServer.readLine();
// JSONObject responseJSON = new JSONObject(response);
// responseStatus = responseJSON.getString("status");
// if(responseStatus.equalsIgnoreCase("ok")) {
// System.out.println("Your Inventory : ");
// List inventory = new ArrayList<Integer>();
// JSONArray jsonarray = responseJSON.getJSONArray("inventory");
// for(int i=0; i<jsonarray.length(); i++) {
// inventory.add(jsonarray.getInt(i));
// }
// printItemList(inventory);
// } else if(responseStatus.equalsIgnoreCase("error")) {
// System.out.println("Error opening inventory");
// }
// System.out.println();
// }
// } while(!command.equalsIgnoreCase("exit"));
//
// // Test Mix Item
// do {
// System.out.println("Mix Item");
// System.out.print("Item 1 ID : "); int id1 = Integer.parseInt(inFromUser.readLine());
// System.out.print("Item 2 ID : "); int id2 = Integer.parseInt(inFromUser.readLine());
// String toSend = mixitemJSON(token, id1, id2).toString();
//
// outToServer.writeBytes(toSend + '\n');
//
// String response = inFromServer.readLine();
// JSONObject responseJSON = new JSONObject(response);
// responseStatus = responseJSON.getString("status");
// if(responseStatus.equalsIgnoreCase("ok")) {
// System.out.println("Mix item succeded, your new item id : " + responseJSON.getInt("item"));
// } else if(responseStatus.equalsIgnoreCase("fail")) {
// System.out.println("Failed to mix item, " + responseJSON.getString("description"));
// } else if(responseStatus.equalsIgnoreCase("error")) {
// System.out.println("Error mixing item");
// }
// System.out.println();
// } while(true);
// } while(!command.equalsIgnoreCase("exit"));
//
// JSONObject response = client.login("ayam", "ayam");
// if(response.getString("status").equals("ok")) {
......@@ -484,13 +522,14 @@ public class TCPClient {
// response = client.map();
// System.out.println(response.toString());
//
// response = client.field(client.token);
// response = client.offer(client.token, 0, 1, 7, 3);
// System.out.println(response.toString());
//
// response = client.offer(client.token, 0, 1, 7, 3);
// response = client.offer(client.token, 1, 1, 7, 3);
// System.out.println(response.toString());
//
// response = client.canceloffer(client.token, "a");
// response = client.tradebox(client.token);
// System.out.println(response.toString());
// }
}
#Sun, 19 Apr 2015 16:30:46 +0700
#Mon, 20 Apr 2015 19:37:20 +0700
C\:\\Users\\Stephen\\Documents\\NetBeansProjects\\TubesSister\\src\\server=
compile.on.save=true
<<<<<<< HEAD
<<<<<<< HEAD
=======
user.properties.file=C:\\Users\\user\\AppData\\Roaming\\NetBeans\\8.0\\build.properties
=======
>>>>>>> 9b3bab9e9eefddca873933beff1cff411b869149
>>>>>>> 4425b3f14619792ceebdcdb264cb7c61d1d9d38c
do.depend=false
do.jar=true
javac.debug=true
javadoc.preview=true
<<<<<<< HEAD
user.properties.file=C:\\Users\\Stephen\\AppData\\Roaming\\NetBeans\\8.0.2\\build.properties
=======
<<<<<<< HEAD
user.properties.file=C:\\Users\\user\\AppData\\Roaming\\NetBeans\\8.0\\build.properties
user.properties.file=C:\\Users\\susanti_2\\AppData\\Roaming\\NetBeans\\8.0.2\\build.properties
=======
user.properties.file=C:\\Users\\Stephen\\AppData\\Roaming\\NetBeans\\8.0.2\\build.properties
>>>>>>> 9b3bab9e9eefddca873933beff1cff411b869149
>>>>>>> 4425b3f14619792ceebdcdb264cb7c61d1d9d38c
......@@ -320,13 +320,14 @@ public class MySQLAccess {
int difference = resultSet.getInt(resultSet2.getString("item_name").toLowerCase()) - n1;
if(difference >= 0) {
preparedStatement = connect.prepareStatement("INSERT INTO offer "
+ "VALUES(NULL, ?, ?, ?, ?, ?, ?)");
+ "VALUES(NULL, ?, ?, ?, ?, ?, ?, ?)");
preparedStatement.setInt(1, userID);
preparedStatement.setInt(2, offered_item);
preparedStatement.setInt(3, n1);
preparedStatement.setInt(4, demanded_item);
preparedStatement.setInt(5, n2);
preparedStatement.setString(6, new BigInteger(130, new SecureRandom()).toString(32));
preparedStatement.setBoolean(6, true);
preparedStatement.setString(7, new BigInteger(130, new SecureRandom()).toString(32));
preparedStatement.executeUpdate();
preparedStatement = connect.prepareStatement("UPDATE inventory SET "
......@@ -353,6 +354,38 @@ public class MySQLAccess {
return response;
}
public JSONObject tradebox(int userID) throws JSONException {
open();
JSONObject response = new JSONObject();
try {
preparedStatement = connect.prepareStatement("SELECT * FROM offer WHERE "
+ "id_user=" + userID + ";");
resultSet = preparedStatement.executeQuery();
response.put("status", "ok");
JSONArray offers = new JSONArray();
while(resultSet.next())
{
JSONArray offer = new JSONArray();
offer.put(resultSet.getString("offered_item"));
offer.put(resultSet.getString("n1"));
offer.put(resultSet.getString("demanded_item"));
offer.put(resultSet.getString("n2"));
offer.put(resultSet.getBoolean("available"));
offer.put(resultSet.getString("offer_token"));
offers.put(offer);
}
response.put("offers", offers);
} catch (SQLException e) {
response.put("status", "error");
e.printStackTrace();
}
close();
return response;
}
public JSONObject canceloffer(int userID, String offer_token) throws JSONException
{
open();
......
......@@ -29,6 +29,7 @@ public class TCPServer implements Runnable {
System.out.println("Server running!");
serverClientSocket = new ServerSocket(port);
servers = new ArrayList<Host>();
sql = new MySQLAccess();
}
......@@ -187,6 +188,16 @@ public class TCPServer implements Runnable {
return responseJSON;
}
}
public JSONObject tradebox(JSONObject clientObject) throws JSONException {
if(clientObject.getString("token").equalsIgnoreCase(token)) {
return sql.tradebox(userID);
} else {
JSONObject responseJSON = new JSONObject();
responseJSON.put("status", "error");
return responseJSON;
}
}
public JSONObject serverstatus(JSONObject clientObject) throws JSONException {
JSONObject responseJSON = new JSONObject();
......@@ -199,19 +210,30 @@ public class TCPServer implements Runnable {
Host host = new Host(server.getString("ip"), server.getInt("port"));
servers.add(host);
}
System.out.println("Newest server list :");
printAllServers();
return responseJSON;
}
public void run() {
// Test join tracker
try{
join(InetAddress.getLocalHost().getHostAddress(), 6790);
} catch(Exception e) {
e.printStackTrace();
}
// Test join tracker, ambil ip dari wifi
boolean joined = false;
int count = 1;
do {
try {
join("167.205.34.99", 6790);
joined = true;
} catch(Exception e) {
System.out.println(count + ". Tracker not responding, retrying in 5 seconds");
count++;
try {
Thread.sleep(5000);
} catch (Exception ex) {}
}
} while(!joined);
while(true) {
int port = 6790;
try {
......@@ -315,6 +337,10 @@ public class TCPServer implements Runnable {
JSONObject responseJSON = server.canceloffer(clientObject);
System.out.println(responseJSON.toString());
outToClient.writeBytes(responseJSON.toString() + "\n");
} else if(method.equalsIgnoreCase("tradebox")) {
JSONObject responseJSON = server.tradebox(clientObject);
System.out.println(responseJSON.toString());
outToClient.writeBytes(responseJSON.toString() + "\n");
}
}
}
......
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