Skip to content
GitLab
Menu
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
Jonathan Sudibya
IF3230-Tugas-Besar-Sister-2015
Commits
0784c738
Commit
0784c738
authored
Apr 26, 2015
by
Jonathan Sudibya
Browse files
step 1 fixing up
parent
bad7827d
Changes
10
Hide whitespace changes
Inline
Side-by-side
src/server/TCPServer/lib/mongo-java-driver-2.13.1-javadoc.jar
0 → 100644
View file @
0784c738
File added
src/server/TCPServer/lib/mongo-java-driver-2.13.1-sources.jar
0 → 100644
View file @
0784c738
File added
src/server/TCPServer/lib/mongo-java-driver-2.13.1.jar
0 → 100644
View file @
0784c738
File added
src/server/TCPServer/nbproject/project.properties
View file @
0784c738
...
...
@@ -28,10 +28,16 @@ dist.jar=${dist.dir}/TCPServer.jar
dist.javadoc.dir
=
${dist.dir}/javadoc
excludes
=
file.reference.json-simple-1.1.1.jar
=
lib
\\
json-simple-1.1.1.jar
file.reference.mongo-java-driver-2.13.1-javadoc.jar
=
lib
\\
mongo-java-driver-2.13.1-javadoc.jar
file.reference.mongo-java-driver-2.13.1-sources.jar
=
lib
\\
mongo-java-driver-2.13.1-sources.jar
file.reference.mongo-java-driver-2.13.1.jar
=
lib
\\
mongo-java-driver-2.13.1.jar
includes
=
**
jar.compress
=
false
javac.classpath
=
\
${file.reference.json-simple-1.1.1.jar}
${file.reference.json-simple-1.1.1.jar}:
\
${file.reference.mongo-java-driver-2.13.1-javadoc.jar}:
\
${file.reference.mongo-java-driver-2.13.1-sources.jar}:
\
${file.reference.mongo-java-driver-2.13.1.jar}
# Space-separated list of extra javac options
javac.compilerargs
=
javac.deprecation
=
false
...
...
src/server/TCPServer/src/Controller/ConnectionHandler.java
View file @
0784c738
package
Controller
;
import
Model.Client
;
import
java.io.BufferedReader
;
import
java.io.DataOutputStream
;
...
...
@@ -10,6 +9,7 @@ import java.net.InetAddress;
import
java.net.ServerSocket
;
import
java.net.Socket
;
import
java.nio.charset.Charset
;
import
java.util.Map
;
import
java.util.Vector
;
import
java.util.logging.Level
;
import
java.util.logging.Logger
;
...
...
@@ -35,8 +35,11 @@ public class ConnectionHandler implements Runnable {
private
int
globalID
;
private
int
serverSocket
;
public
static
Map
<
String
,
String
>
tokenMap
;
public
ConnectionHandler
(){
threadName
=
"Connection-Handler"
;
active
=
true
;
globalID
=
0
;
serverSocket
=
6789
;
...
...
src/server/TCPServer/src/Controller/DBHandler.java
0 → 100644
View file @
0784c738
import
com.mongodb.BasicDBObject
;
import
com.mongodb.DB
;
import
com.mongodb.DBCollection
;
import
com.mongodb.DBCursor
;
import
com.mongodb.DBObject
;
import
com.mongodb.MongoClient
;
import
com.mongodb.WriteResult
;
import
java.net.UnknownHostException
;
import
java.util.ArrayList
;
import
java.util.List
;
import
java.util.Map
;
/*
* 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
DBHandler
{
public
static
MongoClient
mongo
;
public
static
DBCollection
users
;
public
static
DBCollection
offers
;
public
static
void
initialize
()
throws
UnknownHostException
{
mongo
=
new
MongoClient
(
"localhost"
,
27017
);
DB
db
=
mongo
.
getDB
(
"AlchemyDB"
);
users
=
db
.
getCollection
(
"users"
);
offers
=
db
.
getCollection
(
"offers"
);
}
public
static
String
getUserAttribute
(
String
username
,
String
key
)
{
BasicDBObject
obj
=
new
BasicDBObject
();
String
result
=
"error"
;
obj
.
put
(
"username"
,
username
);
DBCursor
cursor
=
users
.
find
(
obj
);
while
(
cursor
.
hasNext
())
{
BasicDBObject
res
=
new
BasicDBObject
();
res
=
(
BasicDBObject
)
cursor
.
next
();
result
=
res
.
get
(
key
).
toString
();
}
return
result
;
}
public
static
void
setUserAttribute
(
String
username
,
String
key
,
String
value
)
{
DBObject
query
=
new
BasicDBObject
(
"username"
,
username
);
DBObject
update
=
new
BasicDBObject
();
query
.
put
(
"username"
,
username
);
update
.
put
(
"$set"
,
new
BasicDBObject
(
key
,
value
));
WriteResult
result
=
users
.
update
(
query
,
update
);
}
public
static
void
signUp
(
String
username
,
String
password
)
{
BasicDBObject
document
=
new
BasicDBObject
();
document
.
put
(
"username"
,
username
);
document
.
put
(
"password"
,
password
);
document
.
put
(
"0"
,
0
);
document
.
put
(
"1"
,
0
);
document
.
put
(
"2"
,
0
);
document
.
put
(
"3"
,
0
);
document
.
put
(
"4"
,
0
);
document
.
put
(
"5"
,
0
);
document
.
put
(
"6"
,
0
);
document
.
put
(
"7"
,
0
);
document
.
put
(
"8"
,
0
);
document
.
put
(
"9"
,
0
);
/*document.put("r11",0);
document.put("r12",0);
document.put("r13",0);
document.put("r14",0);
document.put("r21",0);
document.put("r22",0);
document.put("r23",0);
document.put("r31",0);
document.put("r32",0);
document.put("r41",0);*/
document
.
put
(
"x"
,
0
);
document
.
put
(
"y"
,
0
);
users
.
insert
(
document
);
}
public
static
boolean
isUserExists
(
String
username
)
{
BasicDBObject
queryObj
=
new
BasicDBObject
();
queryObj
.
put
(
"username"
,
username
);
DBCursor
cursor
=
users
.
find
(
queryObj
);
if
(
cursor
.
size
()>
0
)
{
BasicDBObject
obj
=
new
BasicDBObject
();
obj
=
(
BasicDBObject
)
cursor
.
next
();
return
true
;
}
else
return
false
;
}
public
static
boolean
isUserValid
(
String
username
,
String
password
)
{
BasicDBObject
queryObj
=
new
BasicDBObject
();
queryObj
.
put
(
"username"
,
username
);
queryObj
.
put
(
"password"
,
password
);
DBCursor
cursor
=
users
.
find
(
queryObj
);
if
(
cursor
.
size
()>
0
)
{
return
true
;
}
else
return
false
;
}
public
static
int
[]
getInventory
(
String
username
)
{
int
[]
itemArr
=
new
int
[
10
];
BasicDBObject
queryObj
=
new
BasicDBObject
();
queryObj
.
put
(
"username"
,
username
);
DBCursor
cursor
=
users
.
find
(
queryObj
);
while
(
cursor
.
hasNext
())
{
BasicDBObject
curObj
=
new
BasicDBObject
();
curObj
=
(
BasicDBObject
)
cursor
.
next
();
itemArr
[
0
]
=
Integer
.
parseInt
(
curObj
.
get
(
"0"
).
toString
());
itemArr
[
1
]
=
Integer
.
parseInt
(
curObj
.
get
(
"1"
).
toString
());
itemArr
[
2
]
=
Integer
.
parseInt
(
curObj
.
get
(
"2"
).
toString
());
itemArr
[
3
]
=
Integer
.
parseInt
(
curObj
.
get
(
"3"
).
toString
());
itemArr
[
4
]
=
Integer
.
parseInt
(
curObj
.
get
(
"4"
).
toString
());
itemArr
[
5
]
=
Integer
.
parseInt
(
curObj
.
get
(
"5"
).
toString
());
itemArr
[
6
]
=
Integer
.
parseInt
(
curObj
.
get
(
"6"
).
toString
());
itemArr
[
7
]
=
Integer
.
parseInt
(
curObj
.
get
(
"7"
).
toString
());
itemArr
[
8
]
=
Integer
.
parseInt
(
curObj
.
get
(
"8"
).
toString
());
itemArr
[
9
]
=
Integer
.
parseInt
(
curObj
.
get
(
"9"
).
toString
());
/*
itemArr[0] = Integer.parseInt(curObj.get("r11").toString());
itemArr[1] = Integer.parseInt(curObj.get("r12").toString());
itemArr[2] = Integer.parseInt(curObj.get("r13").toString());
itemArr[3] = Integer.parseInt(curObj.get("r14").toString());
itemArr[4] = Integer.parseInt(curObj.get("r21").toString());
itemArr[5] = Integer.parseInt(curObj.get("r22").toString());
itemArr[6] = Integer.parseInt(curObj.get("r23").toString());
itemArr[7] = Integer.parseInt(curObj.get("r31").toString());
itemArr[8] = Integer.parseInt(curObj.get("r32").toString());
itemArr[9] = Integer.parseInt(curObj.get("r41").toString());*/
//for(int i=0; i<itemArr.length; i++) System.out.println(itemArr[i]);
}
return
itemArr
;
}
public
static
int
[]
getPosition
(
String
username
)
{
int
[]
pos
=
new
int
[
2
];
BasicDBObject
obj
=
new
BasicDBObject
();
obj
.
put
(
"username"
,
username
);
DBCursor
cursor
=
users
.
find
(
obj
);
while
(
cursor
.
hasNext
())
{
BasicDBObject
curobj
=
new
BasicDBObject
();
curobj
=
(
BasicDBObject
)
cursor
.
next
();
pos
[
0
]
=
Integer
.
parseInt
(
curobj
.
get
(
"x"
).
toString
());
pos
[
1
]
=
Integer
.
parseInt
(
curobj
.
get
(
"y"
).
toString
());
}
return
pos
;
}
public
static
String
mixItem
(
String
username
,
int
id1
,
int
id2
)
{
String
response
=
"error"
;
int
[]
inv
=
DBHandler
.
getInventory
(
username
);
DBObject
query
=
new
BasicDBObject
(
"username"
,
username
);
DBObject
update
=
new
BasicDBObject
();
if
(
id1
==
0
&&
id2
==
1
)
{
update
.
put
(
"$set"
,
new
BasicDBObject
(
"0"
,
inv
[
0
]-
3
).
append
(
"1"
,
inv
[
1
]-
3
).
append
(
"4"
,
inv
[
4
]+
1
));
WriteResult
result
=
users
.
update
(
query
,
update
);
response
=
result
.
toString
();
}
else
if
(
id1
==
1
&&
id2
==
2
)
{
update
.
put
(
"$set"
,
new
BasicDBObject
(
"1"
,
inv
[
1
]-
3
).
append
(
"2"
,
inv
[
2
]-
3
).
append
(
"5"
,
inv
[
5
]+
1
));
WriteResult
result
=
users
.
update
(
query
,
update
);
response
=
result
.
toString
();
}
else
if
(
id1
==
2
&&
id2
==
3
)
{
update
.
put
(
"$set"
,
new
BasicDBObject
(
"2"
,
inv
[
2
]-
3
).
append
(
"3"
,
inv
[
3
]-
3
).
append
(
"6"
,
inv
[
6
]+
1
));
WriteResult
result
=
users
.
update
(
query
,
update
);
response
=
result
.
toString
();
}
else
if
(
id1
==
4
&&
id2
==
5
)
{
update
.
put
(
"$set"
,
new
BasicDBObject
(
"4"
,
inv
[
4
]-
3
).
append
(
"5"
,
inv
[
5
]-
3
).
append
(
"7"
,
inv
[
7
]+
1
));
WriteResult
result
=
users
.
update
(
query
,
update
);
response
=
result
.
toString
();
}
else
if
(
id1
==
5
&&
id2
==
6
)
{
update
.
put
(
"$set"
,
new
BasicDBObject
(
"5"
,
inv
[
5
]-
3
).
append
(
"6"
,
inv
[
6
]-
3
).
append
(
"8"
,
inv
[
8
]+
1
));
WriteResult
result
=
users
.
update
(
query
,
update
);
response
=
result
.
toString
();
}
else
if
(
id1
==
7
&&
id2
==
8
)
{
update
.
put
(
"$set"
,
new
BasicDBObject
(
"7"
,
inv
[
7
]-
3
).
append
(
"8"
,
inv
[
8
]-
3
).
append
(
"9"
,
inv
[
9
]+
1
));
WriteResult
result
=
users
.
update
(
query
,
update
);
response
=
result
.
toString
();
}
return
response
;
}
public
static
String
getOfferAttribute
(
String
username
,
String
key
)
{
BasicDBObject
obj
=
new
BasicDBObject
();
String
result
=
"error"
;
obj
.
put
(
"username"
,
username
);
DBCursor
cursor
=
offers
.
find
(
obj
);
System
.
out
.
println
(
cursor
.
size
());
while
(
cursor
.
hasNext
())
{
BasicDBObject
res
=
new
BasicDBObject
();
res
=
(
BasicDBObject
)
cursor
.
next
();
result
=
res
.
get
(
key
).
toString
();
}
return
result
;
}
public
static
void
addOffer
(
String
username
,
String
token
,
int
offered
,
int
n1
,
int
demanded
,
int
n2
)
{
BasicDBObject
doc
=
new
BasicDBObject
();
doc
.
put
(
"username"
,
username
);
doc
.
put
(
"offer_token"
,
token
);
doc
.
put
(
"offered_item"
,
offered
);
doc
.
put
(
"n1"
,
n1
);
doc
.
put
(
"demanded_item"
,
demanded
);
doc
.
put
(
"n2"
,
n2
);
offers
.
insert
(
doc
);
}
}
src/server/TCPServer/src/EndpointMethod.java
deleted
100644 → 0
View file @
bad7827d
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
static
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
static
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"));
// }
}
src/server/TCPServer/src/Method/EndpointMethod.java
0 → 100644
View file @
0784c738
package
Method
;
import
Controller.DBHandler
;
import
Controller.ConnectionHandler
;
import
com.mongodb.BasicDBObject
;
import
com.mongodb.DBCursor
;
import
java.io.IOException
;
import
java.net.UnknownHostException
;
import
java.security.MessageDigest
;
import
java.security.NoSuchAlgorithmException
;
import
javax.xml.bind.annotation.adapters.HexBinaryAdapter
;
import
org.json.simple.JSONArray
;
import
org.json.simple.JSONObject
;
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
{
// Ingat username ditukar dengan token dari kontainer!!
//feed = username client + timestamp
public
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"
));