Added a EditorSocial.
[UnsignedByte.git] / src / Initializer / Initializer.cpp
blob2d04a55dad2f170a822f5a90983897e0bd50afcb
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 "Account.h"
22 #include "AccountManager.h"
23 #include "Area.h"
24 #include "AreaManager.h"
25 #include "Channel.h"
26 #include "ChannelManager.h"
27 #include "Character.h"
28 #include "CharacterManager.h"
29 #include "Chunk.h"
30 #include "ChunkManager.h"
31 #include "Cluster.h"
32 #include "ClusterManager.h"
33 #include "Colour.h"
34 #include "ColourManager.h"
35 #include "Command.h"
36 #include "CommandManager.h"
37 #include "Exit.h"
38 #include "ExitManager.h"
39 #include "FieldImpls.h"
40 #include "FieldValues.h"
41 #include "GameVersion.h"
42 #include "Global.h"
43 #include "GrantGroup.h"
44 #include "GrantGroupManager.h"
45 #include "Initializer.h"
46 #include "Managers.h"
47 #include "Race.h"
48 #include "RaceManager.h"
49 #include "Room.h"
50 #include "RoomManager.h"
51 #include "SavableHeaders.h"
52 #include "SavableTypes.h"
53 #include "Sector.h"
54 #include "SectorManager.h"
55 #include "SqliteMgr.h"
56 #include "StringUtilities.h"
57 #include "TableImpls.h"
59 bool Initializer::VerifyDatabaseVersion()
61 bool equal = true;
63 KeyValuePtr key(new KeyValue(db::TableImpls::Get()->VERSION->VERSIONID, 1));
64 SavableManagerPtr manager;
66 try {
67 manager = SavableManager::bykey(key);
68 } catch(RowNotFoundException& e) {
69 Global::Get()->printlog("Could not retreive version info.");
70 return false;
73 if(manager->getValue(db::TableImpls::Get()->VERSION->MAJOR)->getIntegerValue() != game::major)
75 Global::Get()->printlog("Major / Major mismatch.\n");
76 equal = false;
79 if(manager->getValue(db::TableImpls::Get()->VERSION->MINOR)->getIntegerValue() != game::minor)
81 Global::Get()->printlog("Minor / Minor mismatch.\n");
82 equal = false;
85 if(manager->getValue(db::TableImpls::Get()->VERSION->MICRO)->getIntegerValue() != game::micro)
87 Global::Get()->printlog("Micro / Micro mismatch.\n");
88 equal = false;
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);
99 msg.append("'\n");
101 Global::Get()->printlog(msg);
103 equal = false;
106 return equal;
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;
114 Assert(table);
116 SqliteMgr::Get()->initTable(table);
119 return;
122 bool Initializer::VerifyTables(TableImplVector::const_iterator begin, TableImplVector::const_iterator end)
124 bool good = true;
126 for(TableImplVector::const_iterator it = begin; it != end; it++)
128 TableImplPtr table = *it;
129 Assert(table);
131 bool success = SqliteMgr::Get()->tableValid(table);
132 if(!success)
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);
140 good = false;
144 return good;
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
160 ValuePtr value;
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);
178 manager->save();
179 manager->unlock();
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");
194 account->Save();
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");
209 race->Save();
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);
226 character->Save();
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);
241 relation->save();
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);
257 sector->setWater(0);
258 sector->Save();
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);
289 grantgroup->Save();
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);
305 grantgroup->Save();
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);
321 grantgroup->Save();
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);
338 relation->save();
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);
355 relation->save();
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" },
372 // Light colours
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);
402 thecolour->Save();
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);
451 thecommand->Save();
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.");
470 area->setHeight(1);
471 area->setWidth(1);
472 area->setLength(1);
473 area->Save();
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.");
488 cluster->setArea(1);
489 cluster->Save();
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.");
504 room->setSector(1);
505 room->setCluster(1);
506 room->Save();
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());
520 chunk->setRoom(1);
521 chunk->setName("The void");
522 chunk->Save();
526 void Initializer::InitSampleChunks(int size)
528 int xdim = size;
529 int ydim = size;
530 int zdim = size;
532 KeysPtr keys;
534 // Sample sector
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("_");
541 sector->setWater(0);
542 sector->Save();
544 // Sample area
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.");
550 area->Save();
554 // Centre
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");
563 centre->Save();
567 // Defs
568 mud::ClusterPtr cluster;
569 mud::RoomPtr room;
570 mud::ChunkPtr chunk;
572 for(int z = 0; z < zdim; z++)
574 std::string zpart;
576 if(z < zdim/3)
577 zpart = "top";
578 else if(z < zdim*2/3)
579 zpart = "middle";
580 else
581 zpart = "bottom";
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());
589 cluster->Save();
592 for(int y = 0; y < ydim; y++)
594 std::string ypart;
596 if(y < ydim/3)
597 ypart = "left";
598 else if(y < ydim*2/3)
599 ypart = "centre";
600 else
601 ypart = "right";
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());
610 room->Save();
612 for(int x = 0; x < xdim; x++)
614 std::string xpart;
616 if(x < xdim/3)
617 xpart = "front";
618 else if(x < xdim*2/3)
619 xpart = "middle";
620 else
621 xpart = "back";
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"));
630 chunk->Save();
632 int centrepos = (size+1)/2-1;
634 if(z==centrepos && y==centrepos && x==centrepos)
636 centre->setRoom(room->getID());
637 centre->Save();
639 else
641 mud::ExitPtr exit;
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());
650 exit->Save();
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());
659 exit->Save();