Commit 53801f2e authored by Khoirunnisa Afifah's avatar Khoirunnisa Afifah
Browse files

Merge branch 'master' of...

Merge branch 'master' of http://gitlab.informatika.org/sinambelajunita/IF3230-Tugas-Besar-Sister-2015
parents eb58e6eb de5f6247
......@@ -63,7 +63,7 @@ javadoc.splitindex=true
javadoc.use=true
javadoc.version=false
javadoc.windowtitle=
main.class=server.helper.Message
main.class=server.Server
manifest.file=manifest.mf
meta.inf.dir=${src.dir}/META-INF
mkdist.disabled=false
......
......@@ -34,9 +34,7 @@ public class ServerHandler{
InputStream in = s.getInputStream();
DataOutputStream out = new DataOutputStream(s.getOutputStream());
out.write(request.getBytes());
byte[] byt = new byte[4096];
int count = in.read(byt);
request = new String(byt);
if(response != null) {
// request = request.substring(0,count);
......
......@@ -85,19 +85,19 @@ public class Database {
// System.out.println(saveOfferToTradeBox("553b64d23c892b2d5b772c74", 4, 5, 6, 2));
addItemToInventory("553b6adc3c892b2edffa877c", 3);
addItemToInventory("553b6adc3c892b2edffa877c", 2);
addItemToInventory("553b6adc3c892b2edffa877c", 3);
addItemToInventory("553b6adc3c892b2edffa877c", 5);
addItemToInventory("553b6adc3c892b2edffa877c", 5);
addItemToInventory("553b6adc3c892b2edffa877c", 5);
// addItemToInventory("553b6adc3c892b2edffa877c", 3);
// addItemToInventory("553b6adc3c892b2edffa877c", 2);
// addItemToInventory("553b6adc3c892b2edffa877c", 3);
// addItemToInventory("553b6adc3c892b2edffa877c", 5);
// addItemToInventory("553b6adc3c892b2edffa877c", 5);
// addItemToInventory("553b6adc3c892b2edffa877c", 5);
//
// System.out.println(saveOfferToTradeBox("553b6adc3c892b2edffa877c", 4, 1, 5, 3));
// getTradebox("553b64d23c892b2d5b772c74");
// changeLocalTradeboxToFalse("553c36143c892b1bbf7723d6");
// System.out.println(isExist("sijc"));
System.out.println(getInventory("553cd35d3c892b2c6768f77d"));
}
/**
......
......@@ -13,8 +13,20 @@ package server.helper;
public class Map {
private String[][] map;
private String token;
private final String Honey = "R11";
private final String Herbs = "R12";
private final String Clay = "R13";
private final String Mineral = "R14";
private final String Potion = "R21";
private final String Incense = "R22";
private final String Gems = "R23";
private final String LifeElix = "R31";
private final String Mana = "R32";
private final String Stone = "R41";
public Map(String token){
map = Store.getMap();
this.token = token;
}
public String[][] getMap(){
......@@ -23,4 +35,72 @@ public class Map {
public String getToken(){
return token;
}
public void generateMap()
{
map[0][0] = Honey;
map[0][1] = Mineral;
map[0][2] = Herbs;
map[0][3] = Herbs;
map[0][4] = Mineral;
map[1][0] = Clay;
map[1][1] = Clay;
map[1][2] = Herbs;
map[1][3] = Mineral;
map[1][4] = Potion;
map[3][0] = Mineral;
map[3][1] = Herbs;
map[3][2] = Clay;
map[3][3] = Herbs;
map[3][4] = Incense;
map[4][0] = Mana;
map[4][1] = Gems;
map[4][2] = Potion;
map[4][3] = LifeElix;
map[4][4] = Mineral;
}
public int getItemIndex(int xPos, int yPos) {
int HONEY = 0;
int HERBS = 1;
int CLAY = 2;
int MINERAL = 3;
int POTION = 4;
int INCENSE = 5;
int GEMS = 6;
int LIFEELIXIR = 7;
int MANACRYSTAL = 8;
int PHILSTONE = 9;
if (map[xPos][yPos].equals(Honey)) {
return HONEY;
} else if (map[xPos][yPos].equals(Herbs)) {
return HERBS;
} else if (map[xPos][yPos].equals(Clay)) {
return CLAY;
} else if (map[xPos][yPos].equals(Mineral)) {
return MINERAL;
} else if (map[xPos][yPos].equals(Potion)) {
return POTION;
} else if (map[xPos][yPos].equals(Incense)) {
return INCENSE;
} else if (map[xPos][yPos].equals(Gems)) {
return GEMS;
} else if (map[xPos][yPos].equals(LifeElix)) {
return LIFEELIXIR;
} else if (map[xPos][yPos].equals(Mana)) {
return MANACRYSTAL;
} else if (map[xPos][yPos].equals(Stone)) {
return PHILSTONE;
} else {
return -1;
}
}
public void setEmpty(int x, int y)
{
map[x][y] = "-1";
}
}
......@@ -75,7 +75,10 @@ public class Message {
// System.out.println("Mix item: " + mixItem("553b6adc3c892b2edffa877c", 4, 5));
// System.out.println("Get inventory: " + getInventory("553b6adc3c892b2edffa877c"));
// System.out.println("Get tradebox: " + getTradeBox("553a1674179bf4356b7198d5"));
System.out.println(receive("{\"item\": 0, \"method\": \"findoffer\"}"));
// System.out.println(receive("{\"item\": 0, \"method\": \"findoffer\"}"));
// System.out.println(move("553cd35d3c892b2c6768f77d", 4, 4));
// System.out.println(receive("{\"method\":\"field\",\"token\":\"553cd35d3c892b2c6768f77d\"}"));
System.out.println(receive("{\"method\":\"inventory\",\"token\":\"553cd35d3c892b2c6768f77d\"}"));
}
public static String callURL(String myURL) {
......@@ -374,11 +377,7 @@ public class Message {
int honey = 0, herbs = 0, clay = 0, mineral = 0, potion = 0, incense = 0, gems = 0, elixir = 0, crystal = 0, stone = 0;
/* Get masing masing item dari database */
String inv = "[" + honey + "," + herbs + "," + clay + "," + mineral + "," + potion +
"," + incense + "," + gems + "," + elixir + "," + crystal + "," + stone + "]";
if(log)
System.out.println("Inventory = " + inv);
ArrayList<Integer> inv = Database.getInventory(token);
JSONObject jsonObj = new JSONObject();
try {
......@@ -531,8 +530,7 @@ public class Message {
JSONObject jsonPosition = Database.getCurrentPosition(token);
int x;
int y;
String [][] map = Store.getMap();
int item;
String response;
try {
......@@ -540,7 +538,7 @@ public class Message {
x = jsonPosition.getInt("x");
y = jsonPosition.getInt("y");
item = Store.getItemIndex(x, y);
item = Store.getMap(token).getItemIndex(x, y);
if(Database.addItemToInventory(token, item))
{
jsonObj.put("status", "ok");
......@@ -631,8 +629,8 @@ public class Message {
jsonObj = new JSONObject();
jsonObj.put("method", "findoffer");
jsonObj.put("item", itemId);
// listResponse = ServerHandler.broadcast(jsonObj.toString());
listResponse.add("{\"status\":\"ok\",\"offers\":[[3,2,4,3,false,\"553c35e53c892b1b9ddb1b86\"],[4,5,6,2,true,\"553c36143c892b1bbf7723d6\"],[4,1,5,3,false,\"553c372f3c892b1c27a0e924\"]]}");
listResponse = ServerHandler.broadcast(jsonObj.toString());
// listResponse.add("{\"status\":\"ok\",\"offers\":[[3,2,4,3,false,\"553c35e53c892b1b9ddb1b86\"],[4,5,6,2,true,\"553c36143c892b1bbf7723d6\"],[4,1,5,3,false,\"553c372f3c892b1c27a0e924\"]]}");
for (String temp : listResponse) {
JSONObject tempJSON = new JSONObject(temp);
if(tempJSON.getString("status").equals("ok")){
......
......@@ -6,6 +6,7 @@
package server.helper;
import java.util.ArrayList;
import java.util.List;
/**
......@@ -15,44 +16,7 @@ import java.util.List;
public class Store {
private static List<IPAddress> listServer;
private static final String [][] map = new String[5][5];
private static final String Honey = "R11";
private static final String Herbs = "R12";
private static final String Clay = "R13";
private static final String Mineral = "R14";
private static final String Potion = "R21";
private static final String Incense = "R22";
private static final String Gems = "R23";
private static final String LifeElix = "R31";
private static final String Mana = "R32";
private static final String Stone = "R41";
private static void generateMap()
{
map[0][0] = Honey;
map[0][1] = Mineral;
map[0][2] = Herbs;
map[0][3] = Herbs;
map[0][4] = Mineral;
map[1][0] = Clay;
map[1][1] = Clay;
map[1][2] = Herbs;
map[1][3] = Mineral;
map[1][4] = Potion;
map[3][0] = Mineral;
map[3][1] = Herbs;
map[3][2] = Clay;
map[3][3] = Herbs;
map[3][4] = Incense;
map[4][0] = Mana;
map[4][1] = Gems;
map[4][2] = Potion;
map[4][3] = LifeElix;
map[4][4] = Mineral;
}
private static List<Map> listMap = new ArrayList<>();
public static void Store(){
listServer = null;
......@@ -71,42 +35,6 @@ public class Store {
public static String [][]getMap(){
return map;
}
public static int getItemIndex(int xPos, int yPos)
{
int HONEY = 0;
int HERBS = 1;
int CLAY = 2;
int MINERAL = 3;
int POTION = 4;
int INCENSE = 5;
int GEMS = 6;
int LIFEELIXIR = 7;
int MANACRYSTAL = 8;
int PHILSTONE = 9;
if(map[xPos][yPos].equals(Honey))
return HONEY;
else if(map[xPos][yPos].equals(Herbs))
return HERBS;
else if(map[xPos][yPos].equals(Clay))
return CLAY;
else if(map[xPos][yPos].equals(Mineral))
return MINERAL;
else if(map[xPos][yPos].equals(Potion))
return POTION;
else if(map[xPos][yPos].equals(Incense))
return INCENSE;
else if(map[xPos][yPos].equals(Gems))
return GEMS;
else if(map[xPos][yPos].equals(LifeElix))
return LIFEELIXIR;
else if(map[xPos][yPos].equals(Mana))
return MANACRYSTAL;
else if(map[xPos][yPos].equals(Stone))
return PHILSTONE;
else
return -1;
}
/**
* Get an IPAddress from list of server in this class with index
* @param index
......@@ -122,4 +50,33 @@ public class Store {
public static List<IPAddress> getServers(){
return listServer;
}
/**
* Add map for user who login
* @param token
*/
public static void addMap(String token){
Map mp = new Map(token);
listMap.add(mp);
}
/**
* Get map by token
* @param token
* @return
*/
public static Map getMap(String token){
Map mp = null;
for(Map temp : listMap){
if(temp.getToken().equals(token)){
mp = temp;
break;
}
}
return mp;
}
public static void generateMap(String token){
Map mp = new Map(token);
mp.generateMap();
listMap.add(mp);
}
}
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