cyphesis/server/Connection.cpp
Al Riddoch 488f35b52f * Complete re-write of python script interface to allow other
script types to be cleanly dropped in.

	* Re-work on server and communication related classes to use
	  references where they are apropriate. Code is now much more
	  robust.

	* Huge cleanup of header files removing redundant includes.

	* server/WorldRouter.h: Removed is_object_deleted because it
	  is broken and not used.

	* server/Connection.h: Removed unused disconnect() methods.

	* rulesets/Entity.*: Added new in game base class from which
	  all mind and object classes are derived. This removes
	  clutter of Thing class from mind classes.

	* aiclient/*: Progress with creating AI client for mind handling.

	* common/globals.*: Moved some globals out of server specific files.

	* rulesets/Makefile.am: Separated rulesets code into 3 almost stand
	  alone packages; entities, mind and python interface code.


Acorn may well not work now, but I would apreciate it if people could
test the code on their setups. Lots of code has changed, and almost
everything is now cleaner.

Al Riddoch  <alriddoch@zepler.org>
2001-04-10 19:05:14 +00:00

202 lines
6.7 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 Alistair Riddoch
#include <Atlas/Message/Object.h>
#include <Atlas/Net/Stream.h>
#include <Atlas/Objects/Root.h>
#include <Atlas/Objects/Encoder.h>
#include <Atlas/Objects/Decoder.h>
#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 <common/Chop.h>
#include <common/Cut.h>
#include <common/Eat.h>
#include <common/Fire.h>
#include <rulesets/Character.h>
#include <rulesets/ExternalMind.h>
#include <common/debug.h>
#include <common/persistance.h>
#include <common/globals.h>
#include "Connection.h"
#include "ServerRouting.h"
#include "CommClient.h"
#include "CommServer.h"
#include "Player.h"
static const bool debug_flag = false;
using Atlas::Message::Object;
using Atlas::Objects::Operation::Info;
Connection::Connection(CommClient & client) : comm_client(client),
server(comm_client.commServer.server) { }
void Connection::send(const RootOperation * msg) const {
comm_client.send(msg);
}
Account * Connection::add_player(string & username, string & password)
{
Player * player=new Player(this, username, password);
add_object(player);
player->connection=this;
player->world=server.world;
server.add_object(player);
return(player);
}
void Connection::destroy()
{
debug(cout << "destroy called";);
fdict_t::const_iterator I;
for(I = fobjects.begin(); I != fobjects.end(); I++) {
BaseEntity * ent = I->second;
if (ent->in_game == false) {
continue;
}
Thing * obj = (Thing*)ent;
if (obj->is_character == true) {
Character * character = (Character *)obj;
if (character->external_mind != NULL) {
character->external_mind = NULL;
}
}
}
BaseEntity::destroy();
}
oplist Connection::operation(const RootOperation & op)
{
debug(cout << "Connection::operation" << endl << flush;);
const 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;);
if (fobjects.find(from)!=fobjects.end()) {
BaseEntity * ent = fobjects[from];
if ((ent->in_game != false)&&(((Thing *)ent)->is_character != 0) &&
(((Character *)ent)->external_mind == NULL)) {
Character * pchar = (Character *)ent;
pchar->external_mind = new ExternalMind(this, pchar->fullid, pchar ->name);
cout << "Re-connecting existing character to new connection" << endl << flush;
Info * info = new Info();
*info = Info::Instantiate();
Object::ListType args(1,pchar->asObject());
info->SetArgs(args);
info->SetRefno(op.GetSerialno());
oplist res = ent->external_operation(op);
res.insert(res.begin(), info);
return res;
}
return ent->external_operation(op);
} else {
return error(op, "From is illegal");
}
}
return oplist();
}
oplist Connection::Operation(const Login & op)
{
debug(cout << "Got login op" << endl << flush;);
const Object & account = op.GetArgs().front();
if (account.IsMap()) {
string account_id = account.AsMap().find("id")->second.AsString();
string password = account.AsMap().find("password")->second.AsString();
Account * player = (Player *)server.get_object(account_id);
if (player == NULL) {
player = Persistance::instance()->getAccount(account_id);
}
if (player && (account_id.size()!=0) && (password==player->password)) {
add_object(player);
fdict_t::const_iterator I;
for (I=player->characters_dict.begin();
I!=player->characters_dict.end(); I++) {
add_object(I->second);
}
player->connection=this;
Info * info = new Info();
*info = Info::Instantiate();
Object::ListType args(1,player->asObject());
info->SetArgs(args);
info->SetRefno(op.GetSerialno());
debug(cout << "Good login" << endl << flush;);
return(oplist(1,info));
}
}
return error(op, "Login is invalid");
}
oplist Connection::Operation(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()) {
string account_id = account.AsMap().find("id")->second.AsString();
string password = account.AsMap().find("password")->second.AsString();
if ((NULL==server.get_object(account_id)) &&
(!Persistance::instance()->findAccount(account_id)) &&
(account_id.size() != 0) && (password.size() != 0)) {
Account * player = add_player(account_id, password);
Persistance::instance()->putAccount(player);
Info * info = new Info();
*info = Info::Instantiate();
Object::ListType args(1,player->asObject());
info->SetArgs(args);
info->SetRefno(op.GetSerialno());
debug(cout << "Good create" << endl << flush;);
return(oplist(1,info));
}
}
return error(op, "Account creation is invalid");
}
oplist Connection::Operation(const Logout & op)
{
const Object & account = op.GetArgs().front();
if (account.IsMap()) {
string account_id = account.AsMap().find("id")->second.AsString();
string password = account.AsMap().find("password")->second.AsString();
Player * player = (Player *)server.get_object(account_id);
if (player) {
Logout l = op;
l.SetFrom(player->fullid);
debug(cout << "Logout without from. Using " << player->fullid << " instead." << endl << flush;);
operation(l);
}
}
return oplist();
}
oplist Connection::Operation(const Get & op)
{
cout << "Got get" << endl << flush;
Info * info = new Info();
*info = Info::Instantiate();
Object::ListType args(1,server.asObject());
info->SetArgs(args);
info->SetRefno(op.GetSerialno());
cout << "Replying to get" << endl << flush;
return oplist(1,info);
}