cyphesis/tests/ConnectionCreatorIntegration.cpp
2018-04-25 21:05:59 +02:00

1041 lines
24 KiB
C++

// Cyphesis Online RPG Server and AI Engine
// Copyright (C) 2012 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
#ifdef NDEBUG
#undef NDEBUG
#endif
#ifndef DEBUG
#define DEBUG
#endif
#include "TestBase.h"
#include "TestWorld.h"
#include "server/Connection.h"
#include "server/ServerRouting.h"
#include "rulesets/Creator.h"
#include "rulesets/ExternalMind.h"
#include "common/const.h"
#include "common/log.h"
#include "common/TypeNode.h"
#include <Atlas/Objects/Operation.h>
#include <cstdio>
#include <cstring>
#include <cassert>
using Atlas::Objects::Operation::RootOperation;
using String::compose;
class ConnectionCreatorintegration : public Cyphesis::TestBase
{
protected:
long m_id_counter;
static LogEvent m_logEvent_logged;
static Operation m_Link_send_sent;
static Operation m_BaseWorld_message_called;
static LocatedEntity * m_BaseWorld_message_called_from;
static Operation m_Entity_callOperation_called;
ServerRouting * m_server;
Connection * m_connection;
Creator * m_creator;
TypeNode * m_creatorType;
public:
ConnectionCreatorintegration();
void setup();
void teardown();
void test_external_op();
void test_external_op_override();
void test_external_op_puppet();
void test_external_op_puppet_nonexistant();
static void logEvent_logged(LogEvent le);
static void Link_send_sent(const Operation & op);
static void BaseWorld_message_called(const Operation & op, LocatedEntity &);
static void Entity_callOperation_called(const Operation & op);
};
LogEvent ConnectionCreatorintegration::m_logEvent_logged = NONE;
Operation ConnectionCreatorintegration::m_Link_send_sent(0);
Operation ConnectionCreatorintegration::m_BaseWorld_message_called(0);
LocatedEntity * ConnectionCreatorintegration::m_BaseWorld_message_called_from(0);
Operation ConnectionCreatorintegration::m_Entity_callOperation_called(0);
void ConnectionCreatorintegration::logEvent_logged(LogEvent le)
{
m_logEvent_logged = le;
}
void ConnectionCreatorintegration::Link_send_sent(const Operation & op)
{
m_Link_send_sent = op;
}
void ConnectionCreatorintegration::BaseWorld_message_called(
const Operation & op,
LocatedEntity & ent)
{
m_BaseWorld_message_called = op;
m_BaseWorld_message_called_from = &ent;
}
void ConnectionCreatorintegration::Entity_callOperation_called(
const Operation & op)
{
m_Entity_callOperation_called = op;
}
ConnectionCreatorintegration::ConnectionCreatorintegration() :
m_id_counter(0L),
m_connection(0),
m_creator(0),
m_creatorType(0)
{
ADD_TEST(ConnectionCreatorintegration::test_external_op);
ADD_TEST(ConnectionCreatorintegration::test_external_op_override);
ADD_TEST(ConnectionCreatorintegration::test_external_op_puppet);
ADD_TEST(ConnectionCreatorintegration::test_external_op_puppet_nonexistant);
}
void ConnectionCreatorintegration::setup()
{
m_Link_send_sent = 0;
m_BaseWorld_message_called = 0;
m_BaseWorld_message_called_from = 0;
m_Entity_callOperation_called = 0;
Entity * gw = new Entity(compose("%1", m_id_counter),
m_id_counter++);
m_server = new ServerRouting(*new TestWorld(*gw),
"dd7452be-0137-4664-b90e-77dfb395ac39",
"a2feda8e-62e9-4ba0-95c4-09f92eda6a78",
compose("%1", m_id_counter), m_id_counter++,
compose("%1", m_id_counter), m_id_counter++);
m_connection = new Connection(*(CommSocket*)0,
*m_server,
"25251955-7e8c-4043-8a5e-adfb8a1e76f7",
compose("%1", m_id_counter), m_id_counter++);
m_creator = new Creator(compose("%1", m_id_counter), m_id_counter++);
m_creatorType = new TypeNode("test_avatar");
m_creator->setType(m_creatorType);
m_connection->addObject(m_creator);
m_BaseWorld_message_called = 0;
m_BaseWorld_message_called_from = 0;
}
void ConnectionCreatorintegration::teardown()
{
delete m_connection;
delete m_creator;
delete m_creatorType;
delete m_server;
}
void ConnectionCreatorintegration::test_external_op()
{
// Dispatching a Talk external op from the creator should result in
// it being passed on to the world, exactly as if this was a Character
// except that we assume that Creator was set up linked.
m_creator->m_externalMind = new ExternalMind(*m_creator);
m_creator->m_externalMind->linkUp(m_connection);
Atlas::Objects::Operation::Talk op;
op->setFrom(m_creator->getId());
m_connection->externalOperation(op, *m_connection);
// BaseWorld::message should have been called from Enitty::sendWorld
// with the Talk operation, modified to have TO set to the character.
ASSERT_TRUE(m_BaseWorld_message_called.isValid());
ASSERT_EQUAL(m_BaseWorld_message_called->getClassNo(),
Atlas::Objects::Operation::TALK_NO);
ASSERT_TRUE(!m_BaseWorld_message_called->isDefaultTo());
ASSERT_EQUAL(m_BaseWorld_message_called->getTo(), m_creator->getId());
ASSERT_NOT_NULL(m_BaseWorld_message_called_from);
ASSERT_EQUAL(m_BaseWorld_message_called_from, m_creator);
}
void ConnectionCreatorintegration::test_external_op_override()
{
// Dispatching a Talk external op from the creator should result in
// it being passed on to the world, exactly as if this was a Character
// except that we assume that Creator was set up linked.
m_creator->m_externalMind = new ExternalMind(*m_creator);
m_creator->m_externalMind->linkUp(m_connection);
Atlas::Objects::Operation::Talk op;
op->setFrom(m_creator->getId());
op->setTo(m_creator->getId());
m_connection->externalOperation(op, *m_connection);
// The operation should have been passed to Entity::callOperation for
// dispatch, completely unfiltered.
ASSERT_TRUE(m_Entity_callOperation_called.isValid());
ASSERT_EQUAL(m_Entity_callOperation_called->getClassNo(),
Atlas::Objects::Operation::TALK_NO);
ASSERT_TRUE(!m_Entity_callOperation_called->isDefaultTo());
ASSERT_EQUAL(m_Entity_callOperation_called->getTo(), m_creator->getId());
}
void ConnectionCreatorintegration::test_external_op_puppet()
{
// Dispatching a Talk external op from the creator, to the creator should
// result in it being passed directly to the normal op dispatch,
// shortcutting the world.
m_creator->m_externalMind = new ExternalMind(*m_creator);
m_creator->m_externalMind->linkUp(m_connection);
Entity * other = new Entity(compose("%1", m_id_counter), m_id_counter++);
other->setType(m_creatorType);
m_server->m_world.addEntity(other);
Atlas::Objects::Operation::Talk op;
op->setFrom(m_creator->getId());
op->setTo(other->getId());
m_connection->externalOperation(op, *m_connection);
// Operation should be via world dispatch, as if it was from the Entity
// we are puppeting.
ASSERT_TRUE(m_BaseWorld_message_called.isValid());
ASSERT_EQUAL(m_BaseWorld_message_called->getClassNo(),
Atlas::Objects::Operation::TALK_NO);
ASSERT_TRUE(!m_BaseWorld_message_called->isDefaultTo());
ASSERT_EQUAL(m_BaseWorld_message_called->getTo(), other->getId());
ASSERT_NOT_NULL(m_BaseWorld_message_called_from);
ASSERT_EQUAL(m_BaseWorld_message_called_from, other);
}
void ConnectionCreatorintegration::test_external_op_puppet_nonexistant()
{
// Dispatching a Talk external op from the creator, to the creator should
// result in it being passed directly to the normal op dispatch,
// shortcutting the world.
m_creator->m_externalMind = new ExternalMind(*m_creator);
m_creator->m_externalMind->linkUp(m_connection);
Entity * other = new Entity(compose("%1", m_id_counter), m_id_counter++);
other->setType(m_creatorType);
m_server->m_world.addEntity(other);
Atlas::Objects::Operation::Talk op;
op->setFrom(m_creator->getId());
op->setTo(compose("%1", m_id_counter++));
m_connection->externalOperation(op, *m_connection);
// Operation should be via world dispatch, as if it was from the Entity
// we are puppeting.
ASSERT_TRUE(m_Link_send_sent.isValid());
ASSERT_EQUAL(m_Link_send_sent->getParent(),
"unseen");
ASSERT_TRUE(!m_Link_send_sent->isDefaultTo());
ASSERT_EQUAL(m_Link_send_sent->getTo(), m_creator->getId());
}
void TestWorld::message(const Operation & op, LocatedEntity & ent)
{
ConnectionCreatorintegration::BaseWorld_message_called(op, ent);
}
LocatedEntity * TestWorld::addNewEntity(const std::string &,
const Atlas::Objects::Entity::RootEntity &)
{
return 0;
}
int main()
{
ConnectionCreatorintegration t;
return t.run();
}
// stubs
#include "server/Lobby.h"
#include "server/Player.h"
#include "rulesets/AtlasProperties.h"
#include "rulesets/BBoxProperty.h"
#include "rulesets/Domain.h"
#include "rulesets/EntityProperty.h"
#include "rulesets/ExternalProperty.h"
#include "rulesets/OutfitProperty.h"
#include "rulesets/Script.h"
#include "rulesets/StatusProperty.h"
#include "rulesets/Task.h"
#include "rulesets/TasksProperty.h"
#include "common/CommSocket.h"
#include "common/id.h"
#include "common/Inheritance.h"
#include "common/Property_impl.h"
#include "common/PropertyManager.h"
using Atlas::Message::Element;
using Atlas::Message::MapType;
using Atlas::Objects::Root;
using Atlas::Objects::Entity::RootEntity;
bool restricted_flag;
#include "stubs/rulesets/stubProxyMind.h"
#include "stubs/rulesets/stubBaseMind.h"
#include "stubs/rulesets/stubMemEntity.h"
#include "stubs/rulesets/stubMemMap.h"
#include "stubs/rulesets/stubPedestrian.h"
#include "stubs/rulesets/stubMovement.h"
#include "stubs/server/stubExternalMindsManager.h"
#include "stubs/server/stubExternalMindsConnection.h"
#include "stubs/common/stubOperationsDispatcher.h"
#include "stubs/modules/stubDateTime.h"
#include "stubs/modules/stubWorldTime.h"
#include "stubs/modules/stubLocation.h"
#include "stubs/physics/stubVector3D.h"
namespace Atlas { namespace Objects { namespace Operation {
int ACTUATE_NO = -1;
int ATTACK_NO = -1;
int EAT_NO = -1;
int GOAL_INFO_NO = -1;
int NOURISH_NO = -1;
int SETUP_NO = -1;
int TICK_NO = -1;
int THOUGHT_NO = -1;
int UNSEEN_NO = -1;
int UPDATE_NO = -1;
int RELAY_NO = -1;
int THINK_NO = -1;
int COMMUNE_NO = -1;
} } }
CommSocket::CommSocket(boost::asio::io_service & svr) : m_io_service(svr) { }
CommSocket::~CommSocket()
{
}
int CommSocket::flush()
{
return 0;
}
#include "stubs/server/stubPlayer.h"
Account::Account(Connection * conn,
const std::string & uname,
const std::string & passwd,
const std::string & id,
long intId) :
ConnectableRouter(id, intId, conn),
m_username(uname), m_password(passwd)
{
}
const char * Account::getType() const
{
return "testaccount";
}
void Account::store() const
{
}
bool Account::isPersisted() const {
return true;
}
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)
{
}
LocatedEntity * Account::createCharacterEntity(const std::string &,
const Atlas::Objects::Entity::RootEntity &,
const Atlas::Objects::Root &)
{
return 0;
}
void Account::externalOperation(const Operation &, Link &)
{
}
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 &)
{
}
ConnectableRouter::ConnectableRouter(const std::string & id,
long iid,
Connection *c) :
Router(id, iid),
m_connection(c)
{
}
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
{
}
void ServerRouting::externalOperation(const Operation &, Link &)
{
}
void ServerRouting::operation(const Operation &, OpVector &)
{
}
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::externalOperation(const Operation &, Link &)
{
}
void Lobby::operation(const Operation & op, OpVector & res)
{
}
ExternalProperty::ExternalProperty(ExternalMind * & 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
{
}
ExternalProperty * ExternalProperty::copy() const
{
return 0;
}
#include "stubs/rulesets/stubThing.h"
#define STUB_Entity_destroy
void Entity::destroy()
{
destroyed.emit();
}
#define STUB_Entity_callOperation
void Entity::callOperation(const Operation & op, OpVector & res)
{
ConnectionCreatorintegration::Entity_callOperation_called(op);
}
#define STUB_Entity_setProperty
PropertyBase * Entity::setProperty(const std::string & name,
PropertyBase * prop)
{
return m_properties[name] = prop;
}
#define STUB_Entity_sendWorld
void Entity::sendWorld(const Operation & op)
{
BaseWorld::instance().message(op, *this);
}
#define STUB_Entity_setType
void Entity::setType(const TypeNode* t) {
m_type = t;
}
#include "stubs/rulesets/stubEntity.h"
#include "stubs/rulesets/stubLocatedEntity.h"
#include "stubs/rulesets/stubOutfitProperty.h"
#include "stubs/common/stubVariable.h"
#include "stubs/common/stubMonitors.h"
#include "stubs/rulesets/stubEntityProperty.h"
Task::~Task()
{
}
void Task::initTask(const Operation & op, OpVector & res)
{
}
void Task::operation(const Operation & op, OpVector & res)
{
}
void Task::irrelevant()
{
}
TasksProperty::TasksProperty() : PropertyBase(per_ephem), m_task(0)
{
}
int TasksProperty::get(Atlas::Message::Element & val) const
{
return 0;
}
void TasksProperty::set(const Atlas::Message::Element & val)
{
}
TasksProperty * TasksProperty::copy() const
{
return 0;
}
int TasksProperty::startTask(Task *, LocatedEntity *, const Operation &, OpVector &)
{
return 0;
}
int TasksProperty::updateTask(LocatedEntity *, OpVector &)
{
return 0;
}
int TasksProperty::clearTask(LocatedEntity *, OpVector &)
{
return 0;
}
void TasksProperty::stopTask(LocatedEntity *, OpVector &)
{
}
void TasksProperty::TickOperation(LocatedEntity *, const Operation &, OpVector &)
{
}
void TasksProperty::UseOperation(LocatedEntity *, const Operation &, OpVector &)
{
}
HandlerResult TasksProperty::operation(LocatedEntity *, const Operation &, OpVector &)
{
return OPERATION_IGNORED;
}
PropertyBase::PropertyBase(unsigned int flags) : m_flags(flags)
{
}
void PropertyBase::install(LocatedEntity *, const std::string & name)
{
}
void PropertyBase::install(TypeNode *, const std::string & name)
{
}
void PropertyBase::remove(LocatedEntity *, const std::string & name)
{
}
void PropertyBase::apply(LocatedEntity *)
{
}
void PropertyBase::add(const std::string & s,
Atlas::Message::MapType & ent) const
{
get(ent[s]);
}
void PropertyBase::add(const std::string & s,
const Atlas::Objects::Entity::RootEntity & ent) const
{
}
HandlerResult PropertyBase::operation(LocatedEntity *,
const Operation &,
OpVector &)
{
return OPERATION_IGNORED;
}
template<>
void Property<int>::set(const Atlas::Message::Element & e)
{
if (e.isInt()) {
this->m_data = e.asInt();
}
}
template<>
void Property<double>::set(const Atlas::Message::Element & e)
{
if (e.isNum()) {
this->m_data = e.asNum();
}
}
template<>
void Property<std::string>::set(const Atlas::Message::Element & e)
{
if (e.isString()) {
this->m_data = e.String();
}
}
template class Property<int>;
template class Property<double>;
template class Property<std::string>;
SoftProperty::SoftProperty(const Atlas::Message::Element & data) :
PropertyBase(0), m_data(data)
{
}
int SoftProperty::get(Atlas::Message::Element & val) const
{
val = m_data;
return 0;
}
void SoftProperty::set(const Atlas::Message::Element & val)
{
}
SoftProperty * SoftProperty::copy() const
{
return 0;
}
ContainsProperty::ContainsProperty(LocatedEntitySet & data) :
PropertyBase(per_ephem), m_data(data)
{
}
int ContainsProperty::get(Atlas::Message::Element & e) const
{
return 0;
}
void ContainsProperty::set(const Atlas::Message::Element & e)
{
}
void ContainsProperty::add(const std::string & s,
const Atlas::Objects::Entity::RootEntity & ent) const
{
}
ContainsProperty * ContainsProperty::copy() const
{
return 0;
}
StatusProperty * StatusProperty::copy() const
{
return 0;
}
void StatusProperty::apply(LocatedEntity * owner)
{
}
BBoxProperty::BBoxProperty()
{
}
void BBoxProperty::apply(LocatedEntity * ent)
{
}
int BBoxProperty::get(Element & val) const
{
return -1;
}
void BBoxProperty::set(const Element & val)
{
}
void BBoxProperty::add(const std::string & key,
MapType & map) const
{
}
void BBoxProperty::add(const std::string & key,
const RootEntity & ent) const
{
}
BBoxProperty * BBoxProperty::copy() const
{
return 0;
}
#include "stubs/common/stubPropertyManager.h"
Link::Link(CommSocket & socket, const std::string & id, long iid) :
Router(id, iid), m_encoder(0), m_commSocket(socket)
{
}
Link::~Link()
{
}
void Link::send(const OpVector& opVector) const
{
}
void Link::send(const Operation & op) const
{
ConnectionCreatorintegration::Link_send_sent(op);
}
void Link::sendError(const Operation & op,
const std::string &,
const std::string &) const
{
}
void Link::disconnect()
{
}
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
{
}
TypeNode::TypeNode(const std::string & name) : m_name(name), m_parent(0)
{
}
TypeNode::~TypeNode()
{
}
BaseWorld * BaseWorld::m_instance = 0;
BaseWorld::BaseWorld(LocatedEntity & gw) : m_gameWorld(gw)
{
m_instance = this;
}
BaseWorld::~BaseWorld()
{
m_instance = 0;
delete &m_gameWorld;
}
double BaseWorld::getTime() const
{
return .0;
}
LocatedEntity * BaseWorld::getEntity(const std::string & id) const
{
long intId = integerId(id);
EntityDict::const_iterator I = m_eobjects.find(intId);
if (I != m_eobjects.end()) {
assert(I->second != 0);
return I->second;
} else {
return 0;
}
}
LocatedEntity * BaseWorld::getEntity(long id) const
{
EntityDict::const_iterator I = m_eobjects.find(id);
if (I != m_eobjects.end()) {
assert(I->second != 0);
return I->second;
} else {
return 0;
}
}
LocatedEntity& BaseWorld::getDefaultLocation() {
return m_gameWorld;
}
LocatedEntity& BaseWorld::getDefaultLocation() const {
return m_gameWorld;
}
LocatedEntity& BaseWorld::getRootEntity() {
return m_gameWorld;
}
LocatedEntity& BaseWorld::getRootEntity() const {
return m_gameWorld;
}
#ifndef STUB_Inheritance_getClass
#define STUB_Inheritance_getClass
const Atlas::Objects::Root& Inheritance::getClass(const std::string & parent)
{
return noClass;
}
#endif //STUB_Inheritance_getClass
#ifndef STUB_Inheritance_getType
#define STUB_Inheritance_getType
const TypeNode* Inheritance::getType(const std::string & parent)
{
TypeNodeDict::const_iterator I = atlasObjects.find(parent);
if (I == atlasObjects.end()) {
return 0;
}
return I->second;}
#endif //STUB_Inheritance_getType
#include "stubs/common/stubInheritance.h"
EntityRef::EntityRef(LocatedEntity* e) : m_inner(e)
{
}
EntityRef::EntityRef(const EntityRef& ref) : m_inner(ref.m_inner)
{
}
EntityRef& EntityRef::operator=(const EntityRef& ref)
{
m_inner = ref.m_inner;
return *this;
}
void EntityRef::onEntityDeleted()
{
}
template<class V>
const Quaternion quaternionFromTo(const V & from, const V & to)
{
return Quaternion(1.f, 0.f, 0.f, 0.f);
}
template
const Quaternion quaternionFromTo<Vector3D>(const Vector3D &, const Vector3D&);
void log(LogLevel lvl, const std::string & msg)
{
}
void logEvent(LogEvent lev, const std::string & msg)
{
ConnectionCreatorintegration::logEvent_logged(lev);
}
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;
}
Shaker::Shaker()
{
}
std::string Shaker::generateSalt(size_t length)
{
return "";
}
void hash_password(const std::string & pwd, const std::string & salt,
std::string & hash )
{
}
int check_password(const std::string & pwd, const std::string & hash)
{
return 0;
}