cyphesis/tests/Connectiontest.cpp
Alistair Riddoch 111581e56a Remove obsolete m_task in Character
This pointer is made obsolete now that TasksProperty holds it
2011-12-02 02:37:42 +00:00

1052 lines
20 KiB
C++

// Cyphesis Online RPG Server and AI Engine
// Copyright (C) 2009 Alistair Riddoch
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software Foundation,
// Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
// $Id$
#ifdef NDEBUG
#undef NDEBUG
#endif
#ifndef DEBUG
#define DEBUG
#endif
#include "server/Connection.h"
#include "server/Account.h"
#include "server/CommClient.h"
#include "server/CommServer.h"
#include "server/ExternalMind.h"
#include "server/ExternalProperty.h"
#include "server/Lobby.h"
#include "server/Player.h"
#include "server/ServerRouting.h"
#include "rulesets/Character.h"
#include "common/compose.hpp"
#include "common/Inheritance.h"
#include "common/log.h"
#include "common/SystemTime.h"
#include <Atlas/Objects/Anonymous.h>
#include <Atlas/Objects/Operation.h>
#include <Atlas/Objects/SmartPtr.h>
#include <cstdlib>
#include <cstdio>
#include <cassert>
using Atlas::Message::ListType;
using Atlas::Message::MapType;
using Atlas::Objects::Root;
using Atlas::Objects::Entity::RootEntity;
using Atlas::Objects::Entity::Anonymous;
using Atlas::Objects::Operation::Create;
using Atlas::Objects::Operation::Get;
using Atlas::Objects::Operation::Login;
using Atlas::Objects::Operation::Logout;
using Atlas::Objects::Operation::Move;
class TestCommClient : public CommClient {
public:
TestCommClient(CommServer & cs) : CommClient(cs, "") { }
};
class TestConnection : public Connection {
public:
TestConnection(CommClient & cc, ServerRouting & svr,
const std::string & addr, const std::string & id, long iid) :
Connection(cc, svr, addr, id, iid) {
}
Account * testAddAccount(const std::string & username,
const std::string & password) {
return addAccount("player", username, password);
}
Account * testRemoveAccount(Router * obj) {
return removeAccount(obj, "test_event");
}
size_t numObjects() const {
return m_objects.size();
}
const RouterMap & getObjects() const {
return m_objects;
}
void removeObject(Router * obj) {
RouterMap::iterator I = m_objects.find(obj->getIntId());
if (I != m_objects.end()) {
m_objects.erase(I);
}
}
};
int main()
{
// WorldRouter world(SystemTime());
// Entity & e = world.m_gameWorld;
ServerRouting server(*(BaseWorld*)0, "noruleset", "unittesting",
"1", 1, "2", 2);
CommServer commServer;
TestCommClient * tcc = new TestCommClient(commServer);
TestConnection * tc = new TestConnection(*tcc, server, "addr", "3", 3);
Account * ac = tc->testAddAccount("bob", "foo");
assert(ac != 0);
ac = tc->testRemoveAccount(ac);
assert(ac != 0);
tc->removeObject(ac);
assert(tc->numObjects() == 0);
{
Create op;
OpVector res;
tc->operation(op, res);
op->setArgs1(Root());
tc->operation(op, res);
restricted_flag = true;
tc->operation(op, res);
restricted_flag = false;
Anonymous op_arg;
op->setArgs1(op_arg);
tc->operation(op, res);
op_arg->setId("jim");
// Legacy op
tc->operation(op, res);
op_arg->setAttr("username", 1);
// Malformed username
tc->operation(op, res);
op_arg->setAttr("username", "jim");
// username, no password
tc->operation(op, res);
op_arg->setAttr("password", "");
// zero length password
tc->operation(op, res);
op_arg->setAttr("username", "");
op_arg->setAttr("password", "foo");
// zero length username
tc->operation(op, res);
op_arg->setAttr("username", "jim");
// valid username and password
tc->operation(op, res);
assert(tc->numObjects() != 0);
}
{
Login op;
OpVector res;
tc->operation(op, res);
op->setArgs1(Root());
tc->operation(op, res);
Anonymous op_arg;
op->setArgs1(op_arg);
tc->operation(op, res);
op_arg->setId("bob");
tc->operation(op, res);
op_arg->setAttr("username", 1);
tc->operation(op, res);
op_arg->setAttr("username", "");
tc->operation(op, res);
op_arg->setAttr("username", "bob");
tc->operation(op, res);
op_arg->setAttr("password", "foo");
tc->operation(op, res);
tc->operation(op, res);
}
{
Get op;
OpVector res;
tc->operation(op, res);
Root op_arg;
op->setArgs1(op_arg);
tc->operation(op, res);
op_arg->setId("1");
tc->operation(op, res);
op_arg->setId("game_entity");
tc->operation(op, res);
}
{
Logout op;
OpVector res;
tc->operation(op, res);
op->setSerialno(24);
tc->operation(op, res);
Root op_arg;
op->setArgs1(op_arg);
tc->operation(op, res);
op_arg->setId("-1");
tc->operation(op, res);
op_arg->setId("23");
tc->operation(op, res);
// How to determine the real ID?
const RouterMap rm = tc->getObjects();
RouterMap::const_iterator I = rm.begin();
for (;I != rm.end(); ++I) {
std::string object_id = String::compose("%1", I->first);
std::cout << "ID: " << object_id << std::endl;
op_arg->setId(object_id);
tc->operation(op, res);
}
}
delete tc;
}
// Stubs
bool restricted_flag;
namespace Atlas { namespace Objects { namespace Operation {
int UPDATE_NO = -1;
} } }
CommServer::CommServer() : m_congested(false)
{
}
CommServer::~CommServer()
{
}
Idle::Idle(CommServer & svr) : m_idleManager(svr)
{
}
Idle::~Idle()
{
}
CommSocket::CommSocket(CommServer & svr) : m_commServer(svr) { }
CommSocket::~CommSocket()
{
}
CommStreamClient::CommStreamClient(CommServer & svr) :
CommSocket(svr)
{
}
CommStreamClient::~CommStreamClient()
{
}
int CommStreamClient::getFd() const
{
return -1;
}
bool CommStreamClient::isOpen() const
{
return m_clientIos.is_open();
}
bool CommStreamClient::eof()
{
return (m_clientIos.fail() ||
m_clientIos.peek() == std::iostream::traits_type::eof());
}
CommClient::CommClient(CommServer & svr, const std::string &) :
CommStreamClient(svr), Idle(svr),
m_codec(NULL), m_encoder(NULL), m_connection(NULL),
m_connectTime(svr.time())
{
}
CommClient::~CommClient()
{
}
void CommClient::dispatch()
{
}
void CommClient::objectArrived(const Atlas::Objects::Root & obj)
{
}
void CommClient::idle(time_t t)
{
}
int CommClient::read()
{
return 0;
}
int CommClient::send(const Atlas::Objects::Operation::RootOperation & op)
{
return 0;
}
Player::Player(Connection * conn,
const std::string & username,
const std::string & passwd,
const std::string & id,
long intId) :
Account(conn, username, passwd, id, intId)
{
}
Player::~Player() { }
const char * Player::getType() const
{
return "player";
}
void Player::addToMessage(MapType & omap) const
{
}
void Player::addToEntity(const Atlas::Objects::Entity::RootEntity & ent) const
{
}
int Player::characterError(const Operation & op,
const Root & ent, OpVector & res) const
{
return 0;
}
Account::Account(Connection * conn,
const std::string & uname,
const std::string & passwd,
const std::string & id,
long intId) :
ConnectedRouter(id, intId, conn),
m_username(uname), m_password(passwd)
{
}
Account::~Account()
{
}
const char * Account::getType() const
{
return "testaccount";
}
void Account::store() const
{
}
void Account::addToMessage(Atlas::Message::MapType &) const
{
}
void Account::addToEntity(const Atlas::Objects::Entity::RootEntity &) const
{
}
void Account::createObject(const std::string & type_str,
const Root & arg,
const Operation & op,
OpVector & res)
{
}
void Account::operation(const Operation &, OpVector &)
{
}
void Account::LogoutOperation(const Operation &, OpVector &)
{
}
void Account::CreateOperation(const Operation &, OpVector &)
{
}
void Account::SetOperation(const Operation &, OpVector &)
{
}
void Account::ImaginaryOperation(const Operation &, OpVector &)
{
}
void Account::TalkOperation(const Operation &, OpVector &)
{
}
void Account::LookOperation(const Operation &, OpVector &)
{
}
void Account::GetOperation(const Operation &, OpVector &)
{
}
void Account::OtherOperation(const Operation &, OpVector &)
{
}
ConnectedRouter::ConnectedRouter(const std::string & id,
long iid,
Connection *c) :
Router(id, iid),
m_connection(c)
{
}
ConnectedRouter::~ConnectedRouter()
{
}
ServerRouting::ServerRouting(BaseWorld & wrld,
const std::string & ruleset,
const std::string & name,
const std::string & id, long intId,
const std::string & lId, long lIntId) :
Router(id, intId),
m_svrRuleset(ruleset), m_svrName(name),
m_numClients(0), m_world(wrld), m_lobby(*(Lobby*)0)
{
}
ServerRouting::~ServerRouting()
{
}
void ServerRouting::addToMessage(Atlas::Message::MapType & omap) const
{
}
void ServerRouting::addToEntity(const Atlas::Objects::Entity::RootEntity & ent) const
{
}
Account * ServerRouting::getAccountByName(const std::string & username)
{
return 0;
}
void ServerRouting::addAccount(Account * a)
{
}
Lobby::Lobby(ServerRouting & s, const std::string & id, long intId) :
Router(id, intId),
m_server(s)
{
}
Lobby::~Lobby()
{
}
void Lobby::delAccount(Account * ac)
{
}
void Lobby::addToMessage(MapType & omap) const
{
}
void Lobby::addToEntity(const Atlas::Objects::Entity::RootEntity & ent) const
{
}
void Lobby::addAccount(Account * ac)
{
}
void Lobby::operation(const Operation & op, OpVector & res)
{
}
ExternalMind::ExternalMind(Entity & e) : Router(e.getId(), e.getIntId()),
m_connection(0), m_entity(e)
{
}
ExternalMind::~ExternalMind()
{
}
void ExternalMind::operation(const Operation & op, OpVector & res)
{
}
ExternalProperty::ExternalProperty(Router * & data) : m_data(data)
{
}
int ExternalProperty::get(Atlas::Message::Element & val) const
{
return 0;
}
void ExternalProperty::set(const Atlas::Message::Element & val)
{
}
void ExternalProperty::add(const std::string & s,
Atlas::Message::MapType & map) const
{
}
void ExternalProperty::add(const std::string & s,
const Atlas::Objects::Entity::RootEntity & ent) const
{
}
const std::string & ExternalMind::connectionId()
{
assert(m_connection != 0);
return m_connection->getId();
}
void ExternalMind::connect(Connection * c)
{
m_connection = c;
}
Character::Character(const std::string & id, long intId) :
Character_parent(id, intId),
m_movement(*(Movement*)0),
m_mind(0), m_externalMind(0)
{
}
Character::~Character()
{
}
void Character::operation(const Operation & op, OpVector &)
{
}
void Character::externalOperation(const Operation & op)
{
}
void Character::ImaginaryOperation(const Operation & op, OpVector &)
{
}
void Character::TickOperation(const Operation & op, OpVector &)
{
}
void Character::TalkOperation(const Operation & op, OpVector &)
{
}
void Character::NourishOperation(const Operation & op, OpVector &)
{
}
void Character::UseOperation(const Operation & op, OpVector &)
{
}
void Character::WieldOperation(const Operation & op, OpVector &)
{
}
void Character::AttackOperation(const Operation & op, OpVector &)
{
}
void Character::ActuateOperation(const Operation & op, OpVector &)
{
}
void Character::mindActuateOperation(const Operation &, OpVector &)
{
}
void Character::mindAttackOperation(const Operation &, OpVector &)
{
}
void Character::mindCombineOperation(const Operation &, OpVector &)
{
}
void Character::mindCreateOperation(const Operation &, OpVector &)
{
}
void Character::mindDeleteOperation(const Operation &, OpVector &)
{
}
void Character::mindDivideOperation(const Operation &, OpVector &)
{
}
void Character::mindEatOperation(const Operation &, OpVector &)
{
}
void Character::mindGoalInfoOperation(const Operation &, OpVector &)
{
}
void Character::mindImaginaryOperation(const Operation &, OpVector &)
{
}
void Character::mindLookOperation(const Operation &, OpVector &)
{
}
void Character::mindMoveOperation(const Operation &, OpVector &)
{
}
void Character::mindSetOperation(const Operation &, OpVector &)
{
}
void Character::mindSetupOperation(const Operation &, OpVector &)
{
}
void Character::mindTalkOperation(const Operation &, OpVector &)
{
}
void Character::mindThoughtOperation(const Operation &, OpVector &)
{
}
void Character::mindTickOperation(const Operation &, OpVector &)
{
}
void Character::mindTouchOperation(const Operation &, OpVector &)
{
}
void Character::mindUpdateOperation(const Operation &, OpVector &)
{
}
void Character::mindUseOperation(const Operation &, OpVector &)
{
}
void Character::mindWieldOperation(const Operation &, OpVector &)
{
}
void Character::mindOtherOperation(const Operation &, OpVector &)
{
}
Thing::Thing(const std::string & id, long intId) :
Thing_parent(id, intId)
{
}
Thing::~Thing()
{
}
void Thing::DeleteOperation(const Operation & op, OpVector & res)
{
}
void Thing::MoveOperation(const Operation & op, OpVector & res)
{
}
void Thing::SetOperation(const Operation & op, OpVector & res)
{
}
void Thing::LookOperation(const Operation & op, OpVector & res)
{
}
void Thing::CreateOperation(const Operation & op, OpVector & res)
{
}
void Thing::UpdateOperation(const Operation & op, OpVector & res)
{
}
Entity::Entity(const std::string & id, long intId) :
LocatedEntity(id, intId), m_motion(0), m_flags(0)
{
}
Entity::~Entity()
{
}
void Entity::destroy()
{
destroyed.emit();
}
void Entity::ActuateOperation(const Operation &, OpVector &)
{
}
void Entity::AppearanceOperation(const Operation &, OpVector &)
{
}
void Entity::AttackOperation(const Operation &, OpVector &)
{
}
void Entity::CombineOperation(const Operation &, OpVector &)
{
}
void Entity::CreateOperation(const Operation &, OpVector &)
{
}
void Entity::DeleteOperation(const Operation &, OpVector &)
{
}
void Entity::DisappearanceOperation(const Operation &, OpVector &)
{
}
void Entity::DivideOperation(const Operation &, OpVector &)
{
}
void Entity::EatOperation(const Operation &, OpVector &)
{
}
void Entity::ImaginaryOperation(const Operation &, OpVector &)
{
}
void Entity::LookOperation(const Operation &, OpVector &)
{
}
void Entity::MoveOperation(const Operation &, OpVector &)
{
}
void Entity::NourishOperation(const Operation &, OpVector &)
{
}
void Entity::SetOperation(const Operation &, OpVector &)
{
}
void Entity::SightOperation(const Operation &, OpVector &)
{
}
void Entity::SoundOperation(const Operation &, OpVector &)
{
}
void Entity::TalkOperation(const Operation &, OpVector &)
{
}
void Entity::TickOperation(const Operation &, OpVector &)
{
}
void Entity::TouchOperation(const Operation &, OpVector &)
{
}
void Entity::UpdateOperation(const Operation &, OpVector &)
{
}
void Entity::UseOperation(const Operation &, OpVector &)
{
}
void Entity::WieldOperation(const Operation &, OpVector &)
{
}
void Entity::externalOperation(const Operation & op)
{
}
void Entity::operation(const Operation & op, OpVector & res)
{
}
void Entity::addToMessage(Atlas::Message::MapType & omap) const
{
}
void Entity::addToEntity(const Atlas::Objects::Entity::RootEntity & ent) const
{
}
PropertyBase * Entity::setAttr(const std::string & name,
const Atlas::Message::Element & attr)
{
return 0;
}
const PropertyBase * Entity::getProperty(const std::string & name) const
{
return 0;
}
PropertyBase * Entity::setProperty(const std::string & name,
PropertyBase * prop)
{
return m_properties[name] = prop;
}
void Entity::onContainered()
{
}
void Entity::onUpdated()
{
}
LocatedEntity::LocatedEntity(const std::string & id, long intId) :
Router(id, intId),
m_refCount(0), m_seq(0),
m_script(0), m_type(0), m_contains(0)
{
}
LocatedEntity::~LocatedEntity()
{
}
bool LocatedEntity::hasAttr(const std::string & name) const
{
return false;
}
int LocatedEntity::getAttr(const std::string & name,
Atlas::Message::Element & attr) const
{
return -1;
}
int LocatedEntity::getAttrType(const std::string & name,
Atlas::Message::Element & attr,
int type) const
{
return -1;
}
PropertyBase * LocatedEntity::setAttr(const std::string & name,
const Atlas::Message::Element & attr)
{
return 0;
}
const PropertyBase * LocatedEntity::getProperty(const std::string & name) const
{
return 0;
}
void LocatedEntity::onContainered()
{
}
void LocatedEntity::onUpdated()
{
}
Router::Router(const std::string & id, long intId) : m_id(id),
m_intId(intId)
{
}
Router::~Router()
{
}
void Router::addToMessage(Atlas::Message::MapType & omap) const
{
}
void Router::addToEntity(const Atlas::Objects::Entity::RootEntity & ent) const
{
}
void Router::error(const Operation & op,
const std::string & errstring,
OpVector & res,
const std::string & to) const
{
}
void Router::clientError(const Operation & op,
const std::string & errstring,
OpVector & res,
const std::string & to) const
{
}
Location::Location() : m_loc(0)
{
}
PropertyBase::PropertyBase(unsigned int flags) : m_flags(flags)
{
}
PropertyBase::~PropertyBase()
{
}
void PropertyBase::install(Entity *)
{
}
void PropertyBase::apply(Entity *)
{
}
void PropertyBase::add(const std::string & s,
Atlas::Message::MapType & ent) const
{
}
void PropertyBase::add(const std::string & s,
const Atlas::Objects::Entity::RootEntity & ent) const
{
}
BaseWorld * BaseWorld::m_instance = 0;
BaseWorld::BaseWorld(Entity & gw) : m_gameWorld(gw)
{
}
BaseWorld::~BaseWorld()
{
}
Entity * BaseWorld::getEntity(const std::string & id) const
{
return 0;
}
Entity * BaseWorld::getEntity(long id) const
{
return 0;
}
Inheritance * Inheritance::m_instance = NULL;
Inheritance::Inheritance()
{
}
Inheritance & Inheritance::instance()
{
if (m_instance == NULL) {
m_instance = new Inheritance();
}
return *m_instance;
}
const TypeNode * Inheritance::getType(const std::string & parent)
{
TypeNodeDict::const_iterator I = atlasObjects.find(parent);
if (I == atlasObjects.end()) {
return 0;
}
return I->second;
}
const Root & Inheritance::getClass(const std::string & parent)
{
return noClass;
}
void encrypt_password(const std::string & pwd, std::string & hash)
{
}
void log(LogLevel lvl, const std::string & msg)
{
}
void logEvent(LogEvent lev, const std::string & msg)
{
}
long integerId(const std::string & id)
{
long intId = strtol(id.c_str(), 0, 10);
if (intId == 0 && id != "0") {
intId = -1L;
}
return intId;
}
static long idGenerator = 0;
long newId(std::string & id)
{
static char buf[32];
long new_id = ++idGenerator;
sprintf(buf, "%ld", new_id);
id = buf;
assert(!id.empty());
return new_id;
}
void addToEntity(const Vector3D & v, std::vector<double> & vd)
{
vd.resize(3);
vd[0] = v[0];
vd[1] = v[1];
vd[2] = v[2];
}
int check_password(const std::string & pwd, const std::string & hash)
{
return 0;
}