added lots of things
This commit is contained in:
parent
61b4619a55
commit
919e7700c2
@ -1,12 +1,71 @@
|
|||||||
#include "rpgmodule.h"
|
#include "rpgmodule.h"
|
||||||
#include <QString>
|
#include <QString>
|
||||||
#include <QStringList>
|
#include <QStringList>
|
||||||
|
#include <QFileInfo>
|
||||||
|
#include <QTextStream>
|
||||||
#include "message.h"
|
#include "message.h"
|
||||||
#include "user.h"
|
#include "user.h"
|
||||||
|
|
||||||
|
QString RPGCharacter::toString()
|
||||||
|
{
|
||||||
|
QString str;
|
||||||
|
str.append(pseudo).append(" ");
|
||||||
|
str.append(password).append(" ");
|
||||||
|
str.append(level).append(" ");
|
||||||
|
str.append(xp).append(" ");
|
||||||
|
str.append(x).append(" ");
|
||||||
|
str.append(y);
|
||||||
|
return str;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool RPGCharacter::fromString(QString str)
|
||||||
|
{
|
||||||
|
QStringList list = str.split(" ");
|
||||||
|
if(list.size() == 6)
|
||||||
|
{
|
||||||
|
pseudo = list[0];
|
||||||
|
password = list[1];
|
||||||
|
level = list[2].toInt();
|
||||||
|
xp = list[3].toInt();
|
||||||
|
x = list[4].toInt();
|
||||||
|
y = list[5].toInt();
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
RPGModule::RPGModule()
|
RPGModule::RPGModule()
|
||||||
{
|
{
|
||||||
// TODO : load save file
|
QFile f("../../res/rpg.save");
|
||||||
|
if(!f.open(QFile::ReadOnly | QFile::Text))
|
||||||
|
{
|
||||||
|
fprintf(stderr, "can't load save file.\n");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
QTextStream in(&f);
|
||||||
|
QString line = "plop (line init)";
|
||||||
|
do
|
||||||
|
{
|
||||||
|
line = in.readLine();
|
||||||
|
charList.add(new RPGCharacter(line));
|
||||||
|
}
|
||||||
|
while(!line.isNull());
|
||||||
|
f.close();
|
||||||
|
}
|
||||||
|
|
||||||
|
void RPGModule::saveCharacters()
|
||||||
|
{
|
||||||
|
QFile f("../../res/rpg.save");
|
||||||
|
if(!f.open(QFile::WriteOnly | QFile::Text))
|
||||||
|
{
|
||||||
|
fprintf(stderr, "can't load save file.\n");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
QTextStream out(&f);
|
||||||
|
for(const std::string &name : charList.names)
|
||||||
|
out << charList.get(name)->toString() << "\n";
|
||||||
|
f.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
void RPGModule::playerJoin(User *user, RPGCharacter *c)
|
void RPGModule::playerJoin(User *user, RPGCharacter *c)
|
||||||
@ -114,7 +173,7 @@ bool RPGModule::messageHandler(Message msg)
|
|||||||
else
|
else
|
||||||
{
|
{
|
||||||
answer = privateSay(QString("successfully created the character \"%1\"").arg(paramList[2]), msg.nick);
|
answer = privateSay(QString("successfully created the character \"%1\"").arg(paramList[2]), msg.nick);
|
||||||
charList.add(paramList[2].toStdString(), new RPGCharacter(paramList[2], paramList[3]));
|
charList.add(new RPGCharacter(paramList[2], paramList[3]));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
@ -167,6 +226,23 @@ bool RPGModule::messageHandler(Message msg)
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
else if(msg.args.compare("!rpg debug") == 0 && src->isOp())
|
||||||
|
{
|
||||||
|
QString str;
|
||||||
|
for(const std::string &name : charList.names)
|
||||||
|
str.append(name.c_str()).append(" ");
|
||||||
|
if(!str.isEmpty())
|
||||||
|
{
|
||||||
|
answer = say(str);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else if(msg.args.compare("!rpg save") == 0)
|
||||||
|
{
|
||||||
|
saveCharacters();
|
||||||
|
answer = say("saved RPG characters");
|
||||||
|
return true;
|
||||||
|
}
|
||||||
else if(msg.args.startsWith("go "))
|
else if(msg.args.startsWith("go "))
|
||||||
{
|
{
|
||||||
QStringList paramList = msg.args.split(' ');
|
QStringList paramList = msg.args.split(' ');
|
||||||
|
@ -45,6 +45,8 @@ const char WORLDMAP[25][26] = {
|
|||||||
|
|
||||||
struct RPGCharacter
|
struct RPGCharacter
|
||||||
{
|
{
|
||||||
|
static const int VERSION = 1;
|
||||||
|
|
||||||
int level;
|
int level;
|
||||||
int xp;
|
int xp;
|
||||||
QString password;
|
QString password;
|
||||||
@ -62,6 +64,18 @@ struct RPGCharacter
|
|||||||
y(13),
|
y(13),
|
||||||
user(NULL)
|
user(NULL)
|
||||||
{}
|
{}
|
||||||
|
|
||||||
|
RPGCharacter(QString serialisationStr) : user(NULL)
|
||||||
|
{
|
||||||
|
if(!fromString(serialisationStr))
|
||||||
|
{
|
||||||
|
pseudo = QString("trash");
|
||||||
|
password = QString("trashpass");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
QString toString();
|
||||||
|
bool fromString(QString str);
|
||||||
};
|
};
|
||||||
|
|
||||||
class CharacterMap
|
class CharacterMap
|
||||||
@ -70,8 +84,9 @@ public:
|
|||||||
std::vector<std::string> names;
|
std::vector<std::string> names;
|
||||||
std::unordered_map<std::string, RPGCharacter*> data;
|
std::unordered_map<std::string, RPGCharacter*> data;
|
||||||
|
|
||||||
void add(const std::string &name, RPGCharacter* t)
|
void add(RPGCharacter* t)
|
||||||
{
|
{
|
||||||
|
std::string name = t->pseudo.toStdString();
|
||||||
data[name] = t;
|
data[name] = t;
|
||||||
names.push_back(name);
|
names.push_back(name);
|
||||||
}
|
}
|
||||||
@ -94,7 +109,8 @@ class RPGModule : public Module
|
|||||||
|
|
||||||
public:
|
public:
|
||||||
RPGModule();
|
RPGModule();
|
||||||
|
void saveCharacters();
|
||||||
|
|
||||||
void playerJoin(User* user, RPGCharacter *c);
|
void playerJoin(User* user, RPGCharacter *c);
|
||||||
void playerLeave(User* user);
|
void playerLeave(User* user);
|
||||||
QString playerTravel(RPGCharacter *c, QString dir);
|
QString playerTravel(RPGCharacter *c, QString dir);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user