character.cpp
cowmud-0.11/src/character.cpp · 2.3 KB
From the archive cowmud-0.11.tar.gz
Download the archive « Back to the archive contents
#include "character.h"
#include "defines.h"
//Load types
#define LOAD_TYPE_INT 1
#define LOAD_TYPE_STRING 2
Character::Character()
{
pword = "";
}
Character::~Character()
{
}
Character::Character(int socket)
{
setSocket(socket);
}
void Character::save()
{
ofstream oFile;
string filename = PLAYER_DIR + getName();
const char *str = filename.c_str();
oFile.open(str);
outputCharacter(oFile);
oFile.close();
}
void Character::outputCharacter(ofstream & oFile)
{
oFile << LOAD_TYPE_STRING << " Name: " << getName() << endl;
oFile << LOAD_TYPE_STRING << " Pword: " << getPword() << endl;
oFile << LOAD_TYPE_INT << " Level: " << getLevel() << endl;
}
bool Character::load()
{
ifstream iFile;
string filename = PLAYER_DIR + getName();
string identifier, svalue;
int type, ivalue;
const char *str = filename.c_str();
iFile.open(str);
if (iFile.fail())
return false;
while (!iFile.eof()) {
iFile >> type >> identifier;
if (!iFile.eof()) {
switch (type) {
case LOAD_TYPE_STRING:
iFile >> svalue;
// This set of if statements determines which character 'set' funciton to call
if (identifier == "Name:") {
setName(svalue);
break;
} else if (identifier == "Pword:") {
setPword(svalue);
break;
} else {
cout << "Error in reading file: " << filename <<
". No such identifier: " << identifier <<
" for string type" << endl;
exit(1);
break;
}
case LOAD_TYPE_INT:
iFile >> ivalue;
if (identifier == "Level:") {
setLevel(ivalue);
break;
} else {
cout << "Error in reading file: " << filename <<
". No such identifier: " << identifier <<
" for integer type" << endl;
exit(1);
break;
}
}
}
}
return true;
}
void Character::setPword(string str)
{
pword = str;
}
string Character::getPword()
{
return pword;
}
bool Character::isPwordOk()
{
string newPword = pword;
if (newPword.length() < 5 || newPword.length() > 15) {
return false;
}
//character checking
for (uint count = 0; count < newPword.length(); count++) {
uint asciicode = (uint) newPword[count];
if (asciicode < 48 || (asciicode > 57 && asciicode < 65)
|| (asciicode > 133 && asciicode < 141)
|| asciicode > 172) {
return false;
}
}
return true;
}