mirror of
https://github.com/worldforge/cyphesis
synced 2026-08-13 12:26:04 -04:00
* TODO: Update on important issues which allow easy DOS attacks. * client/ClientConnection.cpp, client/ClientConnection.h: Cleaned up unused code from ClientConnection class. * client/client.cpp: Removed monitoring section of client so it simply exits once the world is initialised. * rulesets/basic/mind/goals/humanoid/werewolf.py: Started work on dynamic goals for playing werewolf. * rulesets/werewolf/define_world.py: Added in the mayor, who runs werewolf games. * common/BaseEntity.h: Made id of entity private, and implemented accessors. Al Riddoch <alriddoch@zepler.org>
221 lines
7.5 KiB
C++
221 lines
7.5 KiB
C++
// This file may be redistributed and modified only under the terms of
|
|
// the GNU General Public License (See COPYING for details).
|
|
// Copyright (C) 2000,2001 Alistair Riddoch
|
|
|
|
#include <Atlas/Objects/Operation/Create.h>
|
|
#include <Atlas/Objects/Operation/Login.h>
|
|
#include <Atlas/Objects/Operation/Info.h>
|
|
#include <Atlas/Objects/Operation/Get.h>
|
|
|
|
#include <rulesets/Character.h>
|
|
#include <common/debug.h>
|
|
#include <common/globals.h>
|
|
#include <common/inheritance.h>
|
|
|
|
#include "Connection.h"
|
|
#include "ServerRouting.h"
|
|
#include "Lobby.h"
|
|
#include "CommClient.h"
|
|
#include "CommServer.h"
|
|
#include "Player.h"
|
|
#include "ExternalMind.h"
|
|
#include "Persistance.h"
|
|
|
|
static const bool debug_flag = false;
|
|
|
|
using Atlas::Message::Object;
|
|
using Atlas::Objects::Operation::Info;
|
|
|
|
Connection::Connection(CommClient & client) : commClient(client),
|
|
server(commClient.commServer.server)
|
|
{
|
|
}
|
|
|
|
Connection::~Connection()
|
|
{
|
|
}
|
|
|
|
void Connection::send(const RootOperation * msg) const
|
|
{
|
|
commClient.send(msg);
|
|
}
|
|
|
|
Account * Connection::addPlayer(const std::string& username,
|
|
const std::string& password)
|
|
{
|
|
Player * player=new Player(this, username, password);
|
|
addObject(player);
|
|
player->connection=this;
|
|
player->world=&server.getWorld();
|
|
server.addObject(player);
|
|
server.lobby.addObject(player);
|
|
return player;
|
|
}
|
|
|
|
void Connection::destroy()
|
|
{
|
|
debug(cout << "destroy called";);
|
|
dict_t::const_iterator I;
|
|
for(I = objects.begin(); I != objects.end(); I++) {
|
|
BaseEntity * ent = I->second;
|
|
if (ent->inGame == false) {
|
|
server.lobby.delObject((Account *)ent);
|
|
continue;
|
|
}
|
|
Thing * obj = (Thing*)ent;
|
|
if (obj->isCharacter == true) {
|
|
Character * character = (Character *)obj;
|
|
if (character->externalMind != NULL) {
|
|
delete character->externalMind;
|
|
character->externalMind = NULL;
|
|
}
|
|
}
|
|
}
|
|
BaseEntity::destroy();
|
|
}
|
|
|
|
oplist Connection::operation(const RootOperation & op)
|
|
{
|
|
debug(cout << "Connection::operation" << endl << flush;);
|
|
const std::string & from = op.GetFrom();
|
|
if (0==from.size()) {
|
|
debug(cout << "deliver locally as normal" << endl << flush;);
|
|
return BaseEntity::operation(op);
|
|
} else {
|
|
debug(cout << "Must send on to account" << endl << flush;);
|
|
debug(cout << "[" << from << "]" << endl << flush;);
|
|
dict_t::const_iterator I = objects.find(from);
|
|
if (I != objects.end()) {
|
|
BaseEntity * ent = I->second;
|
|
if ((ent->inGame != false)&&(((Thing *)ent)->isCharacter != 0) &&
|
|
(((Character *)ent)->externalMind == NULL)) {
|
|
Character * pchar = (Character *)ent;
|
|
pchar->externalMind = new ExternalMind(*this, pchar->getId(), pchar ->name);
|
|
debug(cout << "Re-connecting existing character to new connection" << endl << flush;);
|
|
Info * info = new Info(Info::Instantiate());
|
|
info->SetArgs(Object::ListType(1,pchar->asObject()));
|
|
info->SetRefno(op.GetSerialno());
|
|
oplist res = ent->externalOperation(op);
|
|
res.insert(res.begin(), info);
|
|
return res;
|
|
}
|
|
return ent->externalOperation(op);
|
|
} else {
|
|
cout << from;
|
|
return error(op, "From is illegal");
|
|
}
|
|
}
|
|
return oplist();
|
|
}
|
|
|
|
oplist Connection::LoginOperation(const Login & op)
|
|
{
|
|
|
|
debug(cout << "Got login op" << endl << flush;);
|
|
const Object & account = op.GetArgs().front();
|
|
|
|
if (account.IsMap()) {
|
|
const std::string account_id = account.AsMap().find("id")->second.AsString();
|
|
const std::string password = account.AsMap().find("password")->second.AsString();
|
|
|
|
Account * player = (Player *)server.getObject(account_id);
|
|
if (player == NULL) {
|
|
player = Persistance::instance()->getAccount(account_id);
|
|
if (player != NULL) {
|
|
player->world=&server.getWorld();
|
|
server.addObject(player);
|
|
server.lobby.addObject(player);
|
|
}
|
|
}
|
|
if (player && (account_id.size()!=0) && (password==player->password)) {
|
|
addObject(player);
|
|
edict_t::const_iterator I;
|
|
for (I=player->charactersDict.begin();
|
|
I!=player->charactersDict.end(); I++) {
|
|
addObject(I->second);
|
|
}
|
|
player->connection=this;
|
|
Info * info = new Info(Info::Instantiate());
|
|
info->SetArgs(Object::ListType(1,player->asObject()));
|
|
info->SetRefno(op.GetSerialno());
|
|
debug(cout << "Good login" << endl << flush;);
|
|
return oplist(1,info);
|
|
}
|
|
}
|
|
return error(op, "Login is invalid");
|
|
}
|
|
|
|
oplist Connection::CreateOperation(const Create & op)
|
|
{
|
|
debug(cout << "Got create op" << endl << flush;);
|
|
const Object & account = op.GetArgs().front();
|
|
|
|
if (Persistance::restricted) {
|
|
return error(op, "Account creation on this server is restricted");
|
|
}
|
|
|
|
if (account.IsMap()) {
|
|
const std::string & account_id = account.AsMap().find("id")->second.AsString();
|
|
const std::string & password = account.AsMap().find("password")->second.AsString();
|
|
|
|
if ((NULL==server.getObject(account_id)) &&
|
|
(!Persistance::instance()->findAccount(account_id)) &&
|
|
(account_id.size() != 0) && (password.size() != 0)) {
|
|
Account * player = addPlayer(account_id, password);
|
|
Persistance::instance()->putAccount(*player);
|
|
Info * info = new Info(Info::Instantiate());
|
|
info->SetArgs(Object::ListType(1,player->asObject()));
|
|
info->SetRefno(op.GetSerialno());
|
|
debug(cout << "Good create" << endl << flush;);
|
|
return oplist(1,info);
|
|
}
|
|
}
|
|
return error(op, "Account creation is invalid");
|
|
|
|
}
|
|
|
|
oplist Connection::LogoutOperation(const Logout & op)
|
|
{
|
|
const Object & account = op.GetArgs().front();
|
|
|
|
if (account.IsMap()) {
|
|
const std::string & account_id = account.AsMap().find("id")->second.AsString();
|
|
// const std::string & password = account.AsMap().find("password")->second.AsString();
|
|
Player * player = (Player *)server.getObject(account_id);
|
|
if (player) {
|
|
Logout l = op;
|
|
l.SetFrom(player->getId());
|
|
debug(cout << "Logout without from. Using " << player->getId() << " instead." << endl << flush;);
|
|
operation(l);
|
|
}
|
|
}
|
|
return oplist();
|
|
}
|
|
|
|
oplist Connection::GetOperation(const Get & op)
|
|
{
|
|
const Object::ListType & args = op.GetArgs();
|
|
|
|
cout << "Got get" << endl << flush;
|
|
|
|
Info * info;
|
|
if (args.empty()) {
|
|
info = new Info(Info::Instantiate());
|
|
info->SetArgs(Object::ListType(1,server.asObject()));
|
|
info->SetRefno(op.GetSerialno());
|
|
debug(cout << "Replying to empty get" << endl << flush;);
|
|
} else {
|
|
Object::MapType::const_iterator I = args.front().AsMap().find("id");
|
|
if (I == args.front().AsMap().end()) {
|
|
return oplist();
|
|
}
|
|
const std::string & id = I->second.AsString();
|
|
std::cout << "Get got for " << id << std::endl << std::flush;
|
|
Atlas::Objects::Root * o = Inheritance::instance().get(id);
|
|
info = new Info(Info::Instantiate());
|
|
info->SetArgs(Object::ListType(1,o->AsObject()));
|
|
info->SetRefno(op.GetSerialno());
|
|
}
|
|
|
|
return oplist(1,info);
|
|
}
|