Skip to content

Commit

Permalink
Storage finally working - may enter on test
Browse files Browse the repository at this point in the history
  • Loading branch information
Meistache committed Feb 22, 2015
1 parent 576f092 commit bd6b971
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 33 deletions.
51 changes: 26 additions & 25 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
Additionally to the GPL, you are *strongly* encouraged to share any modifications
you do on these sources.
"""

import logging
import logging.handlers
import socket
Expand Down Expand Up @@ -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
Expand All @@ -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:
Expand All @@ -92,19 +91,32 @@ 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

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)
Expand Down Expand Up @@ -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";
Expand All @@ -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
Expand Down Expand Up @@ -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)

Expand All @@ -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.
Expand Down Expand Up @@ -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.
Expand Down
17 changes: 9 additions & 8 deletions storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class Storage:
def __init__(self):
self.storage = {}
self.timer = 0
self.mapserv = 0
self.Open = mutex.mutex()

def reset(self):
Expand Down Expand Up @@ -89,27 +90,27 @@ def check_storage(self, stack_tree, delisted_tree):
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"
Expand Down
14 changes: 14 additions & 0 deletions utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import mutex
import threading
from net.packet_out import *
from threading import _Timer

allowed_chars = "abcdefghijklmnoprstquvwxyzABCDEFGHIJKLMNOPRSTQUVWXYZ1234567890-_+=!@$%^&*();'<>,.?/~`| "

Expand Down Expand Up @@ -138,5 +139,18 @@ def stop(self):
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"

0 comments on commit bd6b971

Please sign in to comment.