Chunk is purely a wrapper class, any 'state variables', like 'characters in room...
[UnsignedByte.git] / src / Core / Editors / EditorChannel.cpp
blob61b13183cf6b79d1a51fddf7647efe0c4bb7d447
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 "EditorChannel.h"
22 #include "EditorString.h"
23 #include "EditorBool.h"
24 #include "UBSocket.h"
25 #include "StringUtilities.h"
26 #include "TableImpls.h"
27 #include "Array.h"
28 #include "Account.h"
29 #include "Channel.h"
30 #include "ChannelManager.h"
31 #include "Managers.h"
33 typedef EditorChannel E;
34 typedef CommandInfoObject<E> O;
35 typedef CommandBinding<E> B;
37 // name function need: object lock
38 static O editName( "Name", &E::editName, true, true);
39 static O editDescription("Description",&E::editDescription,true, true);
40 static O editNeedLogin("NeedLogin", &E::editNeedLogin, true, true);
41 static O showChannel( "Show", &E::showChannel, true, false);
42 static O saveChannel( "Save", &E::saveChannel, true, true);
44 static B commands[] = {
45 B("description", editDescription),
46 B("name", editName),
47 B("needlogin", editNeedLogin),
48 B("save", saveChannel),
49 B("show", showChannel),
52 EditorChannel::EditorChannel(UBSocket* sock) :
53 OLCEditor(sock),
54 m_commands(commands, array_size(commands)),
55 m_channel()
57 listCommands(Global::Get()->EmptyString);
60 EditorChannel::~EditorChannel(void)
65 void EditorChannel::OnFocus()
67 switch(m_target)
69 case M_NONE:
70 return;
72 case M_DESCRIPTION:
73 m_channel->setDescription(m_value);
74 break;
76 case M_NEEDLOGIN:
77 m_channel->setNeedLogin(m_yesno);
78 break;
81 m_target = M_NONE;
84 std::string EditorChannel::lookup(const std::string& action)
86 std::string name = OLCEditor::lookup(action);
87 if(name.size() != 0)
88 return name;
90 const ChannelCommand* act = (ChannelCommand*)m_commands.getObject(action);
91 if(act)
92 return act->getName();
94 return Global::Get()->EmptyString;
97 void EditorChannel::dispatch(const std::string& action, const std::string& argument)
99 const ChannelCommand* act = (ChannelCommand*)m_commands.getObject(action);
101 if(!act)
103 OLCEditor::dispatch(action, argument);
104 return;
107 if(!m_channel)
109 m_sock->Send("You need to be editing an channel first.\n");
110 m_sock->Send("(Use the 'edit' command.)\n");
111 return;
114 if(act->needLock())
116 try {
117 m_channel->Lock();
118 } catch(SavableLocked& e) {
119 m_sock->Send("The channel you are currently editing is locked (being edited by someone else), so you cannot edit it right now.\n");
120 m_sock->Send("Please try again later.\n");
121 return;
125 act->Run(this, argument);
126 return;
129 SavablePtr EditorChannel::getEditing()
131 return m_channel;
134 TableImplPtr EditorChannel::getTable()
136 return db::TableImpls::Get()->AREAS;
139 KeysPtr EditorChannel::addNew()
141 return mud::Managers::Get()->Channel->Add();
144 std::vector<std::string> EditorChannel::getList()
146 return mud::Managers::Get()->Channel->List();
149 void EditorChannel::setEditing(KeysPtr keys)
151 if(!keys->size())
153 m_channel.reset();
154 return;
157 m_channel = mud::Managers::Get()->Channel->GetByKey(keys->first()->getIntegerValue());
158 return;
161 std::vector<std::string> EditorChannel::getCommands()
163 return m_commands.getCommandsVector();
166 void EditorChannel::editName(const std::string& argument)
168 if(argument.size() == 0)
170 m_sock->Send("Channel name can't be zero length!\n");
171 return;
174 m_sock->Sendf("Channel name changed from '%s' to '%s'.\n", m_channel->getName().c_str(), argument.c_str());
175 m_channel->setName(argument);
176 return;
179 void EditorChannel::editDescription(const std::string& argument)
181 if(argument.size() == 0)
183 m_sock->Send("No argument, dropping you into the string editor!\n");
184 m_sock->SetEditor(new EditorString(m_sock, m_value));
185 m_target = M_DESCRIPTION;
186 return;
188 return;
191 m_sock->Sendf("Channel description changed from '%s' to '%s'.\n", m_channel->getDescription().c_str(), argument.c_str());
192 m_channel->setDescription(argument);
193 return;
196 void EditorChannel::editNeedLogin(const std::string& argument)
198 if(argument.size() == 0)
200 m_sock->Send("Do players need to be logged in to use hear the channel?\n");
201 m_sock->SetEditor(new EditorBool(m_sock, m_yesno));
202 m_target = M_NEEDLOGIN;
203 return;
206 if(!argument.compare("yes"))
208 m_channel->setNeedLogin(true);
209 return;
212 if(!argument.compare("no"))
214 m_channel->setNeedLogin(false);
215 return;
218 editNeedLogin(Global::Get()->EmptyString);
219 return;
222 void EditorChannel::showChannel(const std::string& argument)
224 m_sock->Send(m_channel->toString());
227 void EditorChannel::saveChannel(const std::string& argument)
229 m_sock->Sendf("Saving channel '%s'.\n", m_channel->getName().c_str());
230 m_channel->Save();
231 m_sock->Send("Saved.\n");
232 return;