utils.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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. from xml.etree.ElementTree import ElementTree
  9. from player import Item
  10. import time
  11. import mutex
  12. import threading
  13. from net.packet_out import *
  14. allowed_chars = "abcdefghijklmnoprstquvwxyzABCDEFGHIJKLMNOPRSTQUVWXYZ1234567890-_+=!@$%^&*();'\"<>,.?/~`| "
  15. # Process a recieved ip address.
  16. def parse_ip(a):
  17. return "%s.%s.%s.%s" % ((a % 256),((a >> 8) % 256),((a >> 16) % 256),((a >> 24) % 256))
  18. # Remove colors from a message
  19. def remove_colors(msg):
  20. if len(msg) > 2:
  21. for f in range(len(msg)-2):
  22. while (len(msg) > f + 2) and (msg[f] == "#")\
  23. and (msg[f+1] == "#"):
  24. msg = msg[0:f]+msg[f+3:]
  25. return msg
  26. # Encode string - used with 4144 shop compatibility.
  27. def encode_str(value, size):
  28. output = ''
  29. base = 94
  30. start = 33
  31. while value:
  32. output += chr(value % base + start)
  33. value /= base
  34. while len(output) < size:
  35. output += chr(start)
  36. return output
  37. class ItemDB:
  38. """
  39. A simple class to look up information from the items.xml file.
  40. """
  41. def __init__(self):
  42. print "Loading ItemDB"
  43. self.item_names = {}
  44. self.itemdb_file = ElementTree(file="data/items.xml")
  45. for item in self.itemdb_file.getroot():
  46. ## Item declaration
  47. if item.get('id'):
  48. item3 = item
  49. item_struct = Item()
  50. item_struct.name = item3.get('name')
  51. item_struct.weight = int(item3.get('weight', 0))
  52. if item3.get('type'):
  53. item_struct.type = item3.get('type')
  54. item_struct.description = item3.get('description')
  55. self.item_names[int(item3.get('id'))] = item_struct
  56. ## Import statement
  57. elif item.get('name'):
  58. file2 = ElementTree(file=item.get('name'))
  59. for item2 in file2.getroot():
  60. if item2.get('name'):
  61. file3 = ElementTree(file=item2.get('name'))
  62. for item3 in file3.getroot():
  63. item_struct = Item()
  64. item_struct.name = item3.get('name')
  65. item_struct.weight = int(item3.get('weight', 0))
  66. if item3.get('type'):
  67. item_struct.type = item3.get('type')
  68. item_struct.description = item3.get('description')
  69. self.item_names[int(item3.get('id'))] = item_struct
  70. def getItem(self, item_id):
  71. return self.item_names[item_id]
  72. def findId(self, name):
  73. for item_id in self.item_names:
  74. if self.item_names[item_id].name == name:
  75. return item_id
  76. return -10 #Not found
  77. def parse_mail_cmdargs(s):
  78. if len(s) < 3:
  79. return "", ""
  80. if s[0] == '"':
  81. end = s.find('"', 1)
  82. if end > 0:
  83. return s[1:end], s[end+2:]
  84. else:
  85. return "", ""
  86. else:
  87. end = s.find(' ')
  88. return s[:end], s[end+1:]
  89. class ItemLog:
  90. """ Writes all sales to a log file, for later processing."""
  91. def __init__(self):
  92. self.log_file = 'data/logs/sale.log'
  93. def add_item(self, item_id, amount, price, name):
  94. file_node = open(self.log_file, 'a')
  95. file_node.write(str(item_id)+" "+str(amount)+" "+str(price)+" "+str(time.time())+" "+name+"\n")
  96. file_node.close()
  97. class TraderState:
  98. """ Stores information regarding a trade request"""
  99. def __init__(self):
  100. self.Trading = mutex.mutex()
  101. self.item = 0
  102. self.money = 0
  103. self.complete = 0
  104. self.timer = 0
  105. def reset(self):
  106. self.Trading.unlock()
  107. self.item = 0
  108. self.complete = 0
  109. self.money = 0
  110. self.timer = 0
  111. class Broadcast:
  112. """Send a message to the server every 5 minutes to avoid a timeout."""
  113. def __init__(self):
  114. self.mapserv = 0
  115. self.Active = False
  116. self.Timer = 0
  117. self.shop_broadcast = threading.Thread(target=self.send_broadcast, args=())
  118. def send_broadcast(self):
  119. while self.Active:
  120. if (time.time() - self.Timer) > 60:
  121. self.mapserv.sendall(emote(193))
  122. self.Timer = time.time()
  123. #print "shop_broadcast"
  124. else:
  125. time.sleep(0.1)
  126. def start(self):
  127. self.Active = True
  128. self.shop_broadcast.start()
  129. def stop(self):
  130. if self.Active:
  131. self.Active = False
  132. self.shop_broadcast.join()
  133. if __name__ == '__main__':
  134. print "Do not run this file directly. Run main.py"