Imported Upstream version 2008.1+svn1656
[opeanno-debian-packaging.git] / game / util / worldobject.py
bloba7fc31e2e7f3bd0a41f9234da7a0db614f344b79
1 # ###################################################
2 # Copyright (C) 2008 The OpenAnno Team
3 # team@openanno.org
4 # This file is part of OpenAnno.
6 # OpenAnno is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 2 of the License, or
9 # (at your option) any later version.
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the
18 # Free Software Foundation, Inc.,
19 # 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 # ###################################################
22 import weakref
23 from changelistener import Changelistener
25 class WorldObject(Changelistener):
26 __next_id = 1
27 __objects = weakref.WeakValueDictionary()
28 def __init__(self, **kwargs):
29 super(WorldObject, self).__init__(**kwargs)
31 def getId(self):
32 if not hasattr(self, "_WorldObject__id"):
33 assert WorldObject.__next_id not in WorldObject.__objects
34 self.__id = WorldObject.__next_id
35 WorldObject.__next_id = WorldObject.__next_id + 1
36 WorldObject.__objects[self.__id] = self
37 return self.__id
39 @classmethod
40 def getObjectById(cls, id):
41 return cls.__objects[id]
43 @classmethod
44 def reset(cls):
45 cls.__next_id = 1
46 cls.__objects.clear()
48 def save(self, db):
49 pass
51 def load(self, db, worldid):
52 assert not hasattr(self, '_WorldObject__id')
53 assert worldid not in WorldObject.__objects
54 print 'loading worldobject', worldid, self
56 self.__id = worldid
57 WorldObject.__objects[worldid] = self
59 # Make sure that new WorldIDs are always higher than every other WorldObject
60 WorldObject.__next_id = max(self.__next_id, worldid + 1)
62 # for testing:
63 @classmethod
64 def get_objs(self): return self.__objects