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{ ...@@ -324,7 +324,12 @@ public class Client extends Thread{
/* Sukses */ /* Sukses */
public void cmdMove(String token,String x, String y) throws IOException, NoSuchAlgorithmException, JSONException{ public void cmdMove(String token,String x, String y) throws IOException, NoSuchAlgorithmException, JSONException{
JSONObject obj = new JSONObject(); 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()); sendToClient(obj.toString());
} }
/* Sukses 0.7 */ /* Sukses 0.7 */
...@@ -528,27 +533,7 @@ public class Client extends Thread{ ...@@ -528,27 +533,7 @@ public class Client extends Thread{
return name; 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 { ...@@ -422,7 +422,7 @@ public class Database {
try{ try{
Class.forName(driver); Class.forName(driver);
conn = DriverManager.getConnection(url+dbName,username,password); 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(); rs = statement.executeUpdate();
System.out.println(rs); System.out.println(rs);
statement.close(); statement.close();
...@@ -494,6 +494,7 @@ public class Database { ...@@ -494,6 +494,7 @@ public class Database {
statement = conn.createStatement(); statement = conn.createStatement();
rs = statement.executeQuery("SELECT `item1`,`item2`,`item3`,`item4`,`item5`,`item6`,`item7`,`item8`,`item9`,`item0` from `inventory` WHERE `token`='"+token+"'"); rs = statement.executeQuery("SELECT `item1`,`item2`,`item3`,`item4`,`item5`,`item6`,`item7`,`item8`,`item9`,`item0` from `inventory` WHERE `token`='"+token+"'");
while(rs.next()){ while(rs.next()){
ja.put(rs.getInt("item0"));
ja.put(rs.getInt("item1")); ja.put(rs.getInt("item1"));
ja.put(rs.getInt("item2")); ja.put(rs.getInt("item2"));
ja.put(rs.getInt("item3")); ja.put(rs.getInt("item3"));
...@@ -503,7 +504,7 @@ public class Database { ...@@ -503,7 +504,7 @@ public class Database {
ja.put(rs.getInt("item7")); ja.put(rs.getInt("item7"));
ja.put(rs.getInt("item8")); ja.put(rs.getInt("item8"));
ja.put(rs.getInt("item9")); ja.put(rs.getInt("item9"));
ja.put(rs.getInt("item0"));
} }
statement.close(); statement.close();
conn.close(); conn.close();
......
...@@ -24,8 +24,13 @@ import static thealchemist.Client.myPort; ...@@ -24,8 +24,13 @@ import static thealchemist.Client.myPort;
* @author adwisatya * @author adwisatya
*/ */
public class TCPServer { public class TCPServer {
public static int widthMap;
public static int heightMap;
static int portNumber = 6789; static int portNumber = 6789;
public static void setWidthHeight(int width,int height){
widthMap = width;
heightMap = height;
}
public static void main(String argv[]) throws Exception{ public static void main(String argv[]) throws Exception{
// ServerSocket mainSocket ; // ServerSocket mainSocket ;
// mainSocket = new ServerSocket(portNumber); // mainSocket = new ServerSocket(portNumber);
......
...@@ -34,7 +34,7 @@ import org.json.simple.parser.JSONParser; ...@@ -34,7 +34,7 @@ import org.json.simple.parser.JSONParser;
* @author adwisatya * @author adwisatya
*/ */
public class Utility { public class Utility {
public long getTime(){ public long getTime(){
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
TimeZone utcZone = TimeZone.getTimeZone("UTC"); TimeZone utcZone = TimeZone.getTimeZone("UTC");
...@@ -121,8 +121,9 @@ public class Utility { ...@@ -121,8 +121,9 @@ public class Utility {
Pattern p = Pattern.compile("\"([^\"]*)\""); Pattern p = Pattern.compile("\"([^\"]*)\"");
Matcher m = p.matcher(jsonobj.getString("map").toString()); Matcher m = p.matcher(jsonobj.getString("map").toString());
int width = jsonobj.getInt("width"); int width = jsonobj.getInt("width");
System.out.println("Set widthMap to "+ width);
int height = jsonobj.getInt("height"); int height = jsonobj.getInt("height");
TCPServer.setWidthHeight(width, height);
ArrayList<String> tmpItemLoc = new ArrayList<String>(); ArrayList<String> tmpItemLoc = new ArrayList<String>();
while (m.find()) { while (m.find()) {
//System.out.println(m.group(1)); //System.out.println(m.group(1));
...@@ -165,4 +166,27 @@ public class Utility { ...@@ -165,4 +166,27 @@ public class Utility {
} }
return itemCode; 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