Commit 249cf3fb authored by Linda Sekawati's avatar Linda Sekawati
Browse files

Map,Move,Field Masih Salah

parent 6c97bdc4
......@@ -405,19 +405,45 @@ def playserver(portLaptop):
response = "{'status' : 'fail', 'description' : 'offer is not available'}"
elif (method == 'map'):
#random number for width and height
width = randint(5,15)
height = width
#width = randint(5,15)
#height = width
#tokem from client
token = dataJSON['token']
kriteriaObject = {"token" : token}
username = dataJSON['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)],
[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)]
]
}
map = json.dumps(data,
indent=4, separators=(',', ': '))
with open('json_str', 'w') as f:
json.dump(data, f,
indent=4, separators=(',', ': '))
#for update
kriteria1Object = {"$set":{"width":width}}
kriteria2Object = {"$set":{"height":height}}
database.playerCollection.update(kriteriaObject,kriteria1Object)
database.playerCollection.update(kriteriaObject,kriteria2Object)
response = '{"status" : "ok", "name" : "Bandar Behari", "width" : ' + str(width) + ', "height" : ' + str(height) + '}'
#kriteria1Object = {"$set":{"width":width}}
#kriteria2Object = {"$set":{"height":height}}
#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']
......@@ -426,8 +452,8 @@ def playserver(portLaptop):
#from database
user = database.playerCollection.find(token)
width = user['width']
height = user['height']
width = 5 #user['width']
height = 5 #user['height']
posisiX = user['posisiX']
posisiY = user['posisiY']
......@@ -435,6 +461,7 @@ def playserver(portLaptop):
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
......@@ -443,16 +470,29 @@ def playserver(portLaptop):
#comparing datetime
if (current_time > player_time):
#comparing position
if ((posisiX >=0) and (posisiY >= 0) and (posisiXnew < width) and (posisiYnew < height)):
response = '{"status" : "ok", "time" : delta_second}'
if ((posisiX >=0) and (posisiY >= 0) and (posisiXnew < 5) and (posisiYnew < 5)):
response = '{"status" : "ok", "time" : delta_milisecond}'
else:
response = '{"status" : "fail", "time" : "FAILURE MESSAGE HERE"}'
elif (method == 'field'):
#
#if ((current_time > player_time) and ):
# response = '{"status" : "ok", "item" : 2}'
#else:
response = '{"status" : "fail", "time" : "FAILURE MESSAGE HERE"}'
#from client
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]
#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}'
else:
response = '{"status" : "fail", "time" : "FAILURE MESSAGE HERE"}'
else:
response = "{'status' : 'error'}"
......
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