Commit 0ac8dde9 authored by Hendro Triokta Brianto's avatar Hendro Triokta Brianto
Browse files
parents f6ed54ff 9eeb5ae6
......@@ -20,9 +20,14 @@ import java.net.*;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.sql.SQLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;
import java.util.Enumeration;
import java.util.TimeZone;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.json.JSONException;
......@@ -246,7 +251,7 @@ public class Client extends Thread{
obj.put("token", toMD5(username));
obj.put("x", 0);
obj.put("y", 0);
obj.put("time",1427730710);
obj.put("time",new Long(getTime()));
}else{
obj.put("status", "fail");
obj.put("description", "username/password combination is not found");
......@@ -296,12 +301,17 @@ public class Client extends Thread{
sendToClient(obj.toString());
}
/* Sukses */
public String cmdMap(String token) throws IOException, NoSuchAlgorithmException, JSONException{
public void cmdMap(String token) throws IOException, NoSuchAlgorithmException, JSONException{
String response = "";
JSONObject obj = new JSONObject();
loadMap();
return response;
try {
obj = database.getMap();
} catch (ClassNotFoundException ex) {
obj.put("status", "error");
Logger.getLogger(Client.class.getName()).log(Level.SEVERE, null, ex);
}
sendToClient(obj.toString());
}
/* Sukses */
public void cmdMove(String token,String x, String y) throws IOException, NoSuchAlgorithmException, JSONException{
......@@ -637,4 +647,20 @@ public class Client extends Thread{
System.out.println("tidak ada");
}
}
public long getTime(){
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
TimeZone utcZone = TimeZone.getTimeZone("UTC");
simpleDateFormat.setTimeZone(utcZone);
simpleDateFormat.setTimeZone(TimeZone.getDefault());
Date now = new Date();
Calendar calendar = Calendar.getInstance();
String strDate = simpleDateFormat.format(now);
try {
calendar.setTime(simpleDateFormat.parse(strDate));
}catch(ParseException e){
e.printStackTrace();
}
return (calendar.getTimeInMillis()+30000)/1000;
}
}
......@@ -8,7 +8,11 @@ package thealchemist;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.sql.*;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Random;
import java.util.TimeZone;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.json.JSONArray;
......@@ -158,7 +162,7 @@ public class Database {
}
return passwordUser.equals(_password);
}
public int getJmlItem(String itemid) throws ClassNotFoundException{
public int getJmlItem(String token, String itemid) throws ClassNotFoundException{
int jml = 0;
Connection conn = null;
Statement statement = null;
......@@ -168,7 +172,7 @@ public class Database {
Class.forName(driver);
conn = DriverManager.getConnection(url+dbName,username,password);
statement = conn.createStatement();
rs = statement.executeQuery("SELECT "+itemid+" from inventory");
rs = statement.executeQuery("SELECT "+itemid+" from inventory WHERE token='"+token+"'");
while(rs.next()){
jml = rs.getInt(itemid);
}
......@@ -205,6 +209,7 @@ public class Database {
itemid = obj.getString("R"+y+""+x);
objToSent.put("item", new Integer(itemid));
objToSent.put("status", "ok");
updateJmlItem(token,itemid,getJmlItem(token,itemid));
} catch (JSONException ex) {
objToSent.put("description", "tidak ada item");
objToSent.put("status", "fail");
......@@ -219,6 +224,31 @@ public class Database {
}
return objToSent;
}
public JSONObject getMap() throws ClassNotFoundException, JSONException{
Connection conn = null;
Statement statement = null;
ResultSet rs;
JSONObject objToSent = new JSONObject();
String itemid;
try{
Class.forName(driver);
conn = DriverManager.getConnection(url+dbName,username,password);
statement = conn.createStatement();
rs = statement.executeQuery("SELECT `height`,`width`,`nama` from map");
while(rs.next()){
objToSent.put("status", "ok");
objToSent.put("width", rs.getInt("width"));
objToSent.put("height", rs.getInt("height"));
objToSent.put("name", rs.getString("nama"));
}
statement.close();
conn.close();
}catch(SQLException e){
objToSent.put("status", "error");
e.printStackTrace();
}
return objToSent;
}
public JSONObject mixItems(String token, String itemid1, String itemid2) throws ClassNotFoundException, InstantiationException, IllegalAccessException, SQLException, NoSuchAlgorithmException{
Connection conn = null;
Statement statement = null;
......@@ -257,9 +287,11 @@ public class Database {
switch(itemid1){
case "0":
if(item0 >= 3 && item1 >=3){
updateJmlItem("item4",getJmlItem("item4")+1);
updateJmlItem("item0",getJmlItem("item0")-3);
updateJmlItem("item1",getJmlItem("item1")-3);
System.out.println(item0+"x");
System.out.println(item1+"y");
updateJmlItem(token,"item4",getJmlItem(token,"item4")+1);
updateJmlItem(token,"item0",getJmlItem(token,"item0")-3);
updateJmlItem(token,"item1",getJmlItem(token,"item1")-3);
obj.put("item", 4);
obj.put("status", "ok");
}else{
......@@ -269,9 +301,9 @@ public class Database {
break;
case "1":
if(item1 >= 3 && item2 >=3){
updateJmlItem("item5",getJmlItem("item5")+1);
updateJmlItem("item1",getJmlItem("item1")-3);
updateJmlItem("item2",getJmlItem("item2")-3);
updateJmlItem(token,"item5",getJmlItem(token,"item5")+1);
updateJmlItem(token,"item1",getJmlItem(token,"item1")-3);
updateJmlItem(token,"item2",getJmlItem(token,"item2")-3);
}else{
obj.put("description", "jumlah item tidak mencukupi");
obj.put("status", "fail");
......@@ -279,9 +311,9 @@ public class Database {
break;
case "2":
if(item2 >= 3 && item3 >=3){
updateJmlItem("item6",getJmlItem("item6")+1);
updateJmlItem("item2",getJmlItem("item2")-3);
updateJmlItem("item3",getJmlItem("item3")-3);
updateJmlItem(token,"item6",getJmlItem(token,"item6")+1);
updateJmlItem(token,"item2",getJmlItem(token,"item2")-3);
updateJmlItem(token,"item3",getJmlItem(token,"item3")-3);
}else{
obj.put("description", "jumlah item tidak mencukupi");
obj.put("status", "fail");
......@@ -289,9 +321,9 @@ public class Database {
break;
case "4":
if(item4 >= 3 && item5 >=3){
updateJmlItem("item7",getJmlItem("item7")+1);
updateJmlItem("item4",getJmlItem("item4")-3);
updateJmlItem("item5",getJmlItem("item5")-3);
updateJmlItem(token,"item7",getJmlItem(token,"item7")+1);
updateJmlItem(token,"item4",getJmlItem(token,"item4")-3);
updateJmlItem(token,"item5",getJmlItem(token,"item5")-3);
}else{
obj.put("description", "jumlah item tidak mencukupi");
obj.put("status", "fail");
......@@ -299,9 +331,9 @@ public class Database {
break;
case "5":
if(item5 >= 3 && item6 >=3){
updateJmlItem("item8",getJmlItem("item8")+1);
updateJmlItem("item5",getJmlItem("item5")-3);
updateJmlItem("item6",getJmlItem("item6")-3);
updateJmlItem(token,"item8",getJmlItem(token,"item8")+1);
updateJmlItem(token,"item5",getJmlItem(token,"item5")-3);
updateJmlItem(token,"item6",getJmlItem(token,"item6")-3);
}else{
obj.put("description", "jumlah item tidak mencukupi");
obj.put("status", "fail");
......@@ -309,9 +341,9 @@ public class Database {
break;
case "7":
if(item7 >= 3 && item8 >=3){
updateJmlItem("item9",getJmlItem("item9")+1);
updateJmlItem("item7",getJmlItem("item7")-3);
updateJmlItem("item8",getJmlItem("item8")-3);
updateJmlItem(token,"item9",getJmlItem(token,"item9")+1);
updateJmlItem(token,"item7",getJmlItem(token,"item7")-3);
updateJmlItem(token,"item8",getJmlItem(token,"item8")-3);
}else{
obj.put("description", "jumlah item tidak mencukupi");
obj.put("status", "fail");
......@@ -329,14 +361,14 @@ public class Database {
}
return obj;
}
public int updateJmlItem(String itemid,int jml) throws ClassNotFoundException{
public int updateJmlItem(String token, String itemid,int jml) throws ClassNotFoundException{
Connection conn = null;
PreparedStatement statement = null;
int rs = 0;
try{
Class.forName(driver);
conn = DriverManager.getConnection(url+dbName,username,password);
statement = conn.prepareStatement("UPDATE inventory set "+itemid+"="+jml);
statement = conn.prepareStatement("UPDATE inventory set item"+itemid+"="+jml+" WHERE token='"+token+"'");
rs = statement.executeUpdate();
System.out.println(rs);
statement.close();
......@@ -361,12 +393,11 @@ public class Database {
conn = DriverManager.getConnection(url+dbName,username,password);
statement = conn.prepareStatement("UPDATE users set x="+x+",y="+y+" WHERE token='"+token+"'");
rs = statement.executeUpdate();
System.out.println(rs);
statement.close();
conn.close();
try {
obj.put("status","ok");
obj.put("time","1427730710");
obj.put("time",new Long(getTime()));
} catch (JSONException ex) {
Logger.getLogger(Database.class.getName()).log(Level.SEVERE, null, ex);
try {
......@@ -407,7 +438,7 @@ public class Database {
}
conn = DriverManager.getConnection(url+dbName,username,password);
statement = conn.createStatement();
rs = statement.executeQuery("SELECT `item1`,`item2`,`item3`,`item4`,`item5`,`item6`,`item7`,`item8` 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()){
ja.put(rs.getInt("item1"));
ja.put(rs.getInt("item2"));
......@@ -418,7 +449,7 @@ public class Database {
ja.put(rs.getInt("item7"));
ja.put(rs.getInt("item8"));
ja.put(rs.getInt("item9"));
ja.put(rs.getInt("item10"));
ja.put(rs.getInt("item0"));
}
statement.close();
conn.close();
......@@ -562,4 +593,20 @@ public class Database {
JSONObject jsonObj = new JSONObject(JSON);
return jsonObj.getString(param);
}
public long getTime(){
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
TimeZone utcZone = TimeZone.getTimeZone("UTC");
simpleDateFormat.setTimeZone(utcZone);
simpleDateFormat.setTimeZone(TimeZone.getDefault());
java.util.Date now = new java.util.Date();
Calendar calendar = Calendar.getInstance();
String strDate = simpleDateFormat.format(now);
try {
calendar.setTime(simpleDateFormat.parse(strDate));
}catch(ParseException e){
e.printStackTrace();
}
return (calendar.getTimeInMillis()+30000)/1000;
}
}
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