storage.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. Storage Access class by Fernanda Monteiro <crie.fernanda@gmail.com>
  8. """
  9. from utils import ItemDB
  10. from net.packet_out import chat
  11. from net.protocol import *
  12. from net.packet import *
  13. import time
  14. import copy
  15. import mutex
  16. class Storage:
  17. def __init__(self):
  18. self.storage = {}
  19. self.timer = 0
  20. self.mapserv = 0
  21. self.Open = mutex.mutex()
  22. def reset(self):
  23. self.Open.unlock()
  24. self.timer = 0
  25. def find_storage_index(self, item_id):
  26. for item in self.storage:
  27. if item > 1:
  28. if self.storage[item].itemId == item_id:
  29. return item
  30. return -10 # Not found - bug somewhere!
  31. def add_item(self, item):
  32. if not item.itemId or item.amount <= 0:
  33. return -10 # Not an item - something is messy
  34. index = self.find_storage_index(item.itemId)
  35. if ItemDB().getItem(item.itemId).type != 'equip-ammo' and 'equip':
  36. if index != -10:
  37. if (item.amount > MAX_AMOUNT - self.storage[index].amount):
  38. return -10
  39. self.storage[index].amount += item.amount
  40. return index
  41. index = len(self.storage)
  42. self.storage[index] = item
  43. self.storage[index].amount = item.amount
  44. return index
  45. def remove_item(self, index, amount):
  46. if index in self.storage:
  47. self.storage[index].amount -= amount
  48. if self.storage[index].amount == 0:
  49. del self.storage[index]
  50. def check_storage(self, stack_tree, delisted_tree):
  51. # Check the inventory state.
  52. test_node = copy.deepcopy(self.storage)
  53. for elem in stack_tree.root:
  54. item_found = False
  55. for item in test_node:
  56. if int(elem.get('itemId')) == test_node[item].itemId \
  57. and int(elem.get('amount')) <= test_node[item].amount:
  58. test_node[item].amount -= int(elem.get('amount'))
  59. if test_node[item].amount == 0:
  60. del test_node[item]
  61. item_found = True
  62. break
  63. if not item_found:
  64. return "Server and client storage out of sync."
  65. for elem in delisted_tree.root:
  66. item_found = False
  67. for item in test_node:
  68. if int(elem.get('itemId')) == test_node[item].itemId \
  69. and int(elem.get('amount')) <= test_node[item].amount:
  70. test_node[item].amount -= int(elem.get('amount'))
  71. if test_node[item].amount == 0:
  72. del test_node[item]
  73. item_found = True
  74. break
  75. if not item_found:
  76. return "Server and client storage out of sync."
  77. def storage_send(self, index, amount):
  78. packet = PacketOut(CMSG_MOVE_TO_STORAGE)
  79. packet.write_int16(index + inventory_offset)
  80. packet.write_int32(amount)
  81. self.mapserv.sendall(str(packet))
  82. return 0
  83. def storage_get(self, index, amount):
  84. packet = PacketOut(CMSG_MOVE_FROM_STORAGE)
  85. packet.write_int16(index + storage_offset)
  86. packet.write_int32(amount)
  87. self.mapserv.sendall(str(packet))
  88. return 0
  89. def storage_open(self):
  90. self.timer = time.time()
  91. self.mapserv.sendall(chat("@storage"))
  92. def storage_close(self):
  93. self.reset()
  94. self.mapserv.sendall(str(PacketOut(CMSG_CLOSE_STORAGE)))
  95. if __name__ == '__main__':
  96. print "Do not run this file directly. Run main.py"