handle.cpp
cowmud-0.11/src/handle.cpp · 3.6 KB
From the archive cowmud-0.11.tar.gz
Download the archive « Back to the archive contents
#include <iostream.h>
#include <string>
#include "defines.h"
#include "interpret.h"
void handleActions()
{
Character ch, ch2;
CLOOP_START {
switch (ch.getStatus()) {
case STATUS_INGAME:
if (ch.isReadyBuf()) {
string command = stripCarriageReturn(ch.argument(1));
string parameters = stripCarriageReturn(ch.argumentBut(1));
BaseInterpret *iBaseClassPtr;
for (int count2 = 1; count2 <= i_list.listLength();
count2++) {
i_list.listRetrieve(count2, iBaseClassPtr);
if (command == iBaseClassPtr->getCommand() && ch.getLevel() >= iBaseClassPtr->getLevelRequired()) {
iBaseClassPtr->action(ch, parameters);
ch.clearReadBuf();
}
}
// If we haven't cleared the buffer, then we haven't found the command
if (ch.getReadBuf() != "") {
ch.addWriteBuf("Invalid command.\n\r");
ch.clearReadBuf();
}
}
break;
case STATUS_LOGIN:
ch.addWriteBuf("Welcome to COWMUD, please enter your name: ");
ch.setStatus(STATUS_GETNAME);
break;
case STATUS_GETNAME:
if (ch.isReadyBuf()) {
string name;
name = stripCarriageReturn(ch.getReadBuf());
ch.setName(name);
ch.clearReadBuf();
if (!ch.isNameOk()) {
ch.addWriteBuf("That is not a valid user name, please try again: ");
ch.setStatus(STATUS_GETNAME);
break;
}
if (!ch.load()) {
ch.addWriteBuf(ch.getName() + ", is that correct? (y/n): ");
ch.setStatus(STATUS_CONFIRM_NAME);
} else {
ch.addWriteBuf(ch.getName() + ", please enter your password: ");
ch.setStatus(STATUS_PWORD);
}
}
break;
case STATUS_CONFIRM_NAME:
if (ch.isReadyBuf()) {
string confirmation;
confirmation = stripCarriageReturn(ch.getReadBuf());
ch.clearReadBuf();
if (makeUpper(confirmation[0]) == 'Y') {
ch.addWriteBuf("Using " + ch.getName() + ", please enter a new password: ");
ch.setStatus(STATUS_NEW_PWORD);
} else {
ch.addWriteBuf("Then enter the name you would like: ");
ch.setStatus(STATUS_GETNAME);
}
}
break;
case STATUS_NEW_PWORD:
if (ch.isReadyBuf()) {
string newPword;
newPword = stripCarriageReturn(ch.getReadBuf());
ch.setPword(newPword);
ch.clearReadBuf();
if (!ch.isPwordOk()) {
ch.addWriteBuf("Invalid password, please use another: ");
ch.setStatus(STATUS_NEW_PWORD);
break;
} else {
ch.addWriteBuf("Re-enter your password for confirmation: ");
ch.setStatus(STATUS_CONFIRM_NEW_PWORD);
}
}
break;
case STATUS_CONFIRM_NEW_PWORD:
if (ch.isReadyBuf()) {
string newPword;
newPword = stripCarriageReturn(ch.getReadBuf());
ch.clearReadBuf();
if (ch.getPword() == newPword) {
ch.setStatus(STATUS_WELCOME);
} else {
ch.addWriteBuf("Passwords do not match, please enter a new password: ");
ch.setStatus(STATUS_NEW_PWORD);
}
}
break;
case STATUS_PWORD:
if (ch.isReadyBuf()) {
string pword;
pword = stripCarriageReturn(ch.getReadBuf());
ch.clearReadBuf();
if (!(pword == ch.getPword())) {
ch.addWriteBuf("Wrong password, please enter again: \n\r");
ch.setStatus(STATUS_PWORD);
} else {
ch.setStatus(STATUS_WELCOME);
}
}
break;
case STATUS_WELCOME:
ch.setStatus(STATUS_INGAME);
writeChar(ch, "Welcome to Cowmud, " + ch.getName() + "!\n\r");
for (int count2 = 1; count2 <= c_list.listLength(); count2++) {
c_list.listRetrieve(count2, ch2);
if (ch.getSocket() != ch2.getSocket()) {
ch2.addWriteBuf(ch.getName() +
" has entered COWMUD.\n\r");
}
c_list.listReplace(count2, ch2);
}
ch.save();
break;
default:
cout << "ERROR" << endl;
break;
}
CLOOP_END}
}