# Path: scriptserver.py
# class ZoneScriptRunner(BaseTickServer):
#     '''This is a class that holds all sorts of methods for running scripts for
#     a zone. It does not talk to the HTTP handler(s) directly, but instead uses
#     the same database. It might take player movement updates directly in the
#     future for speed, but this is unlikely.'''
# 
#     def __init__(self, zoneid):
#         super(ZoneScriptRunner, self).__init__()
# 
#         # While the zone is not loaded, wait.
#         logger.info("Waiting for zone to complete loading.")
#         while not Object.get_objects(name="Loading Complete."):
#             time.sleep(.1)
# 
#         # Watch the script path for any changes, and reboot the scriptserver if they do.
#         self.observer = Observer()
#         self.observer.schedule(ScriptEventHandler(), path=settings.SCRIPT_PATH, recursive=True)
#         self.observer.start()
# 
#         self.load_scripts()
#         logger.info("Started with data for zone: %s" % zoneid)
# 
#     def load_scripts(self):
#         '''(Re)Load scripts for objects in this zone.'''
#         self.scripts = {}
# 
#         # Query DB for a list of all objects' script names,
#         #   ordered according to proximity to players
#         logger.info(Object.get_objects(scripted=True))
#         for o in Object.get_objects(scripted=True):
#             logger.info("Scripted Object: {0}".format(o.name))
#             # Store list of script names in self
# 
#             # For each script name in the list:
#             for script in o.scripts:
#                 logger.info("Importing %s" % script)
#                 if script not in self.scripts:
#                     self.scripts[script] = []
# 
#                 # Import those by name via __import__
#                 scriptclass = script.split('.')[-1]
#                 module = __import__(script, globals(), locals(), [scriptclass], -1)
#                 # For each entry in the script's dir()
#                 for key in dir(module):
#                     C = getattr(module, key)
# 
#                     # No sense in instantiating the default Script instance.
#                     if C == Script:
#                         continue
# 
#                     try:
#                         # Does this object have the attributes that scripts need?
#                         if not all((C.tick, C.activate, C.create)):
#                             continue
#                     except AttributeError:
#                         continue
# 
#                     # Store object instance in a list.
#                     self.scripts[script].append(C(mongo_engine_object=o))
#         return self.scripts
# 
# 
#     def tick(self):
#         '''Iterate through all known scripts and call their tick method.'''
#         # Tick all the things
#         logger.info(self.scripts)
#         for scriptname, scripts in self.scripts.items():
#             for script in scripts:
#                 logger.debug("Ticking {0}".format(script))
#                 # TODO: Pass some locals or somesuch so that they can query the db
#                 script.tick()
# 
#         # Clean up mongodb's messages by deleting all but the most recent 100 non-player messages
#         for m in Message.select().where(Message.player_generated==False).order_by('-sent')[settings.MAX_ZONE_OBJECT_MESSAGE_COUNT:]:
#             logger.info("Deleting message from %s" % m.sent.time())
#             m.delete()
# 
#     def start(self):
#         logger.info("Running ZoneScript Server.")
#         super(ZoneScriptRunner, self).start()


import unittest
import sys
from mock import patch, Mock
from scriptserver import ZoneScriptRunner

sys.path.append(".")

class TestZoneScriptRunner(unittest.TestCase):

    def test___init__(self):
        zoneid = "zoneid"
        with patch('scriptserver.Object'):

# Path: scriptserver.py
# class ZoneScriptRunner(BaseTickServer):
#     '''This is a class that holds all sorts of methods for running scripts for
#     a zone. It does not talk to the HTTP handler(s) directly, but instead uses
#     the same database. It might take player movement updates directly in the
#     future for speed, but this is unlikely.'''
# 
#     def __init__(self, zoneid):
#         super(ZoneScriptRunner, self).__init__()
# 
#         # While the zone is not loaded, wait.
#         logger.info("Waiting for zone to complete loading.")
#         while not Object.get_objects(name="Loading Complete."):
#             time.sleep(.1)
# 
#         # Watch the script path for any changes, and reboot the scriptserver if they do.
#         self.observer = Observer()
#         self.observer.schedule(ScriptEventHandler(), path=settings.SCRIPT_PATH, recursive=True)
#         self.observer.start()
# 
#         self.load_scripts()
#         logger.info("Started with data for zone: %s" % zoneid)
# 
#     def load_scripts(self):
#         '''(Re)Load scripts for objects in this zone.'''
#         self.scripts = {}
# 
#         # Query DB for a list of all objects' script names,
#         #   ordered according to proximity to players
#         logger.info(Object.get_objects(scripted=True))
#         for o in Object.get_objects(scripted=True):
#             logger.info("Scripted Object: {0}".format(o.name))
#             # Store list of script names in self
# 
#             # For each script name in the list:
#             for script in o.scripts:
#                 logger.info("Importing %s" % script)
#                 if script not in self.scripts:
#                     self.scripts[script] = []
# 
#                 # Import those by name via __import__
#                 scriptclass = script.split('.')[-1]
#                 module = __import__(script, globals(), locals(), [scriptclass], -1)
#                 # For each entry in the script's dir()
#                 for key in dir(module):
#                     C = getattr(module, key)
# 
#                     # No sense in instantiating the default Script instance.
#                     if C == Script:
#                         continue
# 
#                     try:
#                         # Does this object have the attributes that scripts need?
#                         if not all((C.tick, C.activate, C.create)):
#                             continue
#                     except AttributeError:
#                         continue
# 
#                     # Store object instance in a list.
#                     self.scripts[script].append(C(mongo_engine_object=o))
#         return self.scripts
# 
# 
#     def tick(self):
#         '''Iterate through all known scripts and call their tick method.'''
#         # Tick all the things
#         logger.info(self.scripts)
#         for scriptname, scripts in self.scripts.items():
#             for script in scripts:
#                 logger.debug("Ticking {0}".format(script))
#                 # TODO: Pass some locals or somesuch so that they can query the db
#                 script.tick()
# 
#         # Clean up mongodb's messages by deleting all but the most recent 100 non-player messages
#         for m in Message.select().where(Message.player_generated==False).order_by('-sent')[settings.MAX_ZONE_OBJECT_MESSAGE_COUNT:]:
#             logger.info("Deleting message from %s" % m.sent.time())
#             m.delete()
# 
#     def start(self):
#         logger.info("Running ZoneScript Server.")
#         super(ZoneScriptRunner, self).start()


# Path: tests/test_scriptserver.py
import unittest
import sys
from mock import patch, Mock
from scriptserver import ZoneScriptRunner


sys.path.append(".")

class TestZoneScriptRunner(unittest.TestCase):

    def test___init__(self):
        zoneid = "zoneid"
        with patch('scriptserver.Object'):

            with patch.object(ZoneScriptRunner, 'load_scripts') as mock_load_scripts: