diff options
author | Dipesh Amin <yaypunkrock@gmail.com> | 2011-08-23 21:18:58 +0100 |
---|---|---|
committer | Dipesh Amin <yaypunkrock@gmail.com> | 2011-08-23 21:18:58 +0100 |
commit | eac0a33f4862ebdff5ac8e00fb819dbe34422597 (patch) | |
tree | 2782f110f6bab0924b9c92cf7956477eb9a3cf58 | |
parent | 1404493735325ce94539fa939567933255412c02 (diff) | |
download | manamarket-eac0a33f4862ebdff5ac8e00fb819dbe34422597.tar.gz manamarket-eac0a33f4862ebdff5ac8e00fb819dbe34422597.tar.bz2 manamarket-eac0a33f4862ebdff5ac8e00fb819dbe34422597.tar.xz manamarket-eac0a33f4862ebdff5ac8e00fb819dbe34422597.zip |
Move inventory check to player.py and remove prettyprintxml()
having looked at the output this produces on the server, it looks terrible.
For some reason it's adding millions of spaces, not sure if the same thing
was happening on my computer.
-rwxr-xr-x | main.py | 30 | ||||
-rw-r--r-- | player.py | 26 | ||||
-rw-r--r-- | tradey.py | 13 |
3 files changed, 36 insertions, 33 deletions
@@ -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) @@ -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" @@ -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. |