Commit d598baae authored by Stephen's avatar Stephen
Browse files

Updated server

parent d4246d9d
......@@ -796,13 +796,16 @@ public class TCPClient {
response = client.map();
System.out.println(response.toString());
response = client.offer(client.token, 0, 1, 7, 3);
response = client.offer(client.token, 2, 5, 7, 3);
System.out.println(response.toString());
response = client.offer(client.token, 2, 1, 7, 3);
System.out.println(response.toString());
// response = client.sendFind(client.token, 2);
// System.out.println(response.toString());
response = client.findoffer(6);
response = client.findoffer(2);
System.out.println(response.toString());
}
......
......@@ -331,7 +331,7 @@ public class MySQLAccess {
int difference = resultSet.getInt(resultSet2.getString("item_name").toLowerCase().replace(' ', '_')) - n1;
if(difference >= 0) {
preparedStatement = connect.prepareStatement("INSERT INTO offer "
+ "VALUES(NULL, ?, ?, ?, ?, ?, ?, '', 0, ?)");
+ "VALUES(NULL, ?, ?, ?, ?, ?, '', 0, ?, ?)");
preparedStatement.setInt(1, userID);
preparedStatement.setInt(2, offered_item);
preparedStatement.setInt(3, n1);
......
......@@ -389,11 +389,10 @@ public class TCPServer implements Runnable {
try {
new JSONObject(test);
} catch (JSONException ex) {
// edited, to include @Arthur's comment
// e.g. in case JSONArray is valid as well...
try {
new JSONArray(test);
} catch (JSONException ex1) {
System.out.println("Request is not valid, ignored..");
return false;
}
}
......@@ -410,7 +409,7 @@ public class TCPServer implements Runnable {
while(true)
{
System.out.println("======================================================================");
System.out.println("==========================================================================================");
System.out.println("Waiting for client to connect..");
server.clientSocket = server.serverClientSocket.accept();
......@@ -430,14 +429,14 @@ public class TCPServer implements Runnable {
if(method.equalsIgnoreCase("signup")) { // Jika method request adalah "signup"
JSONObject responseJSON = server.signup(clientObject);
System.out.println(responseJSON.toString());
System.out.println("Sent: " + responseJSON.toString());
if(responseJSON.getString("status").equalsIgnoreCase("ok")) {
server.sql.createInventory(clientObject.getString("username"));
}
outToClient.write(responseJSON.toString().getBytes("UTF-8"));
} else if(method.equalsIgnoreCase("login")) { // Jika method request adalah "login"
JSONObject responseJSON = server.login(clientObject);
System.out.println(responseJSON.toString());
System.out.println("Sent: " + responseJSON.toString());
if(responseJSON.getString("status").equalsIgnoreCase("ok")) {
server.userID = server.sql.getUserId(clientObject.getString("username"));
server.token = responseJSON.getString("token");
......@@ -447,55 +446,55 @@ public class TCPServer implements Runnable {
outToClient.write(responseJSON.toString().getBytes("UTF-8"));
} else if(method.equalsIgnoreCase("inventory")) {
JSONObject responseJSON = server.inventory(clientObject);
System.out.println(responseJSON.toString());
System.out.println("Sent: " + responseJSON.toString());
outToClient.write(responseJSON.toString().getBytes("UTF-8"));
} else if(method.equalsIgnoreCase("mixitem")) {
JSONObject responseJSON = server.mixitem(clientObject);
System.out.println(responseJSON.toString());
System.out.println("Sent: " + responseJSON.toString());
outToClient.write(responseJSON.toString().getBytes("UTF-8"));
} else if(method.equalsIgnoreCase("map")) {
JSONObject responseJSON = server.map(clientObject);
System.out.println(responseJSON.toString());
System.out.println("Sent: " + responseJSON.toString());
outToClient.write(responseJSON.toString().getBytes("UTF-8"));
} else if(method.equalsIgnoreCase("move")) {
JSONObject responseJSON = server.move(clientObject);
System.out.println(responseJSON.toString());
System.out.println("Sent: " + responseJSON.toString());
outToClient.write(responseJSON.toString().getBytes("UTF-8"));
} else if(method.equalsIgnoreCase("field")) {
JSONObject responseJSON = server.field(clientObject);
System.out.println(responseJSON.toString());
System.out.println("Sent: " + responseJSON.toString());
outToClient.write(responseJSON.toString().getBytes("UTF-8"));
} else if(method.equalsIgnoreCase("offer")) {
JSONObject responseJSON = server.offer(clientObject);
System.out.println(responseJSON.toString());
System.out.println("Sent: " + responseJSON.toString());
outToClient.write(responseJSON.toString().getBytes("UTF-8"));
} else if(method.equalsIgnoreCase("canceloffer")) {
JSONObject responseJSON = server.canceloffer(clientObject);
System.out.println(responseJSON.toString());
System.out.println("Sent: " + responseJSON.toString());
outToClient.write(responseJSON.toString().getBytes("UTF-8"));
} else if(method.equalsIgnoreCase("tradebox")) {
JSONObject responseJSON = server.tradebox(clientObject);
System.out.println(responseJSON.toString());
System.out.println("Sent: " + responseJSON.toString());
outToClient.write(responseJSON.toString().getBytes("UTF-8"));
} else if(method.equalsIgnoreCase("fetchitem")) {
JSONObject responseJSON = server.fetchitem(clientObject);
System.out.println(responseJSON.toString());
System.out.println("Sent: " + responseJSON.toString());
outToClient.write(responseJSON.toString().getBytes("UTF-8"));
} else if(method.equalsIgnoreCase("findoffer")) {
JSONObject responseJSON = server.findoffer(clientObject);
System.out.println(responseJSON.toString());
System.out.println("Sent: " + responseJSON.toString());
outToClient.write(responseJSON.toString().getBytes("UTF-8"));
} else if(method.equalsIgnoreCase("sendfind")) {
JSONObject responseJSON = server.sendfind(clientObject);
System.out.println(responseJSON.toString());
System.out.println("Sent: " + responseJSON.toString());
outToClient.write(responseJSON.toString().getBytes("UTF-8"));
} else if(method.equalsIgnoreCase("accept")) {
JSONObject responseJSON = server.accept(clientObject);
System.out.println(responseJSON.toString());
System.out.println("Sent: " + responseJSON.toString());
outToClient.write(responseJSON.toString().getBytes("UTF-8"));
} else if(method.equalsIgnoreCase("serverStatus")) {
JSONObject responseJSON = server.serverstatus(clientObject);
System.out.println(responseJSON.toString());
System.out.println("Sent: " + responseJSON.toString());
outToClient.write(responseJSON.toString().getBytes("UTF-8"));
}
}
......
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