Commit fdb947cf authored by Kevin's avatar Kevin
Browse files

Merge branch 'server-tracker' of...

Merge branch 'server-tracker' of http://gitlab.informatika.org/ClearingPath/IF3230-Tugas-Besar-Sister-2015 into server-tracker
parents ca3cabd6 9de0d7f5
......@@ -3,4 +3,4 @@ do.depend=false
do.jar=true
javac.debug=true
javadoc.preview=true
user.properties.file=C:\\Users\\Kevin Zhong\\AppData\\Roaming\\NetBeans\\8.0.2\\build.properties
user.properties.file=C:\\Users\\Fauzan Hilmi\\AppData\\Roaming\\NetBeans\\8.0.2\\build.properties
{"kevin":{"r21":0,"r32":0,"r31":0,"password":"123","r12":5,"r23":0,"r11":6,"r22":0,"r14":0,"r13":0,"position":"nanti_dipikirin","r41":0}}
{"kevin":
{"r21":0,
"r32":0,
"r31":0,
"password":"123",
"r12":5,
"r23":0,
"r11":6,
"r22":0,
"r14":0,
"r13":0,
"position":
{
"x" :10,
"y" :10
},
"r41":0
}
}
{
"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
compile.on.save=true
user.properties.file=C:\\Users\\Kevin Zhong\\AppData\\Roaming\\NetBeans\\8.0.2\\build.properties
user.properties.file=C:\\Users\\Fauzan Hilmi\\AppData\\Roaming\\NetBeans\\8.0.2\\build.properties
......@@ -4,8 +4,12 @@ import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.Socket;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.xml.bind.annotation.adapters.HexBinaryAdapter;
import org.json.simple.JSONObject;
import org.json.simple.JSONArray;
import org.json.simple.parser.JSONParser;
......@@ -29,6 +33,10 @@ class Client implements Runnable {
private boolean active;
private String clientSentence;
private String capitalizedSentence;
private String username;
private String token;
public static Map<String,String> tokenMap;
public Client(int _id,Socket _client)
{
......@@ -52,13 +60,24 @@ class Client implements Runnable {
clientSentence = inFromClient.readLine();
if (clientSentence != null){
outToClient = new DataOutputStream(connectionSocket.getOutputStream());
System.out.println("Received: " + clientSentence);
//System.out.println("Received: " + clientSentence);
Object obj = parser.parse(clientSentence);
if (obj != null){
JSONObject array = (JSONObject) obj;
username = (String) array.get("username");
String feed = username+Long.toString(System.currentTimeMillis());
try {
this.generateToken(feed);
tokenMap.put(feed,username);
} catch (NoSuchAlgorithmException ex) {
Logger.getLogger(Client.class.getName()).log(Level.SEVERE, null, ex);
}
clientSentence = token;
}
capitalizedSentence = clientSentence.toUpperCase() + '\n';
outToClient.writeBytes(capitalizedSentence);
System.out.println(clientSentence);
outToClient.writeBytes(clientSentence);
//capitalizedSentence = clientSentence.toUpperCase() + '\n';
//outToClient.writeBytes(capitalizedSentence);
}
else{
active = false;
......@@ -79,6 +98,12 @@ class Client implements Runnable {
System.out.println("Thread " + threadName + " exiting...");
}
//feed = username client + timestamp
public void generateToken(String feed) throws NoSuchAlgorithmException {
MessageDigest md5 = MessageDigest.getInstance("MD5");
token = (new HexBinaryAdapter()).marshal(md5.digest(feed.getBytes()));
}
public void start(){
if (t == null)
{
......
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
......@@ -189,11 +192,57 @@ public class EndpointMethod {
resultJson.put("status", "error");
return resultJson.toString();
}
// public static void main(String argv[]) throws IOException, ParseException {
public static String move(String token, int x, int y) {
JSONObject resultJson = new JSONObject();
JSONParser parser = new JSONParser();
try {
String basePath = new File("").getAbsolutePath();
Object obj = parser.parse(new FileReader(basePath + "//file_saved.json"));
if(obj!=null) {
JSONObject jsonFile = (JSONObject) obj;
JSONObject userJson = (JSONObject) jsonFile.get(Client.tokenMap.get(token).toLowerCase());
JSONObject posJson = (JSONObject) userJson.get("position");
int curX = (int) posJson.get("x");
int curY = (int) posJson.get("y");
if(x!=curX && y!=curY) {
if(true) { //NANTI DIGANTI JADI PENGECEKAN APAKAH X Y ADA DI MAP ATAU GA
//Ganti data di database
posJson.put("x",x);
posJson.put("y",y);
userJson.put("position",posJson);
resultJson.put("status","ok");
resultJson.put("time", System.currentTimeMillis());
}
else {
resultJson.put("status","fail");
resultJson.put("description","The position is out of map's bound");
}
}
else {
resultJson.put("status","fail");
resultJson.put("description", "You are already in this position");
}
return resultJson.toString();
}
} catch (FileNotFoundException ex) {
Logger.getLogger(EndpointMethod.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(EndpointMethod.class.getName()).log(Level.SEVERE, null, ex);
} catch (ParseException ex) {
Logger.getLogger(EndpointMethod.class.getName()).log(Level.SEVERE, null, ex);
}
resultJson.put("status","error");
return resultJson.toString();
}
public static void main(String argv[]) throws IOException, ParseException {
// EndpointMethod tes = new EndpointMethod();
// System.out.println(tes.inventory("Kevi"));
// System.out.println(tes.mixItem("Kevin", "r11", "r23"));
// }
}
}
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
/*
* 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.
*/
/**
*
* @author Fauzan Hilmi
*/
public class Map {
public static String name;
public static long width;
public static long height;
public static String matrix[][];
public static void load() {
JSONParser parser = new JSONParser();
try {
String basePath = new File("").getAbsolutePath();
Object obj = parser.parse(new FileReader(basePath + "//map.json"));
if(obj!=null) {
JSONObject map = (JSONObject) obj;
name = (String) map.get("name");
width = (long) map.get("width");
height = (long) map.get("height");
matrix = new String[(int)height][(int)width];
JSONArray mapRow = (JSONArray) map.get("map");
for(int i=0; i<height; i++) {
JSONArray mapCol = (JSONArray) mapRow.get(i);
for(int j=0; j<width; j++) {
matrix[i][j] = (String) mapCol.get(j);
}
}
/*for(int i=0; i<height; i++) {
for(int j=0; j<width; j++) {
matrix[i][j] = mapMatrix.
}
}*/
}
} catch (IOException ex) {
Logger.getLogger(Map.class.getName()).log(Level.SEVERE, null, ex);
} catch (ParseException ex) {
Logger.getLogger(Map.class.getName()).log(Level.SEVERE, null, ex);
}
}
public static void main(String argv[]) {
Map.load();
System.out.println(Map.name);
System.out.println(Map.height);
System.out.println(Map.width);
for(int i=0; i<Map.height; i++) {
for(int j=0; j<Map.width; j++) {
System.out.print(Map.matrix[i][j]+" ");
}
System.out.println("");
}
}
}
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