Commit bed8426b authored by Tony's avatar Tony
Browse files

Fixing signUp, adding move method

parent 75a1457c
{
"name" : "Taman Sari",
"width" : 5,
"height" : 5,
"width" : 4,
"height" : 4,
"map" :
[
["0", "1", "2", "3","0"],
["0", "1", "2", "3","0"],
["0", "1", "2", "3","0"],
["0", "1", "2", "3","0"],
["0", "1", "2", "3","0"]
["0", "1", "2", "3"],
["0", "1", "2", "3"],
["0", "1", "2", "3"],
["0", "1", "2", "3"]
]
}
\ No newline at end of file
......@@ -76,7 +76,7 @@ def clientThread(conn):
selectedMap = mapQuery[i]
#Randoming items
randomItems = [1,2,3,4]
randomItems = [2,4,8,12]
random.shuffle(randomItems)
inventory = randomItems + [0,0,0,0,0,0]
......@@ -85,7 +85,8 @@ def clientThread(conn):
"password": md5(decoded['password']),
"inventory": inventory,
"position": {"x": 0, "y": 0},
"map_token" : str(selectedMap['_id'])}
"map_token" : str(selectedMap['_id']),
"time": int(0)}
user.insert(data)
reply = {"status": "ok"}
......@@ -100,8 +101,7 @@ def clientThread(conn):
reply = {"status": "fail", "description": "username/password combination is not found"}
else: #username && password match
timestamp = int(time.time())
reply = {"status": "ok", "token": str(userQuery['_id']), "x": userQuery['position']['x'], "y": userQuery['position']['y'], "time": timestamp}
reply = {"status": "ok", "token": str(userQuery['_id']), "x": userQuery['position']['x'], "y": userQuery['position']['y'], "time": userQuery['time']}
#Inventory
elif (decoded['method'] == 'inventory'):
......@@ -152,11 +152,36 @@ def clientThread(conn):
reply = {"status": "error"}
else:
mapQuery = map.find_one({"_id": ObjectId(userQuery['map_token'])})
if(mapQuery == None):
if(mapQuery == None): #map not found
reply = {"status": "error"}
else:
else: #map OK
reply = {"status": "ok", "name": mapQuery["name"], "width": mapQuery["width"], "height": mapQuery["height"]}
elif (decoded['method'] == 'move'):
userQuery = user.find_one({"_id": ObjectId(decoded['token'])})
if (userQuery == None): #Invalid Request, token mismatch
reply = {"status": "error"}
else:
mapQuery = map.find_one({"_id": ObjectId(userQuery['map_token'])})
if (mapQuery == None): #map not found
reply = {"status": "error"}
else: #map OK
timeNow = int(time.time())
if (userQuery['time'] > timeNow):
reply = {"status":"fail","decription": "You are still on the way"}
else:
if (userQuery['position']['x'] == decoded['x'] and userQuery['position']['y'] == decoded['y']):
reply = {"status": "fail", "description": "You are already here"}
else:
if (decoded['x'] < mapQuery["width"] and decoded['x'] >= 0 and
decoded['x'] < mapQuery["height"] and decoded['y'] >= 0):
jarak = abs(decoded['x'] - userQuery['position']['x']) + abs(decoded['y'] - userQuery['position']['y'])
user.update({"_id": userQuery['_id']}, {"$set": {"time": int(timeNow + 10 * jarak), "position.x": int(decoded['x']), "position.y": int(decoded['y'])}})
reply = {"status": "ok", "time" : int(timeNow+10 * jarak)}
else:
reply = {"status": "fail", "description": "Your destination is out of your reach"}
print(reply)
conn.sendall(bytes(json.dumps(reply), 'UTF-8'))
......
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