Commit 71fef9a8 authored by Tony's avatar Tony
Browse files

Update

parent ea6e36f3
......@@ -34,10 +34,13 @@ offer = db.offer
server = db.server
market = db.market
#ADDRESS SETUP
PORT = 8000
IP = socket.gethostbyname(socket.gethostname())
#IP = "100.100.101.16"
IP = 'localhost'
#IP = 'localhost'
#Variable for Locking Offer Accepting
sendAccept = True
accept = True
......@@ -170,7 +173,7 @@ def clientThread(conn):
selectedMap = mapQuery[random.randrange(nMap)]
#Randoming items
randomItems = [2,4,8,12]
randomItems = [4,6,8,10]
random.shuffle(randomItems)
inventory = randomItems + [0,0,0,0,0,0]
......@@ -387,29 +390,36 @@ def clientThread(conn):
receiveOK = False
if (receiveOK):
res = json.loads(response.decode('UTF-8'))
print(res)
if (res['status'] == 'ok'): #sendfind ok!
#update market
if (market.count() > 0):
market.remove({"offered_item": decoded['item'], "server.ip": serve['ip'], "server.port": serve['port']})
for off in res['offers']:
market.update(
{"offer_token": off[5]},
{
"offer_token": off[5],
"offered_item": off[0],
"n_offered_item": off[1],
"demanded_item": off[2],
"n_demanded_item": off[3],
"availability": True,
"server": {"ip": str(serve['ip']),"port": int(serve['port'])}
},
{"$upsert": True}
)
elif (res['status'] == 'error'): #sendfind error
print("Error response from server "+ str(serve['ip']) + ":" + str(serve['port']))
print("Receive OK")
loadingOK = True
try:
res = json.loads(response.decode('UTF-8'))
except ValueError:
loadingOK = False
print("ERROR LOADING JSON")
if (loadingOK):
if (res['status'] == 'ok'): #sendfind ok!
#update market
if (market.count() > 0):
market.remove({"offered_item": decoded['item'], "server.ip": serve['ip'], "server.port": serve['port']})
for off in res['offers']:
market.update(
{"offer_token": off[5]},
{
"offer_token": off[5],
"offered_item": off[0],
"n_offered_item": off[1],
"demanded_item": off[2],
"n_demanded_item": off[3],
"availability": True,
"server": {"ip": str(serve['ip']),"port": serve['port']}
},
upsert = True
)
elif (res['status'] == 'error'): #sendfind error
print("Error response from server "+ str(serve['ip']) + ":" + str(serve['port']))
sock_serv.close()
......@@ -446,7 +456,7 @@ def clientThread(conn):
elif (decoded['method'] == 'sendaccept'):
global sendAccept
while(not sendAccept):
print("WAITING")
print("WAITING SENDACCEPT")
sendAccept = False
userQuery = user.find_one({"_id": ObjectId(decoded['token'])})
......@@ -508,14 +518,28 @@ def clientThread(conn):
recvOK = False
if (recvOK):
print("Receive OK")
loadingOK = True
try:
res = json.loads(response.decode('UTF-8'))
except ValueError:
print("ERROR LOADING JSON")
reply = {"status": "error"}
loadingOK = False
if (loadingOK):
print("LOADING OK")
#Accept Status OK
if (res['status'] == "OK"):
#Update database
user.update({'_id': ObjectId(decoded['token'])},{"$inc": {'inventory.'+str(marketOff['demanded_item']): -marketOff['n_demanded_item'],
'inventory.'+str(marketOff['offered_item']): marketOff['n_offered_item']}})
market.remove({'offer_token': marketOff['offer_token']})
reply = {"status": "ok"}
#closing socket
sock_serv.close()
#Update database
user.update({'_id': ObjectId(decoded['token'])},{"$inc": {'inventory.'+str(marketOff['demanded_item']): -marketOff['n_demanded_item'],
'inventory.'+str(marketOff['offered_item']): marketOff['n_offered_item']}})
market.remove({'offer_token': marketOff['offer_token']})
reply = {"status": "ok"}
else:
reply = {"status": "error"}
else:
......@@ -544,14 +568,18 @@ def clientThread(conn):
reply = {"status": "fail", "description": "offer is already taken"}
else: #offer already taken
reply = {"status": "fail", "description": "offer is already taken"}
print("AYAM")
print(str(reply))
sendAccept = True
#Accept(server)
elif (decoded['method'] == 'accept'):
global accept
while(not accept):
print("WAITING")
print("WAITING ACCEPT")
accept = False
off = offer.find_one({"offer_token": decoded['offer_token']})
if (offer == None) : #offer token not found
......@@ -567,6 +595,7 @@ def clientThread(conn):
print("response: " + str(reply))
conn.sendall(bytes(json.dumps(reply), 'UTF-8'))
print("LEWAT A")
accept = True
break
......@@ -622,7 +651,7 @@ if __name__ == '__main__':
nMap = 2
#Join
#join()
join()
# LOAD MAP
if (map.count() == 0): # if map Database still empty
......
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