Use the new getComplement method in the Initializer.
[UnsignedByte.git] / src / Initializer / Initializer.cpp
blob8b431798398585f8b81411c1b3ea8c5e777dc354
1 /***************************************************************************
2 * Copyright (C) 2008 by Sverre Rabbelier *
3 * sverre@rabbelier.nl *
4 * *
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. *
9 * *
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. *
14 * *
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"
24 #include "Managers.h"
25 #include "FieldImpls.h"
26 #include "TableImpls.h"
27 #include "FieldValues.h"
28 #include "Global.h"
29 #include "GameVersion.h"
30 #include "SqliteMgr.h"
31 #include "StringUtilities.h"
33 #include "AccountManager.h"
34 #include "Account.h"
35 #include "AreaManager.h"
36 #include "Area.h"
37 #include "ClusterManager.h"
38 #include "Cluster.h"
39 #include "RoomManager.h"
40 #include "Room.h"
41 #include "RaceManager.h"
42 #include "Race.h"
43 #include "CharacterManager.h"
44 #include "Character.h"
45 #include "SectorManager.h"
46 #include "Sector.h"
47 #include "ChannelManager.h"
48 #include "Channel.h"
49 #include "GrantGroupManager.h"
50 #include "GrantGroup.h"
51 #include "ColourManager.h"
52 #include "Colour.h"
53 #include "CommandManager.h"
54 #include "Command.h"
55 #include "ChunkManager.h"
56 #include "Chunk.h"
57 #include "ExitManager.h"
58 #include "Exit.h"
60 bool Initializer::VerifyDatabaseVersion()
62 bool equal = true;
64 KeyValuePtr key(new KeyValue(db::TableImpls::Get()->VERSION->VERSIONID, 1));
65 SavableManagerPtr manager;
67 try {
68 manager = SavableManager::bykey(key);
69 } catch(RowNotFoundException& e) {
70 Global::Get()->log("Could not retreive version info.");
71 return false;
74 if(manager->getValue(db::TableImpls::Get()->VERSION->MAJOR)->getIntegerValue() != game::major)
76 Global::Get()->log("Major / Major mismatch.\n");
77 equal = false;
80 if(manager->getValue(db::TableImpls::Get()->VERSION->MINOR)->getIntegerValue() != game::minor)
82 Global::Get()->logf("Minor / Minor mismatch.\n");
83 equal = false;
86 if(manager->getValue(db::TableImpls::Get()->VERSION->MICRO)->getIntegerValue() != game::micro)
88 Global::Get()->logf("Micro / Micro mismatch.\n");
89 equal = false;
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);
97 equal = false;
100 return equal;
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;
108 Assert(table);
110 SqliteMgr::Get()->initTable(table);
113 return;
116 bool Initializer::VerifyTables(TableImplVector::const_iterator begin, TableImplVector::const_iterator end)
118 bool good = true;
120 for(TableImplVector::const_iterator it = begin; it != end; it++)
122 TableImplPtr table = *it;
123 Assert(table);
125 bool success = SqliteMgr::Get()->tableValid(table);
126 if(!success)
128 Global::Get()->logf("Table %s's creation query does not match the one from the input database.\n", (*it)->getName().c_str());
129 good = false;
133 return good;
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
149 ValuePtr value;
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);
167 manager->save();
168 manager->unlock();
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");
183 account->Save();
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");
198 race->Save();
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);
215 character->Save();
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);
230 relation->save();
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);
246 sector->setWater(0);
247 sector->Save();
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);
278 grantgroup->Save();
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);
294 grantgroup->Save();
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);
310 grantgroup->Save();
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);
327 relation->save();
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);
344 relation->save();
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" },
361 // Light colours
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);
391 thecolour->Save();
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);
440 thecommand->Save();
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.");
459 area->setHeight(1);
460 area->setWidth(1);
461 area->setLength(1);
462 area->Save();
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.");
477 cluster->setArea(1);
478 cluster->Save();
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.");
493 room->setSector(1);
494 room->setCluster(1);
495 room->Save();
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());
509 chunk->setRoom(1);
510 chunk->setName("The void");
511 chunk->Save();
515 void Initializer::InitSampleChunks(int size)
517 int xdim = size;
518 int ydim = size;
519 int zdim = size;
521 KeysPtr keys;
523 // Sample sector
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("_");
530 sector->setWater(0);
531 sector->Save();
533 // Sample area
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.");
539 area->Save();
543 // Centre
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");
552 centre->Save();
556 // Defs
557 mud::ClusterPtr cluster;
558 mud::RoomPtr room;
559 mud::ChunkPtr chunk;
561 for(int z = 0; z < zdim; z++)
563 std::string zpart;
565 if(z < zdim/3)
566 zpart = "top";
567 else if(z < zdim*2/3)
568 zpart = "middle";
569 else
570 zpart = "bottom";
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());
578 cluster->Save();
581 for(int y = 0; y < ydim; y++)
583 std::string ypart;
585 if(y < ydim/3)
586 ypart = "left";
587 else if(y < ydim*2/3)
588 ypart = "centre";
589 else
590 ypart = "right";
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());
599 room->Save();
601 for(int x = 0; x < xdim; x++)
603 std::string xpart;
605 if(x < xdim/3)
606 xpart = "front";
607 else if(x < xdim*2/3)
608 xpart = "middle";
609 else
610 xpart = "back";
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"));
619 chunk->Save();
621 int centrepos = (size+1)/2-1;
623 if(z==centrepos && y==centrepos && x==centrepos)
625 centre->setRoom(room->getID());
626 centre->Save();
628 else
630 mud::ExitPtr exit;
632 keys = mud::Managers::Get()->Exit->Add();
633 exit = mud::Managers::Get()->Exit->GetByKey(keys->first()->getIntegerValue());
635 Coordinate c(x-centrepos,y-centrepos,z-centrepos);
636 exit->setDirection(c);
637 exit->setFromChunk(centre->getID());
638 exit->setToChunk(chunk->getID());
639 exit->Save();
642 keys = mud::Managers::Get()->Exit->Add();
643 exit = mud::Managers::Get()->Exit->GetByKey(keys->first()->getIntegerValue());
645 exit->setDirection(c.getComplement());
646 exit->setFromChunk(chunk->getID());
647 exit->setToChunk(centre->getID());
648 exit->Save();