Commit c4e21383 authored by Rafi Ramadhan's avatar Rafi Ramadhan
Browse files

fix bug signup

parent 2822eea3
......@@ -94,8 +94,8 @@ public class ClientHandler extends Thread {
out.writeUTF(handler.cancelOffer(response));
break;
default:
out.writeUTF("Thank you for connecting to "
+ socketListener.getLocalSocketAddress() + "\nGoodbye!");
out.writeUTF("Error :"
+ socketListener.getLocalSocketAddress());
break;
}
} catch (IOException ex) {
......
......@@ -34,15 +34,18 @@ public class DBHandler {
koneksi = DriverManager.getConnection(databaseURL, username, password);
statement = koneksi.createStatement();
}
//menambahkan user
public void addUser(String username, String password) {
try {
statement.executeUpdate("insert into user (username, password)"
+ "values ('" + username + "', '" + password + "')");
statement.executeUpdate("insert into user (username, password, posisi_x, posisi_y, time)"
+ "values ('" + username + "', '" + password + "', 0, 0, 0)");
} catch (SQLException ex) {
Logger.getLogger(DBHandler.class.getName()).log(Level.SEVERE, null, ex);
}
}
//mengambil waktu terakhir pemain bergerak
public Long getTime(String username) throws SQLException {
Long time = 0l;
ResultSet result = statement.executeQuery("select time from user where username='"+username+"'");
......@@ -52,6 +55,7 @@ public class DBHandler {
return time;
}
//mengambil current posisi dari player
public int getPosX(String username) throws SQLException {
int x = 0;
ResultSet result = statement.executeQuery("select posisi_x from user where username='"+username+"'");
......@@ -70,6 +74,7 @@ public class DBHandler {
return y;
}
//update waktu
public void setTime(String username, Long time) {
try {
statement.executeUpdate("UPDATE user SET time="+time+" where username='"+username+"'");
......@@ -78,6 +83,7 @@ public class DBHandler {
}
}
//update current posisi player
public void setPosX(String username, Long posX) {
try {
statement.executeUpdate("UPDATE user SET posisi_x="+posX+" where username='"+username+"'");
......@@ -94,6 +100,7 @@ public class DBHandler {
}
}
//fungsi autentikasi pengguna
public boolean authenticate(String username, String password) throws SQLException {
boolean authenticated = false;
ResultSet result = statement.executeQuery("select * from user where username like '" + username + "' and password like '" + password + "'");
......@@ -105,6 +112,7 @@ public class DBHandler {
return authenticated;
}
//memeriksa username yang sudah terdaftar
public boolean signup(String username) throws SQLException {
boolean authenticated = true;
ResultSet result = statement.executeQuery("select * from user where username like '" + username+"'");
......@@ -118,6 +126,7 @@ public class DBHandler {
return authenticated;
}
//menambahkan item ke dalam inventory
public void addInventory(String username, int item) {
try {
statement.executeUpdate("insert into inventory (username, item)"
......@@ -127,6 +136,7 @@ public class DBHandler {
}
}
//menghapus item dari inventory
public void removefromInventory(String username, int item, int amount) {
try {
statement.executeUpdate("DELETE FROM inventory where username='"+username+"' and item="+item+" limit "+amount);
......@@ -135,6 +145,7 @@ public class DBHandler {
}
}
//menghitung jumlah satu tipe item
public int getItem(String username, int item) throws SQLException {
int amount = 0;
ResultSet result = statement.executeQuery("select count(item) as amount from inventory where username='"+username+"' and item="+item);
......@@ -144,6 +155,7 @@ public class DBHandler {
return amount;
}
//mengambil item dari map
public int pickItem(String code) throws SQLException {
int item = 0;
ResultSet result = statement.executeQuery("select id from items where id_map='"+code+"'");
......@@ -153,6 +165,7 @@ public class DBHandler {
return item;
}
//menyimpan offer ke dalam tradebox
public void putOffer(String username, Offer offer) {
try {
statement.executeUpdate("insert into tradebox (offer_token, username, item_offered, num_offered, item_demanded, num_demanded, availability)"
......@@ -162,14 +175,16 @@ public class DBHandler {
}
}
public void takeOffer(String token) {
//menghapus offer dari tradebox
public void takeOffer(String token, boolean available) {
try {
statement.executeUpdate("DELETE FROM tradebox where offer_token='"+token+"'");
statement.executeUpdate("DELETE FROM tradebox where offer_token='"+token+"' and availability="+available);
} catch (SQLException ex) {
Logger.getLogger(DBHandler.class.getName()).log(Level.SEVERE, null, ex);
}
}
//mengambil offer dari tradebox
public Offer pullOffer(String token) throws SQLException {
Offer offer = new Offer();
......@@ -185,6 +200,7 @@ public class DBHandler {
return offer;
}
//menampilkan seluruh isi tradebox
public List<Offer> getOffer(String username) throws SQLException {
Offer offer = new Offer();
......@@ -257,8 +273,7 @@ public class DBHandler {
while(result.next()) {
available = result.getBoolean("availability");
}
return available;
}
}
......@@ -36,12 +36,13 @@ import sun.misc.IOUtils;
* @author Rakhmatullah Yoga S
*/
public class MethodHandler {
public static List<Session> session = new ArrayList<>();
public static List<Object> map_field = new ArrayList<>();
public static JSONArray field_map = new JSONArray();
public static Long width, height;
//generate random token
public String genToken(String value) throws NoSuchAlgorithmException {
MessageDigest md = MessageDigest.getInstance("MD5");
......@@ -57,6 +58,7 @@ public class MethodHandler {
return sb.toString();
}
//mencocokkan token dengan username dalam session
public int searchToken(String token) {
boolean found = false;
......@@ -73,6 +75,7 @@ public class MethodHandler {
return index;
}
//convert inputstream ke string
private static String getStringFromInputStream(InputStream is) {
BufferedReader br = null;
......@@ -102,6 +105,7 @@ public class MethodHandler {
}
//generate random string
public String randString(int len) {
String stream = "abcdefghijklmnopqrstuvwxyz0123456789";
......@@ -114,6 +118,7 @@ public class MethodHandler {
return sb.toString();
}
//fungsi signup
public String Signup(String input) throws SQLException, ParseException {
DBHandler db = new DBHandler();
......@@ -135,6 +140,7 @@ public class MethodHandler {
return JSONValue.toJSONString(response);
}
//fungsi login
public String Login(String input) throws ParseException, SQLException, NoSuchAlgorithmException {
DBHandler db = new DBHandler();
......@@ -163,6 +169,7 @@ public class MethodHandler {
return JSONValue.toJSONString(response);
}
//melihat inventory pemain
public String Inventory(String input) throws ParseException, SQLException {
List<Integer> inv = new ArrayList<>();
......@@ -182,16 +189,14 @@ public class MethodHandler {
}
}
System.out.println(amount);
//inv.add(1); inv.add(2); inv.add(3); inv.add(4); inv.add(5);
//inv.add(1); inv.add(2); inv.add(3); inv.add(4); inv.add(5);
response = parse.invJSON(inv);
return JSONValue.toJSONString(response);
}
//mengkombinasikan item
public String MixItem(String input) throws ParseException, SQLException {
JSONParse parse = new JSONParse();
......@@ -220,6 +225,7 @@ public class MethodHandler {
return JSONValue.toJSONString(response);
}
//fungsi kombinasi item
public int Combine(Long item1, Long item2) {
int id = 0;
......@@ -255,6 +261,7 @@ public class MethodHandler {
return id;
}
//fungsi untuk retrieve map
public String Map(String input) throws ParseException, IOException, JSONException {
JSONParse parse = new JSONParse();
......@@ -286,7 +293,8 @@ public class MethodHandler {
System.out.println(JSONValue.toJSONString(response));
return JSONValue.toJSONString(response);
}
//predikat untuk gerak
public boolean isMoving(String username) throws SQLException {
DBHandler db = new DBHandler();
......@@ -300,6 +308,7 @@ public class MethodHandler {
return walking;
}
//fungsi gerak pemain
public String Move(String input) throws ParseException, SQLException {
JSONParse parse = new JSONParse();
......@@ -329,6 +338,7 @@ public class MethodHandler {
return JSONValue.toJSONString(response);
}
//fungsi untuk mengambil item di peta
public String Field(String input) throws ParseException, SQLException, JSONException {
JSONParse parse = new JSONParse();
......@@ -356,6 +366,7 @@ public class MethodHandler {
return JSONValue.toJSONString(response);
}
//predikat ketersediaan item dalam inventory
public boolean checkInventory(int item, int num, String token) throws SQLException {
DBHandler db = new DBHandler();
......@@ -371,6 +382,7 @@ public class MethodHandler {
return exist;
}
//menaruh tawaran ke dalam tradebox
public String Offer(String input) throws ParseException, SQLException, NoSuchAlgorithmException {
JSONParse parse = new JSONParse();
......@@ -402,6 +414,7 @@ public class MethodHandler {
return JSONValue.toJSONString(response);
}
//melihat isi tradebox pemain
public String TradeBox(String input) throws ParseException, SQLException {
List<Object> offer = new ArrayList<>();
......@@ -435,6 +448,7 @@ public class MethodHandler {
return JSONValue.toJSONString(response);
}
//melakukan copy list
public List<Object> copyList(List<Object> target) {
List<Object> result = new ArrayList<>();
......@@ -445,6 +459,7 @@ public class MethodHandler {
return result;
}
//
public String sendFind(String input) throws ParseException {
List<Object> offer = new ArrayList<>();
......@@ -470,6 +485,7 @@ public class MethodHandler {
return JSONValue.toJSONString(response);
}
//mencari item yang diinginkan dari server lain
public String findOffer(String input) throws ParseException, SQLException {
List<Object> offer = new ArrayList<>();
......@@ -503,6 +519,7 @@ public class MethodHandler {
return JSONValue.toJSONString(response);
}
//
public String sendAccept(String input) {
JSONParse parse = new JSONParse();
......@@ -513,6 +530,7 @@ public class MethodHandler {
return JSONValue.toJSONString(response);
}
//menerima offer dari server
public String Accept(String input) throws ParseException, SQLException {
JSONParse parse = new JSONParse();
......@@ -530,6 +548,7 @@ public class MethodHandler {
return JSONValue.toJSONString(response);
}
//memasukkan item tradebox ke dalam inventory
public void pullItem(String username, int item, int amount) throws SQLException {
DBHandler db = new DBHandler();
......@@ -538,6 +557,7 @@ public class MethodHandler {
}
}
//mengambil item yang diinginkan dari tradebox
public String fetchItem(String input) throws ParseException, SQLException {
JSONParse parse = new JSONParse();
......@@ -549,9 +569,9 @@ public class MethodHandler {
String offer_token = parse.parseJSON(input, "offer_token");
String username = session.get(searchToken(token)).getUsername();
if(db.checkTradebox(offer_token)) {
if(db.checkTradebox(offer_token) && db.checkStatus(offer_token)==false) {
item = db.pullOffer(offer_token);
db.takeOffer(offer_token);
db.takeOffer(offer_token, false);
pullItem(username, item.getDemanded().intValue(), item.getDemanded_num().intValue());
response = parse.fetchitemJSON(true);
}
......@@ -562,6 +582,7 @@ public class MethodHandler {
return JSONValue.toJSONString(response);
}
//membatalkan offer pemain
public String cancelOffer(String input) throws SQLException, ParseException {
JSONParse parse = new JSONParse();
......@@ -573,9 +594,9 @@ public class MethodHandler {
String offer_token = parse.parseJSON(input, "offer_token");
String username = session.get(searchToken(token)).getUsername();
if(db.checkTradebox(offer_token)) {
if(db.checkTradebox(offer_token) && db.checkStatus(offer_token)==true) {
item = db.pullOffer(offer_token);
db.takeOffer(offer_token);
db.takeOffer(offer_token, true);
pullItem(username, item.getOffered().intValue(), item.getOffered_num().intValue());
response = parse.cancelofferJSON(true);
}
......
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