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
sister-kering
IF3230-Tugas-Besar-Sister-2015
Commits
1ff7245d
Commit
1ff7245d
authored
Apr 21, 2015
by
Rafi Ramadhan
Browse files
update server
parent
c290cb32
Changes
5
Hide whitespace changes
Inline
Side-by-side
src/server/GrandQuestServer/src/grandquestserver/JSONHandler.java
deleted
100644 → 0
View file @
c290cb32
/*
* 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.
*/
package
grandquestserver
;
/**
*
* @author Rakhmatullah Yoga S
*/
public
class
JSONHandler
{
}
src/server/GrandQuestServer/src/grandquestserver/JSONParse.java
View file @
1ff7245d
...
...
@@ -25,16 +25,110 @@ public class JSONParse {
return
request
;
}
//Respon JSON untuk login
public
Map
loginJSON
()
{
Map
re
quest
=
new
LinkedHashMap
();
Map
re
sponse
=
new
LinkedHashMap
();
re
quest
.
put
(
"status"
,
"OK"
);
re
quest
.
put
(
"token"
,
"bss7d91234fef"
);
re
quest
.
put
(
"x"
,
0
);
re
quest
.
put
(
"y"
,
0
);
re
quest
.
put
(
"time"
,
1213131311
);
re
sponse
.
put
(
"status"
,
"OK"
);
re
sponse
.
put
(
"token"
,
"bss7d91234fef"
);
re
sponse
.
put
(
"x"
,
0
);
re
sponse
.
put
(
"y"
,
0
);
re
sponse
.
put
(
"time"
,
1213131311
);
return
request
;
return
response
;
}
//Respon JSON untuk signup
public
Map
signupJSON
()
{
Map
response
=
new
LinkedHashMap
();
response
.
put
(
"status"
,
"ok"
);
return
response
;
}
//Respon JSON untuk inventory
public
Map
invJSON
(
List
<
Integer
>
inv
)
{
Map
response
=
new
LinkedHashMap
();
response
.
put
(
"status"
,
"ok"
);
response
.
put
(
"inventory"
,
inv
);
return
response
;
}
//Respon JSON untuk mixitem
public
Map
mixItemJSON
(
int
item_id
)
{
Map
response
=
new
LinkedHashMap
();
response
.
put
(
"status"
,
"ok"
);
response
.
put
(
"item"
,
item_id
);
return
response
;
}
//Respon JSON untuk map
public
Map
mapJSON
(
String
location
,
Long
width
,
Long
height
)
{
Map
response
=
new
LinkedHashMap
();
response
.
put
(
"status"
,
"ok"
);
response
.
put
(
"name"
,
location
);
response
.
put
(
"width"
,
width
);
response
.
put
(
"height"
,
height
);
return
response
;
}
//Respon JSON untuk move
public
Map
moveJSON
(
String
token
,
Long
time
)
{
Map
response
=
new
LinkedHashMap
();
response
.
put
(
"status"
,
"ok"
);
response
.
put
(
"time"
,
time
);
return
response
;
}
//Respon JSON untuk field
public
Map
fieldJSON
(
String
token
,
int
item
)
{
Map
response
=
new
LinkedHashMap
();
response
.
put
(
"status"
,
"ok"
);
response
.
put
(
"item"
,
item
);
return
response
;
}
//Respon JSON untuk offer
public
Map
offerJSON
()
{
Map
response
=
new
LinkedHashMap
();
response
.
put
(
"status"
,
"ok"
);
return
response
;
}
//Respon JSON untuk offer
public
Map
tradeboxJSON
(
List
<
Object
>
offers
)
{
Map
response
=
new
LinkedHashMap
();
List
<
String
>
result
=
new
ArrayList
<>();
response
.
put
(
"status"
,
"ok"
);
response
.
put
(
"offers"
,
offers
);
return
response
;
}
//Respon JSON untuk sendfind
public
Map
sendfindJSON
(
List
<
Object
>
target
)
{
Map
response
=
new
LinkedHashMap
();
List
<
String
>
result
=
new
ArrayList
<>();
response
.
put
(
"status"
,
"ok"
);
response
.
put
(
"offers"
,
target
);
return
response
;
}
public
String
parseJSON
(
String
input
,
String
key
)
throws
ParseException
{
...
...
@@ -47,4 +141,15 @@ public class JSONParse {
return
response
;
}
public
Long
parseIntJSON
(
String
input
,
String
key
)
throws
ParseException
{
JSONParser
parser
=
new
JSONParser
();
Object
respond
=
parser
.
parse
(
input
);
JSONObject
obj
=
(
JSONObject
)
respond
;
Long
response
=
(
Long
)
obj
.
get
(
key
);
return
response
;
}
}
src/server/GrandQuestServer/src/grandquestserver/MethodHandler.java
0 → 100644
View file @
1ff7245d
/*
* 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.
*/
package
grandquestserver
;
import
java.util.ArrayList
;
import
java.util.LinkedHashMap
;
import
java.util.List
;
import
java.util.Map
;
import
org.json.simple.JSONValue
;
import
org.json.simple.parser.ParseException
;
/**
*
* @author Rakhmatullah Yoga S
*/
public
class
MethodHandler
{
public
String
Signup
(
String
input
)
{
JSONParse
parse
=
new
JSONParse
();
Map
response
=
new
LinkedHashMap
();
response
=
parse
.
signupJSON
();
return
JSONValue
.
toJSONString
(
response
);
}
public
String
Login
(
String
input
)
throws
ParseException
{
JSONParse
parse
=
new
JSONParse
();
Map
response
=
new
LinkedHashMap
();
if
(
parse
.
parseJSON
(
input
,
"username"
).
equals
(
"user"
))
{
response
=
parse
.
loginJSON
();
}
return
JSONValue
.
toJSONString
(
response
);
}
public
String
Inventory
(
String
input
)
{
List
<
Integer
>
inv
=
new
ArrayList
<>();
JSONParse
parse
=
new
JSONParse
();
Map
response
=
new
LinkedHashMap
();
inv
.
add
(
1
);
inv
.
add
(
2
);
inv
.
add
(
3
);
inv
.
add
(
4
);
inv
.
add
(
5
);
inv
.
add
(
1
);
inv
.
add
(
2
);
inv
.
add
(
3
);
inv
.
add
(
4
);
inv
.
add
(
5
);
response
=
parse
.
invJSON
(
inv
);
return
JSONValue
.
toJSONString
(
response
);
}
public
String
MixItem
(
String
input
)
throws
ParseException
{
JSONParse
parse
=
new
JSONParse
();
Map
response
=
new
LinkedHashMap
();
Long
item1
=
parse
.
parseIntJSON
(
input
,
"item1"
);
Long
item2
=
parse
.
parseIntJSON
(
input
,
"item2"
);
int
item_id
=
Combine
(
item1
,
item2
);
response
=
parse
.
mixItemJSON
(
item_id
);
System
.
out
.
println
(
JSONValue
.
toJSONString
(
response
));
return
JSONValue
.
toJSONString
(
response
);
}
public
int
Combine
(
Long
item1
,
Long
item2
)
{
int
id
=
0
;
return
id
;
}
public
String
Map
(
String
input
)
throws
ParseException
{
JSONParse
parse
=
new
JSONParse
();
Map
response
=
new
LinkedHashMap
();
String
location
=
parse
.
parseJSON
(
input
,
"name"
);
Long
width
=
parse
.
parseIntJSON
(
input
,
"width"
);
Long
height
=
parse
.
parseIntJSON
(
input
,
"height"
);
response
=
parse
.
mapJSON
(
location
,
width
,
height
);
System
.
out
.
println
(
JSONValue
.
toJSONString
(
response
));
return
JSONValue
.
toJSONString
(
response
);
}
public
String
Move
(
String
input
)
throws
ParseException
{
JSONParse
parse
=
new
JSONParse
();
Map
response
=
new
LinkedHashMap
();
String
token
=
parse
.
parseJSON
(
input
,
"token"
);
Long
x
=
parse
.
parseIntJSON
(
input
,
"x"
);
Long
y
=
parse
.
parseIntJSON
(
input
,
"y"
);
Long
time
=
6876781679
l
;
response
=
parse
.
moveJSON
(
token
,
time
);
System
.
out
.
println
(
JSONValue
.
toJSONString
(
response
));
return
JSONValue
.
toJSONString
(
response
);
}
public
String
Field
(
String
input
)
throws
ParseException
{
JSONParse
parse
=
new
JSONParse
();
Map
response
=
new
LinkedHashMap
();
String
token
=
parse
.
parseJSON
(
input
,
"token"
);
int
item
=
4
;
response
=
parse
.
fieldJSON
(
token
,
item
);
System
.
out
.
println
(
JSONValue
.
toJSONString
(
response
));
return
JSONValue
.
toJSONString
(
response
);
}
public
String
Offer
(
String
input
)
throws
ParseException
{
JSONParse
parse
=
new
JSONParse
();
Map
response
=
new
LinkedHashMap
();
response
=
parse
.
offerJSON
();
System
.
out
.
println
(
JSONValue
.
toJSONString
(
response
));
return
JSONValue
.
toJSONString
(
response
);
}
public
String
TradeBox
(
String
input
)
throws
ParseException
{
List
<
Object
>
offer
=
new
ArrayList
<>();
List
<
Object
>
temp
=
new
ArrayList
<>();
List
<
Object
>
offers
=
new
ArrayList
<>();
JSONParse
parse
=
new
JSONParse
();
Map
response
=
new
LinkedHashMap
();
offer
.
add
(
5
);
offer
.
add
(
3
);
offer
.
add
(
6
);
offer
.
add
(
1
);
offer
.
add
(
false
);
offer
.
add
(
"we23klf9vv4"
);
temp
=
copyList
(
offer
);
offer
.
clear
();
offers
.
add
(
temp
);
//offers.add(temp);
response
=
parse
.
tradeboxJSON
(
offers
);
System
.
out
.
println
(
JSONValue
.
toJSONString
(
response
));
return
JSONValue
.
toJSONString
(
response
);
}
public
List
<
Object
>
copyList
(
List
<
Object
>
target
)
{
List
<
Object
>
result
=
new
ArrayList
<>();
for
(
int
i
=
0
;
i
<
target
.
size
();
i
++)
{
result
.
add
(
target
.
get
(
i
));
}
return
result
;
}
public
String
sendFind
(
String
input
)
throws
ParseException
{
List
<
Object
>
offer
=
new
ArrayList
<>();
List
<
Object
>
temp
=
new
ArrayList
<>();
List
<
Object
>
offers
=
new
ArrayList
<>();
JSONParse
parse
=
new
JSONParse
();
Map
response
=
new
LinkedHashMap
();
Long
item_id
=
parse
.
parseIntJSON
(
input
,
"item"
);
System
.
out
.
println
(
item_id
);
if
(
item_id
==
2
)
{
offer
.
add
(
5
);
offer
.
add
(
3
);
offer
.
add
(
6
);
offer
.
add
(
1
);
offer
.
add
(
false
);
offer
.
add
(
"we23klf9vv4"
);
temp
=
copyList
(
offer
);
offer
.
clear
();
offers
.
add
(
temp
);
}
else
{
}
response
=
parse
.
sendfindJSON
(
offers
);
System
.
out
.
println
(
JSONValue
.
toJSONString
(
response
));
return
JSONValue
.
toJSONString
(
response
);
}
}
src/server/GrandQuestServer/src/grandquestserver/Offer.java
0 → 100644
View file @
1ff7245d
/*
* 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.
*/
package
grandquestserver
;
/**
*
* @author Lenovo
*/
public
class
Offer
{
public
int
offered
;
public
int
offered_num
;
public
int
demanded
;
public
int
demanded_num
;
public
boolean
availability
;
public
String
token
;
public
int
getOffered
()
{
return
offered
;
}
public
void
setOffered
(
int
offered
)
{
this
.
offered
=
offered
;
}
public
int
getOffered_num
()
{
return
offered_num
;
}
public
void
setOffered_num
(
int
offered_num
)
{
this
.
offered_num
=
offered_num
;
}
public
int
getDemanded
()
{
return
demanded
;
}
public
void
setDemanded
(
int
demanded
)
{
this
.
demanded
=
demanded
;
}
public
int
getDemanded_num
()
{
return
demanded_num
;
}
public
void
setDemanded_num
(
int
demanded_num
)
{
this
.
demanded_num
=
demanded_num
;
}
public
boolean
isAvailability
()
{
return
availability
;
}
public
void
setAvailability
(
boolean
availability
)
{
this
.
availability
=
availability
;
}
public
String
getToken
()
{
return
token
;
}
public
void
setToken
(
String
token
)
{
this
.
token
=
token
;
}
}
src/server/GrandQuestServer/src/grandquestserver/Server.java
View file @
1ff7245d
...
...
@@ -30,23 +30,12 @@ public class Server extends Thread {
serverSocket
.
setSoTimeout
(
10000
);
}
public
String
Login
(
String
input
)
throws
ParseException
{
JSONParse
parse
=
new
JSONParse
();
Map
response
=
new
LinkedHashMap
();
if
(
parse
.
parseJSON
(
input
,
"username"
).
equals
(
"user"
))
{
response
=
parse
.
loginJSON
();
}
return
JSONValue
.
toJSONString
(
response
);
}
@Override
public
void
run
()
{
String
response
;
JSONParse
parse
=
new
JSONParse
();
MethodHandler
handler
=
new
MethodHandler
();
while
(
true
)
{
try
...
...
@@ -64,13 +53,42 @@ public class Server extends Thread {
DataOutputStream
out
=
new
DataOutputStream
(
server
.
getOutputStream
());
if
(
parse
.
parseJSON
(
response
,
"method"
).
equals
(
"login"
))
{
out
.
writeUTF
(
Login
(
response
));
out
.
writeUTF
(
handler
.
Login
(
response
));
}
else
if
(
parse
.
parseJSON
(
response
,
"method"
).
equals
(
"signup"
))
{
out
.
writeUTF
(
handler
.
Signup
(
response
));
}
else
if
(
parse
.
parseJSON
(
response
,
"method"
).
equals
(
"inventory"
))
{
out
.
writeUTF
(
handler
.
Inventory
(
response
));
}
else
if
(
parse
.
parseJSON
(
response
,
"method"
).
equals
(
"mixitem"
))
{
out
.
writeUTF
(
handler
.
MixItem
(
response
));
}
else
if
(
parse
.
parseJSON
(
response
,
"method"
).
equals
(
"map"
))
{
out
.
writeUTF
(
handler
.
Map
(
response
));
}
else
if
(
parse
.
parseJSON
(
response
,
"method"
).
equals
(
"move"
))
{
out
.
writeUTF
(
handler
.
Move
(
response
));
}
else
if
(
parse
.
parseJSON
(
response
,
"method"
).
equals
(
"field"
))
{
out
.
writeUTF
(
handler
.
Field
(
response
));
}
else
if
(
parse
.
parseJSON
(
response
,
"method"
).
equals
(
"offer"
))
{
out
.
writeUTF
(
handler
.
Offer
(
response
));
}
else
if
(
parse
.
parseJSON
(
response
,
"method"
).
equals
(
"tradebox"
))
{
out
.
writeUTF
(
handler
.
TradeBox
(
response
));
}
else
if
(
parse
.
parseJSON
(
response
,
"method"
).
equals
(
"sendfind"
))
{
out
.
writeUTF
(
handler
.
sendFind
(
response
));
}
else
{
out
.
writeUTF
(
"Thank you for connecting to "
+
server
.
getLocalSocketAddress
()
+
"\nGoodbye!"
);
}
}
catch
(
ParseException
ex
)
{
Logger
.
getLogger
(
Server
.
class
.
getName
()).
log
(
Level
.
SEVERE
,
null
,
ex
);
}
...
...
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment