1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
|
#!/usr/bin/python
"""
Copyright 2011, Dipesh Amin <yaypunkrock@gmail.com>
Copyright 2011, Stefan Beller <stefanbeller@googlemail.com>
This file is part of tradey, a trading bot in the mana world
see www.themanaworld.org
"""
import time
import os
import xml.dom.minidom
from subprocess import call
from xml.etree.ElementTree import *
def clean_xml(parse):
data = ''
pos_start = 0
while parse.find('<', pos_start) != -1:
pos_start = parse.find('<', pos_start)
pos_end = parse.find('>', pos_start+1)
data += parse[pos_start:pos_end+1]
pos_start = pos_end
return data
class UserTree:
def __init__(self):
self.tree = ElementTree(file="data/user.xml")
self.root = self.tree.getroot()
def add_user(self, name, stalls, al):
if self.get_user(name) == -10:
user = SubElement(self.root, "user")
user.set("name", name)
user.set("stalls", str(stalls))
user.set("used_stalls", str(0))
user.set("money", str(0))
user.set("id", str(0))
user.set("accesslevel", str(al))
self.save()
def get_user(self, name):
for elem in self.root:
if elem.get("name") == name:
return elem
return -10
def remove_user(self, name):
for elem in self.root:
if elem.get("name") == name:
self.root.remove(elem)
self.save()
return 1
return -10
def save(self):
# Be sure to call save() after any changes to the tree.
f = open('data/user.xml', 'w')
dom = xml.dom.minidom.parseString(clean_xml(tostring(self.root)))
f.write(dom.toprettyxml(' '))
f.close()
class ItemTree:
def __init__(self):
self.save_file = 'data/sale.xml'
self.tree = ElementTree(file=self.save_file)
self.root = self.tree.getroot()
self.u_id = set()
self.id_itter = 1
for elem in self.root:
self.u_id.add(int(elem.get("uid")))
def getId(self):
while self.id_itter in self.u_id:
self.id_itter += 1
self.u_id.add(self.id_itter)
return self.id_itter
def remove_id(self, uid):
# Free up used id's.
self.u_id.remove(uid)
def add_item(self, name, item_id, amount, price):
user = SubElement(self.root, "item")
user.set("name", name)
user.set("itemId", str(item_id))
user.set("price", str(price))
user.set("add_time", str(time.time()))
user.set("amount", str(amount))
user.set("uid", str(self.getId()))
self.save()
def get_uid(self, uid):
for elem in self.root:
if elem.get("uid") == str(uid):
return elem
return -10
def remove_item_uid(self, uid):
for elem in self.root:
if elem.get("uid") == str(uid):
self.root.remove(elem)
self.remove_id(uid)
self.save()
return 1
return -10
def save(self):
# Be sure to call save() after any changes to the tree.
f = open(self.save_file, 'w')
dom = xml.dom.minidom.parseString(clean_xml(tostring(self.root)))
f.write(dom.toprettyxml(' '))
f.close()
class StackTree(ItemTree):
def __init__(self):
self.save_file = 'data/stack.xml'
self.tree = ElementTree(file=self.save_file)
self.root = self.tree.getroot()
self.u_id = set()
self.id_itter = 101
self.next_id = self.id_itter
for elem in self.root:
self.u_id.add(int(elem.get("uid")))
def add_item(self, name, item_id, amount, price):
user = SubElement(self.root, "item")
user.set("name", name)
user.set("itemId", str(item_id))
user.set("price", str(price))
user.set("amount", str(amount))
user.set("uid", str(self.getId()))
self.save()
# Here we take the oldest id put into stack.xml so we have FIFO
def get_next_id(self):
if len(self.u_id) == 0:
self.next_id = self.id_itter
return
else:
# TODO Here I'm handling rotation (gets a high number then
# back to 101). Any better manners to do this?
if min(self.u_id) - self.next_id < 0:
next_uid = self.next_id - min(self.u_id)
self.next_id = self.id_itter + next_uid + 1
else:
next_uid = min(self.u_id) - self.next_id
self.next_id += next_uid
return
def remove_item_uid(self, uid):
for elem in self.root:
if elem.get("uid") == str(uid):
self.root.remove(elem)
self.remove_id(uid)
self.save()
return 1
self.get_next_id()
return -10
class DelistedTree(ItemTree):
def __init__(self):
self.save_file = 'data/delisted.xml'
self.tree = ElementTree(file=self.save_file)
self.root = self.tree.getroot()
self.u_id = set()
self.id_itter = 301
for elem in self.root:
self.u_id.add(int(elem.get("uid")))
def add_item(self, name, item_id, amount):
user = SubElement(self.root, "item")
user.set("name", name)
user.set("itemId", str(item_id))
user.set("amount", str(amount))
user.set("uid", str(self.getId()))
self.save()
def saveData(commitmessage = "commit"):
# This assumes the current working directory is the tradey directory.
os.chdir("data")
call(["git", "commit","-a", '-m "' + commitmessage + '"'])
os.chdir("..")
if __name__ == '__main__':
print "Do not run this file directly. Run main.py"
|