1 /***************************************************************************
2 * Copyright (C) 2008 by Sverre Rabbelier *
3 * sverre@rabbelier.nl *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 3 of the License, or *
8 * (at your option) any later version. *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19 ***************************************************************************/
21 #include "Initializer.h"
22 #include "SavableHeaders.h"
23 #include "SavableTypes.h"
25 #include "FieldImpls.h"
26 #include "TableImpls.h"
27 #include "FieldValues.h"
29 #include "GameVersion.h"
30 #include "SqliteMgr.h"
31 #include "StringUtilities.h"
33 #include "AccountManager.h"
35 #include "AreaManager.h"
37 #include "ClusterManager.h"
39 #include "RoomManager.h"
41 #include "RaceManager.h"
43 #include "CharacterManager.h"
44 #include "Character.h"
45 #include "SectorManager.h"
47 #include "ChannelManager.h"
49 #include "GrantGroupManager.h"
50 #include "GrantGroup.h"
51 #include "ColourManager.h"
53 #include "CommandManager.h"
55 #include "ChunkManager.h"
57 #include "ExitManager.h"
60 bool Initializer::VerifyDatabaseVersion()
64 KeyValuePtr
key(new KeyValue(db::TableImpls::Get()->VERSION
->VERSIONID
, 1));
65 SavableManagerPtr manager
;
68 manager
= SavableManager::bykey(key
);
69 } catch(RowNotFoundException
& e
) {
70 Global::Get()->log("Could not retreive version info.");
74 if(manager
->getValue(db::TableImpls::Get()->VERSION
->MAJOR
)->getIntegerValue() != game::major
)
76 Global::Get()->log("Major / Major mismatch.\n");
80 if(manager
->getValue(db::TableImpls::Get()->VERSION
->MINOR
)->getIntegerValue() != game::minor
)
82 Global::Get()->logf("Minor / Minor mismatch.\n");
86 if(manager
->getValue(db::TableImpls::Get()->VERSION
->MICRO
)->getIntegerValue() != game::micro
)
88 Global::Get()->logf("Micro / Micro mismatch.\n");
92 if(manager
->getValue(db::TableImpls::Get()->VERSION
->VERSIONTEXT
)->getStringValue().compare(game::vstring
))
94 Global::Get()->logf("Versiontext / Vstring mismatch.\n");
95 Global::Get()->logf("dbversion is: '%s', ours is '%s'\n",
96 manager
->getValue(db::TableImpls::Get()->VERSION
->VERSIONTEXT
)->getStringValue().c_str(), game::vstring
);
103 void Initializer::InitTables(TableImplVector::const_iterator begin
, TableImplVector::const_iterator end
)
105 for(TableImplVector::const_iterator it
= begin
; it
!= end
; it
++)
107 TableImplPtr table
= *it
;
110 SqliteMgr::Get()->initTable(table
);
116 bool Initializer::VerifyTables(TableImplVector::const_iterator begin
, TableImplVector::const_iterator end
)
120 for(TableImplVector::const_iterator it
= begin
; it
!= end
; it
++)
122 TableImplPtr table
= *it
;
125 bool success
= SqliteMgr::Get()->tableValid(table
);
128 Global::Get()->logf("Table %s's creation query does not match the one from the input database.\n", (*it
)->getName().c_str());
136 void Initializer::InitDatabase()
140 KeyValuePtr
key(new KeyValue(db::TableImpls::Get()->VERSION
->VERSIONID
, 1));
141 SavableManagerPtr manager
= SavableManager::bykey(key
);
143 catch(RowNotFoundException
& e
)
145 SavableManagerPtr manager
= SavableManager::getnew(db::TableImpls::Get()->VERSION
);
146 bool locked
= manager
->lock();
147 Assert(locked
); // we're the only one active, should always be lockable
151 KeysPtr
keys(new Keys(db::TableImpls::Get()->VERSION
));
152 keys
->addKey(db::TableImpls::Get()->VERSION
->VERSIONID
, 1);
153 manager
->setKeys(keys
);
155 value
= ValuePtr(new FieldValue(db::TableImpls::Get()->VERSION
->MAJOR
, game::major
));
156 manager
->setValue(value
);
158 value
= ValuePtr(new FieldValue(db::TableImpls::Get()->VERSION
->MINOR
, game::minor
));
159 manager
->setValue(value
);
161 value
= ValuePtr(new FieldValue(db::TableImpls::Get()->VERSION
->MICRO
, game::micro
));
162 manager
->setValue(value
);
164 value
= ValuePtr(new FieldValue(db::TableImpls::Get()->VERSION
->VERSIONTEXT
, std::string(game::vstring
)));
165 manager
->setValue(value
);
173 KeyValuePtr
key(new KeyValue(db::TableImpls::Get()->ACCOUNTS
->ACCOUNTID
, 1));
174 SavableManagerPtr manager
= SavableManager::bykey(key
);
176 catch(RowNotFoundException
& e
)
178 KeysPtr keys
= mud::Managers::Get()->Account
->Add();
179 mud::AccountPtr account
= mud::Managers::Get()->Account
->GetByKey(keys
->first()->getIntegerValue());
181 account
->setName(game::vname
);
182 account
->setPassword("qq");
188 KeyValuePtr
key(new KeyValue(db::TableImpls::Get()->RACES
->RACEID
, 1));
189 SavableManagerPtr manager
= SavableManager::bykey(key
);
192 catch(RowNotFoundException
& e
)
194 KeysPtr keys
= mud::Managers::Get()->Race
->Add();
195 mud::RacePtr race
= mud::Managers::Get()->Race
->GetByKey(keys
->first()->getIntegerValue());
197 race
->setName("human");
203 KeyValuePtr
key(new KeyValue(db::TableImpls::Get()->ENTITIES
->ENTITYID
, 1));
204 SavableManagerPtr manager
= SavableManager::bykey(key
);
206 catch(RowNotFoundException
& e
)
208 KeysPtr keys
= mud::Managers::Get()->Character
->Add();
209 mud::CharacterPtr character
= mud::Managers::Get()->Character
->GetByKey(keys
->first()->getIntegerValue());
211 character
->setName(game::vname
);
212 character
->setDescription("This is the default character.");
213 character
->setRace(1);
214 character
->setChunk(1);
220 KeysPtr
keys(new Keys(db::TableImpls::Get()->CHARACTERACCOUNT
));
221 keys
->addKey(db::TableImpls::Get()->CHARACTERACCOUNT
->FKACCOUNTS
, 1);
222 keys
->addKey(db::TableImpls::Get()->CHARACTERACCOUNT
->FKENTITIES
, 1);
223 SavableManagerPtr manager
= SavableManager::bykeys(keys
);
225 catch(RowNotFoundException
& e
)
227 RelationPtr
relation(new Relation(db::TableImpls::Get()->CHARACTERACCOUNT
));
228 relation
->addKey(db::TableImpls::Get()->CHARACTERACCOUNT
->FKACCOUNTS
, 1);
229 relation
->addKey(db::TableImpls::Get()->CHARACTERACCOUNT
->FKENTITIES
, 1);
235 KeyValuePtr
key(new KeyValue(db::TableImpls::Get()->SECTORS
->SECTORID
, 1));
236 SavableManagerPtr manager
= SavableManager::bykey(key
);
238 catch(RowNotFoundException
& e
)
240 KeysPtr keys
= mud::Managers::Get()->Sector
->Add();
241 mud::SectorPtr sector
= mud::Managers::Get()->Sector
->GetByKey(keys
->first()->getIntegerValue());
243 sector
->setName("grass");
244 sector
->setSymbol(".");
245 sector
->setMoveCost(1);
252 KeyValuePtr
key(new KeyValue(db::TableImpls::Get()->CHANNELS
->CHANNELID
, 1));
253 SavableManagerPtr manager
= SavableManager::bykey(key
);
255 catch(RowNotFoundException
& e
)
257 KeysPtr keys
= mud::Managers::Get()->Channel
->Add();
258 mud::ChannelPtr channel
= mud::Managers::Get()->Channel
->GetByKey(keys
->first()->getIntegerValue());
260 channel
->setName("ooc");
261 channel
->setDescription("The Out of Character channel.");
262 channel
->setNeedLogin(1);
267 KeyValuePtr
key(new KeyValue(db::TableImpls::Get()->GRANTGROUPS
->GRANTGROUPID
, 1));
268 SavableManagerPtr manager
= SavableManager::bykey(key
);
270 catch(RowNotFoundException
& e
)
272 KeysPtr keys
= mud::Managers::Get()->GrantGroup
->Add();
273 mud::GrantGroupPtr grantgroup
= mud::Managers::Get()->GrantGroup
->GetByKey(keys
->first()->getIntegerValue());
275 grantgroup
->setName("All");
276 grantgroup
->setDefaultGrant(1);
277 grantgroup
->setDefaultLog(0);
283 KeyValuePtr
key(new KeyValue(db::TableImpls::Get()->GRANTGROUPS
->GRANTGROUPID
, 2));
284 SavableManagerPtr manager
= SavableManager::bykey(key
);
286 catch(RowNotFoundException
& e
)
288 KeysPtr keys
= mud::Managers::Get()->GrantGroup
->Add();
289 mud::GrantGroupPtr grantgroup
= mud::Managers::Get()->GrantGroup
->GetByKey(keys
->first()->getIntegerValue());
291 grantgroup
->setName("Admin");
292 grantgroup
->setDefaultGrant(0);
293 grantgroup
->setDefaultLog(1);
299 KeyValuePtr
key(new KeyValue(db::TableImpls::Get()->GRANTGROUPS
->GRANTGROUPID
, 3));
300 SavableManagerPtr manager
= SavableManager::bykey(key
);
302 catch(RowNotFoundException
& e
)
304 KeysPtr keys
= mud::Managers::Get()->GrantGroup
->Add();
305 mud::GrantGroupPtr grantgroup
= mud::Managers::Get()->GrantGroup
->GetByKey(keys
->first()->getIntegerValue());
307 grantgroup
->setName("Builder");
308 grantgroup
->setDefaultGrant(0);
309 grantgroup
->setDefaultLog(0);
315 KeysPtr
keys(new Keys(db::TableImpls::Get()->PERMISSIONS
));
316 keys
->addKey(db::TableImpls::Get()->PERMISSIONS
->FKACCOUNTS
, 1);
317 keys
->addKey(db::TableImpls::Get()->PERMISSIONS
->FKGRANTGROUPS
, 2);
318 SavableManagerPtr manager
= SavableManager::bykeys(keys
);
320 catch(RowNotFoundException
& e
)
322 RelationPtr
relation(new Relation(db::TableImpls::Get()->PERMISSIONS
));
323 relation
->addKey(db::TableImpls::Get()->PERMISSIONS
->FKACCOUNTS
, 1);
324 relation
->addKey(db::TableImpls::Get()->PERMISSIONS
->FKGRANTGROUPS
, 2);
325 relation
->addField(db::TableImpls::Get()->PERMISSIONS
->GRANT
, 1);
326 relation
->addField(db::TableImpls::Get()->PERMISSIONS
->LOG
, 1);
332 KeysPtr
keys(new Keys(db::TableImpls::Get()->PERMISSIONS
));
333 keys
->addKey(db::TableImpls::Get()->PERMISSIONS
->FKACCOUNTS
, 1);
334 keys
->addKey(db::TableImpls::Get()->PERMISSIONS
->FKGRANTGROUPS
, 3);
335 SavableManagerPtr manager
= SavableManager::bykeys(keys
);
337 catch(RowNotFoundException
& e
)
339 RelationPtr
relation(new Relation(db::TableImpls::Get()->PERMISSIONS
));
340 relation
->addKey(db::TableImpls::Get()->PERMISSIONS
->FKACCOUNTS
, 1);
341 relation
->addKey(db::TableImpls::Get()->PERMISSIONS
->FKGRANTGROUPS
, 3);
342 relation
->addField(db::TableImpls::Get()->PERMISSIONS
->GRANT
, 1);
343 relation
->addField(db::TableImpls::Get()->PERMISSIONS
->LOG
, 0);
348 void Initializer::InitColours()
350 const struct colour colours
[] =
352 { "Restore", "^", "0;0m" },
353 { "Dark Red", "r", "31m" },
354 { "Dark Green", "g", "32m" },
355 { "Dark Yellow", "y", "33m"},
356 { "Dark Blue", "b", "34m" },
357 { "Dark Purple", "m", "35m" },
358 { "Dark Cyan", "c", "36m" },
359 { "Gray", "w", "37m" },
360 { "Black", "l", "30m" },
362 { "Light Red", "R", "1;31m" },
363 { "Light blue", "G", "1;32m" },
364 { "Light Yellow", "Y", "1;33m" },
365 { "Light Blue", "B", "1;34m" },
366 { "Light Magenta", "M", "1;35m" },
367 { "Light Cyan", "C", "1;36m" },
368 { "White", "W", "1;37m" },
369 { "Light Gray", "L", "1;30m" },
372 int size
= sizeof(colours
) / sizeof(colours
[0]);
373 printf("Colours: %d.\n", size
);
375 for(int i
= 0; i
< size
; i
++)
379 ValuePtr
value(new FieldValue(db::TableImpls::Get()->COLOURS
->CODE
, colours
[i
].code
));
380 SavableManagerPtr manager
= SavableManager::byvalue(value
);
382 catch(RowNotFoundException
& e
)
384 KeysPtr keys
= mud::Managers::Get()->Colour
->Add();
385 mud::ColourPtr thecolour
= mud::Managers::Get()->Colour
->GetByKey(keys
->first()->getIntegerValue());
387 thecolour
->setName(colours
[i
].name
);
388 thecolour
->setCode(colours
[i
].code
);
389 thecolour
->setColourString(colours
[i
].cstr
);
390 thecolour
->setAnsi(1);
396 void Initializer::InitCommands()
398 const struct command commands
[] =
400 { "Account::Commands",1,1,1,0,"This command will list all available commands."},
401 { "Account::Login",1,1,1,0,"This command allows you to log in with one of your IC characters."},
402 { "Account::OLC",3,1,1,0,"This command will drop you into the OLC hub from which you can select any of the OLC editors."},
403 { "Account::OOC",1,1,1,0,"This command will drop you into OOC mode so that you can do ooc commands without the OOC prefix."},
404 { "Account::New",1,1,1,0,"This command will start the creation of a new IC character."},
405 { "Account::Quit",1,1,1,0,"This command will log you out and disconnect you, note that in other editors it will bring you back to the previous editor."},
406 { "Account::Shutdown",2,1,1,0,"This command will shut down the server."},
407 { "Account::List",1,1,1,0,"This command will provide you with a list of your characters."},
410 int size
= sizeof(commands
) / sizeof(commands
[0]);
411 printf("Commands: %d.\n", size
);
413 for(int i
= 0; i
< size
; i
++)
417 ValuePtr
value(new FieldValue(db::TableImpls::Get()->COMMANDS
->NAME
, commands
[i
].name
));
418 SavableManagerPtr manager
= SavableManager::byvalue(value
);
420 catch(RowNotFoundException
& e
)
422 KeysPtr keys
= mud::Managers::Get()->Command
->Add();
423 mud::CommandPtr thecommand
= mud::Managers::Get()->Command
->GetByKey(keys
->first()->getIntegerValue());
425 thecommand
->setName(commands
[i
].name
);
426 thecommand
->setHelp(commands
[i
].help
);
428 if(commands
[i
].grantgroup
!= 0)
429 thecommand
->setGrantGroup(commands
[i
].grantgroup
);
431 if(commands
[i
].highforce
== 0 || commands
[i
].highforce
== 1)
432 thecommand
->setHighForce(commands
[i
].highforce
);
434 if(commands
[i
].force
== 0 || commands
[i
].force
== 1)
435 thecommand
->setForce(commands
[i
].force
);
437 if(commands
[i
].lowforce
== 0 || commands
[i
].lowforce
== 1)
438 thecommand
->setLowForce(commands
[i
].lowforce
);
445 void Initializer::InitSpace()
449 KeyValuePtr
key(new KeyValue(db::TableImpls::Get()->AREAS
->AREAID
, 1));
450 SavableManagerPtr manager
= SavableManager::bykey(key
);
452 catch(RowNotFoundException
& e
)
454 KeysPtr keys
= mud::Managers::Get()->Area
->Add();
455 mud::AreaPtr area
= mud::Managers::Get()->Area
->GetByKey(keys
->first()->getIntegerValue());
457 area
->setName("Space");
458 area
->setDescription("This is space, the final frontier.");
467 KeyValuePtr
key(new KeyValue(db::TableImpls::Get()->CLUSTERS
->CLUSTERID
, 1));
468 SavableManagerPtr manager
= SavableManager::bykey(key
);
470 catch(RowNotFoundException
& e
)
472 KeysPtr keys
= mud::Managers::Get()->Cluster
->Add();
473 mud::ClusterPtr cluster
= mud::Managers::Get()->Cluster
->GetByKey(keys
->first()->getIntegerValue());
475 cluster
->setName("Alpha quadrant");
476 cluster
->setDescription("This is the alpha quadrant, where Earth lies.");
483 KeyValuePtr
key(new KeyValue(db::TableImpls::Get()->ROOMS
->ROOMID
, 1));
484 SavableManagerPtr manager
= SavableManager::bykey(key
);
486 catch(RowNotFoundException
& e
)
488 KeysPtr keys
= mud::Managers::Get()->Room
->Add();
489 mud::RoomPtr room
= mud::Managers::Get()->Room
->GetByKey(keys
->first()->getIntegerValue());
491 room
->setName("The Void");
492 room
->setDescription("You are in The Void.");
500 KeyValuePtr
key(new KeyValue(db::TableImpls::Get()->CHUNKS
->CHUNKID
, 1));
501 SavableManagerPtr manager
= SavableManager::bykey(key
);
504 catch(RowNotFoundException
& e
)
506 KeysPtr keys
= mud::Managers::Get()->Chunk
->Add();
507 mud::ChunkPtr chunk
= mud::Managers::Get()->Chunk
->GetByKey(keys
->first()->getIntegerValue());
510 chunk
->setName("The void");
515 void Initializer::InitSampleChunks(int size
)
524 keys
= mud::Managers::Get()->Sector
->Add();
525 mud::SectorPtr sector
= mud::Managers::Get()->Sector
->GetByKey(keys
->first()->getIntegerValue());
527 sector
->setName("Stone");
528 sector
->setMoveCost(1);
529 sector
->setSymbol("_");
534 keys
= mud::Managers::Get()->Area
->Add();
535 mud::AreaPtr area
= mud::Managers::Get()->Area
->GetByKey(keys
->first()->getIntegerValue());
537 area
->setName("The cube");
538 area
->setDescription("This is an example area, it contains a cube to try out nagivation.");
544 mud::ChunkPtr centre
;
546 keys
= mud::Managers::Get()->Chunk
->Add();
547 centre
= mud::Managers::Get()->Chunk
->GetByKey(keys
->first()->getIntegerValue());
549 centre
->setName("Core of the cube ");
550 centre
->setDescription("You are in the centre part of the Cube.");
551 centre
->setTags("Core Cube");
557 mud::ClusterPtr cluster
;
561 for(int z
= 0; z
< zdim
; z
++)
567 else if(z
< zdim
*2/3)
572 keys
= mud::Managers::Get()->Cluster
->Add();
573 cluster
= mud::Managers::Get()->Cluster
->GetByKey(keys
->first()->getIntegerValue());
575 cluster
->setName(zpart
+ std::string(" of the cube ") + String::Get()->fromInt(z
));
576 cluster
->setDescription(std::string("You are in the ") + zpart
+ std::string(" part of the Cube."));
577 cluster
->setArea(area
->getID());
581 for(int y
= 0; y
< ydim
; y
++)
587 else if(y
< ydim
*2/3)
592 keys
= mud::Managers::Get()->Room
->Add();
593 room
= mud::Managers::Get()->Room
->GetByKey(keys
->first()->getIntegerValue());
595 room
->setName(ypart
+ std::string(" of the cube ") + String::Get()->fromInt(y
));
596 room
->setDescription(std::string("You are in the ") + ypart
+ std::string(" part of the Cube."));
597 room
->setSector(sector
->getID());
598 room
->setCluster(cluster
->getID());
601 for(int x
= 0; x
< xdim
; x
++)
607 else if(x
< xdim
*2/3)
612 keys
= mud::Managers::Get()->Chunk
->Add();
613 chunk
= mud::Managers::Get()->Chunk
->GetByKey(keys
->first()->getIntegerValue());
615 chunk
->setName(xpart
+ std::string(" of the cube ") + String::Get()->fromInt(x
));
616 chunk
->setDescription(std::string("You are in the ") + xpart
+ std::string(" part of the Cube."));
617 chunk
->setRoom(room
->getID());
618 chunk
->setTags(zpart
+ " " + ypart
+ " " + xpart
+ std::string(" Cube"));
621 int centrepos
= (size
+1)/2-1;
623 if(z
==centrepos
&& y
==centrepos
&& x
==centrepos
)
625 centre
->setRoom(room
->getID());
630 keys
= mud::Managers::Get()->Exit
->Add();
631 mud::ExitPtr exit
= mud::Managers::Get()->Exit
->GetByKey(keys
->first()->getIntegerValue());
633 Coordinate
c(x
-centrepos
,y
-centrepos
,z
-centrepos
);
634 exit
->setDirection(c
);
635 exit
->setFromChunk(centre
->getID());
636 exit
->setToChunk(chunk
->getID());