Only allow this language: ((n|s)!(w|e)!(u|d)!;)+ for directions.
[UnsignedByte.git] / src / Core / Editors / EditorNewCharacter.cpp
blob2c366af3e887ce6547c79bea0e67dc722997604d
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 "EditorNewCharacter.h"
22 #include "EditorPlaying.h"
23 #include "EditorAccount.h"
24 #include "UBSocket.h"
25 #include "CharacterManager.h"
26 #include "PCharacter.h"
27 #include "PCharacterManager.h"
28 #include "StringUtilities.h"
29 #include "FieldImpls.h"
30 #include "TableImpls.h"
31 #include "Account.h"
32 #include "Race.h"
33 #include "RaceManager.h"
34 #include "Managers.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 Global::Get()->bug("EditorNewAccount::OnLine(), default m_state!\n");
61 m_sock->Send("BUG! Disconnecting you now...\n");
62 m_sock->SetCloseAndDelete();
63 return;
65 case M_FIRST:
66 m_state++;
67 // fallthrough
69 case M_NAME:
71 if(line.size() == 0)
73 m_sock->Send("Enter a name for your character please: \n");
74 return;
77 if(mud::Managers::Get()->Character->IllegalName(line))
79 m_sock->Sendf("You cannot use the name %s, please pick another name.\n", line.c_str());
80 OnLine(Global::Get()->EmptyString);
81 return;
84 m_name = line;
85 m_state++;
86 OnLine(Global::Get()->EmptyString);
87 break;
90 case M_NAMECONFIRM:
92 if(line.size() == 0)
94 m_sock->Sendf("Create a character named %s?\n", m_name.c_str());
95 return;
98 if(!line.compare("n") || !line.compare("no"))
100 m_sock->Send("Let's try again then.\n");
101 m_state = M_FIRST;
102 OnLine(Global::Get()->EmptyString);
103 return;
106 if(line.compare("y") && line.compare("yes"))
108 OnLine(Global::Get()->EmptyString);
109 m_sock->Send("yes/no?\n");
110 return;
113 m_state++;
114 OnLine(Global::Get()->EmptyString);
115 return;
118 case M_RACE:
120 if(line.size() == 0)
122 m_sock->Send("Please choose a race: \n");
123 m_sock->Send(String::Get()->box(mud::Managers::Get()->Race->List(), "Races"));
124 m_sock->Send("\n");
125 return;
130 int id = mud::Managers::Get()->Race->lookupByName(line);
131 m_raceid = id;
133 m_sock->Sendf("Your race is now %s.\n", line.c_str());
134 m_state++;
135 OnLine(Global::Get()->EmptyString);
137 catch(RowNotFoundException& e)
139 m_sock->Send("No such race.\n");
140 OnLine(Global::Get()->EmptyString);
143 return;
146 case M_DONE:
148 if(line.size() != 0)
150 m_sock->Send("Please hit enter to continue.\n");
151 return;
154 KeysPtr keys(new Keys(db::TableImpls::Get()->ROOMS));
155 KeyValuePtr key(new KeyValue(db::TableImpls::Get()->ROOMS->ROOMID, 1));
156 keys->addKey(key);
158 // TODO
160 if(DatabaseMgr::Get()->CountSavable(db::TableImpls::Get()->ROOMS, keys) <= 0)
162 m_sock->Sendf("Could not fetch room 1!\n");
163 m_sock->Send("Closing your connection now.\n");
164 m_sock->SetCloseAndDelete();
165 return;
169 mud::AccountPtr account = m_sock->GetAccount();
171 KeysPtr newchkeys = mud::Managers::Get()->Character->Add();
172 value_type id = newchkeys->first()->getIntegerValue();
174 Assert(id > 0);
176 mud::CharacterPtr character = mud::Managers::Get()->Character->GetByKey(id);
177 if(!character)
179 m_sock->Send("For some reason your new characters could not be retreived.\n");
180 m_sock->Send("Disconnecting you now.\n");
181 m_sock->SetCloseAndDelete();
182 return;
185 character->setName(m_name);
186 character->setRace(m_raceid);
187 character->setChunk(1);
188 character->Save();
189 character.reset();
191 mud::PCharacterPtr Ch = mud::PCharacterManager::Get()->LoadByKey(m_sock, id);
193 Assert(m_sock->hasAccount());
194 value_type accountid = m_sock->GetAccount()->getID();
196 RelationPtr relation(new Relation(db::TableImpls::Get()->CHARACTERACCOUNT));
197 relation->addKey(db::TableImpls::Get()->CHARACTERACCOUNT->FKACCOUNTS, accountid);
198 relation->addKey(db::TableImpls::Get()->CHARACTERACCOUNT->FKENTITIES, id);
199 relation->save();
201 m_sock->Sendf("Character %s created, enjoy!\n", m_name.c_str());
202 m_sock->SetEditor(new EditorPlaying(m_sock, Ch), true);
203 return;
209 void EditorNewCharacter::OnEmptyLine()
211 OnLine(Global::Get()->EmptyString);