Commit cdf5e150 authored by Alvin Natawiguna's avatar Alvin Natawiguna
Browse files

More syntax fixes

parent 8c604f1b
......@@ -190,6 +190,7 @@ class JoinCommand(Command):
return super().getStatus()
class ServerStatusCommand(Command):
IGNORED_IP = ["192.168.", "169.254.", "127.0.0.1", "0.0.0.0"]
DB_COLLECTION_SERVER = 'activeServers'
def __init__(self, serverList):
......@@ -203,15 +204,19 @@ class ServerStatusCommand(Command):
assert db.authenticate(game.game_class.Game.DB_USERNAME, game.game_class.Game.DB_PASSWORD)
for server in self.servers:
db.activeServers.update_one({
'ip': server['ip'],
'port': server['port']
}, {
'$set': {
for ip in self.IGNORED_IP:
if ip in server['ip']:
break
else:
db.activeServers.update_one({
'ip': server['ip'],
'port': server['port']
}
}, True)
}, {
'$set': {
'ip': server['ip'],
'port': server['port']
}
}, True)
except Exception as e:
super().setError(True)
traceback.print_exc()
......@@ -709,7 +714,7 @@ class SendFindCommand(Command):
assert db.authenticate(game.game_class.Game.DB_USERNAME, game.game_class.Game.DB_PASSWORD)
for server in db.activeServers.find():
servers.append[(server['ip'], server['port'])]
servers.append((server['ip'], server['port']))
for server in servers:
try:
......@@ -857,14 +862,14 @@ class SendItemAcceptOfferCommand(Command):
demandedItem = game.item.Item(id = offer['demand']['id'], count=offer['demand']['count'])
if player.checkAcceptOfferedItem(demandedItem):
# accept the cached offer
command = AcceptItemOfferCommand(offer['offer_token'], offer['ip'], offer['port'])
command = AcceptItemOfferCommand(offer['id'], offer['ip'], offer['port'])
command.execute()
result = command.getStatus()
if result['status'] == "ok":
# create a duplicate in the offer
self.game.duplicateAcceptedOffer(demandedItem,
game.Item(offer['offer']['id'], offer['offer']['count']),
game.item.Item(offer['offer']['id'], offer['offer']['count']),
offer['id'])
player.acceptOffer(demandedItem)
......@@ -1095,7 +1100,6 @@ def findOffers(ip, port, item):
assert db.authenticate(game.game_class.Game.DB_USERNAME, game.game_class.Game.DB_PASSWORD)
for offer in result['offers']:
count += 1
db.cachedOffers.update_one({
'id': offer[5]
}, {
......
......@@ -251,7 +251,7 @@ class Game(object):
assert result.inserted_count == 1
# insert the similar value to the offers
self.offers.append(game.ItemOffer('DUPLICATE_EXTERNAL', demandedItem, offeredItem, offerId, False))
self.offers.append(ItemOffer('DUPLICATE_EXTERNAL', demandedItem, offeredItem, offerId, False))
def deleteCachedOffer(self, offerId):
with pymongo.MongoClient() as client:
......
/** activeServers indexes **/
db.getCollection("activeServers").ensureIndex({
"_id": NumberInt(1)
},[
]);
/** cachedOffers indexes **/
db.getCollection("cachedOffers").ensureIndex({
"_id": NumberInt(1)
},[
]);
/** offers indexes **/
db.getCollection("offers").ensureIndex({
"_id": NumberInt(1)
},[
]);
/** users indexes **/
db.getCollection("users").ensureIndex({
"_id": NumberInt(1)
},[
]);
/** activeServers records **/
db.getCollection("activeServers").insert({
'ip': '167.205.12.34',
'port': 8000
})
/** cachedOffers records **/
db.getCollection("cachedOffers").insert({
"_id": ObjectId("553c3511752eca8817000029"),
"id": "dummy",
"offer": {
"id": NumberInt(1),
"count": NumberInt(3)
},
"demand": {
"id": NumberInt(2),
"count": NumberInt(2)
},
"ip": "167.205.12.34",
"port": NumberInt(8000)
});
/** offers records **/
db.getCollection("offers").insert({
"_id": ObjectId("553bf0d5752eca9817000029"),
"available": true,
"id": "ca81d7bab487e72c0741cfc7fb8495fc",
"username": "alvin",
"demand": {
"id": NumberInt(1),
"count": NumberInt(2)
},
"offer": {
"id": NumberInt(2),
"count": NumberInt(2)
}
});
/** system.indexes records **/
db.getCollection("system.indexes").insert({
"v": NumberInt(1),
"key": {
"_id": NumberInt(1)
},
"name": "_id_",
"ns": "if3230-sister.users"
});
db.getCollection("system.indexes").insert({
"v": NumberInt(1),
"key": {
"_id": NumberInt(1)
},
"name": "_id_",
"ns": "if3230-sister.activeServers"
});
db.getCollection("system.indexes").insert({
"v": NumberInt(1),
"key": {
"_id": NumberInt(1)
},
"name": "_id_",
"ns": "if3230-sister.offers"
});
db.getCollection("system.indexes").insert({
"v": NumberInt(1),
"key": {
"_id": NumberInt(1)
},
"name": "_id_",
"ns": "if3230-sister.cachedOffers"
});
/** users records **/
db.getCollection("users").insert({
"_id": ObjectId("5535e331752eca0c1d000029"),
"username": "alvin",
"password": "$2a$10$H9tj6UFPBC6F9UbjielMUe.AWQbJek9bIouuuoCuk7/nlv92xpUce",
"currentLocation": {
"x": 0,
"y": 0
},
"items": [
{
"id": NumberInt(0),
"count": NumberInt(2)
},
{
"id": NumberInt(1),
"count": NumberInt(2)
},
{
"count": NumberInt(4),
"id": NumberInt(3)
}
]
});
......@@ -105,7 +105,11 @@ def connectTracker(externalAddr, trackerAddr = (command.JoinCommand.DEFAULT_TRAC
if updateActiveServersStatus['status'] == command.Command.RESULT_OK:
for server in status['value']:
print("[TRACKER] New active server: ({}, {})".format(server['ip'], server['port']))
for ip in command.ServerStatusCommand.IGNORED_IP:
if ip in server['ip']:
break
else:
print("[TRACKER] New active server: ({}, {})".format(server['ip'], server['port']))
else:
traceback.print_exc()
else:
......@@ -117,13 +121,11 @@ def connectTracker(externalAddr, trackerAddr = (command.JoinCommand.DEFAULT_TRAC
def getExternalIp():
import netifaces as ni
ignored_ip = ["192.168.", "169.254.", "127.0.0.1"]
for interface in ni.interfaces():
interfaceInfo = ni.ifaddresses(interface)
if ni.AF_INET in interfaceInfo:
ip = interfaceInfo[ni.AF_INET][0]['addr']
for ignore in ignored_ip:
for ignore in command.ServerStatusCommand.IGNORED_IP:
if ignore in ip:
break
else:
......@@ -171,8 +173,8 @@ def scheduleBatchFindOffer(scheduler, externalAddr):
print ("[BATCH_OFFER_FETCH] Cannot fetch offers from ({}, {}): {}"
.format(server[0], server[1], str(e)))
db.activeServers.delete_one({
'ip': server['ip'],
'port': server['port']
'ip': server[0],
'port': server[1]
})
servers[:] = [tup for tup in servers if tup == server]
except Exception as e:
......@@ -198,6 +200,7 @@ if __name__ == '__main__':
HOST, PORT = '0.0.0.0', int(sys.argv[1])
extip = getExternalIp()
command.ServerStatusCommand.IGNORED_IP.append(extip)
connectTracker((extip, PORT))
server = ThreadedTcpServer((HOST, PORT), RequestHandler)
......
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