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