Commit a7ab1b60 authored by adwisatya's avatar adwisatya
Browse files

Revisi lagi

parent c26708b6
{"name" : "Bandar Behari","width" : 4,"height" : 4,"map" :[["R13", "R13", "R13", "R13"],["R13", "R13", "R14", "R14"],["R13", "R14", "R14", "R14"],["R13", "R14", "R14", "R14"]]}
{"name" : "Sekre1","width" : 4,"height" : 4,"map" :[["R13", "R13", "R13", "R13"],["R13", "R13", "R14", "R14"],["R13", "R14", "R14", "R14"],["R13", "R14", "R14", "R14"]]}
......@@ -324,7 +324,12 @@ public class Client extends Thread{
/* Sukses */
public void cmdMove(String token,String x, String y) throws IOException, NoSuchAlgorithmException, JSONException{
JSONObject obj = new JSONObject();
obj = database.updateLokasi(token, x, y);
if(utility.isPassBoundary(new Integer(x), new Integer(y))){
obj = database.updateLokasi(token, x, y);
}else{
obj.put("status","fail");
obj.put("description","out of boundary");
}
sendToClient(obj.toString());
}
/* Sukses 0.7 */
......@@ -528,27 +533,7 @@ public class Client extends Thread{
return name;
}
// methods for move
// apakah move player valid satu kotak
public boolean isMoveValid(int currX, int currY, int destX, int destY) {
boolean valid = false;
if((currX - destX == 1 || currX - destX == -1) && (currY == destY)) {
valid = true;
} else if((currY - destY == 1 || currY - destY == -1) && (currX == destX)) {
valid = true;
}
return valid;
}
// apakah move player tidak melewati batas
public boolean isPassBoundary(int sizeX, int sizeY, int destX, int destY) {
boolean pass;
if(0 <= destX && destX <=sizeX && 0 <= destY && destY <= sizeY) {
pass = false;
} else {
pass = true;
}
return pass;
}
......
......@@ -422,7 +422,7 @@ public class Database {
try{
Class.forName(driver);
conn = DriverManager.getConnection(url+dbName,username,password);
statement = conn.prepareStatement("UPDATE inventory set item"+itemid+"="+jml+" WHERE token='"+token+"'");
statement = conn.prepareStatement("UPDATE inventory set "+itemid+"="+jml+" WHERE token='"+token+"'");
rs = statement.executeUpdate();
System.out.println(rs);
statement.close();
......@@ -494,6 +494,7 @@ public class Database {
statement = conn.createStatement();
rs = statement.executeQuery("SELECT `item1`,`item2`,`item3`,`item4`,`item5`,`item6`,`item7`,`item8`,`item9`,`item0` from `inventory` WHERE `token`='"+token+"'");
while(rs.next()){
ja.put(rs.getInt("item0"));
ja.put(rs.getInt("item1"));
ja.put(rs.getInt("item2"));
ja.put(rs.getInt("item3"));
......@@ -503,7 +504,7 @@ public class Database {
ja.put(rs.getInt("item7"));
ja.put(rs.getInt("item8"));
ja.put(rs.getInt("item9"));
ja.put(rs.getInt("item0"));
}
statement.close();
conn.close();
......
......@@ -24,8 +24,13 @@ import static thealchemist.Client.myPort;
* @author adwisatya
*/
public class TCPServer {
public static int widthMap;
public static int heightMap;
static int portNumber = 6789;
public static void setWidthHeight(int width,int height){
widthMap = width;
heightMap = height;
}
public static void main(String argv[]) throws Exception{
// ServerSocket mainSocket ;
// mainSocket = new ServerSocket(portNumber);
......
......@@ -34,7 +34,7 @@ import org.json.simple.parser.JSONParser;
* @author adwisatya
*/
public class Utility {
public long getTime(){
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
TimeZone utcZone = TimeZone.getTimeZone("UTC");
......@@ -121,8 +121,9 @@ public class Utility {
Pattern p = Pattern.compile("\"([^\"]*)\"");
Matcher m = p.matcher(jsonobj.getString("map").toString());
int width = jsonobj.getInt("width");
System.out.println("Set widthMap to "+ width);
int height = jsonobj.getInt("height");
TCPServer.setWidthHeight(width, height);
ArrayList<String> tmpItemLoc = new ArrayList<String>();
while (m.find()) {
//System.out.println(m.group(1));
......@@ -165,4 +166,27 @@ public class Utility {
}
return itemCode;
}
// methods for move
// apakah move player valid satu kotak
public boolean isMoveValid(int currX, int currY, int destX, int destY) {
boolean valid = false;
if((currX - destX == 1 || currX - destX == -1) && (currY == destY)) {
valid = true;
} else if((currY - destY == 1 || currY - destY == -1) && (currX == destX)) {
valid = true;
}
return valid;
}
// apakah move player tidak melewati batas
public boolean isPassBoundary(int destX, int destY) {
System.out.println(destX + " " + destY + "|" + TCPServer.widthMap + " " + TCPServer.heightMap);
boolean pass = false;
if((0 <= destX) && (destX <TCPServer.widthMap) && (0 <= destY) && (destY < TCPServer.heightMap)) {
pass = false;
} else {
pass = true;
}
System.out.println("move is " + ((0 <= destX) && (destX <TCPServer.widthMap) && (0 <= destY) && (destY < TCPServer.heightMap)));
return ((0 <= destX) && (destX <TCPServer.widthMap) && (0 <= destY) && (destY < TCPServer.heightMap));
}
}
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