Commit 42e99b07 authored by Kevin's avatar Kevin
Browse files

Merge branch 'server-tracker'

parents f67864e1 370872a9
......@@ -3,11 +3,7 @@
<editor-bookmarks xmlns="http://www.netbeans.org/ns/editor-bookmarks/2" lastBookmarkId="0"/>
<open-files xmlns="http://www.netbeans.org/ns/projectui-open-files/2">
<group>
<<<<<<< HEAD
<file>file:/C:/Users/Kevin%20Zhong/Documents/NetBeansProjects/Coba/src/client/TCPClient/src/TCPClient.java</file>
=======
<file>file:/C:/Users/Jonathan/Documents/NetBeansProjects/GrandQuest/src/client/TCPClient/src/TCPClient.java</file>
>>>>>>> origin/JSON-connect
</group>
</open-files>
</project-private>
{"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}}
......@@ -3,7 +3,11 @@
<editor-bookmarks xmlns="http://www.netbeans.org/ns/editor-bookmarks/2" lastBookmarkId="0"/>
<open-files xmlns="http://www.netbeans.org/ns/projectui-open-files/2">
<group>
<file>file:/C:/Users/Kevin%20Zhong/Documents/NetBeansProjects/Coba/src/server/TCPServer/src/ConnectionHandler.java</file>
<file>file:/C:/Users/Kevin%20Zhong/Documents/NetBeansProjects/Coba/src/server/TCPServer/src/crudDB/crudDB.java</file>
<file>file:/C:/Users/Kevin%20Zhong/Documents/NetBeansProjects/Coba/src/server/TCPServer/src/Client.java</file>
<file>file:/C:/Users/Kevin%20Zhong/Documents/NetBeansProjects/Coba/src/server/TCPServer/src/TCPServer.java</file>
<file>file:/C:/Users/Kevin%20Zhong/Documents/NetBeansProjects/Coba/src/server/TCPServer/src/EndpointMethod.java</file>
</group>
</open-files>
</project-private>
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
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 Kevin Zhong Local
*/
public class EndpointMethod {
public String inventory(String username) throws IOException, ParseException {
JSONObject resultJson = new JSONObject();
JSONArray inventory_list = new JSONArray();
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(username.toLowerCase());
inventory_list.add(userJson.get("r11"));
inventory_list.add(userJson.get("r12"));
inventory_list.add(userJson.get("r13"));
inventory_list.add(userJson.get("r14"));
inventory_list.add(userJson.get("r21"));
inventory_list.add(userJson.get("r22"));
inventory_list.add(userJson.get("r23"));
inventory_list.add(userJson.get("r31"));
inventory_list.add(userJson.get("r32"));
inventory_list.add(userJson.get("r41"));
resultJson.put("status", "ok");
resultJson.put("inventory", inventory_list);
return resultJson.toString();
}
} catch (Exception e) {
e.printStackTrace();
}
resultJson.put("status", "error");
return resultJson.toString();
}
public String mixItem(String username, String item1, String item2) {
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(username.toLowerCase());
if (item1.equalsIgnoreCase("r11") && item2.equalsIgnoreCase("r12")) {
long nItem1;
long nItem2;
nItem1 = ((Long) userJson.get(item1)).longValue();
nItem2 = ((Long) userJson.get(item2)).longValue();
if ((nItem1 > 2) && (nItem2 > 2)) {
nItem1 -= 3;
nItem2 -= 3;
// Ganti data di database
// userJson.put(item1,nItem1);
// userJson.put(item2,nItem2);
// userJson.put("r21", );
resultJson.put("status", "ok");
resultJson.put("item", "r21");
return resultJson.toString();
} else {
resultJson.put("status", "fail");
resultJson.put("description", "Your item count doesn't satisfy to make the new item");
return resultJson.toString();
}
} else if (item1.equalsIgnoreCase("r12") && item2.equalsIgnoreCase("r13")) {
long nItem1;
long nItem2;
nItem1 = ((Long) userJson.get(item1)).longValue();
nItem2 = ((Long) userJson.get(item2)).longValue();
if ((nItem1 > 2) && (nItem2 > 2)) {
nItem1 -= 3;
nItem2 -= 3;
// Ganti data di database
// userJson.put(item1,nItem1);
// userJson.put(item2,nItem2);
// userJson.put("r21", );
resultJson.put("status", "ok");
resultJson.put("item", "r22");
return resultJson.toString();
} else {
resultJson.put("status", "fail");
resultJson.put("description", "Your item count doesn't satisfy to make the new item");
return resultJson.toString();
}
} else if (item1.equalsIgnoreCase("r13") && item2.equalsIgnoreCase("r14")) {
long nItem1;
long nItem2;
nItem1 = ((Long) userJson.get(item1)).longValue();
nItem2 = ((Long) userJson.get(item2)).longValue();
if ((nItem1 > 2) && (nItem2 > 2)) {
nItem1 -= 3;
nItem2 -= 3;
// Ganti data di database
// userJson.put(item1,nItem1);
// userJson.put(item2,nItem2);
// userJson.put("r21", );
resultJson.put("status", "ok");
resultJson.put("item", "r23");
return resultJson.toString();
} else {
resultJson.put("status", "fail");
resultJson.put("description", "Your item count doesn't satisfy to make the new item");
return resultJson.toString();
}
} else if (item1.equalsIgnoreCase("r21") && item2.equalsIgnoreCase("r22")) {
long nItem1;
long nItem2;
nItem1 = ((Long) userJson.get(item1)).longValue();
nItem2 = ((Long) userJson.get(item2)).longValue();
if ((nItem1 > 2) && (nItem2 > 2)) {
nItem1 -= 3;
nItem2 -= 3;
// Ganti data di database
// userJson.put(item1,nItem1);
// userJson.put(item2,nItem2);
// userJson.put("r21", );
resultJson.put("status", "ok");
resultJson.put("item", "r31");
return resultJson.toString();
} else {
resultJson.put("status", "fail");
resultJson.put("description", "Your item count doesn't satisfy to make the new item");
return resultJson.toString();
}
} else if (item1.equalsIgnoreCase("r22") && item2.equalsIgnoreCase("r23")) {
long nItem1;
long nItem2;
nItem1 = ((Long) userJson.get(item1)).longValue();
nItem2 = ((Long) userJson.get(item2)).longValue();
if ((nItem1 > 2) && (nItem2 > 2)) {
nItem1 -= 3;
nItem2 -= 3;
// Ganti data di database
// userJson.put(item1,nItem1);
// userJson.put(item2,nItem2);
// userJson.put("r21", );
resultJson.put("status", "ok");
resultJson.put("item", "r32");
return resultJson.toString();
} else {
resultJson.put("status", "fail");
resultJson.put("description", "Your item count doesn't satisfy to make the new item");
return resultJson.toString();
}
} else if (item1.equalsIgnoreCase("r31") && item2.equalsIgnoreCase("r32")) {
long nItem1;
long nItem2;
nItem1 = ((Long) userJson.get(item1)).longValue();
nItem2 = ((Long) userJson.get(item2)).longValue();
if ((nItem1 > 2) && (nItem2 > 2)) {
nItem1 -= 3;
nItem2 -= 3;
// Ganti data di database
// userJson.put(item1,nItem1);
// userJson.put(item2,nItem2);
// userJson.put("r21", );
resultJson.put("status", "ok");
resultJson.put("item", "r41");
return resultJson.toString();
} else {
resultJson.put("status", "fail");
resultJson.put("description", "Your item count doesn't satisfy to make the new item");
return resultJson.toString();
}
} else {
resultJson.put("status", "fail");
resultJson.put("description", "Your mixed items doesn't match the recipe diagram");
return resultJson.toString();
}
}
} catch (Exception e) {
e.printStackTrace();
}
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"));
}
}
......@@ -10,14 +10,15 @@
*/
import java.io.*;
import java.net.*;
import java.util.UUID;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
class TCPServer
{
public static void main(String argv[]) throws Exception
{
ConnectionHandler CH = new ConnectionHandler();
CH.start();
}
class TCPServer {
public static void main(String argv[]) throws Exception {
// ConnectionHandler CH = new ConnectionHandler();
// CH.start();
}
}
......@@ -18,7 +18,7 @@ import org.json.simple.parser.JSONParser;
*/
public class crudDB {
public boolean signUp(String username, String password) throws IOException {
public String signUp(String username, String password) throws IOException {
JSONParser parser = new JSONParser();
try {
String basePath = new File("").getAbsolutePath();
......@@ -26,7 +26,10 @@ public class crudDB {
if (obj != null) {
JSONObject jsonFile = (JSONObject) obj;
if (jsonFile.containsKey(username.toLowerCase())) {
return false;
JSONObject resultJson = new JSONObject();
resultJson.put("status", "fail");
resultJson.put("description", "username exists");
return resultJson.toString();
} else {
JSONObject userObj = new JSONObject();
userObj.put("password", password);
......@@ -47,13 +50,17 @@ public class crudDB {
file.write(jsonFile.toJSONString());
file.flush();
file.close();
return true;
JSONObject resultJson = new JSONObject();
resultJson.put("status", "ok");
return resultJson.toString();
}
}
} catch (Exception e) {
e.printStackTrace();
}
return false;
JSONObject resultJson = new JSONObject();
resultJson.put("status", "error");
return resultJson.toString();
}
public void editAtt(String username, String att, String newValue) {
......@@ -79,7 +86,7 @@ public class crudDB {
}
}
public boolean login(String username, String password) {
public String login(String username, String password) {
JSONParser parser = new JSONParser();
try {
String basePath = new File("").getAbsolutePath();
......@@ -89,15 +96,33 @@ public class crudDB {
if (jsonFile.containsKey(username.toLowerCase())) {
JSONObject usrObj = (JSONObject) jsonFile.get(username.toLowerCase());
if (password.equals(usrObj.get("password").toString())) {
return true;
JSONObject resultJson = new JSONObject();
resultJson.put("status", "ok");
resultJson.put("token", "skill kripto");
resultJson.put("x", "");
resultJson.put("y", "");
long epoch=System.currentTimeMillis()/1000;
resultJson.put("time", epoch);
return resultJson.toString();
} else {
return false;
JSONObject resultJson = new JSONObject();
resultJson.put("status", "fail");
resultJson.put("description","username or password combination is not found");
return resultJson.toString();
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
return false;
JSONObject resultJson = new JSONObject();
resultJson.put("status", "error");
return resultJson.toString();
}
public static void main(String argv[]) throws Exception {
crudDB tes=new crudDB();
// System.out.println(tes.signUp("Kevin", "123"));
// System.out.println(tes.login("Kevin", "123"));
}
}
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