diff options
author | Rawng <rawng.github@gmail.com> | 2015-06-01 19:56:10 -0400 |
---|---|---|
committer | Rawng <rawng.github@gmail.com> | 2015-06-01 19:56:10 -0400 |
commit | 1e8140f0f62ecfd0ccbe046cf6ea9faed926f327 (patch) | |
tree | ca92a7a0cd2256ffbb0b43563ee183400d1aacc5 /storage.py | |
parent | b4f19a8e28cf09b7c8a042c8faf068217669bca5 (diff) | |
parent | b7e3823588ae9354daf5fc0a0cbf27b699afb7d5 (diff) | |
download | manamarket-1e8140f0f62ecfd0ccbe046cf6ea9faed926f327.tar.gz manamarket-1e8140f0f62ecfd0ccbe046cf6ea9faed926f327.tar.bz2 manamarket-1e8140f0f62ecfd0ccbe046cf6ea9faed926f327.tar.xz manamarket-1e8140f0f62ecfd0ccbe046cf6ea9faed926f327.zip |
Merge branch 'storage'
Diffstat (limited to 'storage.py')
-rw-r--r-- | storage.py | 117 |
1 files changed, 117 insertions, 0 deletions
diff --git a/storage.py b/storage.py new file mode 100644 index 0000000..8afda66 --- /dev/null +++ b/storage.py @@ -0,0 +1,117 @@ +#!/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 + + Storage Access class by Fernanda Monteiro <crie.fernanda@gmail.com> +""" + +from utils import ItemDB +from net.packet_out import chat +from net.protocol import * +from net.packet import * +import time +import copy +import mutex + +class Storage: + def __init__(self): + self.storage = {} + self.timer = 0 + self.mapserv = 0 + self.Open = mutex.mutex() + + def reset(self): + self.Open.unlock() + self.timer = 0 + + def find_storage_index(self, item_id): + for item in self.storage: + if item > 1: + if self.storage[item].itemId == item_id: + return item + return -10 # Not found - bug somewhere! + + def add_item(self, item): + if not item.itemId or item.amount <= 0: + return -10 # Not an item - something is messy + + index = self.find_storage_index(item.itemId) + if ItemDB().getItem(item.itemId).type != 'equip-ammo' and 'equip': + if index != -10: + if (item.amount > MAX_AMOUNT - self.storage[index].amount): + return -10 + self.storage[index].amount += item.amount + return index + + index = len(self.storage) + self.storage[index] = item + self.storage[index].amount = item.amount + return index + + def remove_item(self, index, amount): + if index in self.storage: + self.storage[index].amount -= amount + if self.storage[index].amount == 0: + del self.storage[index] + + def check_storage(self, stack_tree, delisted_tree): + # Check the inventory state. + test_node = copy.deepcopy(self.storage) + for elem in stack_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 storage out of sync." + + for elem in delisted_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 storage out of sync." + + def storage_send(self, index, amount): + packet = PacketOut(CMSG_MOVE_TO_STORAGE) + packet.write_int16(index + inventory_offset) + packet.write_int32(amount) + self.mapserv.sendall(str(packet)) + return 0 + + def storage_get(self, index, amount): + packet = PacketOut(CMSG_MOVE_FROM_STORAGE) + packet.write_int16(index + storage_offset) + packet.write_int32(amount) + self.mapserv.sendall(str(packet)) + return 0 + + def storage_open(self): + self.timer = time.time() + self.mapserv.sendall(chat("@storage")) + + def storage_close(self): + self.reset() + self.mapserv.sendall(str(PacketOut(CMSG_CLOSE_STORAGE))) + +if __name__ == '__main__': + print "Do not run this file directly. Run main.py" + |