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

Somewhat done, mods for the move command, refactoring

parent 36b91198
......@@ -353,10 +353,13 @@ class MixItemCommand(Command):
db = client.get_database(game.game_class.Game.DB_NAME)
assert db.authenticate(game.game_class.Game.DB_USERNAME, game.game_class.Game.DB_PASSWORD)
print(item1)
print(item2)
request = [
pymongo.UpdateOne({
'username': player.username,
'item.id': item1.getId()
'items.id': item1.getId()
}, {
'$set': {
'items.$.count': item1.count()
......@@ -365,7 +368,7 @@ class MixItemCommand(Command):
pymongo.UpdateOne({
'username': player.username,
'item.id': item2.getId()
'items.id': item2.getId()
}, {
'$set': {
'items.$.count': item2.count()
......@@ -387,7 +390,7 @@ class MixItemCommand(Command):
else:
request.append(pymongo.UpdateOne({
'username': player.username,
'item.id': newItem.getId()
'items.id': newItem.getId()
}, {
'$inc': {
'items.$.count': newItem.count()
......@@ -897,10 +900,10 @@ class AcceptItemOfferCommand(Command):
sock.settimeout(3)
sock.connect((self.ip, self.port))
sock.sendAll(json.dumps({
sock.sendall(json.dumps({
'method': Command.METHOD_ACCEPT,
'offer_token': self.offer_token
}))
}).encode('utf-8'))
"""
This assumes that the server is going to send small-sized data.
......
......@@ -193,17 +193,16 @@ class Game(object):
assert db.authenticate(Game.DB_USERNAME, Game.DB_PASSWORD)
# deduct the item from the db
requestDict = [{
filterQuery, query = {
'username': username,
'items.id': offeredItem.getId()
}, {
'$inc': {
'items.$.count': -offeredItem.count()
}
}]
result = db.users.update_one(requestDict)
}
result = db.users.update_one(filterQuery, query)
assert result.modified_count == 1
newOffer = ItemOffer(username, demandedItem, offeredItem)
......@@ -222,9 +221,7 @@ class Game(object):
'count': offeredItem.count()
}
}
result = db.offers.insert_one(requestDict)
assert result.inserted_count == 1
db.offers.insert_one(requestDict)
def duplicateAcceptedOffer(self, demandedItem, offeredItem, offerId):
"""
......
......@@ -171,6 +171,9 @@ class Item(game.game_object.GameObject):
else:
raise TypeError('Invalid parameter(s)')
def __str__(self):
return "[Item] (id: {}, count: {})".format(super().getId(), self.__count)
class ItemOffer(game.game_object.GameObject):
"""docstring for ItemOffer"""
......
{
"name" : "Bandar Behari",
"width" : 4,
"height" : 4,
"map" :
[
["R13", "R13", "R13", "R13"],
["R13", "R13", "R14", "R14"],
["R13", "R14", "R14", "R14"],
["R13", "R14", "R14", "R14"]
]
}
\ No newline at end of file
......@@ -6,6 +6,7 @@ from .game_object import GameObject
from .game_class import Game, Location
class Player(GameObject):
DELAY_MOVE = 20
COUNT = 0
KEY_USERNAME = 'username'
......@@ -135,6 +136,8 @@ class Player(GameObject):
x : int = None,
y : int = None,
location : Location = None):
from math import sqrt, ceil, pow
moveTime = int(time.time())
if isinstance(x, int) and isinstance(y, int):
......@@ -144,10 +147,13 @@ class Player(GameObject):
if x == self.location.x and y == self.location.y:
raise ValueError("Position not changed")
else:
distance = sqrt(pow(self.location.x - x, 2) + pow(self.location.y - y, 2))
self.location.x = x
self.location.y = y
self.lastMoveTime = moveTime + 10
moveTime += 10
moveTime += int(ceil(Player.DELAY_MOVE * distance))
self.lastMoveTime = moveTime
self.alreadyFielded = False
else:
raise ValueError('Position out of bounds: ({},{})'.format(x, y))
......@@ -159,9 +165,13 @@ class Player(GameObject):
elif self.location == location:
raise ValueError("Position not changed")
else:
distance = sqrt(pow(self.location.x - location.x, 2) + pow(self.location.y - location.y, 2))
self.location = location
self.lastMoveTime = moveTime + 10
moveTime += 10
moveTime += int(ceil(Player.DELAY_MOVE * distance))
self.lastMoveTime = moveTime
self.alreadyFielded = False
else:
raise ValueError('Position out of bounds: ({},{})'.format(location.x, location.y))
else:
......@@ -257,15 +267,14 @@ class Player(GameObject):
for idx, item in enumerate(self.items):
if item.getId() == offer.getId():
if item.count() >= offer.count():
retval = True
else:
if item.count() < offer.count():
retval = False
else:
retval = True
break
else:
retval = False
print("[DEBUG]", retval)
return retval
def makeItemOffer(self, offeredItem):
......@@ -285,7 +294,6 @@ class Player(GameObject):
retval = False
else:
retval = True
break
else:
retval = False
......
......@@ -191,7 +191,6 @@ def scheduleBatchFindOffer(scheduler, addr, extip):
# repeat
scheduler.enter(60, 2, scheduleBatchFindOffer, argument=(scheduler, addr, extip,))
scheduler.run()
if __name__ == '__main__':
HOST, PORT = '0.0.0.0', int(sys.argv[1])
......
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