Commit 090a0495 authored by Tony's avatar Tony
Browse files

Update offer, tradebox, canceloffer

parent bed8426b
......@@ -27,7 +27,7 @@ client = MongoClient('localhost',27017)
db = client.gamesister
user = db.user
map = db.map
tradebox = db.tradebox
offer = db.offer
#Defining All Items in Inventory
honey = 0
......@@ -54,6 +54,7 @@ philosopherStone = 9
#Function for handling connections. This will be used to create threads
def clientThread(conn):
#infinite loop so that function do not terminate and thread do not end.
while True:
try:
#Receiving from client
......@@ -86,7 +87,8 @@ def clientThread(conn):
"inventory": inventory,
"position": {"x": 0, "y": 0},
"map_token" : str(selectedMap['_id']),
"time": int(0)}
"time": int(0),
"collected": True}
user.insert(data)
reply = {"status": "ok"}
......@@ -95,14 +97,15 @@ def clientThread(conn):
#Login
elif (decoded['method'] == "login"):
userQuery = user.find_one({"username": decoded['username'], "password": md5(decoded['password'])})
if (userQuery == None): #username || password not match
reply = {"status": "fail", "description": "username/password combination is not found"}
else: #username && password match
reply = {"status": "ok", "token": str(userQuery['_id']), "x": userQuery['position']['x'], "y": userQuery['position']['y'], "time": userQuery['time']}
userQuery = user.find_one({"username": decoded['username'].lower()})
if (userQuery == None): #username not exist
reply = {"status": "fail","description": "username doesn't exist, please register first"}
else: #username exist
if (userQuery['password'] == md5(decoded['password'])): #password match
reply = {"status": "ok", "token": str(userQuery['_id']), "x": userQuery['position']['x'], "y": userQuery['position']['y'], "time": userQuery['time']}
else: #password not match
reply = {"status": "fail", "description": "username/password combination is not found"}
#Inventory
elif (decoded['method'] == 'inventory'):
userQuery = user.find_one({"_id": ObjectId(decoded['token'])})
......@@ -145,18 +148,20 @@ def clientThread(conn):
reply = {"status":"fail", "description": "Item Combination is not valid"}
else:
reply = {"status": "fail", "description": "You have not enough items to combine"}
#Map
elif (decoded['method'] == 'map'):
userQuery = user.find_one({"_id": ObjectId(decoded['token'])})
if (userQuery == None): #Invalid Request, token mismatch
reply = {"status": "error"}
else:
else: #valid request
mapQuery = map.find_one({"_id": ObjectId(userQuery['map_token'])})
if(mapQuery == None): #map not found
reply = {"status": "error"}
else: #map OK
reply = {"status": "ok", "name": mapQuery["name"], "width": mapQuery["width"], "height": mapQuery["height"]}
#Move
elif (decoded['method'] == 'move'):
userQuery = user.find_one({"_id": ObjectId(decoded['token'])})
if (userQuery == None): #Invalid Request, token mismatch
......@@ -167,21 +172,93 @@ def clientThread(conn):
reply = {"status": "error"}
else: #map OK
timeNow = int(time.time())
if (userQuery['time'] > timeNow):
if (userQuery['time'] > timeNow): # still on the way
reply = {"status":"fail","decription": "You are still on the way"}
else:
if (userQuery['position']['x'] == decoded['x'] and userQuery['position']['y'] == decoded['y']):
else: # character has been arrived
if (userQuery['position']['x'] == decoded['x'] and userQuery['position']['y'] == decoded['y']): #move to same place
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):
decoded['x'] < mapQuery["height"] and decoded['y'] >= 0): #move OK
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:
user.update({"_id": userQuery['_id']}, {"$set": {"time": int(timeNow + 7 * jarak), "position.x": int(decoded['x']), "position.y": int(decoded['y']), "collected": False}})
reply = {"status": "ok", "time" : int(timeNow+7 * jarak)}
else: # out of bound target
reply = {"status": "fail", "description": "Your destination is out of your reach"}
#Field
elif (decoded['method'] == 'field'):
userQuery = user.find_one({"_id": ObjectId(decoded['token'])})
if (userQuery == None): #Invalid Request, token mismatch
reply = {"status": "error"}
else: #valid request
timeNow = int(time.time())
if (userQuery['time'] > timeNow): #still on the way
reply = {"status":"fail","decription": "You are still on the way"}
else:
if (userQuery['collected']): # item already collected
reply = {"status":"fail", "description": "Item is already collected"}
else: #collect granted
mapQuery = map.find_one({"_id": ObjectId(userQuery['map_token'])})
item = mapQuery['map'][userQuery['position']['y']][userQuery['position']['x']]
user.update({"_id": userQuery['_id']},{"$set": {"collected": True},"$inc": {"inventory."+str(item): 1}})
reply = {"status": "ok", "item": item}
#Offer
elif (decoded['method'] == 'offer'):
userQuery = user.find_one({"_id": ObjectId(decoded['token'])})
if (userQuery == None): #Invalid Request, token mismatch
reply = {"status": "error"}
else: #valid request
if (userQuery['inventory'][decoded['offered_item']] < decoded['n1']): #resource not enough
reply = {"status": "fail", "description": "insufficient resource"}
else:
#update inventory
user.update({"_id": userQuery['_id']}, {"$inc": {"inventory."+ str(decoded['offered_item']) : -decoded['n1']}})
#insert new offer
offer.insert({"user_token": str(userQuery['_id']),
"offered_item": decoded['offered_item'],
"n_offered_item": decoded['n1'],
"demanded_item": decoded['demanded_item'],
"n_demanded_item": decoded['n2'],
"availability": True})
reply = {"status": "ok"}
#Tradebox
elif (decoded['method'] == 'tradebox'):
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": decoded['token']})
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
elif (decoded['method'] == 'canceloffer'):
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 not found
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"}
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