Commit 3cb0010b authored by opelhoward's avatar opelhoward
Browse files

kok bisa

parent 26a69ca0
This diff is collapsed.
......@@ -33,6 +33,7 @@ class Market:
@staticmethod
def add(item_id, raw_offers, ip, port):
Market.__time[item_id] = int(time.time())
for offer in raw_offers:
Market.__market.insert_one({'offered_item': offer[0], 'offered_amount': offer[1],
'demanded_item': offer[2], 'demanded_amount': offer[3],
......@@ -54,8 +55,10 @@ class Market:
@staticmethod
def exist(token):
return Market.__market.find({'offer_token': token}, {'availability': False}) != None
temp = Market.__market.find({'offer_token': token}).count()
print(temp)
return temp > 0
@staticmethod
def dirty(item):
return time.time() - Market.__time[item] > 30
\ No newline at end of file
return int(time.time()) - Market.__time[item] > 0
\ No newline at end of file
......@@ -21,7 +21,13 @@ def invoke(data):
print('')
print('invoking login')
response = invoke(json.dumps({'method': 'login', 'username': 'dummy', 'password': 'dummy'}))
response = invoke(json.dumps({'method': 'login', 'username': 'willy', 'password': 'willy'}))
token = response.get('token')
print(response)
print('')
print('invoking mixitem')
response = invoke(json.dumps({'method': 'login', 'username': 'willy', 'password': 'willy'}))
token = response.get('token')
print(response)
......@@ -76,10 +82,10 @@ print(response)
# print(response)
# PASSED
print('')
print('invoking sendfind')
response = invoke(json.dumps({"method": "sendfind","item": 2,"token":token}))
print(response)
# print('')
# print('invoking sendfind')
# response = invoke(json.dumps({"method": "sendfind","item": 2,"token":token}))
# print(response)
# PASSED
# print('')
......
......@@ -150,21 +150,19 @@ class ConnectionHandler:
# invoke mix item
def handle_mixitem(self,token,item1,item2):
user = User(token)
if (user.inventory.items[item1] > 0) and (user.inventory.items[item2] > 0):
mixitem = [[]]
for i in range(0, 10):
for j in range(0, 10):
mixitem[i][j] = -1
mixitem[0][1] = 4
mixitem[1][2] = 5
mixitem[2][3] = 6
mixitem[4][5] = 7
mixitem[5][6] = 8
mixitem[7][8] = 9
if (user.inventory.items[item1] > 2) and (user.inventory.items[item2] > 2):
mixitem = [[-1 for x in range(10)] for x in range(10)]
mixitem[0][1] = mixitem[1][0] = 4
mixitem[1][2] = mixitem[2][1] = 5
mixitem[2][3] = mixitem[3][2] = 6
mixitem[4][5] = mixitem[5][4] = 7
mixitem[5][6] = mixitem[6][5] = 8
mixitem[7][8] = mixitem[8][7] = 9
if mixitem[item1][item2] != -1:
user.inventory.items[mixitem[item1][item2]] += 1
user.inventory.items[item1] -= 1
user.inventory.items[item2] -= 1
user.inventory.items[item1] -= 3
user.inventory.items[item2] -= 3
user.save()
data = json.dumps({'status':'ok','item': mixitem[item1][item2]})
else:
data = json.dumps({'status': 'fail', 'description': 'Invalid Combination'})
......@@ -260,7 +258,7 @@ class ConnectionHandler:
thread_list = []
# untuk setiap server lain
if True:#Market.dirty(item):
if Market.dirty(item):
Market.clear(item)
for other_server in self._server._server_list:
host = other_server.get('ip')
......@@ -283,11 +281,13 @@ class ConnectionHandler:
for offer in offer_list:
count += 1
for my_offer in my_offers:
if offer.offer_token == my_offer.id:
print(str(offer.offer_token)+' '+str(my_offer.id))
if str(offer.offer_token) == str(my_offer.id):
delete_list.append(count)
delete_list.reverse()
for index in delete_list:
delete_list.pop(index)
offer_list.pop(index)
raw_offer_list = []
for offer in offer_list:
......@@ -343,7 +343,7 @@ class ConnectionHandler:
def handle_sendaccept(self, token, offer_token):
data = ''
if (not Market.exist(offer_token)):
if not Market.exist(offer_token):
data = json.dumps({'status': 'fail', 'description': 'offer cannot be found'})
else:
offer = Market.find(offer_token)
......@@ -375,17 +375,17 @@ class ConnectionHandler:
user.inventory.items[offer.demanded_id] -= offer.demanded_amount
user.inventory.items[offer.offered_id] += offer.offered_amount
user.save()
data = (json.dumps({'status':'ok'})).encode('ascii')
data = json.dumps({'status':'ok'})
#call db
elif (target_response.get('status') == 'fail'):
#fail
data = (json.dumps({'status':'fail','description':target_response.get('description')}).encode('ascii'))
else:
#error
data = (json.dumps({'status':'error'}).enocde('ascii'))
data = json.dumps({'status':'error'})
else:
data = json.dumps({'status': 'fail', 'description': 'offer is not available'})
self._socketio.write_to_socket(data)
self._socketio.write_to_socket(data.encode('ascii'))
# invoke map
......
......@@ -54,5 +54,5 @@ class SocketIO:
# tulis data ke socket
# be sure to handle socket exception
def write_to_socket(self,data):
recv = self._conn.send(data + b'')
self._conn.send(data + b'')
self._already_write = True
\ No newline at end of file
......@@ -15,7 +15,7 @@ class Location:
class User:
MOVE_TIME = 1
MOVE_TIME = 20
__users = MongoClient('localhost', 27017)['sister'].get_collection('users')
def __init__(self, id):
......
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