player.py 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. #!/usr/bin/python
  2. """
  3. Copyright 2011, Dipesh Amin <yaypunkrock@gmail.com>
  4. Copyright 2011, Stefan Beller <stefanbeller@googlemail.com>
  5. This file is part of tradey, a trading bot in the mana world
  6. see www.themanaworld.org
  7. """
  8. import copy
  9. class Item:
  10. pass
  11. class Player:
  12. def __init__(self, name):
  13. self.inventory = {}
  14. self.name = name
  15. self.id = 0
  16. self.sex = 0
  17. self.map = ""
  18. self.x = 0
  19. self.y = 0
  20. self.EXP = 0
  21. self.MONEY = 0
  22. self.WEIGHT = 0
  23. self.MaxWEIGHT = 0
  24. def find_inventory_index(self, item_id):
  25. for item in self.inventory:
  26. if item > 1:
  27. if self.inventory[item].itemId == item_id:
  28. return item
  29. return -10 # Not found - bug somewhere!
  30. def remove_item(self, index, amount):
  31. if index in self.inventory:
  32. self.inventory[index].amount -= amount
  33. if self.inventory[index].amount == 0:
  34. del self.inventory[index]
  35. def check_inventory(self, user_tree, sale_tree):
  36. # Check the inventory state.
  37. test_node = copy.deepcopy(self.inventory)
  38. for elem in sale_tree.root:
  39. item_found = False
  40. for item in test_node:
  41. if int(elem.get('itemId')) == test_node[item].itemId \
  42. and int(elem.get('amount')) <= test_node[item].amount:
  43. test_node[item].amount -= int(elem.get('amount'))
  44. if test_node[item].amount == 0:
  45. del test_node[item]
  46. item_found = True
  47. break
  48. if not item_found:
  49. return "Server and client inventory out of sync: missing %i (x%i)" % (int(elem.get('itemId')), int(elem.get('amount')))
  50. total_money = 0
  51. for user in user_tree.root:
  52. total_money += int(user.get('money'))
  53. if total_money != self.MONEY:
  54. return "Server and client money out of sync. Market: %s Player %s" % (total_money, self.MONEY)
  55. return 0
  56. if __name__ == '__main__':
  57. print "Do not run this file directly. Run main.py"