Well don't that own? Sorted all the #include's.
[UnsignedByte.git] / src / Core / Editors / EditorNewCharacter.cpp
blobd5669456fb2e34bb9f80fa41e633a44f55e3f453
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 "CharacterManager.h"
23 #include "EditorAccount.h"
24 #include "EditorNewCharacter.h"
25 #include "EditorPlaying.h"
26 #include "FieldImpls.h"
27 #include "Managers.h"
28 #include "PCharacter.h"
29 #include "PCharacterManager.h"
30 #include "Race.h"
31 #include "RaceManager.h"
32 #include "StringUtilities.h"
33 #include "TableImpls.h"
34 #include "UBSocket.h"
36 EditorNewCharacter::EditorNewCharacter(UBSocket* sock) :
37 Editor(sock),
38 m_state(0)
40 OnLine(Global::Get()->EmptyString);
43 EditorNewCharacter::~EditorNewCharacter(void)
47 void EditorNewCharacter::OnLine(const std::string &line)
49 if(!line.compare("quit"))
51 m_sock->Send("Ok\n");
52 // m_sock->SetEditor(new EditorAccount(m_sock));
53 m_sock->PopEditor();
54 return;
57 switch(m_state)
59 default:
60 Assert(m_state != m_state);
61 break;
63 case M_FIRST:
64 m_state++;
65 // fallthrough
67 case M_NAME:
69 if(line.size() == 0)
71 m_sock->Send("Enter a name for your character please: \n");
72 return;
75 if(mud::Managers::Get()->Character->IllegalName(line))
77 m_sock->Sendf("You cannot use the name %s, please pick another name.\n", line.c_str());
78 OnLine(Global::Get()->EmptyString);
79 return;
82 m_name = line;
83 m_state++;
84 OnLine(Global::Get()->EmptyString);
85 break;
88 case M_NAMECONFIRM:
90 if(line.size() == 0)
92 m_sock->Sendf("Create a character named %s?\n", m_name.c_str());
93 return;
96 if(!line.compare("n") || !line.compare("no"))
98 m_sock->Send("Let's try again then.\n");
99 m_state = M_FIRST;
100 OnLine(Global::Get()->EmptyString);
101 return;
104 if(line.compare("y") && line.compare("yes"))
106 OnLine(Global::Get()->EmptyString);
107 m_sock->Send("yes/no?\n");
108 return;
111 m_state++;
112 OnLine(Global::Get()->EmptyString);
113 return;
116 case M_RACE:
118 if(line.size() == 0)
120 m_sock->Send("Please choose a race: \n");
121 m_sock->Send(String::Get()->box(mud::Managers::Get()->Race->List(), "Races"));
122 m_sock->Send("\n");
123 return;
128 int id = mud::Managers::Get()->Race->lookupByName(line);
129 m_raceid = id;
131 m_sock->Sendf("Your race is now %s.\n", line.c_str());
132 m_state++;
133 OnLine(Global::Get()->EmptyString);
135 catch(RowNotFoundException& e)
137 m_sock->Send("No such race.\n");
138 OnLine(Global::Get()->EmptyString);
141 return;
144 case M_DONE:
146 if(line.size() != 0)
148 m_sock->Send("Please hit enter to continue.\n");
149 return;
152 KeysPtr keys(new Keys(db::TableImpls::Get()->ROOMS));
153 KeyValuePtr key(new KeyValue(db::TableImpls::Get()->ROOMS->ROOMID, 1));
154 keys->addKey(key);
156 // TODO
158 if(DatabaseMgr::Get()->CountSavable(db::TableImpls::Get()->ROOMS, keys) <= 0)
160 m_sock->Sendf("Could not fetch room 1!\n");
161 m_sock->Send("Closing your connection now.\n");
162 m_sock->SetCloseAndDelete();
163 return;
167 mud::AccountPtr account = m_sock->GetAccount();
169 KeysPtr newchkeys = mud::Managers::Get()->Character->Add();
170 value_type id = newchkeys->first()->getIntegerValue();
172 Assert(id > 0);
174 mud::CharacterPtr character = mud::Managers::Get()->Character->GetByKey(id);
175 if(!character)
177 m_sock->Send("For some reason your new characters could not be retreived.\n");
178 m_sock->Send("Disconnecting you now.\n");
179 m_sock->SetCloseAndDelete();
180 return;
183 character->setName(m_name);
184 character->setRace(m_raceid);
185 character->setChunk(1);
186 character->Save();
187 character.reset();
189 mud::PCharacterPtr Ch = mud::PCharacterManager::Get()->LoadByKey(m_sock, id);
191 Assert(m_sock->hasAccount());
192 value_type accountid = m_sock->GetAccount()->getID();
194 RelationPtr relation(new Relation(db::TableImpls::Get()->CHARACTERACCOUNT));
195 relation->addKey(db::TableImpls::Get()->CHARACTERACCOUNT->FKACCOUNTS, accountid);
196 relation->addKey(db::TableImpls::Get()->CHARACTERACCOUNT->FKENTITIES, id);
197 relation->save();
199 m_sock->Sendf("Character %s created, enjoy!\n", m_name.c_str());
200 m_sock->SetEditor(new EditorPlaying(m_sock, Ch), true);
201 return;
207 void EditorNewCharacter::OnEmptyLine()
209 OnLine(Global::Get()->EmptyString);