2001-01-26 16:38:23 +00:00
|
|
|
// This file may be redistributed and modified only under the terms of
|
2001-03-16 11:38:14 +00:00
|
|
|
// the GNU General Public License (See COPYING for details).
|
2001-04-16 16:26:44 +00:00
|
|
|
// Copyright (C) 2000,2001 Alistair Riddoch
|
2001-01-26 16:38:23 +00:00
|
|
|
|
2000-08-19 16:42:18 +00:00
|
|
|
#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>
|
2001-02-19 18:50:53 +00:00
|
|
|
#include <Atlas/Objects/Operation/Get.h>
|
2000-08-19 16:42:18 +00:00
|
|
|
|
2001-02-26 14:41:10 +00:00
|
|
|
#include <common/Chop.h>
|
|
|
|
|
#include <common/Cut.h>
|
|
|
|
|
#include <common/Eat.h>
|
|
|
|
|
#include <common/Fire.h>
|
|
|
|
|
|
2000-12-07 12:14:35 +00:00
|
|
|
#include <rulesets/Character.h>
|
2001-01-06 17:38:07 +00:00
|
|
|
#include <rulesets/ExternalMind.h>
|
2001-02-04 07:08:07 +00:00
|
|
|
#include <common/debug.h>
|
2001-03-15 11:41:34 +00:00
|
|
|
#include <common/persistance.h>
|
2001-04-10 19:05:14 +00:00
|
|
|
#include <common/globals.h>
|
2000-12-07 12:14:35 +00:00
|
|
|
|
2000-08-19 16:42:18 +00:00
|
|
|
#include "Connection.h"
|
2000-12-12 13:10:17 +00:00
|
|
|
#include "ServerRouting.h"
|
|
|
|
|
#include "CommClient.h"
|
2001-04-10 19:05:14 +00:00
|
|
|
#include "CommServer.h"
|
2000-08-19 16:42:18 +00:00
|
|
|
#include "Player.h"
|
|
|
|
|
|
2001-02-04 07:08:07 +00:00
|
|
|
static const bool debug_flag = false;
|
2000-11-10 20:41:24 +00:00
|
|
|
|
2001-03-14 12:31:16 +00:00
|
|
|
using Atlas::Message::Object;
|
|
|
|
|
using Atlas::Objects::Operation::Info;
|
2000-08-19 16:42:18 +00:00
|
|
|
|
2001-04-11 14:34:15 +00:00
|
|
|
Connection::Connection(CommClient & client) : commClient(client),
|
2001-04-16 16:26:44 +00:00
|
|
|
server(commClient.commServer.server)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Connection::~Connection()
|
|
|
|
|
{
|
|
|
|
|
}
|
2001-04-10 19:05:14 +00:00
|
|
|
|
2001-04-16 16:26:44 +00:00
|
|
|
void Connection::send(const RootOperation * msg) const
|
|
|
|
|
{
|
2001-04-11 14:34:15 +00:00
|
|
|
commClient.send(msg);
|
2001-04-10 19:05:14 +00:00
|
|
|
}
|
|
|
|
|
|
2001-04-16 16:26:44 +00:00
|
|
|
Account * Connection::addPlayer(const string& username, const string& password)
|
2000-08-19 16:42:18 +00:00
|
|
|
{
|
2000-12-12 13:10:17 +00:00
|
|
|
Player * player=new Player(this, username, password);
|
2001-04-11 14:34:15 +00:00
|
|
|
addObject(player);
|
2000-12-12 13:10:17 +00:00
|
|
|
player->connection=this;
|
2001-04-16 16:26:44 +00:00
|
|
|
player->world=&server.getWorld();
|
2001-04-11 14:34:15 +00:00
|
|
|
server.addObject(player);
|
2000-12-12 13:10:17 +00:00
|
|
|
return(player);
|
2000-08-19 16:42:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Connection::destroy()
|
|
|
|
|
{
|
2001-02-04 07:08:07 +00:00
|
|
|
debug(cout << "destroy called";);
|
2001-04-11 14:34:15 +00:00
|
|
|
dict_t::const_iterator I;
|
|
|
|
|
for(I = objects.begin(); I != objects.end(); I++) {
|
2000-12-07 12:14:35 +00:00
|
|
|
BaseEntity * ent = I->second;
|
2001-04-11 14:34:15 +00:00
|
|
|
if (ent->inGame == false) {
|
2000-12-07 12:14:35 +00:00
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
Thing * obj = (Thing*)ent;
|
2001-04-11 14:34:15 +00:00
|
|
|
if (obj->isCharacter == true) {
|
2000-12-07 12:14:35 +00:00
|
|
|
Character * character = (Character *)obj;
|
2001-04-16 16:26:44 +00:00
|
|
|
// FIXME Should do something to delete the external mind
|
2001-04-11 14:34:15 +00:00
|
|
|
if (character->externalMind != NULL) {
|
|
|
|
|
character->externalMind = NULL;
|
2000-12-07 12:14:35 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
BaseEntity::destroy();
|
2000-08-19 16:42:18 +00:00
|
|
|
}
|
|
|
|
|
|
2000-08-31 21:48:46 +00:00
|
|
|
oplist Connection::operation(const RootOperation & op)
|
2000-08-19 16:42:18 +00:00
|
|
|
{
|
2001-02-04 07:08:07 +00:00
|
|
|
debug(cout << "Connection::operation" << endl << flush;);
|
2000-08-19 16:42:18 +00:00
|
|
|
const string & from = op.GetFrom();
|
|
|
|
|
if (0==from.size()) {
|
2001-02-04 07:08:07 +00:00
|
|
|
debug(cout << "deliver locally as normal" << endl << flush;);
|
2000-08-19 16:42:18 +00:00
|
|
|
return BaseEntity::operation(op);
|
|
|
|
|
} else {
|
2001-02-04 07:08:07 +00:00
|
|
|
debug(cout << "Must send on to account" << endl << flush;);
|
|
|
|
|
debug(cout << "[" << from << "]" << endl << flush;);
|
2001-04-16 16:26:44 +00:00
|
|
|
dict_t::const_iterator I = objects.find(from);
|
|
|
|
|
if (I != objects.end()) {
|
|
|
|
|
BaseEntity * ent = I->second;
|
2001-04-11 14:34:15 +00:00
|
|
|
if ((ent->inGame != false)&&(((Thing *)ent)->isCharacter != 0) &&
|
|
|
|
|
(((Character *)ent)->externalMind == NULL)) {
|
2001-01-06 17:38:07 +00:00
|
|
|
Character * pchar = (Character *)ent;
|
2001-04-11 14:34:15 +00:00
|
|
|
pchar->externalMind = new ExternalMind(*this, pchar->fullid, pchar ->name);
|
2001-01-06 17:38:07 +00:00
|
|
|
cout << "Re-connecting existing character to new connection" << endl << flush;
|
2001-04-12 11:13:06 +00:00
|
|
|
Info * info = new Info(Info::Instantiate());
|
2001-03-14 12:31:16 +00:00
|
|
|
Object::ListType args(1,pchar->asObject());
|
2001-01-06 17:38:07 +00:00
|
|
|
info->SetArgs(args);
|
|
|
|
|
info->SetRefno(op.GetSerialno());
|
2001-04-11 14:34:15 +00:00
|
|
|
oplist res = ent->externalOperation(op);
|
2001-02-26 14:41:10 +00:00
|
|
|
res.insert(res.begin(), info);
|
2001-01-06 17:38:07 +00:00
|
|
|
return res;
|
|
|
|
|
}
|
2001-04-11 14:34:15 +00:00
|
|
|
return ent->externalOperation(op);
|
2000-08-19 16:42:18 +00:00
|
|
|
} else {
|
|
|
|
|
return error(op, "From is illegal");
|
|
|
|
|
}
|
|
|
|
|
}
|
2001-02-26 14:41:10 +00:00
|
|
|
return oplist();
|
2000-08-19 16:42:18 +00:00
|
|
|
}
|
|
|
|
|
|
2000-11-10 00:57:34 +00:00
|
|
|
oplist Connection::Operation(const Login & op)
|
2000-08-19 16:42:18 +00:00
|
|
|
{
|
|
|
|
|
|
2001-02-04 07:08:07 +00:00
|
|
|
debug(cout << "Got login op" << endl << flush;);
|
2001-03-14 12:31:16 +00:00
|
|
|
const Object & account = op.GetArgs().front();
|
2000-08-19 16:42:18 +00:00
|
|
|
|
|
|
|
|
if (account.IsMap()) {
|
2001-04-16 16:26:44 +00:00
|
|
|
const string account_id = account.AsMap().find("id")->second.AsString();
|
|
|
|
|
const string password = account.AsMap().find("password")->second.AsString();
|
2000-08-19 16:42:18 +00:00
|
|
|
|
2001-04-11 14:34:15 +00:00
|
|
|
Account * player = (Player *)server.getObject(account_id);
|
2001-03-15 11:41:34 +00:00
|
|
|
if (player == NULL) {
|
|
|
|
|
player = Persistance::instance()->getAccount(account_id);
|
2001-04-11 14:34:15 +00:00
|
|
|
if (player != NULL) {
|
2001-04-16 16:26:44 +00:00
|
|
|
player->world=&server.getWorld();
|
2001-04-11 14:34:15 +00:00
|
|
|
server.addObject(player);
|
|
|
|
|
}
|
2001-03-15 11:41:34 +00:00
|
|
|
}
|
2000-08-19 16:42:18 +00:00
|
|
|
if (player && (account_id.size()!=0) && (password==player->password)) {
|
2001-04-11 14:34:15 +00:00
|
|
|
addObject(player);
|
2001-04-16 16:26:44 +00:00
|
|
|
edict_t::const_iterator I;
|
2001-04-11 14:34:15 +00:00
|
|
|
for (I=player->charactersDict.begin();
|
|
|
|
|
I!=player->charactersDict.end(); I++) {
|
|
|
|
|
addObject(I->second);
|
2001-01-06 17:38:07 +00:00
|
|
|
}
|
2000-08-19 16:42:18 +00:00
|
|
|
player->connection=this;
|
2001-04-12 11:13:06 +00:00
|
|
|
Info * info = new Info(Info::Instantiate());
|
2001-03-14 12:31:16 +00:00
|
|
|
Object::ListType args(1,player->asObject());
|
2000-08-19 16:42:18 +00:00
|
|
|
info->SetArgs(args);
|
2000-11-10 00:57:34 +00:00
|
|
|
info->SetRefno(op.GetSerialno());
|
2001-02-04 07:08:07 +00:00
|
|
|
debug(cout << "Good login" << endl << flush;);
|
2000-08-31 21:48:46 +00:00
|
|
|
return(oplist(1,info));
|
2000-08-19 16:42:18 +00:00
|
|
|
}
|
|
|
|
|
}
|
2001-02-26 14:41:10 +00:00
|
|
|
return error(op, "Login is invalid");
|
2000-08-19 16:42:18 +00:00
|
|
|
}
|
|
|
|
|
|
2000-11-10 00:57:34 +00:00
|
|
|
oplist Connection::Operation(const Create & op)
|
2000-08-19 16:42:18 +00:00
|
|
|
{
|
2001-02-04 07:08:07 +00:00
|
|
|
debug(cout << "Got create op" << endl << flush;);
|
2001-03-14 12:31:16 +00:00
|
|
|
const Object & account = op.GetArgs().front();
|
2000-08-19 16:42:18 +00:00
|
|
|
|
2001-03-15 11:41:34 +00:00
|
|
|
if (Persistance::restricted) {
|
|
|
|
|
return error(op, "Account creation on this server is restricted");
|
|
|
|
|
}
|
|
|
|
|
|
2000-08-19 16:42:18 +00:00
|
|
|
if (account.IsMap()) {
|
2001-04-16 16:26:44 +00:00
|
|
|
const string & account_id = account.AsMap().find("id")->second.AsString();
|
|
|
|
|
const string & password = account.AsMap().find("password")->second.AsString();
|
2000-08-19 16:42:18 +00:00
|
|
|
|
2001-04-11 14:34:15 +00:00
|
|
|
if ((NULL==server.getObject(account_id)) &&
|
2001-03-15 20:36:14 +00:00
|
|
|
(!Persistance::instance()->findAccount(account_id)) &&
|
2000-08-19 16:42:18 +00:00
|
|
|
(account_id.size() != 0) && (password.size() != 0)) {
|
2001-04-11 14:34:15 +00:00
|
|
|
Account * player = addPlayer(account_id, password);
|
2001-03-15 11:41:34 +00:00
|
|
|
Persistance::instance()->putAccount(player);
|
2001-04-12 11:13:06 +00:00
|
|
|
Info * info = new Info(Info::Instantiate());
|
2001-03-14 12:31:16 +00:00
|
|
|
Object::ListType args(1,player->asObject());
|
2000-08-19 16:42:18 +00:00
|
|
|
info->SetArgs(args);
|
2000-11-10 00:57:34 +00:00
|
|
|
info->SetRefno(op.GetSerialno());
|
2001-02-04 07:08:07 +00:00
|
|
|
debug(cout << "Good create" << endl << flush;);
|
2000-08-31 21:48:46 +00:00
|
|
|
return(oplist(1,info));
|
2000-08-19 16:42:18 +00:00
|
|
|
}
|
|
|
|
|
}
|
2001-02-26 14:41:10 +00:00
|
|
|
return error(op, "Account creation is invalid");
|
2000-08-19 16:42:18 +00:00
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2001-02-19 18:50:53 +00:00
|
|
|
oplist Connection::Operation(const Logout & op)
|
2000-08-19 16:42:18 +00:00
|
|
|
{
|
2001-03-14 12:31:16 +00:00
|
|
|
const Object & account = op.GetArgs().front();
|
2000-08-25 17:54:30 +00:00
|
|
|
|
|
|
|
|
if (account.IsMap()) {
|
2001-04-16 16:26:44 +00:00
|
|
|
const string & account_id = account.AsMap().find("id")->second.AsString();
|
|
|
|
|
// const string & password = account.AsMap().find("password")->second.AsString();
|
2001-04-11 14:34:15 +00:00
|
|
|
Player * player = (Player *)server.getObject(account_id);
|
2000-08-25 17:54:30 +00:00
|
|
|
if (player) {
|
2001-02-19 18:50:53 +00:00
|
|
|
Logout l = op;
|
2000-08-25 17:54:30 +00:00
|
|
|
l.SetFrom(player->fullid);
|
2001-02-04 07:08:07 +00:00
|
|
|
debug(cout << "Logout without from. Using " << player->fullid << " instead." << endl << flush;);
|
2000-08-25 17:54:30 +00:00
|
|
|
operation(l);
|
|
|
|
|
}
|
|
|
|
|
}
|
2001-02-26 14:41:10 +00:00
|
|
|
return oplist();
|
2000-08-19 16:42:18 +00:00
|
|
|
}
|
2001-02-19 18:50:53 +00:00
|
|
|
|
|
|
|
|
oplist Connection::Operation(const Get & op)
|
|
|
|
|
{
|
|
|
|
|
cout << "Got get" << endl << flush;
|
2001-04-12 11:13:06 +00:00
|
|
|
Info * info = new Info(Info::Instantiate());
|
2001-04-10 19:05:14 +00:00
|
|
|
Object::ListType args(1,server.asObject());
|
2001-02-19 18:50:53 +00:00
|
|
|
info->SetArgs(args);
|
|
|
|
|
info->SetRefno(op.GetSerialno());
|
|
|
|
|
cout << "Replying to get" << endl << flush;
|
|
|
|
|
|
2001-02-26 14:41:10 +00:00
|
|
|
return oplist(1,info);
|
2001-02-19 18:50:53 +00:00
|
|
|
}
|