cyphesis/tests/server/AccountIntegration.cpp

484 lines
12 KiB
C++
Raw Permalink Normal View History

2009-11-16 21:07:52 +00:00
// 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
2011-02-15 09:30:46 +00:00
#ifdef NDEBUG
#undef NDEBUG
#endif
#ifndef DEBUG
#define DEBUG
#endif
#include "../TestBase.h"
2009-11-16 21:07:52 +00:00
#include "server/Account.h"
2012-12-14 09:04:06 +00:00
#include "server/EntityBuilder.h"
#include "server/EntityRuleHandler.h"
2012-12-14 09:04:06 +00:00
#include "server/Ruleset.h"
2009-11-16 21:07:52 +00:00
#include "server/ServerRouting.h"
#include "server/Connection.h"
2020-04-13 11:17:52 +02:00
#include "rules/simulation/WorldRouter.h"
2009-11-16 21:07:52 +00:00
#include "rules/Domain.h"
#include "rules/simulation/Entity.h"
2018-11-03 16:43:33 +01:00
#include "rules/simulation/ExternalMind.h"
#include "rules/simulation/MindsProperty.h"
2009-11-16 21:07:52 +00:00
2012-12-14 09:04:06 +00:00
#include "common/CommSocket.h"
#include "common/Inheritance.h"
2011-10-31 12:47:56 +01:00
#include "../TestWorld.h"
#include "../DatabaseNull.h"
#include "../TestPropertyManager.h"
2009-11-16 21:07:52 +00:00
#include <Atlas/Objects/SmartPtr.h>
#include <Atlas/Objects/Anonymous.h>
#include <Atlas/Objects/Operation.h>
2009-11-16 21:07:52 +00:00
#include <cassert>
#include <server/Persistence.h>
2018-10-27 13:13:45 +02:00
#include <common/Monitors.h>
#include <server/ExternalMindsManager.h>
#include <server/PossessionAuthenticator.h>
2009-11-16 21:07:52 +00:00
using Atlas::Message::Element;
using Atlas::Message::ListType;
using Atlas::Message::MapType;
using Atlas::Objects::Root;
using Atlas::Objects::Entity::RootEntity;
2009-11-16 21:07:52 +00:00
using Atlas::Objects::Entity::Anonymous;
using Atlas::Objects::Operation::Create;
using Atlas::Objects::Operation::Get;
using Atlas::Objects::Operation::Imaginary;
using Atlas::Objects::Operation::Logout;
using Atlas::Objects::Operation::Look;
using Atlas::Objects::Operation::Set;
using Atlas::Objects::Operation::Talk;
2009-11-17 02:10:08 +00:00
using Atlas::Objects::Operation::Move;
2009-11-16 21:07:52 +00:00
Atlas::Objects::Factories factories;
class TestCommSocket : public CommSocket
{
public:
TestCommSocket() : CommSocket(*(boost::asio::io_context*) 0)
{
}
virtual void disconnect()
{
}
virtual int flush()
{
return 0;
}
2009-11-16 21:07:52 +00:00
};
class TestAccount : public Account
{
public:
TestAccount(Connection* conn, const std::string& username,
const std::string& passwd,
RouterId id) :
Account(conn, username, passwd, std::move(id))
{
}
virtual int characterError(const Operation& op,
const Atlas::Objects::Root& ent,
OpVector& res) const
{
return 0;
}
2009-11-16 21:07:52 +00:00
};
2012-12-13 14:27:34 +00:00
class Accountintegration : public Cyphesis::TestBase
2009-11-16 21:07:52 +00:00
{
DatabaseNull m_database;
Persistence* m_persistence;
WorldRouter* m_world;
ServerRouting* m_server;
CommSocket* m_tc;
Connection* m_c;
TestAccount* m_ac;
EntityRuleHandler* m_entityRuleHandler;
2012-12-13 14:27:34 +00:00
public:
Accountintegration();
2012-12-13 14:27:34 +00:00
void setup();
2012-12-13 14:27:34 +00:00
void teardown();
2012-12-13 14:27:34 +00:00
void test_addNewCharacter();
2009-11-16 21:07:52 +00:00
void test_getType();
void test_addToMessage();
void test_addToEntity();
void test_CreateOperation();
void test_GetOperation();
void test_ImaginaryOperation();
void test_LookOperation();
void test_SetOperation();
void test_TalkOperation();
void test_LogoutOperation();
void test_connectCharacter_entity();
void test_connectCharacter_character();
Inheritance* m_inheritance;
2018-10-27 13:13:45 +02:00
Ref<Entity> m_rootEntity;
EntityBuilder* m_eb;
PropertyManager* m_propertyManager;
2012-12-13 14:27:34 +00:00
};
Accountintegration::Accountintegration()
{
2012-12-14 20:28:43 +00:00
ADD_TEST(Accountintegration::test_addNewCharacter);
ADD_TEST(Accountintegration::test_getType);
ADD_TEST(Accountintegration::test_addToMessage);
ADD_TEST(Accountintegration::test_addToEntity);
2012-12-14 20:27:16 +00:00
ADD_TEST(Accountintegration::test_CreateOperation);
2012-12-14 20:25:52 +00:00
ADD_TEST(Accountintegration::test_GetOperation);
ADD_TEST(Accountintegration::test_ImaginaryOperation);
2012-12-14 20:22:20 +00:00
ADD_TEST(Accountintegration::test_LookOperation);
ADD_TEST(Accountintegration::test_SetOperation);
ADD_TEST(Accountintegration::test_TalkOperation);
ADD_TEST(Accountintegration::test_LogoutOperation);
2012-12-14 12:39:53 +00:00
ADD_TEST(Accountintegration::test_connectCharacter_entity);
ADD_TEST(Accountintegration::test_connectCharacter_character);
2012-12-13 14:27:34 +00:00
}
Atlas::Objects::Root composeDeclaration(std::string class_name, std::string parent, Atlas::Message::MapType rawAttributes)
{
Atlas::Objects::Root decl;
decl->setObjtype("class");
decl->setId(class_name);
decl->setParent(parent);
Atlas::Message::MapType composed;
for (const auto& entry : rawAttributes) {
composed[entry.first] = Atlas::Message::MapType{
{"default", entry.second}
};
}
decl->setAttr("attributes", composed);
return decl;
};
2012-12-13 14:27:34 +00:00
void Accountintegration::setup()
{
m_rootEntity = new Entity(0);
m_persistence = new Persistence(m_database);
m_inheritance = new Inheritance(factories);
m_eb = new EntityBuilder();
m_propertyManager = new TestPropertyManager();
m_entityRuleHandler = new EntityRuleHandler(*m_eb, *m_propertyManager);
m_world = new WorldRouter(m_rootEntity, *m_eb, []() { return std::chrono::milliseconds(0); });
2009-11-16 21:07:52 +00:00
2021-08-01 23:17:40 +02:00
m_server = new ServerRouting(*m_world, *m_persistence, "noruleset", "unittesting",
2, AssetsHandler({}));
2009-11-16 21:07:52 +00:00
m_tc = new TestCommSocket();
m_c = new Connection(*m_tc, *m_server, "addr", 3);
m_ac = new TestAccount(m_c, "user", "password", 4);
std::string dependent, reason;
{
auto decl = composeDeclaration("thing", "game_entity", {});
2018-05-10 00:21:30 +02:00
std::map<const TypeNode*, TypeNode::PropertiesUpdate> changes;
m_entityRuleHandler->install(decl->getId(), decl->getParent(), decl, dependent, reason, changes);
}
2012-12-13 14:27:34 +00:00
}
2009-11-16 21:07:52 +00:00
2012-12-13 14:27:34 +00:00
void Accountintegration::teardown()
{
delete m_entityRuleHandler;
delete m_c;
delete m_server;
2012-12-13 14:27:34 +00:00
delete m_ac;
delete m_tc;
m_world->shutdown();
2012-12-14 09:04:06 +00:00
delete m_world;
2018-10-27 13:13:45 +02:00
m_rootEntity = nullptr;
delete m_eb;
delete m_inheritance;
delete m_persistence;
delete m_propertyManager;
2012-12-13 14:27:34 +00:00
}
2009-11-16 21:07:52 +00:00
2012-12-14 20:28:43 +00:00
void Accountintegration::test_addNewCharacter()
2012-12-13 14:27:34 +00:00
{
// WorldRouter::instance().createSpawnPoint(Atlas::Message::MapType{
// {"name", "foo"},
// {"entities", Atlas::Message::MapType{{"thing", Atlas::Message::MapType{}}}}
// }, m_rootEntity.get());
//
// OpVector res;
// Anonymous new_char;
// RootEntity args;
// args->setAttr("spawn_name", "foo");
// args->setParent("thing");
// auto chr = m_ac->addNewCharacter(new_char, args, res);
// assert(chr);
//
// std::cout << "Test 1" << std::endl << std::flush;
2012-12-14 09:04:06 +00:00
}
2012-12-14 20:28:43 +00:00
void Accountintegration::test_getType()
2012-12-14 09:04:06 +00:00
{
m_ac->getType();
}
2012-12-14 20:28:43 +00:00
void Accountintegration::test_addToMessage()
2012-12-14 09:04:06 +00:00
{
MapType emap;
m_ac->addToMessage(emap);
}
2012-12-14 20:28:43 +00:00
void Accountintegration::test_addToEntity()
2012-12-14 09:04:06 +00:00
{
RootEntity ent;
m_ac->addToEntity(ent);
}
2012-12-14 20:27:16 +00:00
void Accountintegration::test_CreateOperation()
2012-12-14 09:04:06 +00:00
{
Anonymous op_arg;
2017-07-24 13:39:22 +02:00
op_arg->setParent("game_entity");
2012-12-14 09:04:06 +00:00
op_arg->setName("Bob");
2012-12-14 20:27:16 +00:00
Create op;
op->setArgs1(op_arg);
OpVector res;
2012-12-14 09:04:06 +00:00
m_ac->operation(op, res);
}
2012-12-14 20:25:52 +00:00
void Accountintegration::test_GetOperation()
2012-12-14 09:04:06 +00:00
{
Anonymous op_arg;
2017-07-24 13:39:22 +02:00
op_arg->setParent("");
2012-12-14 20:25:52 +00:00
Get op;
op->setArgs1(op_arg);
OpVector res;
2012-12-14 09:04:06 +00:00
m_ac->operation(op, res);
}
void Accountintegration::test_ImaginaryOperation()
2012-12-14 09:04:06 +00:00
{
Anonymous op_arg;
op_arg->setLoc("2");
Imaginary op;
op->setArgs1(op_arg);
op->setSerialno(1);
OpVector res;
2012-12-14 09:04:06 +00:00
m_ac->operation(op, res);
// FIXME Test response is sent to Lobby
2012-12-14 09:04:06 +00:00
}
2012-12-14 20:22:20 +00:00
void Accountintegration::test_LookOperation()
2012-12-14 09:04:06 +00:00
{
// WorldRouter::instance().createSpawnPoint(Atlas::Message::MapType{
// {"name", "foo"},
// {"entities", Atlas::Message::MapType{{"thing", Atlas::Message::MapType{}}}}
// }, m_rootEntity.get());
//
// OpVector res;
// Anonymous new_char;
// RootEntity args;
// args->setAttr("spawn_name", "foo");
// args->setParent("thing");
// auto chr = m_ac->addNewCharacter(new_char, args, res);
//
// Anonymous op_arg;
// op_arg->setId("1");
// op_arg->setId(chr->getId());
//
// Look op;
// op->setArgs1(op_arg);
//
// m_ac->operation(op, res);
//
// // FIXME This doesn't test a lot
2012-12-14 09:04:06 +00:00
}
void Accountintegration::test_SetOperation()
2012-12-14 09:04:06 +00:00
{
// WorldRouter::instance().createSpawnPoint(Atlas::Message::MapType{
// {"name", "foo"},
// {"entities", Atlas::Message::MapType{{"thing", Atlas::Message::MapType{}}}}
// }, m_rootEntity.get());
//
// OpVector res;
// Anonymous new_char;
// RootEntity args;
// args->setAttr("spawn_name", "foo");
// args->setParent("thing");
// auto chr = m_ac->addNewCharacter(new_char, args, res);
//
// BBox newBox(WFMath::Point<3>(-0.5, 0.0, -0.5),
// WFMath::Point<3>(-0.5, 2.0, -0.5));
// chr->m_location.setBBox(newBox);
//
// Anonymous op_arg;
//
// op_arg->setId(chr->getId());
// op_arg->setAttr("guise", "foo");
// op_arg->setAttr("height", 3.0);
// op_arg->setAttr("tasks", ListType());
//
// Set op;
// op->setArgs1(op_arg);
//
// m_ac->operation(op, res);
//
// // FIXME Ensure character has been modified
2012-12-14 09:04:06 +00:00
}
void Accountintegration::test_TalkOperation()
2012-12-14 09:04:06 +00:00
{
Anonymous op_arg;
2017-07-24 13:39:22 +02:00
op_arg->setParent("");
2012-12-14 09:04:06 +00:00
op_arg->setLoc("1");
Talk op;
op->setSerialno(1);
op->setArgs1(op_arg);
OpVector res;
2012-12-14 09:04:06 +00:00
m_ac->operation(op, res);
// FIXME Sound op should have been sent to the lobby
2012-12-14 09:04:06 +00:00
}
2009-11-16 21:07:52 +00:00
void Accountintegration::test_LogoutOperation()
2012-12-14 09:04:06 +00:00
{
Logout op;
op->setSerialno(1);
2012-12-14 09:04:06 +00:00
Anonymous op_arg;
2017-07-24 13:39:22 +02:00
op_arg->setParent("");
op->setArgs1(op_arg);
OpVector res;
2012-12-14 09:04:06 +00:00
m_ac->operation(op, res);
// FIXME Account should have been removed from Lobby, and also from
// Connection
2012-12-14 09:04:06 +00:00
}
2009-11-17 02:10:08 +00:00
2012-12-14 12:39:53 +00:00
void Accountintegration::test_connectCharacter_entity()
2012-12-14 09:04:06 +00:00
{
2018-10-27 13:13:45 +02:00
OpVector res;
Ref<Entity> e = new Entity(7);
2010-11-26 22:22:36 +00:00
2018-10-27 13:13:45 +02:00
int ret = m_ac->connectCharacter(e.get(), res);
ASSERT_EQUAL(ret, 0);
2012-12-14 09:04:06 +00:00
}
2010-11-26 22:22:36 +00:00
2012-12-14 12:39:53 +00:00
void Accountintegration::test_connectCharacter_character()
2012-12-14 09:04:06 +00:00
{
Ref<Entity> e = new Entity(8);
2018-10-27 13:13:45 +02:00
OpVector res;
int ret = m_ac->connectCharacter(e.get(), res);
2012-12-14 12:39:53 +00:00
ASSERT_EQUAL(ret, 0);
2018-10-27 13:13:45 +02:00
ASSERT_NOT_NULL(e->getPropertyClassFixed<MindsProperty>());
ASSERT_FALSE(e->getPropertyClassFixed<MindsProperty>()->getMinds().empty());
2012-12-13 14:27:34 +00:00
}
2009-11-16 21:07:52 +00:00
2012-12-13 14:27:34 +00:00
int main()
{
2018-10-27 13:13:45 +02:00
PossessionAuthenticator possesionAuthenticator;
2021-07-10 18:48:18 +02:00
ExternalMindsManager externalMindsManager(possesionAuthenticator);
2018-10-27 13:13:45 +02:00
Monitors m;
2012-12-13 14:27:34 +00:00
Accountintegration t;
return t.run();
2009-11-16 21:07:52 +00:00
}
2012-09-11 22:53:15 +01:00
// stubs
#include "server/EntityFactory.h"
#include "server/ArchetypeFactory.h"
#include "server/Juncture.h"
#include "server/Persistence.h"
#include "server/Player.h"
#include "server/Ruleset.h"
#include "server/TeleportProperty.h"
2018-11-03 16:43:33 +01:00
#include "rules/simulation/AreaProperty.h"
#include "rules/AtlasProperties.h"
#include "rules/BBoxProperty.h"
2018-11-03 16:43:33 +01:00
#include "rules/simulation/CalendarProperty.h"
#include "rules/simulation/EntityProperty.h"
2018-11-03 16:43:33 +01:00
#include "rules/simulation/LineProperty.h"
#include "server/MindProperty.h"
#include "rules/SolidProperty.h"
2018-11-03 16:43:33 +01:00
#include "rules/simulation/SpawnProperty.h"
#include "rules/simulation/StatusProperty.h"
#include "rules/simulation/TasksProperty.h"
#include "rules/simulation/TerrainModProperty.h"
#include "rules/simulation/TerrainProperty.h"
#include "rules/simulation/TransientProperty.h"
#include "rules/simulation/VisibilityProperty.h"
#include "rules/simulation/SuspendedProperty.h"
#include "rules/simulation/DomainProperty.h"
#include "../stubs/rules/stubScaleProperty.h"
#include "common/const.h"
#include "common/globals.h"
#include "common/log.h"
#include "common/Monitors.h"
#include "common/PropertyFactory.h"
#include "common/system.h"
#include "common/TypeNode.h"
#include "common/Variable.h"
2018-11-03 16:43:33 +01:00
#include "rules/python/PythonScriptFactory.h"
#include "../stubs/common/stubAssetsHandler.h"