00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include <libfuture.h>
00022 #include <Parse.h>
00023
00024 #include "FutureHandler.h"
00025 #include "InSocket.h"
00026 #include "Quit.h"
00027 #include "Who.h"
00028 #include "Help.h"
00029 #include "Create.h"
00030 #include "List.h"
00031 #include "Login.h"
00032 #include "Chunk.h"
00033 #include "Look.h"
00034 #include "Set.h"
00035 #include "North.h"
00036 #include "South.h"
00037 #include "East.h"
00038 #include "West.h"
00039 #include "Fill.h"
00040 #include "Player.h"
00041 #include "Save.h"
00042 #include "Left.h"
00043 #include "Right.h"
00044 #include "Forward.h"
00045 #include "Area.h"
00046 #include "Up.h"
00047 #include "Down.h"
00048 #include "Clear.h"
00049 #include "Edge.h"
00050 #include "Enter.h"
00051 #include "Go.h"
00052 #include "Map.h"
00053 #include "Event.h"
00054
00055
00056 FutureHandler::FutureHandler()
00057 :EventHandler()
00058 ,m_db("future.db")
00059 ,m_login(new Login())
00060 {
00061 m_command.push_back(new Quit());
00062 m_command.push_back(new Who());
00063 m_command.push_back(new Help());
00064 m_command.push_back(new Create());
00065 m_command.push_back(new List());
00066 m_command.push_back(new Look());
00067 m_command.push_back(new Setcmd());
00068 m_command.push_back(new North());
00069 m_command.push_back(new South());
00070 m_command.push_back(new East());
00071 m_command.push_back(new West());
00072 m_command.push_back(new Fill());
00073 m_command.push_back(new Save());
00074 m_command.push_back(new Left());
00075 m_command.push_back(new Right());
00076 m_command.push_back(new Forward());
00077 m_command.push_back(new Up());
00078 m_command.push_back(new Down());
00079 m_command.push_back(new Clear());
00080 m_command.push_back(new Edge());
00081 m_command.push_back(new Enter());
00082 m_command.push_back(new Go());
00083 m_command.push_back(new Map());
00084
00085
00086 Query q(m_db);
00087 if (!q.get_count("select count(*) from area"))
00088 {
00089 Area a(m_db, "World");
00090 a.SetWidth(15);
00091 a.SetHeight(9);
00092 a.Save();
00093 }
00094 if (!q.get_count("select count(*) from chunk"))
00095 {
00096 Area a(m_db, "World");
00097 Chunk cc(m_db, a, 1000, 1000, 0);
00098 cc.Init(NULL, 1000, 1000, 0, ' ');
00099 cc.Save();
00100 }
00101 }
00102
00103
00104 FutureHandler::~FutureHandler()
00105 {
00106 while (m_command.size())
00107 {
00108 std::list<Command *>::iterator it = m_command.begin();
00109 Command *p = *it;
00110 delete p;
00111 m_command.erase(it);
00112 }
00113 delete m_login;
00114 }
00115
00116
00117 void FutureHandler::Talk(InSocket *from,const std::string& line)
00118 {
00119 size_t i = 0;
00120 while (i < line.size() && line[i] == ' ')
00121 i++;
00122 std::string does;
00123 if (line.size() > 2 && line.substr(line.size() - 2) == ":)")
00124 {
00125 does = "smiles and says";
00126 from -> Send("You smile and say '" + line + "&n'.\n");
00127 }
00128 else
00129 if (line.size() > 2 && line.substr(line.size() - 2) == ";)")
00130 {
00131 does = "flirts and says";
00132 from -> Send("You flirt and say '" + line + "&n'.\n");
00133 }
00134 else
00135 if (line.size() > 2 && line.substr(line.size() - 2) == "!!")
00136 {
00137 does = "shouts";
00138 from -> Send("You shout '" + line + "&n'.\n");
00139 }
00140 else
00141 if (line.substr(line.size() - 1) == "?")
00142 {
00143 does = "asks";
00144 from -> Send("You ask '" + line + "&n'.\n");
00145 }
00146 else
00147 if (line.substr(line.size() - 1) == "!")
00148 {
00149 does = "exclaims";
00150 from -> Send("You exclaim '" + line + "&n'.\n");
00151 }
00152 else
00153 {
00154 does = "says";
00155 from -> Send("You say '" + line + "&n'.\n");
00156 }
00157 Player pl(from -> GetDatabase(),from -> GetAccountName());
00158 std::string talk = "\n" + pl.GetDisplayName() + " " + does + " '" + line.substr(i) + "&n'.\n";
00159 for (socket_m::iterator it = m_sockets.begin(); it != m_sockets.end(); it++)
00160 {
00161 Socket *p0 = (*it).second;
00162 InSocket *p = dynamic_cast<InSocket *>(p0);
00163 if (p && p != from)
00164 {
00165 p -> Send(talk);
00166 p -> SendPrompt();
00167 }
00168 }
00169 }
00170
00171
00172 void FutureHandler::GlobalEvent(InSocket *from,const std::string& s)
00173 {
00174 std::string talk = "\n" + s + "\n";
00175 for (socket_m::iterator it = m_sockets.begin(); it != m_sockets.end(); it++)
00176 {
00177 Socket *p0 = (*it).second;
00178 InSocket *p = dynamic_cast<InSocket *>(p0);
00179 if (p && p != from)
00180 {
00181 p -> Send(talk);
00182 p -> SendPrompt();
00183 }
00184 }
00185 }
00186
00187
00188 void FutureHandler::DoCommand(InSocket *from,const std::string& cmd,const std::string& params)
00189 {
00190 Command *match = NULL;
00191 if (cmd.size() == 1)
00192 {
00193 match = Command::GetCommandFromChar(cmd[0]);
00194 }
00195 if (match)
00196 {
00197 Player pl(from -> GetDatabase(),from -> GetAccountName());
00198 if (!match -> NeedWiz() || pl.IsWizard())
00199 {
00200 Parse pa(params);
00201 std::string arg = pa.getword();
00202 std::string complete;
00203 if (match -> CheckArgument(from, arg, complete))
00204 {
00205 match -> Execute(from, complete, pa);
00206 }
00207 return;
00208 }
00209 }
00210 int score = 0;
00211 int count = 0;
00212 for (std::list<Command *>::iterator it = m_command.begin(); it != m_command.end(); it++)
00213 {
00214 Command *p = *it;
00215 int i = p -> Match(from, cmd, params);
00216 score = (i > score) ? i : score;
00217 if (i)
00218 {
00219 match = p;
00220 count++;
00221 }
00222 if (score == 2)
00223 {
00224 break;
00225 }
00226 }
00227 if (score == 1 && count > 1)
00228 {
00229 from -> Send("Ambiguous command.\n");
00230 for (std::list<Command *>::iterator it = m_command.begin(); it != m_command.end(); it++)
00231 {
00232 Command *p = *it;
00233 if (p -> Match(from, cmd, params) == 1)
00234 {
00235 from -> Send(" " + p -> GetCommand() + "\n");
00236 }
00237 }
00238 from -> SetPrompt();
00239 }
00240 else
00241 if (score == 2 || (score == 1 && count == 1))
00242 {
00243 Parse pa(params);
00244 std::string arg = pa.getword();
00245 std::string complete;
00246 if (match -> CheckArgument(from, arg, complete))
00247 {
00248 match -> Execute(from, complete, pa);
00249 }
00250 }
00251 else
00252 {
00253 from -> Send("Huh?\n");
00254 from -> SetPrompt();
00255 }
00256 }
00257
00258
00259 void FutureHandler::ShowHelp(InSocket *from,const std::string& params)
00260 {
00261 std::map<std::string,std::map<std::string,Command *> > mmap;
00262 Player pl(from -> GetDatabase(),from -> GetAccountName());
00263 for (std::list<Command *>::iterator it = m_command.begin(); it != m_command.end(); it++)
00264 {
00265 Command *cc = *it;
00266 if (!cc -> NeedWiz() || pl.IsWizard())
00267 {
00268 mmap[cc -> Category()][cc -> GetCommand()] = cc;
00269 }
00270 }
00271 for (std::map<std::string,std::map<std::string,Command *> >::iterator it = mmap.begin(); it != mmap.end(); it++)
00272 {
00273 std::string category = (*it).first;
00274 std::map<std::string,Command *>& ref = (*it).second;
00275 from -> Send("&W" + category + "&n\n");
00276 for (std::map<std::string,Command *>::iterator it = ref.begin(); it != ref.end(); it++)
00277 {
00278 std::string command = (*it).first;
00279 Command *cc = (*it).second;
00280 strbool_m& ref = cc -> GetArguments();
00281 std::string charstr;
00282 std::string argstr;
00283 std::string descr;
00284 if (cc -> GetCommandChar())
00285 {
00286 charstr = " &g(&y";
00287 charstr += cc -> GetCommandChar();
00288 charstr += "&g)&n";
00289 }
00290 for (strbool_m::iterator it = ref.begin(); it != ref.end(); it++)
00291 {
00292 std::string arg = (*it).first;
00293 bool need_wiz = (*it).second;
00294 if (!need_wiz || pl.IsWizard())
00295 {
00296 if (argstr.size())
00297 argstr += "&g|&y" + arg;
00298 else
00299 argstr = " &g[&y" + arg;
00300 }
00301 }
00302 if (argstr.size())
00303 argstr += "&g]&n";
00304 if (cc -> Description().size())
00305 descr = " - &M" + cc -> Description();
00306 from -> Send(" &G" + command + charstr + argstr + descr + "&n\n");
00307 }
00308 }
00309 }
00310
00311
00312 void FutureHandler::Count(const std::string& event)
00313 {
00314 db::Eventcount ec(m_db, event);
00315 ec.event_name = event;
00316 ec.count++;
00317 ec.save();
00318 }
00319
00320
00321 std::string FutureHandler::GetDateTime()
00322 {
00323 time_t t = time(NULL);
00324 struct tm *tp = localtime(&t);
00325 char dt[40];
00326 sprintf(dt, "%d-%02d-%02d %02d:%02d:%02d", tp -> tm_year + 1900, tp -> tm_mon + 1,
00327 tp -> tm_mday, tp -> tm_hour, tp -> tm_min, tp -> tm_sec);
00328 return dt;
00329 }
00330
00331
00332 Command *FutureHandler::GetCommand(const std::string& x)
00333 {
00334 for (std::list<Command *>::iterator it = m_command.begin(); it != m_command.end(); it++)
00335 {
00336 Command *cc = *it;
00337 if (cc -> GetCommand() == x)
00338 return cc;
00339 }
00340 return NULL;
00341 }
00342
00343