# Path: games/zones/basezone.py
# class BaseZone(object):
#     def __init__(self, logger=None):
#         '''Initialize the zone.
#         Insert whatever objects into the database programmatically.
#         This includes loading things from a disk file if you so choose.
# 
#         It will not run more than once on the zone's database.
#         If you want new content on an existing database, either make
#         a script to apply changes, or just delete the database for
#         that zone and recreate it when the zone is started up again.
#         '''
# 
#         self.setup_logging(logger=logger)
#         self.load()
# 
#     @staticmethod
#     def randobj(name="Object #%s", resource='object', count=1, states=None, scripts=None):
#         objs = []
#         for i in xrange(count):
#             obj = Object()
#             obj.name = name % i
#             obj.resource = resource
#             obj.loc_x, obj.loc_y, obj.loc_z = randloc(), randloc(), randloc()
#             obj.rot_x, obj.rot_y, obj.rot_z = randrot(), randrot(), randrot()
#             obj.scale_x, obj.scale_y, obj.scale_z = randscale(), randscale(), randscale()
#             obj.vel_x, obj.vel_y, obj.vel_z = 0, 0, 0
#             if states:
#                 obj.states.extend(states)
#             if scripts:
#                 obj.scripts.extend(scripts)
#             obj.save()
#             objs.append(obj)
#         return objs
# 
#     def setup_logging(self, logger=None):
#         if logger:
#             self.logger = logger
#         else:
#             import logging
#             self.logger = logging.getLogger('zoneserver.'+__file__)
# 
#     def load(self):
#         if not self.is_loaded():
#             self.insert_objects()
#             # Loading complete.
#             self.set_loaded()
# 
#     def is_loaded(self):
#         if Object.get_objects(name='Loading Complete.'):
#             return True
#         else:
#             return False
# 
#     def set_loaded(self):
#         from sys import maxint
# 
#         obj = Object()
#         obj.name = "Loading Complete."
#         far = maxint*-1
#         obj.loc_x, obj.loc_y, obj.loc_z = far, far, far
#         obj.states.extend(['hidden'])
#         obj.save()
#         print obj.name
# 
#     def insert_objects(self):
#         '''Insert any objects you want to be present in the zone into the
#         database in this call.
# 
#         This gets called exactly once per database. If you change something here
#         and want it to appear in the zone's database, you will need to clear the
#         database first.
# 
#         Deleting the "Loading Complete" object will only cause duplicates.
#         Do not do this.
#         '''
#         pass
# 
# Path: elixir_models.py
# class Object(BaseModel):
#     '''In-world objects.'''
# 
#     name = CharField()
#     resource = CharField(default="none")
#     owner = CharField(null=True)
# 
#     loc_x = FloatField(default=0)
#     loc_y = FloatField(default=0)
#     loc_z = FloatField(default=0)
# 
#     rot_x = FloatField(default=0)
#     rot_y = FloatField(default=0)
#     rot_z = FloatField(default=0)
# 
#     scale_x = FloatField(default=1)
#     scale_y = FloatField(default=1)
#     scale_z = FloatField(default=1)
# 
#     speed = FloatField(default=1)
#     vel_x = FloatField(default=0)
#     vel_y = FloatField(default=0)
#     vel_z = FloatField(default=0)
# 
#     states = JSONField(default=[])
#     physical = BooleanField(default=True)
#     last_modified = DateTimeField(default=datetime.datetime.now)
# 
#     scripts = JSONField(default=[])
# 
#     @property
#     def loc(self):
#         return (self.loc_x, self.loc_y, self.loc_z)
# 
#     @loc.setter
#     def loc(self, val):
#         self.loc_x, self.loc_y, self.loc_z = val
# 
#     def set_modified(self, date_time=None):
#         if date_time is None:
#             date_time = datetime.datetime.now()
#         self.last_modified = date_time
#         self.save()
# 
#     @staticmethod
#     def get_objects(since=None, physical=None, limit=None, player=None, scripted=None, name=None):
#         # TODO: Needs integration test.
#         obj = Object.select()
# 
#         if since is not None:
#             obj = obj.where(Object.last_modified>=since)
# 
#         if physical is not None:
#             obj = obj.where(Object.physical==physical)
# 
#         if player is not None:
#             obj = obj.where(Object.states.contains('player')).where(Object.name==player)
# 
#         if scripted is not None:
#             obj = obj.where(Object.scripts!=None) # TODO: Have this only return objects with scripts.
# 
#         if name is not None:
#             obj = obj.where(Object.name==name)
# 
#         obj = obj.order_by(Object.last_modified.desc())
# 
#         if limit is not None:
#             obj = obj.limit(limit)
# 
#         if limit == 1:
#             return obj
# 
#         return [o for o in obj]


from games.zones.basezone import BaseZone
from elixir_models import Object
# This file is imported when the zone wants to load an instance of this zone.

class Zone(BaseZone):

    def insert_objects(self):
        '''Insert any objects you want to be present in the zone into the
        database in this call.

        This gets called exactly once per database. If you change something here
        and want it to appear in the zone's database, you will need to clear the
        database first.

        Deleting the "Loading Complete" object will only cause duplicates.
        Do not do this.
        '''

        # Place 100 barrels randomly:
        self.randobj(name="Barrel #%d", resource='barrel', states=['closed', 'whole', 'clickable'], count=100)

        # Place 10 chickens randomly:
        self.randobj(name="Chicken #%d", resource='chicken', scripts=['games.objects.chicken'], count=10)


# Path: games/zones/basezone.py
# class BaseZone(object):
#     def __init__(self, logger=None):
#         '''Initialize the zone.
#         Insert whatever objects into the database programmatically.
#         This includes loading things from a disk file if you so choose.
# 
#         It will not run more than once on the zone's database.
#         If you want new content on an existing database, either make
#         a script to apply changes, or just delete the database for
#         that zone and recreate it when the zone is started up again.
#         '''
# 
#         self.setup_logging(logger=logger)
#         self.load()
# 
#     @staticmethod
#     def randobj(name="Object #%s", resource='object', count=1, states=None, scripts=None):
#         objs = []
#         for i in xrange(count):
#             obj = Object()
#             obj.name = name % i
#             obj.resource = resource
#             obj.loc_x, obj.loc_y, obj.loc_z = randloc(), randloc(), randloc()
#             obj.rot_x, obj.rot_y, obj.rot_z = randrot(), randrot(), randrot()
#             obj.scale_x, obj.scale_y, obj.scale_z = randscale(), randscale(), randscale()
#             obj.vel_x, obj.vel_y, obj.vel_z = 0, 0, 0
#             if states:
#                 obj.states.extend(states)
#             if scripts:
#                 obj.scripts.extend(scripts)
#             obj.save()
#             objs.append(obj)
#         return objs
# 
#     def setup_logging(self, logger=None):
#         if logger:
#             self.logger = logger
#         else:
#             import logging
#             self.logger = logging.getLogger('zoneserver.'+__file__)
# 
#     def load(self):
#         if not self.is_loaded():
#             self.insert_objects()
#             # Loading complete.
#             self.set_loaded()
# 
#     def is_loaded(self):
#         if Object.get_objects(name='Loading Complete.'):
#             return True
#         else:
#             return False
# 
#     def set_loaded(self):
#         from sys import maxint
# 
#         obj = Object()
#         obj.name = "Loading Complete."
#         far = maxint*-1
#         obj.loc_x, obj.loc_y, obj.loc_z = far, far, far
#         obj.states.extend(['hidden'])
#         obj.save()
#         print obj.name
# 
#     def insert_objects(self):
#         '''Insert any objects you want to be present in the zone into the
#         database in this call.
# 
#         This gets called exactly once per database. If you change something here
#         and want it to appear in the zone's database, you will need to clear the
#         database first.
# 
#         Deleting the "Loading Complete" object will only cause duplicates.
#         Do not do this.
#         '''
#         pass
# 
# Path: elixir_models.py
# class Object(BaseModel):
#     '''In-world objects.'''
# 
#     name = CharField()
#     resource = CharField(default="none")
#     owner = CharField(null=True)
# 
#     loc_x = FloatField(default=0)
#     loc_y = FloatField(default=0)
#     loc_z = FloatField(default=0)
# 
#     rot_x = FloatField(default=0)
#     rot_y = FloatField(default=0)
#     rot_z = FloatField(default=0)
# 
#     scale_x = FloatField(default=1)
#     scale_y = FloatField(default=1)
#     scale_z = FloatField(default=1)
# 
#     speed = FloatField(default=1)
#     vel_x = FloatField(default=0)
#     vel_y = FloatField(default=0)
#     vel_z = FloatField(default=0)
# 
#     states = JSONField(default=[])
#     physical = BooleanField(default=True)
#     last_modified = DateTimeField(default=datetime.datetime.now)
# 
#     scripts = JSONField(default=[])
# 
#     @property
#     def loc(self):
#         return (self.loc_x, self.loc_y, self.loc_z)
# 
#     @loc.setter
#     def loc(self, val):
#         self.loc_x, self.loc_y, self.loc_z = val
# 
#     def set_modified(self, date_time=None):
#         if date_time is None:
#             date_time = datetime.datetime.now()
#         self.last_modified = date_time
#         self.save()
# 
#     @staticmethod
#     def get_objects(since=None, physical=None, limit=None, player=None, scripted=None, name=None):
#         # TODO: Needs integration test.
#         obj = Object.select()
# 
#         if since is not None:
#             obj = obj.where(Object.last_modified>=since)
# 
#         if physical is not None:
#             obj = obj.where(Object.physical==physical)
# 
#         if player is not None:
#             obj = obj.where(Object.states.contains('player')).where(Object.name==player)
# 
#         if scripted is not None:
#             obj = obj.where(Object.scripts!=None) # TODO: Have this only return objects with scripts.
# 
#         if name is not None:
#             obj = obj.where(Object.name==name)
# 
#         obj = obj.order_by(Object.last_modified.desc())
# 
#         if limit is not None:
#             obj = obj.limit(limit)
# 
#         if limit == 1:
#             return obj
# 
#         return [o for o in obj]


# Path: games/zones/defaultzone.py
from games.zones.basezone import BaseZone
from elixir_models import Object

# This file is imported when the zone wants to load an instance of this zone.

class Zone(BaseZone):

    def insert_objects(self):
        '''Insert any objects you want to be present in the zone into the
        database in this call.

        This gets called exactly once per database. If you change something here
        and want it to appear in the zone's database, you will need to clear the
        database first.

        Deleting the "Loading Complete" object will only cause duplicates.
        Do not do this.
        '''

        # Place 100 barrels randomly:
        self.randobj(name="Barrel #%d", resource='barrel', states=['closed', 'whole', 'clickable'], count=100)

        # Place 10 chickens randomly:
        self.randobj(name="Chicken #%d", resource='chicken', scripts=['games.objects.chicken'], count=10)


        print [o.name for o in Object.get_objects()]