Commit 9ddbb542 authored by Rafi Ramadhan's avatar Rafi Ramadhan
Browse files

update server

parent 0046a56f
{"name" : "Bandar Behari","width" : 4, "height" : 4,"map" : [["R13", "R13", "R13", "R13"],["R13", "R13", "R14", "R14"],["R13", "R14", "R14", "R14"],["R13", "R14", "R14", "R14"]]}
\ No newline at end of file
......@@ -29,13 +29,15 @@ dist.jar=${dist.dir}/GrandQuestServer.jar
dist.javadoc.dir=${dist.dir}/javadoc
endorsed.classpath=
excludes=
file.reference.java-json.jar=lib\\java-json.jar
file.reference.json-simple-1.1.1.jar=lib\\json-simple-1.1.1.jar
file.reference.mysql-connector-java-5.1.34-bin.jar=lib/mysql-connector-java-5.1.34-bin.jar
includes=**
jar.compress=false
javac.classpath=\
${file.reference.json-simple-1.1.1.jar}:\
${file.reference.mysql-connector-java-5.1.34-bin.jar}
${file.reference.mysql-connector-java-5.1.34-bin.jar}:\
${file.reference.java-json.jar}
# Space-separated list of extra javac options
javac.compilerargs=
javac.deprecation=false
......
......@@ -16,6 +16,7 @@ import java.security.NoSuchAlgorithmException;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.json.JSONException;
import org.json.simple.parser.ParseException;
/**
......@@ -42,7 +43,7 @@ public class ClientHandler extends Thread {
DataInputStream in = new DataInputStream(socketListener.getInputStream());
response = in.readUTF();
System.out.println(response);
DataOutputStream out = new DataOutputStream(socketListener.getOutputStream());
switch (parse.parseJSON(response, "method")) {
......@@ -92,11 +93,11 @@ public class ClientHandler extends Thread {
break;
}
} catch (IOException ex) {
Logger.getLogger(ClientHandler.class.getName()).log(Level.SEVERE, null, ex);
//Logger.getLogger(ClientHandler.class.getName()).log(Level.SEVERE, null, ex);
try {
socketListener.close();
} catch (IOException ex1) {
Logger.getLogger(ClientHandler.class.getName()).log(Level.SEVERE, null, ex1);
//Logger.getLogger(ClientHandler.class.getName()).log(Level.SEVERE, null, ex1);
}
} catch (ParseException ex) {
Logger.getLogger(ClientHandler.class.getName()).log(Level.SEVERE, null, ex);
......@@ -104,6 +105,8 @@ public class ClientHandler extends Thread {
Logger.getLogger(ClientHandler.class.getName()).log(Level.SEVERE, null, ex);
} catch (NoSuchAlgorithmException ex) {
Logger.getLogger(ClientHandler.class.getName()).log(Level.SEVERE, null, ex);
} catch (JSONException ex) {
Logger.getLogger(ClientHandler.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
......
......@@ -40,6 +40,7 @@ public class DBHandler {
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 + "'");
......@@ -50,15 +51,20 @@ public class DBHandler {
}
return authenticated;
}
public boolean signup(String username) throws SQLException {
boolean authenticated = false;
ResultSet result = statement.executeQuery("select * from user where username like '" + username);
boolean authenticated = true;
ResultSet result = statement.executeQuery("select * from user where username like '" + username+"'");
while(result.next()) {
String user = result.getString("username");
authenticated = (username.equals(user));
if(username.equals(user)) {
authenticated = false;
}
System.out.println(authenticated);
}
return authenticated;
}
public void addInventory(String username, int item) {
try {
statement.executeUpdate("insert into inventory (username, item)"
......@@ -67,6 +73,16 @@ public class DBHandler {
Logger.getLogger(DBHandler.class.getName()).log(Level.SEVERE, null, ex);
}
}
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);
while(result.next()) {
amount = result.getInt("amount");
}
return amount;
}
public void removefromInventory() {
}
......
......@@ -28,13 +28,13 @@ public class JSONParse {
//Respon JSON untuk login
public Map loginJSON(boolean valid, String token) {
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("time", 1213131311);
response.put("time", time);
}
else {
response.put("status", "fail");
......
......@@ -6,22 +6,40 @@
package grandquestserver;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.sql.*;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.json.simple.JSONValue;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
import java.sql.*;
import java.util.Calendar;
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 JSONArray field_map = new JSONArray();
public String genToken(String value) throws NoSuchAlgorithmException {
MessageDigest md = MessageDigest.getInstance("MD5");
......@@ -37,26 +55,84 @@ public class MethodHandler {
return sb.toString();
}
public int searchToken(String token) {
boolean found = false;
int index = -999;
int i=0;
while(!found && i<session.size()) {
if(session.get(i).getToken().equals(token)) {
index = i;
found = true;
}
i++;
}
return index;
}
public String Signup(String input) {
private static String getStringFromInputStream(InputStream is) {
BufferedReader br = null;
StringBuilder sb = new StringBuilder();
String line;
try {
br = new BufferedReader(new InputStreamReader(is));
while ((line = br.readLine()) != null) {
sb.append(line);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (br != null) {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return sb.toString();
}
public String Signup(String input) throws SQLException, ParseException {
DBHandler db = new DBHandler();
JSONParse parse = new JSONParse();
Map response = new LinkedHashMap();
response = parse.signupJSON(true);
String username = parse.parseJSON(input, "username");
String password = parse.parseJSON(input, "password");
boolean valid = db.signup(username);
System.out.println(valid);
if(valid) {
response = parse.signupJSON(true);
db.addUser(username, password);
}
else {
response = parse.signupJSON(false);
}
return JSONValue.toJSONString(response);
}
public String Login(String input) throws ParseException, SQLException, NoSuchAlgorithmException {
DBHandler db = new DBHandler();
Session log = new Session();
JSONParse parse = new JSONParse();
Map response = new LinkedHashMap();
String username = parse.parseJSON(input, "username");
String password = parse.parseJSON(input, "password");
String token = genToken(username);
log.setUsername(username); log.setToken(token);
session.add(log);
boolean valid = db.authenticate(username, password);
......@@ -69,14 +145,30 @@ public class MethodHandler {
return JSONValue.toJSONString(response);
}
public String Inventory(String input) {
public String Inventory(String input) throws ParseException, SQLException {
List<Integer> inv = new ArrayList<>();
DBHandler db = new DBHandler();
JSONParse parse = new JSONParse();
Map response = new LinkedHashMap();
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);
String token = parse.parseJSON(input, "token");
String username = null;
int amount = 0;
int index = searchToken(token);
if(index != -999) {
username = session.get(index).getUsername();
for (int i=0; i<10; i++) {
amount = db.getItem(username, i);
inv.add(amount);
}
}
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);
......@@ -99,23 +191,75 @@ public class MethodHandler {
public int Combine(Long item1, Long item2) {
int id = 0;
Long mix = item1 + item2;
if(mix == 1) {
id = 4;
}
else if (mix==3) {
id = 5;
}
else if (mix == 5) {
id = 6;
}
else if (mix == 9) {
id = 7;
}
else if (mix == 11) {
id = 8;
}
else if (mix == 15) {
id = 9;
}
else {
id = -999;
}
return id;
}
public String Map(String input) throws ParseException {
public String Map(String input) throws ParseException, IOException, JSONException {
JSONParse parse = new JSONParse();
JSONParser map = new JSONParser();
Map response = new LinkedHashMap();
String location = parse.parseJSON(input, "name");
Long width = parse.parseIntJSON(input, "width");
Long height = parse.parseIntJSON(input, "height");
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);
//JSONObject json = new JSONObject(obj);
String location = json.getString("name");
Long width = json.getLong("width");
Long height = json.getLong("height");
// loop array
field_map = json.getJSONArray("map");
System.out.println(field_map.getJSONArray(1).getString(1));
//externalMap.getJSONARRAY("map").getJSONARRAY(1).getString(2);
// String location = parse.parseJSON(input, "name");
// Long width = parse.parseIntJSON(input, "width");
// Long height = parse.parseIntJSON(input, "height");
response = parse.mapJSON(location, width, height);
System.out.println(JSONValue.toJSONString(response));
return JSONValue.toJSONString(response);
}
public String Move(String input) throws ParseException {
JSONParse parse = new JSONParse();
......
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package grandquestserver;
/**
*
* @author Lenovo
*/
public class Session {
private String username;
private String token;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getToken() {
return token;
}
public void setToken(String token) {
this.token = token;
}
}
{"status":"ok","name":null,"width":null,"height":null}
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<!-- You may freely edit this file. See commented blocks below for -->
<!-- some examples of how to customize the build. -->
<!-- (If you delete it and reopen the project it will be recreated.) -->
<!-- By default, only the Clean and Build commands use this build script. -->
<!-- Commands such as Run, Debug, and Test only use this build script if -->
<!-- the Compile on Save feature is turned off for the project. -->
<!-- You can turn off the Compile on Save (or Deploy on Save) setting -->
<!-- in the project's Project Properties dialog box.-->
<project name="GrandQuestServer" default="default" basedir=".">
<description>Builds, tests, and runs the project GrandQuestServer.</description>
<import file="nbproject/build-impl.xml"/>
<!--
There exist several targets which are by default empty and which can be
used for execution of your tasks. These targets are usually executed
before and after some main targets. They are:
-pre-init: called before initialization of project properties
-post-init: called after initialization of project properties
-pre-compile: called before javac compilation
-post-compile: called after javac compilation
-pre-compile-single: called before javac compilation of single file
-post-compile-single: called after javac compilation of single file
-pre-compile-test: called before javac compilation of JUnit tests
-post-compile-test: called after javac compilation of JUnit tests
-pre-compile-test-single: called before javac compilation of single JUnit test
-post-compile-test-single: called after javac compilation of single JUunit test
-pre-jar: called before JAR building
-post-jar: called after JAR building
-post-clean: called after cleaning build products
(Targets beginning with '-' are not intended to be called on their own.)
Example of inserting an obfuscator after compilation could look like this:
<target name="-post-compile">
<obfuscate>
<fileset dir="${build.classes.dir}"/>
</obfuscate>
</target>
For list of available properties check the imported
nbproject/build-impl.xml file.
Another way to customize the build is by overriding existing main targets.
The targets of interest are:
-init-macrodef-javac: defines macro for javac compilation
-init-macrodef-junit: defines macro for junit execution
-init-macrodef-debug: defines macro for class debugging
-init-macrodef-java: defines macro for class execution
-do-jar: JAR building
run: execution of project
-javadoc-build: Javadoc generation
test-report: JUnit report generation
An example of overriding the target for project execution could look like this:
<target name="run" depends="GrandQuestServer-impl.jar">
<exec dir="bin" executable="launcher.exe">
<arg file="${dist.jar}"/>
</exec>
</target>
Notice that the overridden target depends on the jar target and not only on
the compile target as the regular run target does. Again, for a list of available
properties which you can use, check the target you are overriding in the
nbproject/build-impl.xml file.
-->
</project>
#Wed, 22 Apr 2015 13:56:47 +0700
D\:\\Semester\ 6\\Sister\\GrandQuestServer2=
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