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 ***************************************************************************/
22 #include "AccountManager.h"
24 #include "AreaManager.h"
26 #include "ChannelManager.h"
27 #include "Character.h"
28 #include "CharacterManager.h"
30 #include "ChunkManager.h"
32 #include "ClusterManager.h"
34 #include "ColourManager.h"
36 #include "CommandManager.h"
38 #include "ExitManager.h"
39 #include "FieldImpls.h"
40 #include "FieldValues.h"
41 #include "GameVersion.h"
43 #include "GrantGroup.h"
44 #include "GrantGroupManager.h"
45 #include "Initializer.h"
48 #include "RaceManager.h"
50 #include "RoomManager.h"
51 #include "SavableHeaders.h"
52 #include "SavableTypes.h"
54 #include "SectorManager.h"
55 #include "SqliteMgr.h"
56 #include "StringUtilities.h"
57 #include "TableImpls.h"
59 bool Initializer::VerifyDatabaseVersion()
63 KeyValuePtr
key(new KeyValue(db::TableImpls::Get()->VERSION
->VERSIONID
, 1));
64 SavableManagerPtr manager
;
67 manager
= SavableManager::bykey(key
);
68 } catch(RowNotFoundException
& e
) {
69 Global::Get()->printlog("Could not retreive version info.");
73 if(manager
->getValue(db::TableImpls::Get()->VERSION
->MAJOR
)->getIntegerValue() != game::major
)
75 Global::Get()->printlog("Major / Major mismatch.\n");
79 if(manager
->getValue(db::TableImpls::Get()->VERSION
->MINOR
)->getIntegerValue() != game::minor
)
81 Global::Get()->printlog("Minor / Minor mismatch.\n");
85 if(manager
->getValue(db::TableImpls::Get()->VERSION
->MICRO
)->getIntegerValue() != game::micro
)
87 Global::Get()->printlog("Micro / Micro mismatch.\n");
91 if(manager
->getValue(db::TableImpls::Get()->VERSION
->VERSIONTEXT
)->getStringValue().compare(game::vstring
))
93 Global::Get()->printlog("Versiontext / Vstring mismatch.\n");
95 std::string msg
= "dbversion is: '";
96 msg
.append(manager
->getValue(db::TableImpls::Get()->VERSION
->VERSIONTEXT
)->getStringValue());
97 msg
.append("', ours is '");
98 msg
.append(game::vstring
);
101 Global::Get()->printlog(msg
);
109 void Initializer::InitTables(TableImplVector::const_iterator begin
, TableImplVector::const_iterator end
)
111 for(TableImplVector::const_iterator it
= begin
; it
!= end
; it
++)
113 TableImplPtr table
= *it
;
116 SqliteMgr::Get()->initTable(table
);
122 bool Initializer::VerifyTables(TableImplVector::const_iterator begin
, TableImplVector::const_iterator end
)
126 for(TableImplVector::const_iterator it
= begin
; it
!= end
; it
++)
128 TableImplPtr table
= *it
;
131 bool success
= SqliteMgr::Get()->tableValid(table
);
134 std::string msg
= "Table ";
135 msg
.append((*it
)->getName());
136 msg
.append("'s creation query does not match the one from the input database.\n");
138 Global::Get()->printlog(msg
);
147 void Initializer::InitDatabase()
151 KeyValuePtr
key(new KeyValue(db::TableImpls::Get()->VERSION
->VERSIONID
, 1));
152 SavableManagerPtr manager
= SavableManager::bykey(key
);
154 catch(RowNotFoundException
& e
)
156 SavableManagerPtr manager
= SavableManager::getnew(db::TableImpls::Get()->VERSION
);
157 bool locked
= manager
->lock();
158 Assert(locked
); // we're the only one active, should always be lockable
162 KeysPtr
keys(new Keys(db::TableImpls::Get()->VERSION
));
163 keys
->addKey(db::TableImpls::Get()->VERSION
->VERSIONID
, 1);
164 manager
->setKeys(keys
);
166 value
= ValuePtr(new FieldValue(db::TableImpls::Get()->VERSION
->MAJOR
, game::major
));
167 manager
->setValue(value
);
169 value
= ValuePtr(new FieldValue(db::TableImpls::Get()->VERSION
->MINOR
, game::minor
));
170 manager
->setValue(value
);
172 value
= ValuePtr(new FieldValue(db::TableImpls::Get()->VERSION
->MICRO
, game::micro
));
173 manager
->setValue(value
);
175 value
= ValuePtr(new FieldValue(db::TableImpls::Get()->VERSION
->VERSIONTEXT
, std::string(game::vstring
)));
176 manager
->setValue(value
);
184 KeyValuePtr
key(new KeyValue(db::TableImpls::Get()->ACCOUNTS
->ACCOUNTID
, 1));
185 SavableManagerPtr manager
= SavableManager::bykey(key
);
187 catch(RowNotFoundException
& e
)
189 KeysPtr keys
= mud::Managers::Get()->Account
->Add();
190 mud::AccountPtr account
= mud::Managers::Get()->Account
->GetByKey(keys
->first()->getIntegerValue());
192 account
->setName(game::vname
);
193 account
->setPassword("qq");
199 KeyValuePtr
key(new KeyValue(db::TableImpls::Get()->RACES
->RACEID
, 1));
200 SavableManagerPtr manager
= SavableManager::bykey(key
);
203 catch(RowNotFoundException
& e
)
205 KeysPtr keys
= mud::Managers::Get()->Race
->Add();
206 mud::RacePtr race
= mud::Managers::Get()->Race
->GetByKey(keys
->first()->getIntegerValue());
208 race
->setName("human");
214 KeyValuePtr
key(new KeyValue(db::TableImpls::Get()->ENTITIES
->ENTITYID
, 1));
215 SavableManagerPtr manager
= SavableManager::bykey(key
);
217 catch(RowNotFoundException
& e
)
219 KeysPtr keys
= mud::Managers::Get()->Character
->Add();
220 mud::CharacterPtr character
= mud::Managers::Get()->Character
->GetByKey(keys
->first()->getIntegerValue());
222 character
->setName(game::vname
);
223 character
->setDescription("This is the default character.");
224 character
->setRace(1);
225 character
->setChunk(1);
231 KeysPtr
keys(new Keys(db::TableImpls::Get()->CHARACTERACCOUNT
));
232 keys
->addKey(db::TableImpls::Get()->CHARACTERACCOUNT
->FKACCOUNTS
, 1);
233 keys
->addKey(db::TableImpls::Get()->CHARACTERACCOUNT
->FKENTITIES
, 1);
234 SavableManagerPtr manager
= SavableManager::bykeys(keys
);
236 catch(RowNotFoundException
& e
)
238 RelationPtr
relation(new Relation(db::TableImpls::Get()->CHARACTERACCOUNT
));
239 relation
->addKey(db::TableImpls::Get()->CHARACTERACCOUNT
->FKACCOUNTS
, 1);
240 relation
->addKey(db::TableImpls::Get()->CHARACTERACCOUNT
->FKENTITIES
, 1);
246 KeyValuePtr
key(new KeyValue(db::TableImpls::Get()->SECTORS
->SECTORID
, 1));
247 SavableManagerPtr manager
= SavableManager::bykey(key
);
249 catch(RowNotFoundException
& e
)
251 KeysPtr keys
= mud::Managers::Get()->Sector
->Add();
252 mud::SectorPtr sector
= mud::Managers::Get()->Sector
->GetByKey(keys
->first()->getIntegerValue());
254 sector
->setName("grass");
255 sector
->setSymbol(".");
256 sector
->setMoveCost(1);
263 KeyValuePtr
key(new KeyValue(db::TableImpls::Get()->CHANNELS
->CHANNELID
, 1));
264 SavableManagerPtr manager
= SavableManager::bykey(key
);
266 catch(RowNotFoundException
& e
)
268 KeysPtr keys
= mud::Managers::Get()->Channel
->Add();
269 mud::ChannelPtr channel
= mud::Managers::Get()->Channel
->GetByKey(keys
->first()->getIntegerValue());
271 channel
->setName("ooc");
272 channel
->setDescription("The Out of Character channel.");
273 channel
->setNeedLogin(1);
278 KeyValuePtr
key(new KeyValue(db::TableImpls::Get()->GRANTGROUPS
->GRANTGROUPID
, 1));
279 SavableManagerPtr manager
= SavableManager::bykey(key
);
281 catch(RowNotFoundException
& e
)
283 KeysPtr keys
= mud::Managers::Get()->GrantGroup
->Add();
284 mud::GrantGroupPtr grantgroup
= mud::Managers::Get()->GrantGroup
->GetByKey(keys
->first()->getIntegerValue());
286 grantgroup
->setName("All");
287 grantgroup
->setDefaultGrant(1);
288 grantgroup
->setDefaultLog(0);
294 KeyValuePtr
key(new KeyValue(db::TableImpls::Get()->GRANTGROUPS
->GRANTGROUPID
, 2));
295 SavableManagerPtr manager
= SavableManager::bykey(key
);
297 catch(RowNotFoundException
& e
)
299 KeysPtr keys
= mud::Managers::Get()->GrantGroup
->Add();
300 mud::GrantGroupPtr grantgroup
= mud::Managers::Get()->GrantGroup
->GetByKey(keys
->first()->getIntegerValue());
302 grantgroup
->setName("Admin");
303 grantgroup
->setDefaultGrant(0);
304 grantgroup
->setDefaultLog(1);
310 KeyValuePtr
key(new KeyValue(db::TableImpls::Get()->GRANTGROUPS
->GRANTGROUPID
, 3));
311 SavableManagerPtr manager
= SavableManager::bykey(key
);
313 catch(RowNotFoundException
& e
)
315 KeysPtr keys
= mud::Managers::Get()->GrantGroup
->Add();
316 mud::GrantGroupPtr grantgroup
= mud::Managers::Get()->GrantGroup
->GetByKey(keys
->first()->getIntegerValue());
318 grantgroup
->setName("Builder");
319 grantgroup
->setDefaultGrant(0);
320 grantgroup
->setDefaultLog(0);
326 KeysPtr
keys(new Keys(db::TableImpls::Get()->PERMISSIONS
));
327 keys
->addKey(db::TableImpls::Get()->PERMISSIONS
->FKACCOUNTS
, 1);
328 keys
->addKey(db::TableImpls::Get()->PERMISSIONS
->FKGRANTGROUPS
, 2);
329 SavableManagerPtr manager
= SavableManager::bykeys(keys
);
331 catch(RowNotFoundException
& e
)
333 RelationPtr
relation(new Relation(db::TableImpls::Get()->PERMISSIONS
));
334 relation
->addKey(db::TableImpls::Get()->PERMISSIONS
->FKACCOUNTS
, 1);
335 relation
->addKey(db::TableImpls::Get()->PERMISSIONS
->FKGRANTGROUPS
, 2);
336 relation
->addField(db::TableImpls::Get()->PERMISSIONS
->GRANT
, 1);
337 relation
->addField(db::TableImpls::Get()->PERMISSIONS
->LOG
, 1);
343 KeysPtr
keys(new Keys(db::TableImpls::Get()->PERMISSIONS
));
344 keys
->addKey(db::TableImpls::Get()->PERMISSIONS
->FKACCOUNTS
, 1);
345 keys
->addKey(db::TableImpls::Get()->PERMISSIONS
->FKGRANTGROUPS
, 3);
346 SavableManagerPtr manager
= SavableManager::bykeys(keys
);
348 catch(RowNotFoundException
& e
)
350 RelationPtr
relation(new Relation(db::TableImpls::Get()->PERMISSIONS
));
351 relation
->addKey(db::TableImpls::Get()->PERMISSIONS
->FKACCOUNTS
, 1);
352 relation
->addKey(db::TableImpls::Get()->PERMISSIONS
->FKGRANTGROUPS
, 3);
353 relation
->addField(db::TableImpls::Get()->PERMISSIONS
->GRANT
, 1);
354 relation
->addField(db::TableImpls::Get()->PERMISSIONS
->LOG
, 0);
359 void Initializer::InitColours()
361 const struct colour colours
[] =
363 { "Restore", "^", "0;0m" },
364 { "Dark Red", "r", "31m" },
365 { "Dark Green", "g", "32m" },
366 { "Dark Yellow", "y", "33m"},
367 { "Dark Blue", "b", "34m" },
368 { "Dark Purple", "m", "35m" },
369 { "Dark Cyan", "c", "36m" },
370 { "Gray", "w", "37m" },
371 { "Black", "l", "30m" },
373 { "Light Red", "R", "1;31m" },
374 { "Light blue", "G", "1;32m" },
375 { "Light Yellow", "Y", "1;33m" },
376 { "Light Blue", "B", "1;34m" },
377 { "Light Magenta", "M", "1;35m" },
378 { "Light Cyan", "C", "1;36m" },
379 { "White", "W", "1;37m" },
380 { "Light Gray", "L", "1;30m" },
383 int size
= sizeof(colours
) / sizeof(colours
[0]);
384 printf("Colours: %d.\n", size
);
386 for(int i
= 0; i
< size
; i
++)
390 ValuePtr
value(new FieldValue(db::TableImpls::Get()->COLOURS
->CODE
, colours
[i
].code
));
391 SavableManagerPtr manager
= SavableManager::byvalue(value
);
393 catch(RowNotFoundException
& e
)
395 KeysPtr keys
= mud::Managers::Get()->Colour
->Add();
396 mud::ColourPtr thecolour
= mud::Managers::Get()->Colour
->GetByKey(keys
->first()->getIntegerValue());
398 thecolour
->setName(colours
[i
].name
);
399 thecolour
->setCode(colours
[i
].code
);
400 thecolour
->setColourString(colours
[i
].cstr
);
401 thecolour
->setAnsi(1);
407 void Initializer::InitCommands()
409 const struct command commands
[] =
411 { "Account::Commands",1,1,1,0,"This command will list all available commands."},
412 { "Account::Login",1,1,1,0,"This command allows you to log in with one of your IC characters."},
413 { "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."},
414 { "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."},
415 { "Account::New",1,1,1,0,"This command will start the creation of a new IC character."},
416 { "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."},
417 { "Account::Shutdown",2,1,1,0,"This command will shut down the server."},
418 { "Account::List",1,1,1,0,"This command will provide you with a list of your characters."},
421 int size
= sizeof(commands
) / sizeof(commands
[0]);
422 printf("Commands: %d.\n", size
);
424 for(int i
= 0; i
< size
; i
++)
428 ValuePtr
value(new FieldValue(db::TableImpls::Get()->COMMANDS
->NAME
, commands
[i
].name
));
429 SavableManagerPtr manager
= SavableManager::byvalue(value
);
431 catch(RowNotFoundException
& e
)
433 KeysPtr keys
= mud::Managers::Get()->Command
->Add();
434 mud::CommandPtr thecommand
= mud::Managers::Get()->Command
->GetByKey(keys
->first()->getIntegerValue());
436 thecommand
->setName(commands
[i
].name
);
437 thecommand
->setHelp(commands
[i
].help
);
439 if(commands
[i
].grantgroup
!= 0)
440 thecommand
->setGrantGroup(commands
[i
].grantgroup
);
442 if(commands
[i
].highforce
== 0 || commands
[i
].highforce
== 1)
443 thecommand
->setHighForce(commands
[i
].highforce
);
445 if(commands
[i
].force
== 0 || commands
[i
].force
== 1)
446 thecommand
->setForce(commands
[i
].force
);
448 if(commands
[i
].lowforce
== 0 || commands
[i
].lowforce
== 1)
449 thecommand
->setLowForce(commands
[i
].lowforce
);
456 void Initializer::InitSpace()
460 KeyValuePtr
key(new KeyValue(db::TableImpls::Get()->AREAS
->AREAID
, 1));
461 SavableManagerPtr manager
= SavableManager::bykey(key
);
463 catch(RowNotFoundException
& e
)
465 KeysPtr keys
= mud::Managers::Get()->Area
->Add();
466 mud::AreaPtr area
= mud::Managers::Get()->Area
->GetByKey(keys
->first()->getIntegerValue());
468 area
->setName("Space");
469 area
->setDescription("This is space, the final frontier.");
478 KeyValuePtr
key(new KeyValue(db::TableImpls::Get()->CLUSTERS
->CLUSTERID
, 1));
479 SavableManagerPtr manager
= SavableManager::bykey(key
);
481 catch(RowNotFoundException
& e
)
483 KeysPtr keys
= mud::Managers::Get()->Cluster
->Add();
484 mud::ClusterPtr cluster
= mud::Managers::Get()->Cluster
->GetByKey(keys
->first()->getIntegerValue());
486 cluster
->setName("Alpha quadrant");
487 cluster
->setDescription("This is the alpha quadrant, where Earth lies.");
494 KeyValuePtr
key(new KeyValue(db::TableImpls::Get()->ROOMS
->ROOMID
, 1));
495 SavableManagerPtr manager
= SavableManager::bykey(key
);
497 catch(RowNotFoundException
& e
)
499 KeysPtr keys
= mud::Managers::Get()->Room
->Add();
500 mud::RoomPtr room
= mud::Managers::Get()->Room
->GetByKey(keys
->first()->getIntegerValue());
502 room
->setName("The Void");
503 room
->setDescription("You are in The Void.");
511 KeyValuePtr
key(new KeyValue(db::TableImpls::Get()->CHUNKS
->CHUNKID
, 1));
512 SavableManagerPtr manager
= SavableManager::bykey(key
);
515 catch(RowNotFoundException
& e
)
517 KeysPtr keys
= mud::Managers::Get()->Chunk
->Add();
518 mud::ChunkPtr chunk
= mud::Managers::Get()->Chunk
->GetByKey(keys
->first()->getIntegerValue());
521 chunk
->setName("The void");
526 void Initializer::InitSampleChunks(int size
)
535 keys
= mud::Managers::Get()->Sector
->Add();
536 mud::SectorPtr sector
= mud::Managers::Get()->Sector
->GetByKey(keys
->first()->getIntegerValue());
538 sector
->setName("Stone");
539 sector
->setMoveCost(1);
540 sector
->setSymbol("_");
545 keys
= mud::Managers::Get()->Area
->Add();
546 mud::AreaPtr area
= mud::Managers::Get()->Area
->GetByKey(keys
->first()->getIntegerValue());
548 area
->setName("The cube");
549 area
->setDescription("This is an example area, it contains a cube to try out nagivation.");
555 mud::ChunkPtr centre
;
557 keys
= mud::Managers::Get()->Chunk
->Add();
558 centre
= mud::Managers::Get()->Chunk
->GetByKey(keys
->first()->getIntegerValue());
560 centre
->setName("Core of the cube ");
561 centre
->setDescription("You are in the centre part of the Cube.");
562 centre
->setTags("Core Cube");
568 mud::ClusterPtr cluster
;
572 for(int z
= 0; z
< zdim
; z
++)
578 else if(z
< zdim
*2/3)
583 keys
= mud::Managers::Get()->Cluster
->Add();
584 cluster
= mud::Managers::Get()->Cluster
->GetByKey(keys
->first()->getIntegerValue());
586 cluster
->setName(zpart
+ std::string(" of the cube ") + String::Get()->fromInt(z
));
587 cluster
->setDescription(std::string("You are in the ") + zpart
+ std::string(" part of the Cube."));
588 cluster
->setArea(area
->getID());
592 for(int y
= 0; y
< ydim
; y
++)
598 else if(y
< ydim
*2/3)
603 keys
= mud::Managers::Get()->Room
->Add();
604 room
= mud::Managers::Get()->Room
->GetByKey(keys
->first()->getIntegerValue());
606 room
->setName(ypart
+ std::string(" of the cube ") + String::Get()->fromInt(y
));
607 room
->setDescription(std::string("You are in the ") + ypart
+ std::string(" part of the Cube."));
608 room
->setSector(sector
->getID());
609 room
->setCluster(cluster
->getID());
612 for(int x
= 0; x
< xdim
; x
++)
618 else if(x
< xdim
*2/3)
623 keys
= mud::Managers::Get()->Chunk
->Add();
624 chunk
= mud::Managers::Get()->Chunk
->GetByKey(keys
->first()->getIntegerValue());
626 chunk
->setName(xpart
+ std::string(" of the cube ") + String::Get()->fromInt(x
));
627 chunk
->setDescription(std::string("You are in the ") + xpart
+ std::string(" part of the Cube."));
628 chunk
->setRoom(room
->getID());
629 chunk
->setTags(zpart
+ " " + ypart
+ " " + xpart
+ std::string(" Cube"));
632 int centrepos
= (size
+1)/2-1;
634 if(z
==centrepos
&& y
==centrepos
&& x
==centrepos
)
636 centre
->setRoom(room
->getID());
643 keys
= mud::Managers::Get()->Exit
->Add();
644 exit
= mud::Managers::Get()->Exit
->GetByKey(keys
->first()->getIntegerValue());
646 Coordinate
c(x
-centrepos
,y
-centrepos
,z
-centrepos
);
647 exit
->setDirection(c
);
648 exit
->setFromChunk(centre
->getID());
649 exit
->setToChunk(chunk
->getID());
653 keys
= mud::Managers::Get()->Exit
->Add();
654 exit
= mud::Managers::Get()->Exit
->GetByKey(keys
->first()->getIntegerValue());
656 exit
->setDirection(c
.getComplement());
657 exit
->setFromChunk(chunk
->getID());
658 exit
->setToChunk(centre
->getID());