1 /* Copyright (C) 2000-2005 Thomas Bopp, Thorsten Hampel, Ludger Merkens
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 * $Id: ContainerFactory.pike,v 1.2 2010/08/18 20:32:45 astra Exp $
19 inherit "/factories/ObjectFactory";
25 #include <attributes.h>
27 //! This factory creates intances of the class Container.
28 class ContainerFactory : public ObjectFactory{
44 register_class_attribute(Attribute(
45 CONT_SIZE_X, "x-size", CMD_TYPE_FLOAT,
46 0.0, 0, CONTROL_ATTR_CLIENT));
47 register_class_attribute(Attribute(
48 CONT_SIZE_Y, "y-size", CMD_TYPE_FLOAT,
49 0.0, 0, CONTROL_ATTR_CLIENT));
50 register_class_attribute(Attribute(
51 CONT_SIZE_Z, "z-size", CMD_TYPE_FLOAT,
52 0.0, 0, CONTROL_ATTR_CLIENT));
53 register_class_attribute(Attribute(
54 CONT_EXCHANGE_LINKS, "exchange links",
55 CMD_TYPE_INT, 0, // link exchange turned off
56 REG_ACQ_ENVIRONMENT, CONTROL_ATTR_USER));
57 register_class_attribute(Attribute(
58 CONT_WSDL, "Container WSDL Description",
60 REG_ACQ_ENVIRONMENT, CONTROL_ATTR_USER));
66 * Execute this Container factory to get a new container object.
67 * The vars mapping takes indices: "name", "attributes","attributesAcquired",
68 * and "attributesLocked".
70 * @param mapping vars - execute vars, especially the containers name.
71 * @return proxy of the newly created container.
73 object execute(mapping vars)
76 string name = vars["name"];
77 try_event(EVENT_EXECUTE, CALLER, obj);
78 if ( vars->transient ) {
79 if ( mappingp(vars->attributes) )
80 vars->attributes[OBJ_TEMP] = 1;
82 vars->attributes = ([ OBJ_TEMP : 1 ]);
85 obj = ::object_create(
86 name, CLASS_NAME_CONTAINER, 0, vars["attributes"],
87 vars["attributesAcquired"], vars["attributesLocked"],
89 vars["sanctionMeta"]);
91 run_event(EVENT_EXECUTE, CALLER, obj);
94 int change_object_to_container ( object obj ) {
95 if ( !_SECURITY->access_write(0, obj, CALLER) )
98 string old_class = obj->get_class();
99 if ( old_class == CLASS_NAME_CONTAINER )
100 return 0; // obj already is a container
101 if ( !(old_class == CLASS_NAME_ROOM) )
102 steam_error("Class "+old_class+" cannot be changed to a container, only"
105 steam_error( "Rooms cannot be changed to containers, yet." );
107 //TODO: check for invalid contents for containers (e.g. users, exits
108 // or other rooms) and remove them. Also recurse through all sub-rooms
109 // and change them to containers, too.
111 if ( _Persistence->change_object_class(obj, CLASS_NAME_CONTAINER) ) {
112 obj->set_attribute( OBJ_LAST_CHANGED, query_attribute(FACTORY_LAST_REGISTER)-1 );
113 call(obj->drop, 0.0);
119 string get_identifier() { return "Container.factory"; }
120 string get_class_name() { return "Container"; }
121 int get_class_id() { return CLASS_CONTAINER; }
124 object cont = execute( ([ "name": "testcontainer", ]) );
125 object doc1 = get_factory(CLASS_DOCUMENT)->execute( ([ "name": "test1", ]));
126 object doc2 = get_factory(CLASS_DOCUMENT)->execute( ([ "name": "test2", ]));
129 Test.test("Inventory",
130 doc1->get_environment() == cont);
131 Test.test("Inventory 2",
132 search(cont->get_inventory(), doc1) >= 0 );
134 cont->add_annotation(doc2);
136 Test.test("Anotations Annotating",
137 doc2->get_annotating() == cont);
138 Test.test("Annotation available",
139 search(cont->get_annotations(), doc2) >= 0);
141 object dup = cont->duplicate(1);
143 Test.test("Duplicate Container",
144 dup->get_identifier() == "testcontainer");
145 Test.test("Duplicated Inventory",
146 sizeof(dup->get_inventory()) != 0);
147 Test.test("Duplicated Annotation",
148 sizeof(dup->get_annotations()) > 0);
150 object doc3 = get_factory(CLASS_DOCUMENT)->execute( ([ "name": "test3", ]));
152 doc3->set_attribute(OBJ_NAME, "test1");
153 Test.test("Identifier should be unique after rename!",
154 doc3->get_identifier() == doc3->get_object_id() + "__test1");
155 Test.test("OBJ_NAME should be changed after rename!",
156 doc3->query_attribute(OBJ_NAME) == "test1");
158 object doc4 = get_factory(CLASS_DOCUMENT)->execute( ([ "name": "test1",
160 Test.test("Create with non-unique identifier in cont should make unique",
161 doc4->get_identifier() != "test1");
163 object doc5 = get_factory(CLASS_DOCUMENT)->execute( ([ "name": "test1", ]));
165 Test.test("Name should change on move!", doc5->get_identifier() != "test1");
166 Test.test("OBJ_NAME should be the same after move!",
167 doc5->query_attribute(OBJ_NAME) == "test1");