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

update move

parent 49fc0c14
......@@ -41,6 +41,57 @@ public class DBHandler {
}
}
public Long getTime(String username) throws SQLException {
Long time = 0l;
ResultSet result = statement.executeQuery("select time from user where username='"+username+"'");
while(result.next()) {
time = result.getLong("time");
}
return time;
}
public Long getPosX(String username) throws SQLException {
Long x = 0l;
ResultSet result = statement.executeQuery("select posisi_x from user where username='"+username+"'");
while(result.next()) {
x = result.getLong("posisi_x");
}
return x;
}
public Long getPosY(String username) throws SQLException {
Long y = 0l;
ResultSet result = statement.executeQuery("select posisi_y from user where username='"+username+"'");
while(result.next()) {
y = result.getLong("posisi_y");
}
return y;
}
public void setTime(String username, Long time) {
try {
statement.executeUpdate("UPDATE user SET time="+time+" where username='"+username+"'");
} catch (SQLException ex) {
Logger.getLogger(DBHandler.class.getName()).log(Level.SEVERE, null, ex);
}
}
public void setPosX(String username, Long posX) {
try {
statement.executeUpdate("UPDATE user SET posisi_x="+posX+" where username='"+username+"'");
} catch (SQLException ex) {
Logger.getLogger(DBHandler.class.getName()).log(Level.SEVERE, null, ex);
}
}
public void setPosY(String username, Long posY) {
try {
statement.executeUpdate("UPDATE user SET posisi_y="+posY+" where username='"+username+"'");
} catch (SQLException ex) {
Logger.getLogger(DBHandler.class.getName()).log(Level.SEVERE, null, ex);
}
}
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 + "'");
......
......@@ -12,6 +12,7 @@ package grandquestserver;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
import java.sql.SQLException;
import java.util.*;
import org.json.simple.*;
import org.json.simple.parser.JSONParser;
......@@ -43,14 +44,14 @@ public class JSONParse {
}
//Respon JSON untuk login
public Map loginJSON(boolean valid, String token) {
public Map loginJSON(boolean valid, String token, Long x, Long y, Long time) {
Map response = new LinkedHashMap();
Long time = System.currentTimeMillis();
if (valid) {
response.put("status", "OK");
response.put("token", token);
response.put("x", 0);
response.put("y", 0);
response.put("x", x);
response.put("y", y);
response.put("time", time);
}
else {
......
......@@ -38,7 +38,8 @@ public class MethodHandler {
public static List<Session> session = new ArrayList<>();
public static List<Object> map_field = new ArrayList<>();
public JSONArray field_map = new JSONArray();
public static JSONArray field_map = new JSONArray();
public static Long width, height;
public String genToken(String value) throws NoSuchAlgorithmException {
......@@ -131,16 +132,20 @@ public class MethodHandler {
String username = parse.parseJSON(input, "username");
String password = parse.parseJSON(input, "password");
String token = genToken(username);
Long time = db.getTime(username);
Long x = db.getPosX(username);
Long y = db.getPosY(username);
log.setUsername(username); log.setToken(token);
session.add(log);
boolean valid = db.authenticate(username, password);
if(valid) {
response = parse.loginJSON(true, token);
response = parse.loginJSON(true, token, x, y, time);
}
else {
response = parse.loginJSON(false, token);
response = parse.loginJSON(false, token, x, y, time);
}
return JSONValue.toJSONString(response);
}
......@@ -231,21 +236,17 @@ public class MethodHandler {
map_field.clear();
//Object obj = map.parse(new FileReader("map.json"));
//File f = new File("map.json");
InputStream is = new FileInputStream("map.json");
String jsonTxt = getStringFromInputStream(is);
System.out.println(jsonTxt);
JSONObject json = new JSONObject(jsonTxt);
// String a = json.getString("1000");
// System.out.println(a);
InputStream is = new FileInputStream("map.json");
String jsonTxt = getStringFromInputStream(is);
System.out.println(jsonTxt);
JSONObject json = new JSONObject(jsonTxt);
//JSONObject json = new JSONObject(obj);
String location = json.getString("name");
Long width = json.getLong("width");
Long height = json.getLong("height");
width = json.getLong("width");
height = json.getLong("height");
// loop array
field_map = json.getJSONArray("map");
System.out.println(field_map.getJSONArray(1).getString(1));
......@@ -260,16 +261,28 @@ public class MethodHandler {
return JSONValue.toJSONString(response);
}
public String Move(String input) throws ParseException {
public String Move(String input) throws ParseException, SQLException {
JSONParse parse = new JSONParse();
Map response = new LinkedHashMap();
DBHandler db = new DBHandler();
Long time = System.currentTimeMillis()+10000;
String token = parse.parseJSON(input, "token");
Long x = parse.parseIntJSON(input, "x");
Long y = parse.parseIntJSON(input, "y");
Long time = 6876781679l;
response = parse.moveJSON(token, time, true);
System.out.println(width); System.out.println(height);
if(x<width && y<height) {
int index = searchToken(token);
db.setPosX(session.get(index).getUsername(), x);
db.setPosY(session.get(index).getUsername(), y);
db.setTime(session.get(index).getUsername(), time);
response = parse.moveJSON(token, time, true);
}
else {
response = parse.moveJSON(token, time, false);
}
System.out.println(JSONValue.toJSONString(response));
return JSONValue.toJSONString(response);
}
......
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