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
|
#!/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
This program is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by the Free
Software Foundation; either version 2 of the License, or (at your option)
any later version.
This program is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
more details.
You should have received a copy of the GNU General Public License along with
this program. If not, see <http://www.gnu.org/licenses/>.
Additionally to the GPL, you are *strongly* encouraged to share any modifications
you do on these sources.
"""
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.tree = ElementTree(file="data/sale.xml")
self.root = self.tree.getroot()
self.u_id = set()
for elem in self.root:
self.u_id.add(int(elem.get("uid")))
def getId(self):
id_itter = 1
while id_itter in self.u_id:
id_itter += 1
self.u_id.add(id_itter)
return 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("relisted", str(0))
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('data/sale.xml', 'w')
dom = xml.dom.minidom.parseString(clean_xml(tostring(self.root)))
f.write(dom.toprettyxml(' '))
f.close()
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"
|