Commit 5d38d8f9 authored by Jonathan Sudibya's avatar Jonathan Sudibya
Browse files

merging servers

parent eee92573
......@@ -22,113 +22,17 @@ import org.json.simple.parser.ParseException;
*
* @author Kevin Zhong Local
*/
public class EndpointMethod {
public final class EndpointMethod {
// Ingat username ditukar dengan token dari kontainer!!
//feed = username client + timestamp
public String generateToken(String feed) throws NoSuchAlgorithmException {
public static String generateToken(String feed) throws NoSuchAlgorithmException {
MessageDigest md5 = MessageDigest.getInstance("MD5");
String token= (new HexBinaryAdapter()).marshal(md5.digest(feed.getBytes()));
return token;
}
public String getInventory(JSONObject obj) {
JSONObject res = new JSONObject();
res.put("status", "error");
String token = (String) obj.get("token");
String username = ConnectionHandler.tokenMap.get(token);
System.out.println("Usernameny adalah" +username);
if (username != null) {
JSONArray arr = new JSONArray();
int[] pos = DBHandler.getInventory(username);
for (int i = 0; i < pos.length; i++) {
arr.add(pos[i]);
}
res.put("status", "ok");
res.put("inventory", arr);
}
System.out.println(res.toString());
return res.toString();
}
public String mixItem(JSONObject obj) {
JSONObject res = new JSONObject();
res.put("status", "error");
String token = (String) obj.get("token");
String username = ConnectionHandler.tokenMap.get(token);
if (username != null) {
int[] inv = DBHandler.getInventory(username);
int id1 = (int) (long) obj.get("item1");
int id2 = (int) (long) obj.get("item2");
if (inv[id1] < 3 && inv[id2] < 3) {
res.put("status", "fail");
res.put("description", "You don't have enough items to do that");
} else {
String response = DBHandler.mixItem(username, id1, id2);
System.out.println(response);
if ("error".equals(response)) {
res.put("status", "fail");
res.put("description", "You cannot mix those items");
} else {
res.put("status", "ok");
int result = 0;
if (id1 == 0 && id2 == 1) {
result = 4;
} else if (id1 == 1 && id2 == 2) {
result = 5;
} else if (id1 == 2 && id2 == 3) {
result = 6;
} else if (id1 == 4 && id2 == 5) {
result = 7;
} else if (id1 == 5 && id2 == 6) {
result = 8;
} else if (id1 == 7 && id2 == 8) {
result = 9;
}
res.put("item", result);
}
}
}
System.out.println(res.toString());
return res.toString();
}
public String offer(JSONObject obj) throws NoSuchAlgorithmException {
JSONObject res = new JSONObject();
res.put("status", "error");
String token = (String) obj.get("token");
String username = ConnectionHandler.tokenMap.get(token);
if (username != null) {
String offered = Long.toString((long) obj.get("offered_item"));
String demanded = Long.toString((long) obj.get("demanded_item"));
int n1 = (int) (long) obj.get("n1");
int n2 = (int) (long) obj.get("n2");
//int sum1 = (int)(DBHandler.getUserAttribute(username,offered));
//int sum2 = (int)(DBHandler.getUserAttribute(username,demanded));
int sum1 = Integer.parseInt(DBHandler.getUserAttribute(username, offered));
int sum2 = Integer.parseInt(DBHandler.getUserAttribute(username, demanded));
if (n1 <= sum1) {
String timestamp = Long.toString(System.currentTimeMillis());
String offer_token=this.generateToken(username + timestamp);
BasicDBObject queryObj = new BasicDBObject();
queryObj.put("username", username);
DBCursor cursorUser = DBHandler.users.find(queryObj);
if (cursorUser.size() > 0) {
BasicDBObject userObj = new BasicDBObject();
userObj = (BasicDBObject) cursorUser.next();
userObj.put(offered, sum1 - n1);
DBHandler.addOffer(username, offer_token, Integer.parseInt(offered), n1, Integer.parseInt(demanded), n2);
res.put("status", "ok");
}
} else {
res.put("status", "fail");
res.put("description", "You don't have enough items to do that");
}
}
return res.toString();
}
public static String tradebox(JSONObject obj) throws IOException, ParseException {
JSONObject res = new JSONObject();
res.put("status", "error");
......@@ -217,5 +121,224 @@ public class EndpointMethod {
}
return res.toString();
}
public static String login(JSONObject obj) throws NoSuchAlgorithmException {
String username = (String) obj.get("username");
String password = (String) obj.get("password");
String token;
JSONObject res = new JSONObject();
res.put("status","error");
if(DBHandler.isUserValid(username,password)) {
int[] pos = DBHandler.getPosition(username);
String timestamp = Long.toString(System.currentTimeMillis()+5000);
token = EndpointMethod.generateToken(username+timestamp); System.out.println(token);
ConnectionHandler.tokenMap.put(token,username);
res.put("status","ok");
res.put("token",token);
res.put("x",pos[0]);
res.put("y",pos[1]);
res.put("time",timestamp);
}
else {
res.put("status","fail");
res.put("description", "username/password combination is not found");
}
return res.toString();
}
public static String signUp(JSONObject obj) {
String username = (String) obj.get("username");
String password = (String) obj.get("password");
JSONObject res = new JSONObject();
res.put("status","error");
if(DBHandler.isUserExists(username)) {
res.put("status","fail");
res.put("description","username exists");
}
else {
DBHandler.signUp(username, password);
res.put("status","ok");
}
return res.toString();
}
public static String getInventory(JSONObject obj) {
JSONObject res = new JSONObject();
res.put("status", "error");
String token = (String) obj.get("token");
String username = ConnectionHandler.tokenMap.get(token);
if(username!=null) {
JSONArray arr = new JSONArray();
int[] pos = DBHandler.getInventory(username);
for(int i=0; i<pos.length; i++) {
arr.add(pos[i]);
}
res.put("status","ok");
res.put("inventory",arr);
} System.out.println(res.toString());
return res.toString();
}
public static String mixItem(JSONObject obj) {
JSONObject res = new JSONObject();
res.put("status", "error");
String token = (String) obj.get("token");
String username = ConnectionHandler.tokenMap.get(token);
if(username!=null) {
int[] inv = DBHandler.getInventory(username);
int id1 = (int)(long) obj.get("item1");
int id2 = (int)(long) obj.get("item2");
//System.out.println(inv[id1] + inv[id2]);
if(inv[id1]<3 || inv[id2]<3) {
res.put("status", "fail");
res.put("description", "You don't have enough items to do that");
}
else {
String response = DBHandler.mixItem(username, id1, id2); System.out.println(response);
if("error".equals(response)) {
res.put("status", "fail");
res.put("description","You cannot mix those items");
}
else {
res.put("status","ok");
int result = 0;
if(id1==0 && id2==1) {
result = 4;
}
else if(id1==1 && id2==2) {
result = 5;
}
else if(id1==2 && id2==3) {
result = 6;
}
else if(id1==4 && id2==5) {
result = 7;
}
else if(id1==5 && id2==6) {
result = 8;
}
else if(id1==7 && id2==8) {
result = 9;
}
res.put("item",result);
}
}
}
System.out.println(res.toString());
return res.toString();
}
public static String getMap(JSONObject obj) {
JSONObject res = new JSONObject();
res.put("status", "error");
String token = (String) obj.get("token");
String username = ConnectionHandler.tokenMap.get(token);
if(username!=null) {
JSONArray arr = new JSONArray();
int[] pos = DBHandler.getInventory(username);
for(int i=0; i<pos.length; i++) {
arr.add(pos[i]);
}
//MapHandler.load();
res.put("status","ok");
res.put("name", MapHandler.name);
res.put("width", MapHandler.width);
res.put("height", MapHandler.height);
}
return res.toString();
}
public static String LetsMove(JSONObject obj) {
JSONObject res = new JSONObject();
res.put("status", "error");
String token = (String) obj.get("token");
String username = ConnectionHandler.tokenMap.get(token);
//System.out.println(username);
if(username!=null) {
//MapHandler.load();
int[] pos = DBHandler.getPosition(username);
int x = Integer.parseInt((String) obj.get("x"));
int y = Integer.parseInt((String) obj.get("y"));
if (x < MapHandler.height){
if ((x == pos[0])&&(y == pos[1])){
res.put("status", "fail");
res.put("description", "WHAT?! YOU ARE ALREADY HERE, THE HELL ?!!!");
}else{
res.put("status", "ok");
String timestamp = Long.toString(System.currentTimeMillis()+10000);
res.put("time", timestamp);
DBHandler.setUserAttribute(username, "x", Integer.toString(x));
DBHandler.setUserAttribute(username, "y", Integer.toString(y));
}
}else{
res.put("status", "fail");
res.put("description", "WHAT?! YOU CAN'T GO FAR, THE HELL ?!!!");
}
}
return res.toString();
}
public static String LetsField(JSONObject obj) {
JSONObject res = new JSONObject();
res.put("status", "error");
String token = (String) obj.get("token");
String username = ConnectionHandler.tokenMap.get(token);
//System.out.println(username);
if(username!=null) {
int[] pos = DBHandler.getPosition(username);
int x = pos[0];
int y = pos[1];
if (!"n".equals(MapHandler.matrix[x][y])){
res.put("status", "ok");
res.put("item", MapHandler.matrix[x][y]);
String values = DBHandler.getUserAttribute(username, MapHandler.matrix[x][y]);
System.out.println(values);
int value = Integer.parseInt(values);
value++;
values = Integer.toString(value);
DBHandler.setUserAttribute(username, MapHandler.matrix[x][y], values);
MapHandler.matrix[x][y] = "n";
}else{
res.put("status", "fail");
res.put("description", "SHORT TERM MEMORY!! IT'S TAKEN!!");
}
}
return res.toString();
}
public static String offer(JSONObject obj) throws NoSuchAlgorithmException {
JSONObject res = new JSONObject();
res.put("status", "error");
String token = (String) obj.get("token");
String tokenOffer;
String username = ConnectionHandler.tokenMap.get(token);
if(username!=null) {
String offered = Long.toString((long) obj.get("offered_item"));
String demanded = Long.toString((long) obj.get("demanded_item"));
int n1 = (int)(long) obj.get("n1");
int n2 = (int)(long) obj.get("n2");
//int sum1 = (int)(DBHandler.getUserAttribute(username,offered));
//int sum2 = (int)(DBHandler.getUserAttribute(username,demanded));
int sum1 = Integer.parseInt(DBHandler.getUserAttribute(username,offered));
int sum2 = Integer.parseInt(DBHandler.getUserAttribute(username,demanded));
if(n1<=sum1 && n2<=sum2) {
String timestamp = Long.toString(System.currentTimeMillis());
tokenOffer = EndpointMethod.generateToken(username+timestamp);
DBHandler.addOffer(username, tokenOffer, Integer.parseInt(offered), n1, Integer.parseInt(demanded), n2);
res.put("status", "ok");
}
else {
res.put("status", "fail");
res.put("description","You don't have enough items to do that");
}
}
return res.toString();
}
public static String statusError()
{
JSONObject error = new JSONObject();
error.put("status","error");
return error.toJSONString();
}
}
package Method;
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 MapHandler {
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(MapHandler.class.getName()).log(Level.SEVERE, null, ex);
} catch (ParseException ex) {
Logger.getLogger(MapHandler.class.getName()).log(Level.SEVERE, null, ex);
}
}
// public static void main(String argv[]) {
// MapHandler.load();
// System.out.println(MapHandler.name);
// System.out.println(MapHandler.height);
// System.out.println(MapHandler.width);
// for(int i=0; i<MapHandler.height; i++) {
// for(int j=0; j<MapHandler.width; j++) {
// System.out.print(MapHandler.matrix[i][j]+" ");
// }
// System.out.println("");
// }
// }
}
......@@ -5,7 +5,11 @@
*/
package Model;
import Controller.DBHandler;
import Method.EndpointMethod;
import java.security.NoSuchAlgorithmException;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.json.simple.JSONObject;
/**
......@@ -13,18 +17,86 @@ import org.json.simple.JSONObject;
* @author Jonathan
*/
public final class MessageProcessor {
private EndpointMethod EM;
private MessageProcessor()
public static String getReply(JSONObject data)
{
EM = new EndpointMethod();
}
public static JSONObject getReply(JSONObject data)
{
JSONObject ret = new JSONObject();
return ret;
String method = (String) data.get("method");
String response = null;
switch (method){
case "login" : {
try {
response = EndpointMethod.login(data);
} catch (NoSuchAlgorithmException ex) {
Logger.getLogger(Client.class.getName()).log(Level.SEVERE, null, ex);
}
break;
}
case "signup" : {
response = EndpointMethod.signUp(data);
break;
}
case "inventory" : {
response = EndpointMethod.getInventory(data);
break;
}
case "mixitem" : {
response = EndpointMethod.mixItem(data);
break;
}
case "map" : {
response = EndpointMethod.getMap(data);
break;
}
case "move" : {
response = EndpointMethod.LetsMove(data);
break;
}
case "field" : {
response = EndpointMethod.LetsField(data);
break;
}
case "offer" : {
try {
response = EndpointMethod.offer(data);
} catch (NoSuchAlgorithmException ex) {
Logger.getLogger(Client.class.getName()).log(Level.SEVERE, null, ex);
}
break;
}
case "tradebox" : {
System.out.println("BELUM!");
break;
}
case "sendfind" : {
System.out.println("BELUM!");
break;
}
case "findoffer" : {
System.out.println("BELUM!");
break;
}
case "sendaccept" : {
System.out.println("BELUM!");
break;
}
case "accept" : {
System.out.println("BELUM!");
break;
}
case "fetchitem" : {
System.out.println("BELUM!");
break;
}
case "canceloffer" : {
System.out.println("BELUM!");
break;
}
default : {
//senderror
response = EndpointMethod.statusError();
}
}
return response;
}
}
......@@ -10,6 +10,7 @@
*/
import Controller.ConnectionHandler;
import Controller.DBHandler;
import Method.EndpointMethod;
import java.io.*;
import java.net.*;
import java.util.Vector;
......
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