Commit 2fc7bb43 authored by Tony's avatar Tony
Browse files

Add Sendfind, FindOffer(not tested), SendAccept, FetchItem. Minus FindOffer,...

Add Sendfind, FindOffer(not tested), SendAccept, FetchItem. Minus FindOffer, Accept (Caller: Server), serverStatus(Caller: Tracker)
parent 090a0495
......@@ -60,12 +60,17 @@ def clientThread(conn):
#Receiving from client
json_input = conn.recv(4096)
if not json_input: #no data received
break
else:
decoded = json.loads(json_input.decode('UTF-8'))
print (decoded)
#ServerStatus(tracker)
#if (decoded['method'] == 'serverStatus'):
#Sign Up
#Signup(client)
if (decoded['method'] == "signup"):
userQuery = user.find_one({"username": decoded['username'].lower()})
if (userQuery == None): #user available
......@@ -95,7 +100,7 @@ def clientThread(conn):
else: #user exist
reply = {"status": "fail", "description": "username exists"}
#Login
#Login(client)
elif (decoded['method'] == "login"):
userQuery = user.find_one({"username": decoded['username'].lower()})
if (userQuery == None): #username not exist
......@@ -106,7 +111,7 @@ def clientThread(conn):
else: #password not match
reply = {"status": "fail", "description": "username/password combination is not found"}
#Inventory
#Inventory(client)
elif (decoded['method'] == 'inventory'):
userQuery = user.find_one({"_id": ObjectId(decoded['token'])})
if (userQuery == None): #Invalid Request, token mismatch
......@@ -114,7 +119,7 @@ def clientThread(conn):
else: #valid request
reply = {"status": "ok", "inventory": userQuery['inventory']}
#MixItem
#MixItem(client)
elif (decoded['method'] == 'mixitem'):
userQuery = user.find_one({"_id": ObjectId(decoded['token'])})
if (userQuery == None): #Invalid Request, token mismatch
......@@ -149,7 +154,7 @@ def clientThread(conn):
else:
reply = {"status": "fail", "description": "You have not enough items to combine"}
#Map
#Map(client)
elif (decoded['method'] == 'map'):
userQuery = user.find_one({"_id": ObjectId(decoded['token'])})
if (userQuery == None): #Invalid Request, token mismatch
......@@ -161,7 +166,7 @@ def clientThread(conn):
else: #map OK
reply = {"status": "ok", "name": mapQuery["name"], "width": mapQuery["width"], "height": mapQuery["height"]}
#Move
#Move(client)
elif (decoded['method'] == 'move'):
userQuery = user.find_one({"_id": ObjectId(decoded['token'])})
if (userQuery == None): #Invalid Request, token mismatch
......@@ -187,7 +192,7 @@ def clientThread(conn):
else: # out of bound target
reply = {"status": "fail", "description": "Your destination is out of your reach"}
#Field
#Field(client)
elif (decoded['method'] == 'field'):
userQuery = user.find_one({"_id": ObjectId(decoded['token'])})
if (userQuery == None): #Invalid Request, token mismatch
......@@ -205,7 +210,7 @@ def clientThread(conn):
user.update({"_id": userQuery['_id']},{"$set": {"collected": True},"$inc": {"inventory."+str(item): 1}})
reply = {"status": "ok", "item": item}
#Offer
#Offer(client)
elif (decoded['method'] == 'offer'):
userQuery = user.find_one({"_id": ObjectId(decoded['token'])})
if (userQuery == None): #Invalid Request, token mismatch
......@@ -225,7 +230,7 @@ def clientThread(conn):
"availability": True})
reply = {"status": "ok"}
#Tradebox
#Tradebox(client)
elif (decoded['method'] == 'tradebox'):
userQuery = user.find_one({"_id": ObjectId(decoded['token'])})
if (userQuery == None): #Invalid Request, token mismatch
......@@ -236,10 +241,79 @@ def clientThread(conn):
for off in allOffer:
singleOffer = [int(off['offered_item']),int(off['n_offered_item']),int(off['demanded_item']),int(off['n_demanded_item']),off['availability'],str(off['_id'])]
offers.append(singleOffer)
reply = {"status": "ok", "offers": offers}
#SendFind(client)
elif (decoded['method'] == 'sendfind'):
userQuery = user.find_one({"_id": ObjectId(decoded['token'])})
if (userQuery == None): #Invalid Request, token mismatch
reply = {"status": "error"}
else: #valid request
allOffer = offer.find({"user_token": {"$ne": decoded['token']}, "offered_item": decoded['item'], "availability": True})
offers = []
for off in allOffer:
singleOffer = [int(off['offered_item']),int(off['n_offered_item']),int(off['demanded_item']),int(off['n_demanded_item']),off['availability'],str(off['_id'])]
offers.append(singleOffer)
reply = {"status": "ok", "offers": offers}
#CancelOffer
#FindOffer(server)
elif (decoded['method'] == 'findoffer'):
allOffer = offer.find({"offered_item": decoded['item']})
offers = []
for off in allOffer:
singleOffer = [int(off['offered_item']),int(off['n_offered_item']),int(off['demanded_item']),int(off['n_demanded_item']),off['availability'],str(off['_id'])]
offers.append(singleOffer)
reply = {"status": "ok", "offers": offers}
conn.sendall(bytes(json.dumps(reply), 'UTF-8'))
break;
#SendAccept(client)
elif (decoded['method'] == 'sendaccept'):
userQuery = user.find_one({"_id": ObjectId(decoded['token'])})
if (userQuery == None): #Invalid Request, token mismatch
reply = {"status": "error"}
else: #valid request
off = offer.find_one({"_id": ObjectId(decoded['offer_token'])})
if (off == None): #offer token not found
reply = {"status": "fail", "description": "offer not found"}
else: #offer found
if (off['n_demanded_item'] > userQuery['inventory'][off['demanded_item']]): #insufficient item
reply = {"status": "fail", "description": "you have insufficient item"}
else: #sufficient item. Accept OK
#update inventory of acceptor
user.update({'_id': ObjectId(decoded['token'])},{"$inc": {'inventory.'+str(off['demanded_item']): -off['n_demanded_item'],
'inventory.'+str(off['offered_item']): off['n_offered_item']}})
#turn availability of offer to false
offer.update({'_id': ObjectId(decoded['offer_token'])}, {"$set": {'availability': False}})
reply = {"status": "ok"}
#Accept(server)
#elif (decoded['method'] == 'accept'):
#FetchItem(client)
elif (decoded['method'] == 'fetchitem'):
userQuery = user.find_one({"_id": ObjectId(decoded['token'])})
if (userQuery == None): #Invalid Request, token mismatch
reply = {"status": "error"}
else: #valid request
off = offer.find_one({"_id": ObjectId(decoded['offer_token'])})
if (off == None): #offer token not found
reply = {"status": "fail", "description": "offer not found"}
else: #offer found
if (off['availability']): #still available
reply = {"status": "fail", "description": "offer still available"}
else: #not available, ready to fetch
#update inventory of offerer
user.update({'_id': ObjectId(decoded['token'])},{"$inc": {'inventory.'+str(off['demanded_item']): off['n_demanded_item']}})
#remove offer
offer.remove({'_id': ObjectId(decoded['offer_token'])})
reply = {"status": "ok"}
#CancelOffer(client)
elif (decoded['method'] == 'canceloffer'):
userQuery = user.find_one({"_id": ObjectId(decoded['token'])})
if (userQuery == None): #Invalid Request, token mismatch
......@@ -250,11 +324,8 @@ def clientThread(conn):
reply = {"status": "fail", "description": "Offer not found"}
else: # Offer found
if (off['availability']): #offer still available, cancel OK
print("E")
user.update({"_id": userQuery['_id']},{"$inc": {"inventory."+str(off['offered_item']): off['n_offered_item']}})
print("F")
offer.remove({"_id": off['_id']})
print("G")
reply = {"status": "ok"}
else: # not available, offer already accepted (taken)
reply = {"status": "fail", "description": "Offer already accepted / taken"}
......
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