Commit 3c69b2b7 authored by Darwin's avatar Darwin
Browse files

fixed error

parents 26a2f123 bc4556ff
......@@ -13,6 +13,11 @@ src/client/nbbuild/
src/client/nbdist/
src/client/nbactions.xml
src/client/nb-configuration.xml
<<<<<<< HEAD
src/client/build/classes/tcpclient/LoginPage.class
src/client/build/classes/tcpclient/TCPClient.class
.nb-gradle/
\ No newline at end of file
.nb-gradle/
=======
.nb-gradle/
/src/client/dist/
>>>>>>> bc4556fff5c99bc4b898c7dd8720e7cf066d81a5
compile.on.save=true
<<<<<<< HEAD
<<<<<<< HEAD
user.properties.file=C:\\Users\\user\\AppData\\Roaming\\NetBeans\\8.0\\build.properties
=======
<<<<<<< HEAD
=======
user.properties.file=C:\\Users\\Stephen\\AppData\\Roaming\\NetBeans\\8.0.2\\build.properties
\ No newline at end of file
>>>>>>> 2e2ad538abfe623e38cc24be02c48f5ad2ee0768
user.properties.file=C:\\Users\\susanti_2\\AppData\\Roaming\\NetBeans\\8.0.2\\build.properties
>>>>>>> 43e41c8c08f84039568ca701a584f38f9911e548
=======
user.properties.file=C:\\Users\\user\\AppData\\Roaming\\NetBeans\\8.0\\build.properties
>>>>>>> 9b3bab9e9eefddca873933beff1cff411b869149
......@@ -3,8 +3,14 @@
<editor-bookmarks xmlns="http://www.netbeans.org/ns/editor-bookmarks/2" lastBookmarkId="0"/>
<open-files xmlns="http://www.netbeans.org/ns/projectui-open-files/2">
<group>
<<<<<<< HEAD
<file>file:/E:/semester%206/Sistem%20Paralel%20dan%20Terdistribusi/SISTER/IF3230-Tugas-Besar-Sister-2015/src/client/src/tcpclient/TCPClient.java</file>
<file>file:/E:/semester%206/Sistem%20Paralel%20dan%20Terdistribusi/SISTER/IF3230-Tugas-Besar-Sister-2015/src/client/src/tcpclient/MainPage.java</file>
=======
<file>file:/D:/IF3230-Tugas-Besar-Sister-2015/src/client/src/tcpclient/MainPage.java</file>
<file>file:/D:/IF3230-Tugas-Besar-Sister-2015/src/client/src/tcpclient/TCPClient.java</file>
<file>file:/D:/IF3230-Tugas-Besar-Sister-2015/src/client/src/tcpclient/LoginPage.java</file>
>>>>>>> bc4556fff5c99bc4b898c7dd8720e7cf066d81a5
</group>
</open-files>
</project-private>
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,31 @@ public class TCPClient {
return obj;
}
private Object tradeboxJSON(String token) {
JSONObject obj = new JSONObject();
try {
obj.put("method", "tradebox");
obj.put("token", token);
} catch(Exception e) {
e.printStackTrace();
}
return obj;
}
private Object sendAcceptJSON(String offerToken) {
JSONObject obj = new JSONObject();
try {
obj.put("method", "sendaccept");
obj.put("token", token);
obj.put("offer_token", offerToken);
} catch(Exception e) {
e.printStackTrace();
}
return obj;
}
public JSONObject cancelofferJSON(String token, String offer_token) {
JSONObject obj = new JSONObject();
......@@ -339,7 +364,7 @@ public class TCPClient {
return responseJSON;
}
public JSONObject move(String token, int x, int y) throws Exception
public JSONObject move(int x, int y) throws Exception
{
System.out.println("Move");
String toSend = moveJSON(token, x, y).toString();
......@@ -362,7 +387,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();
......@@ -375,6 +400,53 @@ 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
BufferedReader inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
String response = inFromServer.readLine();
JSONObject responseJSON = new JSONObject(response);
return responseJSON;
}
public JSONObject tradebox() 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 sendAccept(String offerToken) throws Exception
{
System.out.println("Send Accept");
String toSend = sendAcceptJSON(offerToken).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 send accept exception");
}
//ambil balasan server
......@@ -398,6 +470,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
......@@ -407,130 +480,133 @@ public class TCPClient {
return responseJSON;
}
// public static void main(String argv[]) throws Exception
// {
// 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"));
//
// JSONObject response = client.login("ayam", "ayam");
// if(response.getString("status").equals("ok")) {
// client.token = response.getString("token");
// client.x=response.getInt("x");
// client.y=response.getInt("y");
// }
// System.out.println(response.toString());
//
// response = client.map();
// System.out.println(response.toString());
//
// response = client.field(client.token);
// System.out.println(response.toString());
//
// response = client.offer(client.token, 0, 1, 7, 3);
// System.out.println(response.toString());
//
// response = client.canceloffer(client.token, "a");
// System.out.println(response.toString());
// }
public static void main(String argv[]) throws Exception
{
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"));
JSONObject response = client.login("ayam", "ayam");
if(response.getString("status").equals("ok")) {
client.token = response.getString("token");
client.x=response.getInt("x");
client.y=response.getInt("y");
}
System.out.println(response.toString());
response = client.map();
System.out.println(response.toString());
response = client.field();
System.out.println(response.toString());
response = client.offer(client.token, 0, 1, 7, 3);
System.out.println(response.toString());
response = client.canceloffer(client.token, "a");
System.out.println(response.toString());
}
}
<<<<<<< HEAD
#Sun, 19 Apr 2015 14:13:30 +0700
C\:\\Users\\susanti_2\\Documents\\ITB\\IF\\netbeans\\IF3230-Tugas-Besar-Sister-2015\\src\\server=
=======
#Sun, 19 Apr 2015 16:30:46 +0700
C\:\\Users\\Stephen\\Documents\\NetBeansProjects\\TubesSister\\src\\server=
>>>>>>> 9b3bab9e9eefddca873933beff1cff411b869149
compile.on.save=true
<<<<<<< HEAD
user.properties.file=C:\\Users\\user\\AppData\\Roaming\\NetBeans\\8.0\\build.properties
=======
>>>>>>> 9b3bab9e9eefddca873933beff1cff411b869149
do.depend=false
do.jar=true
javac.debug=true
javadoc.preview=true
<<<<<<< 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
user.properties.file=C:\\Users\\user\\AppData\\Roaming\\NetBeans\\8.0\\build.properties
>>>>>>> 9b3bab9e9eefddca873933beff1cff411b869149
......@@ -3,7 +3,13 @@
<editor-bookmarks xmlns="http://www.netbeans.org/ns/editor-bookmarks/2" lastBookmarkId="0"/>
<open-files xmlns="http://www.netbeans.org/ns/projectui-open-files/2">
<group>
<<<<<<< HEAD
<file>file:/E:/semester%206/Sistem%20Paralel%20dan%20Terdistribusi/SISTER/IF3230-Tugas-Besar-Sister-2015/src/server/src/tcpserver/TCPServer.java</file>
=======
<file>file:/D:/IF3230-Tugas-Besar-Sister-2015/src/server/map.txt</file>
<file>file:/D:/IF3230-Tugas-Besar-Sister-2015/src/server/src/tcpserver/MySQLAccess.java</file>
<file>file:/D:/IF3230-Tugas-Besar-Sister-2015/src/server/src/tcpserver/TCPServer.java</file>
>>>>>>> bc4556fff5c99bc4b898c7dd8720e7cf066d81a5
</group>
</open-files>
</project-private>
......@@ -24,7 +24,8 @@ public class MySQLAccess {
private Statement statement;
private PreparedStatement preparedStatement;
private ResultSet resultSet;
private boolean ada = true;
public MySQLAccess() {}
private void open() {
......@@ -246,6 +247,8 @@ public class MySQLAccess {
preparedStatement.executeUpdate();
response.put("status", "ok");
response.put("time", System.currentTimeMillis()/1000 + 5);
//barang menjadi ada lagi karena berpindah tempat
ada = true;
}
} else {
response.put("status", "error");
......@@ -275,25 +278,32 @@ public class MySQLAccess {
//System.out.println("nih map :"+TCPServer.map[curr_x][curr_y]);
String id_other = TCPServer.map[curr_x][curr_y];
if(id_other.equalsIgnoreCase(""))
{
response.put("status", "fail");
response.put("description", "Item has been taken");
}
else //item belom diambil
{
preparedStatement = connect.prepareStatement("SELECT id_item FROM items WHERE "+ "id_other=\"" + id_other + "\";");
preparedStatement = connect.prepareStatement("SELECT * FROM items WHERE "+ "id_other=\"" + id_other + "\";");
resultSet = preparedStatement.executeQuery();
if(resultSet.first()) {
int item_taken = resultSet.getInt("id_item");
//map dijadikan kosong
TCPServer.map[curr_x][curr_y] = "";
//kembalikan response
response.put("status", "ok");
response.put("item", item_taken);
//masukkan ke inventory
if(ada)
{
preparedStatement = connect.prepareStatement("UPDATE inventory SET "
+ resultSet.getString("item_name").toLowerCase() + "="
+ resultSet.getString("item_name").toLowerCase() + "+1"
+ " WHERE id_user=" + userID + ";");
preparedStatement.executeUpdate();
//kembalikan response
response.put("status", "ok");
response.put("item", item_taken);
ada = false;
}
else
{
response.put("status", "fail");
response.put("description", "Item has been taken");
}
}
}
}
} catch (SQLException e) {
response.put("status", "error");
......
......@@ -190,6 +190,7 @@ public class TCPServer implements Runnable {
}
}
public JSONObject serverstatus(JSONObject clientObject) throws JSONException {
JSONObject responseJSON = new JSONObject();
responseJSON.put("status", "ok");
......@@ -311,6 +312,11 @@ public class TCPServer implements Runnable {
System.out.println(responseJSON.toString());
outToClient.writeBytes(responseJSON.toString() + "\n");
}
else if(method.equalsIgnoreCase("canceloffer")) {
JSONObject responseJSON = server.canceloffer(clientObject);
System.out.println(responseJSON.toString());