summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rwxr-xr-xmain.py30
-rw-r--r--player.py26
-rw-r--r--tradey.py13
3 files changed, 36 insertions, 33 deletions
diff --git a/main.py b/main.py
index 88fe85f..9307031 100755
--- a/main.py
+++ b/main.py
@@ -793,30 +793,12 @@ def main():
ItemDB.getItem(player_node.inventory[item].itemId).name, \
player_node.inventory[item].itemId, item, player_node.inventory[item].amount)
- # Check the inventory state and the amount of money match the information saved.
- test_node = player_node.inventory.copy()
- for elem in sale_tree.root:
- item_found = False
- for item in test_node:
- if int(elem.get('itemId')) == test_node[item].itemId \
- and int(elem.get('amount')) <= test_node[item].amount:
- test_node[item].amount -= int(elem.get('amount'))
- if test_node[item].amount == 0:
- del test_node[item]
- item_found = True
- break
-
- if not item_found:
- logging.info("Server and client inventory out of sync.")
- exit(0)
-
- total_money = 0
- for user in user_tree.root:
- total_money += int(user.get('money'))
-
- if total_money > player_node.MONEY:
- logging.info("Server and client money out of sync.")
- exit(0)
+ isclean = player_node.check_inventory(user_tree, sale_tree)
+ if isclean:
+ logging.info(isclean)
+ exit(0)
+ else:
+ logging.info("Inventory Check Passed.")
elif packet.is_type(SMSG_TRADE_REQUEST):
name = packet.read_string(24)
diff --git a/player.py b/player.py
index 2e3eafa..6277837 100644
--- a/player.py
+++ b/player.py
@@ -38,5 +38,31 @@ class Player:
return item
return -10 # Not found - bug somewhere!
+ def check_inventory(self, user_tree, sale_tree):
+ # Check the inventory state.
+ test_node = self.inventory.copy()
+ for elem in sale_tree.root:
+ item_found = False
+ for item in test_node:
+ if int(elem.get('itemId')) == test_node[item].itemId \
+ and int(elem.get('amount')) <= test_node[item].amount:
+ test_node[item].amount -= int(elem.get('amount'))
+ if test_node[item].amount == 0:
+ del test_node[item]
+ item_found = True
+ break
+
+ if not item_found:
+ return "Server and client inventory out of sync."
+
+ total_money = 0
+ for user in user_tree.root:
+ total_money += int(user.get('money'))
+
+ if total_money > self.MONEY:
+ return "Server and client money out of sync."
+
+ return 0
+
if __name__ == '__main__':
print "Do not run this file directly. Run main.py"
diff --git a/tradey.py b/tradey.py
index b70fd24..8ec6c6b 100644
--- a/tradey.py
+++ b/tradey.py
@@ -8,7 +8,6 @@
"""
import time
import os
-import xml.dom.minidom
from subprocess import call
from xml.etree.ElementTree import *
@@ -44,10 +43,8 @@ class UserTree:
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(tostring(self.root))
- f.write(str(dom.toprettyxml(' ')))
- f.close()
+ self.tree = ElementTree(self.root)
+ self.tree.write("data/user.xml")
class ItemTree:
def __init__(self):
@@ -97,10 +94,8 @@ class ItemTree:
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(tostring(self.root))
- f.write(str(dom.toprettyxml(' ')))
- f.close()
+ self.tree = ElementTree(self.root)
+ self.tree.write("data/sale.xml")
def saveData(commitmessage = "commit"):
# This assumes the current working directory is the tradey directory.