Sorted Socket.
[UnsignedByte.git] / src / Initializer / Initializer.cpp
blob4912fd16a485519e33c422a3c325f252a817cf7c
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()->printlog("Could not retreive version info.");
71 return false;
74 if(manager->getValue(db::TableImpls::Get()->VERSION->MAJOR)->getIntegerValue() != game::major)
76 Global::Get()->printlog("Major / Major mismatch.\n");
77 equal = false;
80 if(manager->getValue(db::TableImpls::Get()->VERSION->MINOR)->getIntegerValue() != game::minor)
82 Global::Get()->printlog("Minor / Minor mismatch.\n");
83 equal = false;
86 if(manager->getValue(db::TableImpls::Get()->VERSION->MICRO)->getIntegerValue() != game::micro)
88 Global::Get()->printlog("Micro / Micro mismatch.\n");
89 equal = false;
92 if(manager->getValue(db::TableImpls::Get()->VERSION->VERSIONTEXT)->getStringValue().compare(game::vstring))
94 Global::Get()->printlog("Versiontext / Vstring mismatch.\n");
96 std::string msg = "dbversion is: '";
97 msg.append(manager->getValue(db::TableImpls::Get()->VERSION->VERSIONTEXT)->getStringValue());
98 msg.append("', ours is '");
99 msg.append(game::vstring);
100 msg.append("'\n");
102 Global::Get()->printlog(msg);
104 equal = false;
107 return equal;
110 void Initializer::InitTables(TableImplVector::const_iterator begin, TableImplVector::const_iterator end)
112 for(TableImplVector::const_iterator it = begin; it != end; it++)
114 TableImplPtr table = *it;
115 Assert(table);
117 SqliteMgr::Get()->initTable(table);
120 return;
123 bool Initializer::VerifyTables(TableImplVector::const_iterator begin, TableImplVector::const_iterator end)
125 bool good = true;
127 for(TableImplVector::const_iterator it = begin; it != end; it++)
129 TableImplPtr table = *it;
130 Assert(table);
132 bool success = SqliteMgr::Get()->tableValid(table);
133 if(!success)
135 std::string msg = "Table ";
136 msg.append((*it)->getName());
137 msg.append("'s creation query does not match the one from the input database.\n");
139 Global::Get()->printlog(msg);
141 good = false;
145 return good;
148 void Initializer::InitDatabase()
152 KeyValuePtr key(new KeyValue(db::TableImpls::Get()->VERSION->VERSIONID, 1));
153 SavableManagerPtr manager = SavableManager::bykey(key);
155 catch(RowNotFoundException& e)
157 SavableManagerPtr manager = SavableManager::getnew(db::TableImpls::Get()->VERSION);
158 bool locked = manager->lock();
159 Assert(locked); // we're the only one active, should always be lockable
161 ValuePtr value;
163 KeysPtr keys(new Keys(db::TableImpls::Get()->VERSION));
164 keys->addKey(db::TableImpls::Get()->VERSION->VERSIONID, 1);
165 manager->setKeys(keys);
167 value = ValuePtr(new FieldValue(db::TableImpls::Get()->VERSION->MAJOR, game::major));
168 manager->setValue(value);
170 value = ValuePtr(new FieldValue(db::TableImpls::Get()->VERSION->MINOR, game::minor));
171 manager->setValue(value);
173 value = ValuePtr(new FieldValue(db::TableImpls::Get()->VERSION->MICRO, game::micro));
174 manager->setValue(value);
176 value = ValuePtr(new FieldValue(db::TableImpls::Get()->VERSION->VERSIONTEXT, std::string(game::vstring)));
177 manager->setValue(value);
179 manager->save();
180 manager->unlock();
185 KeyValuePtr key(new KeyValue(db::TableImpls::Get()->ACCOUNTS->ACCOUNTID, 1));
186 SavableManagerPtr manager = SavableManager::bykey(key);
188 catch(RowNotFoundException& e)
190 KeysPtr keys = mud::Managers::Get()->Account->Add();
191 mud::AccountPtr account = mud::Managers::Get()->Account->GetByKey(keys->first()->getIntegerValue());
193 account->setName(game::vname);
194 account->setPassword("qq");
195 account->Save();
200 KeyValuePtr key(new KeyValue(db::TableImpls::Get()->RACES->RACEID, 1));
201 SavableManagerPtr manager = SavableManager::bykey(key);
204 catch(RowNotFoundException& e)
206 KeysPtr keys = mud::Managers::Get()->Race->Add();
207 mud::RacePtr race = mud::Managers::Get()->Race->GetByKey(keys->first()->getIntegerValue());
209 race->setName("human");
210 race->Save();
215 KeyValuePtr key(new KeyValue(db::TableImpls::Get()->ENTITIES->ENTITYID, 1));
216 SavableManagerPtr manager = SavableManager::bykey(key);
218 catch(RowNotFoundException& e)
220 KeysPtr keys = mud::Managers::Get()->Character->Add();
221 mud::CharacterPtr character = mud::Managers::Get()->Character->GetByKey(keys->first()->getIntegerValue());
223 character->setName(game::vname);
224 character->setDescription("This is the default character.");
225 character->setRace(1);
226 character->setChunk(1);
227 character->Save();
232 KeysPtr keys(new Keys(db::TableImpls::Get()->CHARACTERACCOUNT));
233 keys->addKey(db::TableImpls::Get()->CHARACTERACCOUNT->FKACCOUNTS, 1);
234 keys->addKey(db::TableImpls::Get()->CHARACTERACCOUNT->FKENTITIES, 1);
235 SavableManagerPtr manager = SavableManager::bykeys(keys);
237 catch(RowNotFoundException& e)
239 RelationPtr relation(new Relation(db::TableImpls::Get()->CHARACTERACCOUNT));
240 relation->addKey(db::TableImpls::Get()->CHARACTERACCOUNT->FKACCOUNTS, 1);
241 relation->addKey(db::TableImpls::Get()->CHARACTERACCOUNT->FKENTITIES, 1);
242 relation->save();
247 KeyValuePtr key(new KeyValue(db::TableImpls::Get()->SECTORS->SECTORID, 1));
248 SavableManagerPtr manager = SavableManager::bykey(key);
250 catch(RowNotFoundException& e)
252 KeysPtr keys = mud::Managers::Get()->Sector->Add();
253 mud::SectorPtr sector = mud::Managers::Get()->Sector->GetByKey(keys->first()->getIntegerValue());
255 sector->setName("grass");
256 sector->setSymbol(".");
257 sector->setMoveCost(1);
258 sector->setWater(0);
259 sector->Save();
264 KeyValuePtr key(new KeyValue(db::TableImpls::Get()->CHANNELS->CHANNELID, 1));
265 SavableManagerPtr manager = SavableManager::bykey(key);
267 catch(RowNotFoundException& e)
269 KeysPtr keys = mud::Managers::Get()->Channel->Add();
270 mud::ChannelPtr channel = mud::Managers::Get()->Channel->GetByKey(keys->first()->getIntegerValue());
272 channel->setName("ooc");
273 channel->setDescription("The Out of Character channel.");
274 channel->setNeedLogin(1);
279 KeyValuePtr key(new KeyValue(db::TableImpls::Get()->GRANTGROUPS->GRANTGROUPID, 1));
280 SavableManagerPtr manager = SavableManager::bykey(key);
282 catch(RowNotFoundException& e)
284 KeysPtr keys = mud::Managers::Get()->GrantGroup->Add();
285 mud::GrantGroupPtr grantgroup = mud::Managers::Get()->GrantGroup->GetByKey(keys->first()->getIntegerValue());
287 grantgroup->setName("All");
288 grantgroup->setDefaultGrant(1);
289 grantgroup->setDefaultLog(0);
290 grantgroup->Save();
295 KeyValuePtr key(new KeyValue(db::TableImpls::Get()->GRANTGROUPS->GRANTGROUPID, 2));
296 SavableManagerPtr manager = SavableManager::bykey(key);
298 catch(RowNotFoundException& e)
300 KeysPtr keys = mud::Managers::Get()->GrantGroup->Add();
301 mud::GrantGroupPtr grantgroup = mud::Managers::Get()->GrantGroup->GetByKey(keys->first()->getIntegerValue());
303 grantgroup->setName("Admin");
304 grantgroup->setDefaultGrant(0);
305 grantgroup->setDefaultLog(1);
306 grantgroup->Save();
311 KeyValuePtr key(new KeyValue(db::TableImpls::Get()->GRANTGROUPS->GRANTGROUPID, 3));
312 SavableManagerPtr manager = SavableManager::bykey(key);
314 catch(RowNotFoundException& e)
316 KeysPtr keys = mud::Managers::Get()->GrantGroup->Add();
317 mud::GrantGroupPtr grantgroup = mud::Managers::Get()->GrantGroup->GetByKey(keys->first()->getIntegerValue());
319 grantgroup->setName("Builder");
320 grantgroup->setDefaultGrant(0);
321 grantgroup->setDefaultLog(0);
322 grantgroup->Save();
327 KeysPtr keys(new Keys(db::TableImpls::Get()->PERMISSIONS));
328 keys->addKey(db::TableImpls::Get()->PERMISSIONS->FKACCOUNTS, 1);
329 keys->addKey(db::TableImpls::Get()->PERMISSIONS->FKGRANTGROUPS, 2);
330 SavableManagerPtr manager = SavableManager::bykeys(keys);
332 catch(RowNotFoundException& e)
334 RelationPtr relation(new Relation(db::TableImpls::Get()->PERMISSIONS));
335 relation->addKey(db::TableImpls::Get()->PERMISSIONS->FKACCOUNTS, 1);
336 relation->addKey(db::TableImpls::Get()->PERMISSIONS->FKGRANTGROUPS, 2);
337 relation->addField(db::TableImpls::Get()->PERMISSIONS->GRANT, 1);
338 relation->addField(db::TableImpls::Get()->PERMISSIONS->LOG, 1);
339 relation->save();
344 KeysPtr keys(new Keys(db::TableImpls::Get()->PERMISSIONS));
345 keys->addKey(db::TableImpls::Get()->PERMISSIONS->FKACCOUNTS, 1);
346 keys->addKey(db::TableImpls::Get()->PERMISSIONS->FKGRANTGROUPS, 3);
347 SavableManagerPtr manager = SavableManager::bykeys(keys);
349 catch(RowNotFoundException& e)
351 RelationPtr relation(new Relation(db::TableImpls::Get()->PERMISSIONS));
352 relation->addKey(db::TableImpls::Get()->PERMISSIONS->FKACCOUNTS, 1);
353 relation->addKey(db::TableImpls::Get()->PERMISSIONS->FKGRANTGROUPS, 3);
354 relation->addField(db::TableImpls::Get()->PERMISSIONS->GRANT, 1);
355 relation->addField(db::TableImpls::Get()->PERMISSIONS->LOG, 0);
356 relation->save();
360 void Initializer::InitColours()
362 const struct colour colours[] =
364 { "Restore", "^", "0;0m" },
365 { "Dark Red", "r", "31m" },
366 { "Dark Green", "g", "32m" },
367 { "Dark Yellow", "y", "33m"},
368 { "Dark Blue", "b", "34m" },
369 { "Dark Purple", "m", "35m" },
370 { "Dark Cyan", "c", "36m" },
371 { "Gray", "w", "37m" },
372 { "Black", "l", "30m" },
373 // Light colours
374 { "Light Red", "R", "1;31m" },
375 { "Light blue", "G", "1;32m" },
376 { "Light Yellow", "Y", "1;33m" },
377 { "Light Blue", "B", "1;34m" },
378 { "Light Magenta", "M", "1;35m" },
379 { "Light Cyan", "C", "1;36m" },
380 { "White", "W", "1;37m" },
381 { "Light Gray", "L", "1;30m" },
384 int size = sizeof(colours) / sizeof(colours[0]);
385 printf("Colours: %d.\n", size);
387 for(int i = 0; i < size; i++)
391 ValuePtr value(new FieldValue(db::TableImpls::Get()->COLOURS->CODE, colours[i].code));
392 SavableManagerPtr manager = SavableManager::byvalue(value);
394 catch(RowNotFoundException& e)
396 KeysPtr keys = mud::Managers::Get()->Colour->Add();
397 mud::ColourPtr thecolour = mud::Managers::Get()->Colour->GetByKey(keys->first()->getIntegerValue());
399 thecolour->setName(colours[i].name);
400 thecolour->setCode(colours[i].code);
401 thecolour->setColourString(colours[i].cstr);
402 thecolour->setAnsi(1);
403 thecolour->Save();
408 void Initializer::InitCommands()
410 const struct command commands[] =
412 { "Account::Commands",1,1,1,0,"This command will list all available commands."},
413 { "Account::Login",1,1,1,0,"This command allows you to log in with one of your IC characters."},
414 { "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."},
415 { "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."},
416 { "Account::New",1,1,1,0,"This command will start the creation of a new IC character."},
417 { "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."},
418 { "Account::Shutdown",2,1,1,0,"This command will shut down the server."},
419 { "Account::List",1,1,1,0,"This command will provide you with a list of your characters."},
422 int size = sizeof(commands) / sizeof(commands[0]);
423 printf("Commands: %d.\n", size);
425 for(int i = 0; i < size; i++)
429 ValuePtr value(new FieldValue(db::TableImpls::Get()->COMMANDS->NAME, commands[i].name));
430 SavableManagerPtr manager = SavableManager::byvalue(value);
432 catch(RowNotFoundException& e)
434 KeysPtr keys = mud::Managers::Get()->Command->Add();
435 mud::CommandPtr thecommand = mud::Managers::Get()->Command->GetByKey(keys->first()->getIntegerValue());
437 thecommand->setName(commands[i].name);
438 thecommand->setHelp(commands[i].help);
440 if(commands[i].grantgroup != 0)
441 thecommand->setGrantGroup(commands[i].grantgroup);
443 if(commands[i].highforce == 0 || commands[i].highforce == 1)
444 thecommand->setHighForce(commands[i].highforce);
446 if(commands[i].force == 0 || commands[i].force == 1)
447 thecommand->setForce(commands[i].force);
449 if(commands[i].lowforce == 0 || commands[i].lowforce == 1)
450 thecommand->setLowForce(commands[i].lowforce);
452 thecommand->Save();
457 void Initializer::InitSpace()
461 KeyValuePtr key(new KeyValue(db::TableImpls::Get()->AREAS->AREAID, 1));
462 SavableManagerPtr manager = SavableManager::bykey(key);
464 catch(RowNotFoundException& e)
466 KeysPtr keys = mud::Managers::Get()->Area->Add();
467 mud::AreaPtr area = mud::Managers::Get()->Area->GetByKey(keys->first()->getIntegerValue());
469 area->setName("Space");
470 area->setDescription("This is space, the final frontier.");
471 area->setHeight(1);
472 area->setWidth(1);
473 area->setLength(1);
474 area->Save();
479 KeyValuePtr key(new KeyValue(db::TableImpls::Get()->CLUSTERS->CLUSTERID, 1));
480 SavableManagerPtr manager = SavableManager::bykey(key);
482 catch(RowNotFoundException& e)
484 KeysPtr keys = mud::Managers::Get()->Cluster->Add();
485 mud::ClusterPtr cluster = mud::Managers::Get()->Cluster->GetByKey(keys->first()->getIntegerValue());
487 cluster->setName("Alpha quadrant");
488 cluster->setDescription("This is the alpha quadrant, where Earth lies.");
489 cluster->setArea(1);
490 cluster->Save();
495 KeyValuePtr key(new KeyValue(db::TableImpls::Get()->ROOMS->ROOMID, 1));
496 SavableManagerPtr manager = SavableManager::bykey(key);
498 catch(RowNotFoundException& e)
500 KeysPtr keys = mud::Managers::Get()->Room->Add();
501 mud::RoomPtr room = mud::Managers::Get()->Room->GetByKey(keys->first()->getIntegerValue());
503 room->setName("The Void");
504 room->setDescription("You are in The Void.");
505 room->setSector(1);
506 room->setCluster(1);
507 room->Save();
512 KeyValuePtr key(new KeyValue(db::TableImpls::Get()->CHUNKS->CHUNKID, 1));
513 SavableManagerPtr manager = SavableManager::bykey(key);
516 catch(RowNotFoundException& e)
518 KeysPtr keys = mud::Managers::Get()->Chunk->Add();
519 mud::ChunkPtr chunk = mud::Managers::Get()->Chunk->GetByKey(keys->first()->getIntegerValue());
521 chunk->setRoom(1);
522 chunk->setName("The void");
523 chunk->Save();
527 void Initializer::InitSampleChunks(int size)
529 int xdim = size;
530 int ydim = size;
531 int zdim = size;
533 KeysPtr keys;
535 // Sample sector
536 keys = mud::Managers::Get()->Sector->Add();
537 mud::SectorPtr sector = mud::Managers::Get()->Sector->GetByKey(keys->first()->getIntegerValue());
539 sector->setName("Stone");
540 sector->setMoveCost(1);
541 sector->setSymbol("_");
542 sector->setWater(0);
543 sector->Save();
545 // Sample area
546 keys = mud::Managers::Get()->Area->Add();
547 mud::AreaPtr area = mud::Managers::Get()->Area->GetByKey(keys->first()->getIntegerValue());
549 area->setName("The cube");
550 area->setDescription("This is an example area, it contains a cube to try out nagivation.");
551 area->Save();
555 // Centre
556 mud::ChunkPtr centre;
558 keys = mud::Managers::Get()->Chunk->Add();
559 centre = mud::Managers::Get()->Chunk->GetByKey(keys->first()->getIntegerValue());
561 centre->setName("Core of the cube ");
562 centre->setDescription("You are in the centre part of the Cube.");
563 centre->setTags("Core Cube");
564 centre->Save();
568 // Defs
569 mud::ClusterPtr cluster;
570 mud::RoomPtr room;
571 mud::ChunkPtr chunk;
573 for(int z = 0; z < zdim; z++)
575 std::string zpart;
577 if(z < zdim/3)
578 zpart = "top";
579 else if(z < zdim*2/3)
580 zpart = "middle";
581 else
582 zpart = "bottom";
584 keys = mud::Managers::Get()->Cluster->Add();
585 cluster = mud::Managers::Get()->Cluster->GetByKey(keys->first()->getIntegerValue());
587 cluster->setName(zpart + std::string(" of the cube ") + String::Get()->fromInt(z));
588 cluster->setDescription(std::string("You are in the ") + zpart + std::string(" part of the Cube."));
589 cluster->setArea(area->getID());
590 cluster->Save();
593 for(int y = 0; y < ydim; y++)
595 std::string ypart;
597 if(y < ydim/3)
598 ypart = "left";
599 else if(y < ydim*2/3)
600 ypart = "centre";
601 else
602 ypart = "right";
604 keys = mud::Managers::Get()->Room->Add();
605 room = mud::Managers::Get()->Room->GetByKey(keys->first()->getIntegerValue());
607 room->setName(ypart + std::string(" of the cube ") + String::Get()->fromInt(y));
608 room->setDescription(std::string("You are in the ") + ypart + std::string(" part of the Cube."));
609 room->setSector(sector->getID());
610 room->setCluster(cluster->getID());
611 room->Save();
613 for(int x = 0; x < xdim; x++)
615 std::string xpart;
617 if(x < xdim/3)
618 xpart = "front";
619 else if(x < xdim*2/3)
620 xpart = "middle";
621 else
622 xpart = "back";
624 keys = mud::Managers::Get()->Chunk->Add();
625 chunk = mud::Managers::Get()->Chunk->GetByKey(keys->first()->getIntegerValue());
627 chunk->setName(xpart + std::string(" of the cube ") + String::Get()->fromInt(x));
628 chunk->setDescription(std::string("You are in the ") + xpart + std::string(" part of the Cube."));
629 chunk->setRoom(room->getID());
630 chunk->setTags(zpart + " " + ypart + " " + xpart + std::string(" Cube"));
631 chunk->Save();
633 int centrepos = (size+1)/2-1;
635 if(z==centrepos && y==centrepos && x==centrepos)
637 centre->setRoom(room->getID());
638 centre->Save();
640 else
642 mud::ExitPtr exit;
644 keys = mud::Managers::Get()->Exit->Add();
645 exit = mud::Managers::Get()->Exit->GetByKey(keys->first()->getIntegerValue());
647 Coordinate c(x-centrepos,y-centrepos,z-centrepos);
648 exit->setDirection(c);
649 exit->setFromChunk(centre->getID());
650 exit->setToChunk(chunk->getID());
651 exit->Save();
654 keys = mud::Managers::Get()->Exit->Add();
655 exit = mud::Managers::Get()->Exit->GetByKey(keys->first()->getIntegerValue());
657 exit->setDirection(c.getComplement());
658 exit->setFromChunk(chunk->getID());
659 exit->setToChunk(centre->getID());
660 exit->Save();