Commit 18d81f2d authored by mamat-rahmat's avatar mamat-rahmat
Browse files
parents 8da36854 8c07494b
......@@ -410,16 +410,15 @@ def playserver(portLaptop):
#tokem from client
token = dataJSON['token']
username = dataJSON['username']
#from database
kriteriaObject = {"token" : token}
user = database.playerCollection.find_one(kriteriaObject)
username = user['username']
#kriteriaObject = {"token" : token}
#write json file
data={
'name' : username,
'width' : 5,
'height' : 5,
'map' :
[
[randint(0, 9), randint(0, 9), randint(0, 9), randint(0, 9), randint(0, 9)],
......@@ -428,71 +427,73 @@ def playserver(portLaptop):
[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('json_str', 'w') as f:
with open('map', 'w') as f:
json.dump(data, f,
indent=4, separators=(',', ': '))
#for update
#kriteria1Object = {"$set":{"width":width}}
#kriteria2Object = {"$set":{"height":height}}
#kriteria1Object = {"$set":{"width":5}}
#kriteria2Object = {"$set":{"height":5}}
#database.playerCollection.update(kriteriaObject,kriteria1Object)
#database.playerCollection.update(kriteriaObject,kriteria2Object)
#response = '{"status" : "ok", "name" : ' + username + ', "width" : ' + str(width) + ', "height" : ' + str(height) + '}'
response = '{"status" : "ok", "name" : ' + username + ', "width" : 5, "height" : 5}'
elif (method == 'move'):
#from client
token = dataJSON['token']
posisiXnew = dataJSON['posisiX']
posisiYnew = dataJSON['posisiY']
#token = dataJSON['token']
#posisiXnew = dataJSON['posisiX']
#posisiYnew = dataJSON['posisiY']
#from database
user = database.playerCollection.find(token)
width = 5 #user['width']
height = 5 #user['height']
posisiX = user['posisiX']
posisiY = user['posisiY']
#user = database.playerCollection.find_one(token)
#width = 5 #user['width']
#height = 5 #user['height']
#posisiX = user['posisiX']
#posisiY = user['posisiY']
#comparing datetime
#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
player_time = current_time + datetime.timedelta(seconds=delta_second)
# 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":player_time}})
#comparing datetime
if (current_time > player_time):
#comparing position
if ((posisiX >=0) and (posisiY >= 0) and (posisiXnew < 5) and (posisiYnew < 5)):
# database.playerCollection.update(kriteriaObject, {"$set":{"player_time":time}})
response = '{"status" : "ok", "time" : delta_milisecond}'
else:
response = '{"status" : "fail", "time" : "FAILURE MESSAGE HERE"}'
#else:
# response = '{"status" : "fail", "time" : "FAILURE MESSAGE HERE"}'
elif (method == 'field'):
#from client
token = dataJSON['token']
posisiX = dataJSON['posisiX']
posisiY = dataJSON['posisiY']
#token = dataJSON['token']
#posisiX = dataJSON['posisiX']
#posisiY = dataJSON['posisiY']
#from map.json
with open('map', 'r') as f:
field_item = json.load(f)
item_here = field_item["map"][posisiY][posisiX]
#with open('map', 'r') as f:
# field_item = json.load(f)
#item_here = field_item["map"][posisiY][posisiX]
#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}})
#kriteriaObject = {"token":token}
#if (current_time >= time):
# new_n_item += 1;
# database.inventoriCollection.update(kriteriaObject, {"$set":{getItemName(item_here):new_n_item}})
response = '{"status" : "ok", "item" : 2}'
else:
response = '{"status" : "fail", "time" : "FAILURE MESSAGE HERE"}'
#else:
# response = '{"status" : "fail", "time" : "FAILURE MESSAGE HERE"}'
else:
response = "{'status' : 'error'}"
......
{
"map": [
[
9,
4,
6,
9,
2
],
[
2,
7,
0,
1,
4
],
[
2,
0,
6,
7,
4
],
[
1,
8,
7,
7,
2
],
[
0,
1,
7,
8,
3
]
],
"name": "pulpen",
"width": 5,
"height": 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