Commit 559c0b6d authored by mamat-rahmat's avatar mamat-rahmat
Browse files

add to mainserver_thread.py

parent d9add63c
......@@ -254,9 +254,56 @@ def handler(a, c):
else:
response = "{'status' : 'fail','description':'not exists'}"
#elif (method == "move"):
#elif (method == "field"):
elif (method == "move"):
#from client
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"]
#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
if (current_time > player_time):
#comparing position
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"):
#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"}'
elif (method == "map"):
response = '{"status" : "ok", "name" : "Bandar Behari", "width" : 5, "height" : 5}'
......
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