2012-12-03 23:45:56 +00:00
|
|
|
// 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"
|
|
|
|
|
|
2018-11-03 16:43:33 +01:00
|
|
|
#include "rules/simulation/ExternalMind.h"
|
2012-12-03 23:45:56 +00:00
|
|
|
|
|
|
|
|
#include "common/const.h"
|
|
|
|
|
#include "common/log.h"
|
|
|
|
|
#include "common/TypeNode.h"
|
|
|
|
|
|
|
|
|
|
#include <Atlas/Objects/Operation.h>
|
|
|
|
|
|
|
|
|
|
#include <cstdio>
|
|
|
|
|
#include <cstring>
|
|
|
|
|
|
|
|
|
|
#include <cassert>
|
2018-10-31 21:38:35 +01:00
|
|
|
#include <rules/simulation/Entity.h>
|
2018-08-19 17:15:30 +02:00
|
|
|
#include <common/TypeNode.h>
|
2018-11-03 16:43:33 +01:00
|
|
|
#include <rules/simulation/AdminMind.h>
|
|
|
|
|
#include <rules/simulation/MindsProperty.h>
|
2018-10-27 13:13:45 +02:00
|
|
|
#include <common/Monitors.h>
|
2018-10-30 20:12:45 +01:00
|
|
|
#include <common/operations/Think.h>
|
2018-10-27 13:13:45 +02:00
|
|
|
#include <common/CommSocket.h>
|
|
|
|
|
|
|
|
|
|
#include <Atlas/Objects/Decoder.h>
|
|
|
|
|
#include <Atlas/Objects/Encoder.h>
|
2018-08-06 21:07:31 +02:00
|
|
|
|
2012-12-03 23:45:56 +00:00
|
|
|
|
|
|
|
|
using Atlas::Objects::Operation::RootOperation;
|
|
|
|
|
using String::compose;
|
|
|
|
|
|
2018-10-27 13:13:45 +02:00
|
|
|
class StubSocket : public CommSocket
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
explicit StubSocket(boost::asio::io_service& io_service) : CommSocket(io_service)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void disconnect() override
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int flush() override
|
|
|
|
|
{
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
struct TestDecoder : public Atlas::Objects::ObjectsDecoder {
|
|
|
|
|
Atlas::Objects::Root m_obj;
|
|
|
|
|
|
|
|
|
|
void objectArrived(const Atlas::Objects::Root & obj) override
|
|
|
|
|
{
|
|
|
|
|
m_obj = obj;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2012-12-03 23:45:56 +00:00
|
|
|
class ConnectionCreatorintegration : public Cyphesis::TestBase
|
|
|
|
|
{
|
|
|
|
|
protected:
|
|
|
|
|
long m_id_counter;
|
|
|
|
|
|
2018-10-27 13:13:45 +02:00
|
|
|
StubSocket* m_commSocket;
|
2012-12-03 23:45:56 +00:00
|
|
|
ServerRouting * m_server;
|
|
|
|
|
Connection * m_connection;
|
2018-10-27 13:13:45 +02:00
|
|
|
Ref<Entity> m_creator;
|
2012-12-03 23:45:56 +00:00
|
|
|
TypeNode * m_creatorType;
|
2018-07-25 00:16:09 +02:00
|
|
|
std::unique_ptr<TestWorld> m_world;
|
2012-12-03 23:45:56 +00:00
|
|
|
public:
|
2018-10-27 13:13:45 +02:00
|
|
|
|
|
|
|
|
static Operation m_BaseWorld_message_called;
|
|
|
|
|
static Ref<LocatedEntity> m_BaseWorld_message_called_from;
|
|
|
|
|
|
2012-12-03 23:45:56 +00:00
|
|
|
ConnectionCreatorintegration();
|
|
|
|
|
|
|
|
|
|
void setup();
|
|
|
|
|
|
|
|
|
|
void teardown();
|
|
|
|
|
|
|
|
|
|
void test_external_op();
|
2012-12-04 01:11:30 +00:00
|
|
|
void test_external_op_override();
|
|
|
|
|
void test_external_op_puppet();
|
2012-12-04 10:08:55 +00:00
|
|
|
void test_external_op_puppet_nonexistant();
|
2012-12-03 23:45:56 +00:00
|
|
|
|
2013-01-17 10:17:07 +00:00
|
|
|
static void BaseWorld_message_called(const Operation & op, LocatedEntity &);
|
2012-12-03 23:45:56 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
Operation ConnectionCreatorintegration::m_BaseWorld_message_called(0);
|
2018-08-05 14:31:04 +02:00
|
|
|
Ref<LocatedEntity> ConnectionCreatorintegration::m_BaseWorld_message_called_from(0);
|
2012-12-03 23:45:56 +00:00
|
|
|
|
|
|
|
|
void ConnectionCreatorintegration::BaseWorld_message_called(
|
|
|
|
|
const Operation & op,
|
2013-01-17 10:17:07 +00:00
|
|
|
LocatedEntity & ent)
|
2012-12-03 23:45:56 +00:00
|
|
|
{
|
|
|
|
|
m_BaseWorld_message_called = op;
|
|
|
|
|
m_BaseWorld_message_called_from = &ent;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ConnectionCreatorintegration::ConnectionCreatorintegration() :
|
|
|
|
|
m_id_counter(0L),
|
|
|
|
|
m_connection(0),
|
|
|
|
|
m_creator(0),
|
|
|
|
|
m_creatorType(0)
|
|
|
|
|
{
|
2018-10-27 13:13:45 +02:00
|
|
|
|
|
|
|
|
new Monitors();
|
|
|
|
|
|
2012-12-03 23:45:56 +00:00
|
|
|
ADD_TEST(ConnectionCreatorintegration::test_external_op);
|
2012-12-04 01:11:30 +00:00
|
|
|
ADD_TEST(ConnectionCreatorintegration::test_external_op_override);
|
|
|
|
|
ADD_TEST(ConnectionCreatorintegration::test_external_op_puppet);
|
2012-12-04 10:08:55 +00:00
|
|
|
ADD_TEST(ConnectionCreatorintegration::test_external_op_puppet_nonexistant);
|
2012-12-03 23:45:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ConnectionCreatorintegration::setup()
|
|
|
|
|
{
|
2018-10-27 13:13:45 +02:00
|
|
|
boost::asio::io_service io_service;
|
2012-12-04 10:08:55 +00:00
|
|
|
m_BaseWorld_message_called = 0;
|
|
|
|
|
m_BaseWorld_message_called_from = 0;
|
2012-12-03 23:45:56 +00:00
|
|
|
|
2018-07-25 00:16:09 +02:00
|
|
|
Ref<Entity> gw = new Entity(compose("%1", m_id_counter),
|
2012-12-03 23:45:56 +00:00
|
|
|
m_id_counter++);
|
2018-07-25 00:16:09 +02:00
|
|
|
m_world.reset();
|
2018-08-05 14:31:04 +02:00
|
|
|
m_world.reset(new TestWorld(gw));
|
2018-07-24 21:18:50 +02:00
|
|
|
TestWorld::extension.messageFn = [](const Operation & op, LocatedEntity & ent) {
|
|
|
|
|
ConnectionCreatorintegration::BaseWorld_message_called(op, ent);
|
|
|
|
|
};
|
2018-07-25 00:16:09 +02:00
|
|
|
m_server = new ServerRouting(*m_world,
|
2012-12-03 23:45:56 +00:00
|
|
|
"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++);
|
2018-10-27 13:13:45 +02:00
|
|
|
m_commSocket = new StubSocket(io_service);
|
|
|
|
|
m_connection = new Connection(*m_commSocket,
|
2012-12-03 23:45:56 +00:00
|
|
|
*m_server,
|
|
|
|
|
"25251955-7e8c-4043-8a5e-adfb8a1e76f7",
|
|
|
|
|
compose("%1", m_id_counter), m_id_counter++);
|
2018-10-27 13:13:45 +02:00
|
|
|
m_creator = new Entity(compose("%1", m_id_counter), m_id_counter++);
|
2012-12-03 23:45:56 +00:00
|
|
|
m_creatorType = new TypeNode("test_avatar");
|
|
|
|
|
m_creator->setType(m_creatorType);
|
2018-10-27 13:13:45 +02:00
|
|
|
BaseWorld::instance().addEntity(m_creator);
|
2012-12-03 23:45:56 +00:00
|
|
|
|
2018-08-05 20:47:09 +02:00
|
|
|
m_connection->addObject(m_creator.get());
|
2012-12-03 23:45:56 +00:00
|
|
|
|
|
|
|
|
m_BaseWorld_message_called = 0;
|
|
|
|
|
m_BaseWorld_message_called_from = 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ConnectionCreatorintegration::teardown()
|
|
|
|
|
{
|
|
|
|
|
delete m_connection;
|
2018-08-05 14:31:04 +02:00
|
|
|
m_creator = nullptr;
|
2012-12-03 23:45:56 +00:00
|
|
|
delete m_creatorType;
|
|
|
|
|
delete m_server;
|
2018-10-27 13:13:45 +02:00
|
|
|
delete m_commSocket;
|
2012-12-03 23:45:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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.
|
|
|
|
|
|
2018-10-27 13:13:45 +02:00
|
|
|
auto mind = new AdminMind("1", 1, *m_creator);
|
|
|
|
|
m_connection->addObject(mind);
|
|
|
|
|
mind->linkUp(m_connection);
|
|
|
|
|
m_creator->requirePropertyClassFixed<MindsProperty>()->addMind(mind);
|
2012-12-03 23:45:56 +00:00
|
|
|
|
|
|
|
|
Atlas::Objects::Operation::Talk op;
|
2018-10-27 13:13:45 +02:00
|
|
|
op->setFrom(mind->getId());
|
2012-12-03 23:45:56 +00:00
|
|
|
|
2012-12-04 20:39:35 +00:00
|
|
|
m_connection->externalOperation(op, *m_connection);
|
2012-12-03 23:45:56 +00:00
|
|
|
|
|
|
|
|
// 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());
|
2018-08-05 14:31:04 +02:00
|
|
|
ASSERT_TRUE(m_BaseWorld_message_called_from);
|
|
|
|
|
ASSERT_EQUAL(m_BaseWorld_message_called_from.get(), m_creator.get());
|
2012-12-03 23:45:56 +00:00
|
|
|
}
|
|
|
|
|
|
2012-12-04 01:11:30 +00:00
|
|
|
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.
|
|
|
|
|
|
2018-10-27 13:13:45 +02:00
|
|
|
auto mind = new AdminMind("1", 1, *m_creator);
|
|
|
|
|
m_connection->addObject(mind);
|
|
|
|
|
mind->linkUp(m_connection);
|
|
|
|
|
m_creator->requirePropertyClassFixed<MindsProperty>()->addMind(mind);
|
2012-12-04 01:11:30 +00:00
|
|
|
|
|
|
|
|
Atlas::Objects::Operation::Talk op;
|
2018-10-27 13:13:45 +02:00
|
|
|
op->setFrom(mind->getId());
|
2012-12-04 01:11:30 +00:00
|
|
|
op->setTo(m_creator->getId());
|
|
|
|
|
|
2012-12-04 20:39:35 +00:00
|
|
|
m_connection->externalOperation(op, *m_connection);
|
2012-12-04 01:11:30 +00:00
|
|
|
|
2012-12-04 02:16:39 +00:00
|
|
|
// The operation should have been passed to Entity::callOperation for
|
|
|
|
|
// dispatch, completely unfiltered.
|
2018-10-27 13:13:45 +02:00
|
|
|
ASSERT_TRUE(m_BaseWorld_message_called.isValid());
|
|
|
|
|
ASSERT_EQUAL(m_BaseWorld_message_called->getClassNo(),
|
2012-12-04 02:16:39 +00:00
|
|
|
Atlas::Objects::Operation::TALK_NO);
|
2018-10-27 13:13:45 +02:00
|
|
|
ASSERT_TRUE(!m_BaseWorld_message_called->isDefaultTo());
|
|
|
|
|
ASSERT_EQUAL(m_BaseWorld_message_called->getTo(), m_creator->getId());
|
2012-12-04 01:11:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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.
|
|
|
|
|
|
2018-10-27 13:13:45 +02:00
|
|
|
auto mind = new AdminMind("1", 1, *m_creator);
|
|
|
|
|
m_connection->addObject(mind);
|
|
|
|
|
mind->linkUp(m_connection);
|
|
|
|
|
m_creator->requirePropertyClassFixed<MindsProperty>()->addMind(mind);
|
2012-12-04 01:11:30 +00:00
|
|
|
|
2018-10-27 13:13:45 +02:00
|
|
|
Ref<Entity> other = new Entity(compose("%1", m_id_counter), m_id_counter++);
|
2012-12-04 02:16:39 +00:00
|
|
|
other->setType(m_creatorType);
|
|
|
|
|
m_server->m_world.addEntity(other);
|
|
|
|
|
|
2012-12-04 01:11:30 +00:00
|
|
|
Atlas::Objects::Operation::Talk op;
|
2018-10-27 13:13:45 +02:00
|
|
|
op->setFrom(mind->getId());
|
2012-12-04 02:16:39 +00:00
|
|
|
op->setTo(other->getId());
|
2012-12-04 01:11:30 +00:00
|
|
|
|
2012-12-04 20:39:35 +00:00
|
|
|
m_connection->externalOperation(op, *m_connection);
|
2012-12-04 01:11:30 +00:00
|
|
|
|
2012-12-04 02:16:39 +00:00
|
|
|
// 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());
|
2018-08-05 14:31:04 +02:00
|
|
|
ASSERT_TRUE(m_BaseWorld_message_called_from);
|
2018-10-27 13:13:45 +02:00
|
|
|
ASSERT_EQUAL(m_BaseWorld_message_called_from.get(), m_creator.get());
|
2012-12-04 01:11:30 +00:00
|
|
|
}
|
|
|
|
|
|
2012-12-04 10:08:55 +00:00
|
|
|
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.
|
|
|
|
|
|
2018-10-27 13:13:45 +02:00
|
|
|
auto mind = new AdminMind("1", 1, *m_creator);
|
|
|
|
|
m_connection->addObject(mind);
|
|
|
|
|
mind->linkUp(m_connection);
|
|
|
|
|
m_creator->requirePropertyClassFixed<MindsProperty>()->addMind(mind);
|
2012-12-04 10:08:55 +00:00
|
|
|
|
2018-08-05 14:31:04 +02:00
|
|
|
Ref<Entity> other = new Entity(compose("%1", m_id_counter), m_id_counter++);
|
2012-12-04 10:08:55 +00:00
|
|
|
other->setType(m_creatorType);
|
|
|
|
|
m_server->m_world.addEntity(other);
|
|
|
|
|
|
|
|
|
|
Atlas::Objects::Operation::Talk op;
|
2018-10-27 13:13:45 +02:00
|
|
|
op->setFrom(mind->getId());
|
2012-12-04 10:08:55 +00:00
|
|
|
op->setTo(compose("%1", m_id_counter++));
|
|
|
|
|
|
2018-10-27 13:13:45 +02:00
|
|
|
|
|
|
|
|
TestDecoder decoder{};
|
|
|
|
|
decoder.streamBegin();
|
|
|
|
|
Atlas::Objects::ObjectsEncoder encoder(decoder);
|
|
|
|
|
m_connection->setEncoder(&encoder);
|
2012-12-04 20:39:35 +00:00
|
|
|
m_connection->externalOperation(op, *m_connection);
|
2012-12-04 10:08:55 +00:00
|
|
|
|
2018-10-27 13:13:45 +02:00
|
|
|
// Should result in Unseen
|
|
|
|
|
ASSERT_TRUE(decoder.m_obj.isValid());
|
|
|
|
|
ASSERT_EQUAL(decoder.m_obj->getParent(),
|
2012-12-04 10:08:55 +00:00
|
|
|
"unseen");
|
2018-10-27 13:13:45 +02:00
|
|
|
ASSERT_TRUE(!Atlas::Objects::smart_dynamic_cast<Atlas::Objects::Operation::RootOperation>(decoder.m_obj)->isDefaultTo());
|
|
|
|
|
ASSERT_EQUAL(Atlas::Objects::smart_dynamic_cast<Atlas::Objects::Operation::RootOperation>(decoder.m_obj)->getTo(), mind->getId());
|
2012-12-04 10:08:55 +00:00
|
|
|
}
|
|
|
|
|
|
2012-12-03 23:45:56 +00:00
|
|
|
int main()
|
|
|
|
|
{
|
|
|
|
|
ConnectionCreatorintegration t;
|
|
|
|
|
|
|
|
|
|
return t.run();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// stubs
|
|
|
|
|
|
|
|
|
|
#include "server/Lobby.h"
|
|
|
|
|
#include "server/Player.h"
|
|
|
|
|
|
2018-10-31 21:38:35 +01:00
|
|
|
#include "rules/AtlasProperties.h"
|
2018-12-16 20:33:13 +01:00
|
|
|
#include "rules/BBoxProperty.h"
|
2018-10-31 21:38:35 +01:00
|
|
|
#include "rules/Domain.h"
|
|
|
|
|
#include "rules/simulation/EntityProperty.h"
|
2018-11-03 16:43:33 +01:00
|
|
|
#include "rules/simulation/ExternalProperty.h"
|
2018-10-31 21:38:35 +01:00
|
|
|
#include "rules/Script.h"
|
2018-11-03 16:43:33 +01:00
|
|
|
#include "rules/simulation/StatusProperty.h"
|
|
|
|
|
#include "rules/simulation/Task.h"
|
|
|
|
|
#include "rules/simulation/TasksProperty.h"
|
2012-12-03 23:45:56 +00:00
|
|
|
|
|
|
|
|
#include "common/CommSocket.h"
|
2012-12-04 02:16:39 +00:00
|
|
|
#include "common/id.h"
|
2012-12-03 23:45:56 +00:00
|
|
|
#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;
|
|
|
|
|
|
2015-05-23 16:47:45 +02:00
|
|
|
#include "stubs/server/stubExternalMindsManager.h"
|
|
|
|
|
#include "stubs/server/stubExternalMindsConnection.h"
|
2012-12-03 23:45:56 +00:00
|
|
|
|
2018-04-25 21:05:59 +02:00
|
|
|
#include "stubs/server/stubPlayer.h"
|
2018-08-05 20:47:09 +02:00
|
|
|
#include "stubs/server/stubAccount.h"
|
2018-10-27 13:13:45 +02:00
|
|
|
#include "stubs/server/stubConnectableRouter.h"
|
2018-08-04 01:12:45 +02:00
|
|
|
#include "stubs/server/stubServerRouting.h"
|
2018-10-27 13:13:45 +02:00
|
|
|
#include "stubs/server/stubLobby.h"
|
2012-12-03 23:45:56 +00:00
|
|
|
|