tradey.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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 time
  9. import os
  10. import xml.dom.minidom
  11. from subprocess import call
  12. from xml.etree.ElementTree import *
  13. def clean_xml(parse):
  14. data = ''
  15. pos_start = 0
  16. while parse.find('<', pos_start) != -1:
  17. pos_start = parse.find('<', pos_start)
  18. pos_end = parse.find('>', pos_start+1)
  19. data += parse[pos_start:pos_end+1]
  20. pos_start = pos_end
  21. return data
  22. class UserTree:
  23. def __init__(self):
  24. self.tree = ElementTree(file="data/user.xml")
  25. self.root = self.tree.getroot()
  26. def add_user(self, name, stalls, al):
  27. if self.get_user(name) == -10:
  28. user = SubElement(self.root, "user")
  29. user.set("name", name)
  30. user.set("stalls", str(stalls))
  31. user.set("used_stalls", str(0))
  32. user.set("money", str(0))
  33. user.set("id", str(0))
  34. user.set("accesslevel", str(al))
  35. self.save()
  36. def get_user(self, name):
  37. for elem in self.root:
  38. if elem.get("name") == name:
  39. return elem
  40. return -10
  41. def remove_user(self, name):
  42. for elem in self.root:
  43. if elem.get("name") == name:
  44. self.root.remove(elem)
  45. self.save()
  46. return 1
  47. return -10
  48. def save(self):
  49. # Be sure to call save() after any changes to the tree.
  50. f = open('data/user.xml', 'w')
  51. dom = xml.dom.minidom.parseString(clean_xml(tostring(self.root)))
  52. f.write(dom.toprettyxml(' '))
  53. f.close()
  54. class ItemTree:
  55. def __init__(self):
  56. self.tree = ElementTree(file="data/sale.xml")
  57. self.root = self.tree.getroot()
  58. self.u_id = set()
  59. for elem in self.root:
  60. self.u_id.add(int(elem.get("uid")))
  61. def getId(self):
  62. id_itter = 1
  63. while id_itter in self.u_id:
  64. id_itter += 1
  65. self.u_id.add(id_itter)
  66. return id_itter
  67. def remove_id(self, uid):
  68. # Free up used id's.
  69. self.u_id.remove(uid)
  70. def add_item(self, name, item_id, amount, price):
  71. user = SubElement(self.root, "item")
  72. user.set("name", name)
  73. user.set("itemId", str(item_id))
  74. user.set("price", str(price))
  75. user.set("add_time", str(time.time()))
  76. user.set("relisted", str(0))
  77. user.set("amount", str(amount))
  78. user.set("uid", str(self.getId()))
  79. self.save()
  80. def get_uid(self, uid):
  81. for elem in self.root:
  82. if elem.get("uid") == str(uid):
  83. return elem
  84. return -10
  85. def remove_item_uid(self, uid):
  86. for elem in self.root:
  87. if elem.get("uid") == str(uid):
  88. self.root.remove(elem)
  89. self.remove_id(uid)
  90. self.save()
  91. return 1
  92. return -10
  93. def save(self):
  94. # Be sure to call save() after any changes to the tree.
  95. f = open('data/sale.xml', 'w')
  96. dom = xml.dom.minidom.parseString(clean_xml(tostring(self.root)))
  97. f.write(dom.toprettyxml(' '))
  98. f.close()
  99. def saveData(commitmessage = "commit"):
  100. # This assumes the current working directory is the tradey directory.
  101. os.chdir("data")
  102. call(["git", "commit","-a", '-m "' + commitmessage + '"'])
  103. os.chdir("..")
  104. if __name__ == '__main__':
  105. print "Do not run this file directly. Run main.py"