summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMeistache <crie.fernanda@gmail.com>2015-02-22 14:15:36 -0300
committerMeistache <crie.fernanda@gmail.com>2015-02-22 14:15:36 -0300
commitbd6b97110b2e73bcdb3ad83c6b605e10bc75736a (patch)
tree27ddc4a6011b0d8639a3a9a97cc66232408cf372
parent576f092fda4d3b89f9cf6fd3de96e280a0d8b775 (diff)
downloadmanamarket-bd6b97110b2e73bcdb3ad83c6b605e10bc75736a.tar.gz
manamarket-bd6b97110b2e73bcdb3ad83c6b605e10bc75736a.tar.bz2
manamarket-bd6b97110b2e73bcdb3ad83c6b605e10bc75736a.tar.xz
manamarket-bd6b97110b2e73bcdb3ad83c6b605e10bc75736a.zip
Storage finally working - may enter on test
-rwxr-xr-xmain.py51
-rw-r--r--storage.py17
-rw-r--r--utils.py14
3 files changed, 49 insertions, 33 deletions
diff --git a/main.py b/main.py
index eaa617c..61cb1df 100755
--- a/main.py
+++ b/main.py
@@ -22,7 +22,6 @@ 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 logging
import logging.handlers
import socket
@@ -70,7 +69,7 @@ def do_delist():
item.amount = int(elem.get('amount'))
if time.time() - float(elem.get('add_time')) > config.delist_time:
try:
- storage.storage_send(mapserv, item.index, item.amount)
+ storage.storage_send(item.index, item.amount)
except:
logger.info("Couldn't send item to storage")
return -10
@@ -83,7 +82,7 @@ def do_delist():
if cleaned > 0:
logger.info("Delisting routine done. %d items added to delisted.xml", cleaned)
stacked = len(stack_tree.u_id)
- if stack > 0:
+ if stacked > 0:
if cleaned - stacked < 0:
stacked = cleaned
while stacked:
@@ -92,12 +91,13 @@ def do_delist():
return err
else:
stacked -= 1
-
+ return 0
+
def unstack():
elem = stack_tree.get_uid(stack_tree.next_id)
index = storage.find_storage_index(int(elem.get('itemId')))
try:
- storage.storage_get(mapserv, index, int(elem.get('amount')))
+ storage.storage_get(index, int(elem.get('amount')))
except:
logger.info("Couldn't remove item from storage")
return -10
@@ -105,6 +105,18 @@ def unstack():
storage.remove_item(index, int(elem.get('amount')))
sale_tree.add_item(elem.get('name'), int(elem.get('itemId')), int(elem.get('amount')), int(elem.get('price')))
stack_tree.remove_item_uid(stack_tree.next_id)
+ return 0
+
+def storage_operation(func, args=[]):
+ storage.storage_open()
+ # Using a timed thread here to wait storage response.
+ # Main reason: time.sleep() stops execution, thus it denies storage to open. Yeah, that bad.
+ timer = utils.CustomTimer(2.0, func, args)
+ timer.start()
+ result = timer.join()
+ if timer.finished:
+ storage.storage_close()
+ return result
def process_whisper(nick, msg, mapserv):
msg = filter(lambda x: x in utils.allowed_chars, msg)
@@ -850,6 +862,7 @@ def main():
pb = PacketBuffer()
shop_broadcaster.mapserv = mapserv
+ storage.mapserv = mapserv
# Map server packet loop
print "Entering map packet loop\n";
@@ -870,7 +883,7 @@ def main():
if storage.Open.test():
if time.time() - storage.timer > 2*60:
logger.info("Storage operation cancelled - Timeout.")
- mapserv.sendall(str(PacketOut(CMSG_CLOSE_STORAGE)))
+ storage.storage_close()
for packet in pb:
if packet.is_type(SMSG_MAP_LOGIN_SUCCESS): # connected
@@ -954,23 +967,17 @@ def main():
err = packet.read_int8()
if err == 0:
- if len(player_node.inventory) <= MAX_INVENTORY:
+ if len(player_node.inventory) < MAX_INVENTORY:
if item.index in player_node.inventory:
player_node.inventory[item.index].amount += item.amount
else:
player_node.inventory[item.index] = item
placement = "Inventory"
else: # If only one slot left, move to stack
- storage.storage_open(mapserv)
- time.sleep(3)
- try:
- storage.storage_send(mapserv, item.index, item.amount)
- except:
- logger.info("Couldn't send item to storage")
- return -10
- storage.storage_close(mapserv)
- item.index = storage.add_item(item)
- placement = "Storage"
+ err = storage_operation(storage.storage_send, (item.index, item.amount))
+ if err == 0:
+ item.index = storage.add_item(item)
+ placement = "Storage"
logger.info("Picked up: %s, Amount: %s, Index: %s - %s", ItemDB.getItem(item.itemId).name, str(item.amount), str(item.index), placement)
@@ -983,10 +990,7 @@ def main():
# Now taking an item from stack if inventory was full before
if len(player_node.inventory) == MAX_INVENTORY-1:
- storage.storage_open(mapserv)
- time.sleep(3)
- unstack()
- storage.storage_close(mapserv)
+ storage_operation(unstack)
elif packet.is_type(SMSG_PLAYER_INVENTORY):
player_node.inventory.clear() # Clear the inventory - incase of new index.
@@ -1027,10 +1031,7 @@ def main():
logger.info("Inventory Check Passed.")
# IMO the best moment to run delisting
- storage.storage_open(mapserv)
- time.sleep(3)
- #do_delist()
- #storage.storage_close(mapserv)
+ storage_operation(do_delist)
elif packet.is_type(SMSG_PLAYER_STORAGE_ITEMS):
storage.storage.clear() # Clear storage - same as inventory.
diff --git a/storage.py b/storage.py
index 9ad0841..4de818c 100644
--- a/storage.py
+++ b/storage.py
@@ -22,6 +22,7 @@ class Storage:
def __init__(self):
self.storage = {}
self.timer = 0
+ self.mapserv = 0
self.Open = mutex.mutex()
def reset(self):
@@ -89,27 +90,27 @@ class Storage:
if not item_found:
return "Server and client storage out of sync."
- def storage_send(self, mapserv, index, amount):
+ def storage_send(self, index, amount):
packet = PacketOut(CMSG_MOVE_TO_STORAGE)
packet.write_int16(index + inventory_offset)
packet.write_int32(amount)
- mapserv.sendall(str(packet))
+ self.mapserv.sendall(str(packet))
return 0
- def storage_get(self, mapserv, index, amount):
+ def storage_get(self, index, amount):
packet = PacketOut(CMSG_MOVE_FROM_STORAGE)
packet.write_int16(index + storage_offset)
packet.write_int32(amount)
- mapserv.sendall(str(packet))
+ self.mapserv.sendall(str(packet))
return 0
- def storage_open(self, mapserv):
+ def storage_open(self):
self.timer = time.time()
- mapserv.sendall(chat("@storage"))
+ self.mapserv.sendall(chat("@storage"))
- def storage_close(self, mapserv):
+ def storage_close(self):
self.reset()
- mapserv.sendall(str(PacketOut(CMSG_CLOSE_STORAGE)))
+ self.mapserv.sendall(str(PacketOut(CMSG_CLOSE_STORAGE)))
if __name__ == '__main__':
print "Do not run this file directly. Run main.py"
diff --git a/utils.py b/utils.py
index 542b7af..6273bef 100644
--- a/utils.py
+++ b/utils.py
@@ -13,6 +13,7 @@ import time
import mutex
import threading
from net.packet_out import *
+from threading import _Timer
allowed_chars = "abcdefghijklmnoprstquvwxyzABCDEFGHIJKLMNOPRSTQUVWXYZ1234567890-_+=!@$%^&*();'<>,.?/~`| "
@@ -138,5 +139,18 @@ class Broadcast:
self.Active = False
self.shop_broadcast.join()
+class CustomTimer(_Timer):
+ def __init__(self, interval, function, args=[], kwargs={}):
+ self._original_function = function
+ super(CustomTimer, self).__init__(interval, self._do_execute, args, kwargs)
+ self._result = None
+
+ def _do_execute(self, *a, **kw):
+ self._result = self._original_function(*a, **kw)
+
+ def join(self):
+ super(CustomTimer, self).join()
+ return self._result
+
if __name__ == '__main__':
print "Do not run this file directly. Run main.py"