Commit daaf65d9 authored by Linda Sekawati's avatar Linda Sekawati
Browse files

Hampir Selese

parent e0e93ba1
......@@ -472,7 +472,8 @@ def playserver(portLaptop):
#update database for player_time
# database.playerCollection.update(kriteriaObject, {"$set":{"player_time":time}})
response = '{"status" : "ok", "time" : delta_milisecond}'
#response = '{"status" : "ok", "time" : delta_milisecond}'
response = '{"status" : "ok", "time" : 1}'
#else:
# response = '{"status" : "fail", "time" : "FAILURE MESSAGE HERE"}'
elif (method == 'field'):
......
......@@ -4,6 +4,7 @@ import hashlib
import pymongo
import threading
from pymongo import MongoClient
from random import randint
# informasi port dan socket
hostTracker = "167.205.32.46"
......@@ -253,61 +254,98 @@ def handler(a, c):
response = '{"status" : "fail", "description" : "still available"}'
else:
response = "{'status' : 'fail','description':'not exists'}"
elif (method == "move"):
#from client
token = dataJSON["token"]
posisiXnew = dataJSON["posisiX"]
posisiYnew = dataJSON["posisiY"]
elif (method == "map"):
#tokem from client
token = dataJSON['token']
#from database
user = database.playerCollection.find(token)
width = 5 #user["width"]
height = 5 #user["height"]
posisiX = user["posisiX"]
posisiY = user["posisiY"]
kriteriaObject = {"token" : token}
user = database.playerCollection.find_one(kriteriaObject)
username = user['username']
#datetime
current_time = datetime.datetime.now()
move_time = 1
delta_second = (move_time * (abs(posisiXnew-posisiX) + abs(posisiYnew-posisiY)))
delta_milisecond = delta_second * 1000
player_time = current_time + datetime.timedelta(seconds=delta_second)
#write json file
data={
'map' :
[
[randint(0, 9), randint(0, 9), randint(0, 9), randint(0, 9), randint(0, 9)],
[randint(0, 9), randint(0, 9), randint(0, 9), randint(0, 9), randint(0, 9)],
[randint(0, 9), randint(0, 9), randint(0, 9), randint(0, 9), randint(0, 9)],
[randint(0, 9), randint(0, 9), randint(0, 9), randint(0, 9), randint(0, 9)],
[randint(0, 9), randint(0, 9), randint(0, 9), randint(0, 9), randint(0, 9)]
],
'height' : 5,
'width' : 5,
'name' : username
}
map = json.dumps(data,
indent=4, separators=(',', ': '))
with open('map', 'w') as f:
json.dump(data, f,
indent=4, separators=(',', ': '))
response = '{"status" : "ok", "name" : "' + username + '", "width" : 5, "height" : 5}'
elif (method == 'move'):
#from client
token = dataJSON['token']
posisiXnew = dataJSON['posisiX']
posisiYnew = dataJSON['posisiY']
#from database
kriteriaObject = {'token' : token}
user = database.playerCollection.find_one(kriteriaObject)
width = 5
height = 5
posisiX = user['posisiX']
posisiY = user['posisiY']
#update database for player_time
database.playerCollection.update(kriteriaObject, {"$set" : {"player_time" : player_time}})
#comparing datetime
if (current_time > player_time):
if (current_time > time):
#comparing position
if ((posisiX >=0) and (posisiY >= 0) and (posisiXnew < 5) and (posisiYnew < 5)):
#datetime
current_time = datetime.datetime.now()
move_time = 1
delta_second = (move_time * (abs(posisiXnew-posisiX) + abs(posisiYnew-posisiY)))
delta_milisecond = delta_second * 1000
time = current_time + datetime.timedelta(seconds=delta_second)
#update database for player_time
database.playerCollection.update(kriteriaObject, {"$set":{"player_time":time}})
response = '{"status" : "ok", "time" : delta_milisecond}'
else:
response = '{"status" : "fail", "time" : "FAILURE MESSAGE HERE"}'
elif (method == "field"):
elif (method == 'field'):
#from client
token = dataJSON["token"]
posisiX = dataJSON["posisiX"]
posisiY = dataJSON["posisiY"]
token = dataJSON['token']
posisiX = dataJSON['posisiX']
posisiY = dataJSON['posisiY']
#Datetime
current_time = datetime.datetime.now()
kriteriaObject = {'token' : token}
user = database.playerCollection.find_one(kriteriaObject)
time = user['player_time']
#from map.json
with open('map', 'r') as f:
field_item = json.load(f)
item_here = field_item["map"][posisiY][posisiX]
#item
name_item_here = getItemName(item_here)
inventori = database.inventoriCollection.find_one(kriteriaObject)
n_item = inventori[name_item_here]
#update database inventory
kriteriaObject = {"token":token}
if (current_time >= player_time):
new_n_item += 1;
database.inventoriCollection.update(kriteriaObject, {"$set" : {getItemName(item_here) : new_n_item}})
response = '{"status" : "ok", "item" : 2}'
if (current_time >= time):
new_n_item = n_item+1;
database.inventoriCollection.update(kriteriaObject, {"$set":name_item_here:new_n_item}})
response = '{"status" : "ok", "item" : item_here}'
else:
response = '{"status" : "fail", "time" : "FAILURE MESSAGE HERE"}'
elif (method == "map"):
response = '{"status" : "ok", "name" : "Bandar Behari", "width" : 5, "height" : 5}'
response = '{"status" : "fail", "time" : "FAILURE MESSAGE HERE"}'
elif (method == "offer"):
token = dataJSON["token"]
offered_item = dataJSON["offered_item"]
......
{
"height": 5,
"name": "pulpen",
"map": [
[
9,
4,
6,
9,
2
],
[
2,
3,
7,
0,
1,
4
9,
8,
5
],
[
2,
0,
6,
7,
4
9,
8,
3
],
[
1,
3,
8,
7,
7,
9,
0,
2
],
[
0,
2,
1,
9,
1,
5
],
[
2,
9,
7,
8,
3
2,
7
]
],
"name": "pulpen",
"width": 5,
"height": 5
"width": 5
}
\ No newline at end of file
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