My favorites | Sign in
Project Home Downloads Wiki Issues Source
Checkout   Browse   Changes    
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
<?php
/**
* Zeitgeist Application Framework
* http://www.zeitgeist-framework.com
*
* Gamedata class
*
* A generic layer to handle game data based on a simple entity
* model
*
* @author Dirk Songür <dirk@zeitalter3.de>
* @license MIT License <http://creativecommons.org/licenses/MIT/>
*
* @package ZEITGEIST
* @subpackage ZEITGEIST GAMESYSTEM
*/

defined( 'ZEITGEIST_ACTIVE' ) or die( );

class zgGamedata
{
protected $debug;
protected $messages;
protected $database;


/**
* Class constructor
*/
public function __construct( )
{
$this->debug = zgDebug::init( );
$this->messages = zgMessages::init( );

$this->database = new zgDatabase( );
$this->database->connect( );
}


/**
* Creates a new entity
* The new entity will be empty in the sense that no components will be added at this stage
* Returns the id of the new entity if successful or false if not
*
* @param string $name name of the entity
* @param int $assemblage assemblage used to add initial components to the entity
*
* @return int|boolean
*/
public function createEntity( $name = '', $assemblage = false )
{
$this->debug->guard( );

$sql = "INSERT INTO game_entities(entity_name) VALUES('" . $name . "')";
$res = $this->database->query( $sql );
if ( !$res )
{
$this->debug->write( 'Problem creating new entity: could not insert entity into database', 'warning' );
$this->messages->setMessage( 'Problem creating new entity: could not insert entity into database', 'warning' );
$this->debug->unguard( false );
return false;
}

$entity = $this->database->insertId( );
if ( !$entity )
{
$this->debug->write( 'Problem creating new entity: could not get entity id', 'warning' );
$this->messages->setMessage( 'Problem creating new entity: could not get entity id', 'warning' );
$this->debug->unguard( false );
return false;
}

if ( $assemblage )
{
$this->addAssemblageToEntity( $assemblage, $entity );
}

$this->debug->unguard( $entity );
return $entity;
}


/**
* Deletes a given entity
*
* @param int $entity the id of the entity to delete
*
* @return boolean
*/
public function deleteEntity( $entity )
{
$this->debug->guard( );

$sql = "DELETE FROM game_entities WHERE entity_id='" . $entity . "'";
$res = $this->database->query( $sql );
if ( !$res )
{
$this->debug->write( 'Problem deleting an entity: could not delete entity from database', 'warning' );
$this->messages->setMessage( 'Problem deleting an entity: could not delete entity from database', 'warning' );
$this->debug->unguard( false );
return false;
}

$this->debug->unguard( true );
return true;
}


/**
* Adds a new component to an entity
* Optionally the initial data of the component can be given as third parameter
*
* @param int $component id of the component that should be added to the entity
* @param int $entity id of the entity to add the component to
* @param array $componentdata the initial data for the new component as key / value pairs
*
* @return boolean
*/
public function addComponentToEntity( $component, $entity, $componentdata = false )
{
$this->debug->guard( );

$sql = "INSERT INTO game_component_" . $component . "() VALUES()";
$res = $this->database->query( $sql );
if ( !$res )
{
$this->debug->write( 'Problem adding a new component to an entity: could not insert component data into database', 'warning' );
$this->messages->setMessage( 'Problem adding a new component to an entity: could not insert component data into database', 'warning' );
$this->debug->unguard( false );
return false;
}

$componentDataId = $this->database->insertId( );
if ( !$componentDataId )
{
$this->debug->write( 'Problem adding a new component to an entity: could not get the component data id', 'warning' );
$this->messages->setMessage( 'Problem adding a new component to an entity: could not get the component data id', 'warning' );
$this->debug->unguard( false );
return false;
}

$sql = "INSERT INTO game_entity_components(entitycomponent_entity, entitycomponent_component, entitycomponent_componentdata) ";
$sql .= "VALUES('" . $entity . "', '" . $component . "', '" . $componentDataId . "')";
$res = $this->database->query( $sql );
if ( !$res )
{
$this->debug->write( 'Problem adding a new component to an entity: could not insert component data into database', 'warning' );
$this->messages->setMessage( 'Problem adding a new component to an entity: could not insert component data into database', 'warning' );
$this->debug->unguard( false );
return false;
}

if ( is_array( $componentdata ) )
{
if ( $this->setComponentData( $component, $entity, $componentdata ) )
{
$this->debug->write( 'Problem adding a new component to an entity: could not insert initial component data', 'warning' );
$this->messages->setMessage( 'Problem adding a new component to an entity: could not insert initial component data', 'warning' );
$this->debug->unguard( false );
return false;
}
}

$this->debug->unguard( $componentDataId );
return $componentDataId;
}


/**
* Removes a component from an existing entity
*
* @param int $component id of the component that should be removed from the entity
* @param int $entity id of the entity to delete the component from
*
* @return boolean
*/
public function removeComponentFromEntity( $component, $entity )
{
$this->debug->guard( );

$sql = "DELETE FROM game_component_" . $component . " WHERE id=(";
$sql .= "SELECT entitycomponent_componentdata FROM game_entity_components ";
$sql .= "WHERE entitycomponent_entity='" . $entity . "' AND entitycomponent_component='" . $component . "')";
$res = $this->database->query( $sql );
if ( !$res )
{
$this->debug->write( 'Problem removing a component from an entity: could not delete component data', 'warning' );
$this->messages->setMessage( 'Problem removing a component from an entity: could not delete component data', 'warning' );
$this->debug->unguard( false );
return false;
}

$sql = "DELETE FROM game_entity_components ";
$sql .= "WHERE entitycomponent_entity='" . $entity . "' AND entitycomponent_component='" . $component . "'";
$res = $this->database->query( $sql );
if ( !$res )
{
$this->debug->write( 'Problem removing a component from an entity: could not delete component to entity mapping', 'warning' );
$this->messages->setMessage( 'Problem removing a component from an entity: could not delete component to entity mapping', 'warning' );
$this->debug->unguard( false );
return false;
}

$this->debug->unguard( true );
return true;
}


/**
* Adds aassemblage components to a given entity
*
* @param int $assemblage id of the assemblage that should be applied to the entity
* @param int $entity id of the entity to add the assemblage components to
*
* @return boolean
*/
public function addAssemblageToEntity( $assemblage, $entity )
{
$this->debug->guard( );

$sql = "SELECT assemblagecomponent_component FROM game_assemblage_components WHERE assemblagecomponent_assemblage = '" . $assemblage . "'";
$res = $this->database->query( $sql );
if ( !$res )
{
$this->debug->write( 'Problem adding assemblage components to entity: could not get assemblage data from database', 'warning' );
$this->messages->setMessage( 'Problem adding assemblage components to entity: could not get assemblage data from database', 'warning' );
$this->debug->unguard( false );
return false;
}

while ( ( $component = $this->database->fetchArray( $res ) ) !== false )
{
if ( !$this->addComponentToEntity( $component ['assemblagecomponent_component'], $entity ) )
{
$this->debug->write( 'Problem adding assemblage components to entity: could not add the component to the entity', 'warning' );
$this->messages->setMessage( 'Problem adding assemblage components to entity: could not add the component to the entity', 'warning' );
$this->debug->unguard( false );
return false;
}
}

$this->debug->unguard( true );
return true;
}


/**
* Gets all datasets for a component
* This may be filtered by the second parameter, containing
* the component attribute name as key and the supposed value
* as value
* The return value will be $return[component_id][key] = value
*
*
* @param int $component id of the component to get the data from
* @param array $filter array containing the filter rules
*
* @return array
*/
public function getComponentData( $component, $filter = false )
{
$this->debug->guard( );

$sql = "SELECT * FROM game_component_" . $component . " ";

if ( is_array( $filter ) )
{
$sql .= "WHERE ";
foreach ( $filter as $attribute => $value )
{
$sql .= $attribute . "='" . $value . "' AND ";
}
$sql = substr( $sql, 0, -4 );
}

$res = $this->database->query( $sql );
if ( !$res )
{
$this->debug->write( 'Problem getting data for component: could not get component data', 'warning' );
$this->messages->setMessage( 'Problem getting data for component: could not get component data', 'warning' );
$this->debug->unguard( false );
return false;
}

$ret = array();
while ( ( $row = $this->database->fetchArray( $res ) ) !== false )
{
$ret [$row ['id']] = $row;
}

$this->debug->unguard( $ret );
return $ret;
}


/**
* Gets the dataset from a component of a specific entity
* This obviously can be only one dataset, so the return value
* is $return[key] = value
*
* @param int $component id of the component to get the data from
* @param int $entity id of the entity the component is bound to
*
* @return array
*/
public function getComponentDataForEntity( $component, $entity )
{
$this->debug->guard( );

$sql = "SELECT gc.* FROM game_component_" . $component . " gc ";
$sql .= "JOIN game_entity_components gec ON gc.id = gec.entitycomponent_componentdata ";
$sql .= "WHERE gec.entitycomponent_entity='" . $entity . "' AND gec.entitycomponent_component='" . $component . "'";

$res = $this->database->query( $sql );
if ( !$res )
{
$this->debug->write( 'Problem getting data for the component of an entity: could not get component data', 'warning' );
$this->messages->setMessage( 'Problem getting data for the component of an entity: could not get component data', 'warning' );
$this->debug->unguard( false );
return false;
}

$ret = $this->database->fetchArray( $res );

$this->debug->unguard( $ret );
return $ret;
}


/**
* Sets the data from a component of a specific component data entry
*
* @param int $component id of the component to set the data to
* @param int $entity id of the entity the component is bound to
* @param array $componentdata new component data as key / value pairs
*
* @return array
*/
public function setComponentData( $component, $entity, $componentdata )
{
$this->debug->guard( );

$sqlData = '';
foreach ( $componentdata as $componentKey => $componentValue )
{
$sqlData .= $componentKey . "='" . $componentValue . "',";
}
$sqlData = substr( $sqlData, 0, -1 );

$sql = "UPDATE game_component_" . $component . " gi SET " . $sqlData . " WHERE gi.id=(";
$sql .= "SELECT entitycomponent_componentdata FROM game_entity_components ";
$sql .= "WHERE entitycomponent_entity='" . $entity . "' AND entitycomponent_component='" . $component . "')";
$res = $this->database->query( $sql );
if ( !$res )
{
$this->debug->write( 'Problem setting data for the component of an entity: could not write component data', 'warning' );
$this->messages->setMessage( 'Problem setting data for the component of an entity: could not write component data', 'warning' );
$this->debug->unguard( false );
return false;
}

$ret = $this->database->fetchArray( $res );

$this->debug->unguard( $ret );
return $ret;
}


/**
* Gets the list of components for a specific entity
* Returns an array with the component id as key and the component
* data id as value
*
* @param int $entity id of the entity
*
* @return array
*/
public function getComponentListForEntity( $entity )
{
$this->debug->guard( );

$sql .= "SELECT entitycomponent_component, entitycomponent_componentdata FROM game_entity_components ";
$sql .= "WHERE entitycomponent_entity='" . $entity . "'";
$res = $this->database->query( $sql );
if ( !$res )
{
$this->debug->write( 'Problem getting data for the component of an entity: could not get component data', 'warning' );
$this->messages->setMessage( 'Problem getting data for the component of an entity: could not get component data', 'warning' );
$this->debug->unguard( false );
return false;
}

$componentList = array();
while ( ( $row = $this->database->fetchArray( $res ) ) !== false )
{
$componentList [$row ['entitycomponent_component']] = $row ['entitycomponent_component'];
}

$this->debug->unguard( $componentList );
return $componentList;
}


/**
* Gets the entity the owns a specific component dataset
*
* @param int $component id of the component
* @param int $componentdata id of the component data
*
* @return int
*/
public function getEntityForComponent( $component, $componentdata )
{
$this->debug->guard( );

$sql .= "SELECT entitycomponent_entity FROM game_entity_components ";
$sql .= "WHERE entitycomponent_component='" . $component . "' AND entitycomponent_componentdata='" . $componentdata . "')";
$res = $this->database->query( $sql );
if ( !$res )
{
$this->debug->write( 'Problem getting entity for component: could not get entity to component connection', 'warning' );
$this->messages->setMessage( 'Problem getting entity for component: could not get entity to component connection', 'warning' );
$this->debug->unguard( false );
return false;
}

$ret = $this->database->fetchArray( $res );

$this->debug->unguard( $ret );
return $ret;
}
}

?>

Change log

r716 by d...@songuer.de on May 27, 2010   Diff
New code style guidelines
Go to: 
Project members, sign in to write a code review

Older revisions

r702 by d...@songuer.de on Apr 18, 2010   Diff
Reintegrated PHPUnit3 branch.
The branch converted the old unit
tests for SimpleTest to PHPUnit3.
It also contains several style fixes
and some bugfixes of the framework
...
r644 by d...@songuer.de on Feb 6, 2010   Diff
Added new license information and
class descriptions
r619 by dirk on Dec 20, 2009   Diff
updated gamehandler to more consistent
interface
All revisions of this file

File info

Size: 14458 bytes, 434 lines
Powered by Google Project Hosting