• There is NO official Otland's Discord server and NO official Otland's server list. The Otland's Staff does not manage any Discord server or server list. Moderators or administrator of any Discord server or server lists have NO connection to the Otland's Staff. Do not get scammed!
  • New resources must be posted under Resources tab. A discussion thread will be created automatically, you can't open threads manually anymore.
Resource icon

Create debug characters with lua config

Sorky

Premium User
Premium User
Joined
Dec 17, 2024
Messages
106
Solutions
3
Reaction score
56
GitHub
Sorky96
Sorky submitted a new resource:

Create debug characters with lua config - Adding account for debug purposes

Hello, so I'm a lazy person and couldn't find any will to configure acc or make 5 sql calls so I created simple code to create characters for: Admin, Knight, Sorcerer, Druid and Paladin.

Here is an instruction how to implement it into your Tibia engine.

1. In config.lua add this line:
LUA:
debugAccount = false

2. In configmanager.cpp inside ConfigManager::load() add:
C++:
boolean[CREATE_DEBUG_ACCOUNT] = getGlobalBoolean(L, "debugAccount", false);
...

Read more about this resource...
Hello, so I'm a lazy person and couldn't find any will to configure acc or make 5 sql calls so I created simple code to create characters for: Admin, Knight, Sorcerer, Druid and Paladin.

Here is an instruction how to implement it into your Tibia engine.

1. In config.lua add this line:
LUA:
debugAccount = false

2. In configmanager.cpp inside ConfigManager::load() add:
C++:
boolean[CREATE_DEBUG_ACCOUNT] = getGlobalBoolean(L, "debugAccount", false);

3. In configmanager.h inside boolean_config_t add:
C++:
CREATE_DEBUG_ACCOUNT,

4. In game.cpp add this function:
C++:
void Game::toggleDebugAccount() {
    Database& db = Database::getInstance();
    DBTransaction transaction;

    if (!transaction.begin()) {
        std::cout << "[DebugAccount] Failed to begin transaction." << std::endl;
        return;
    }

    bool debugEnabled = g_config.getBoolean(ConfigManager::CREATE_DEBUG_ACCOUNT);

    if (debugEnabled) {
        std::cout << "[DebugAccount] Creating debug account and players..." << std::endl;

        db.executeQuery(
            "INSERT IGNORE INTO `accounts` (`name`, `password`, `secret`, `type`, `premium_ends_at`, `email`, `creation`) "
            "VALUES ('test', SHA1('test'), NULL, 6, 0, '[email protected]', UNIX_TIMESTAMP());");

        int32_t accountId = -1;
        DBResult_ptr result = db.storeQuery("SELECT `id` FROM `accounts` WHERE `name` = 'test' LIMIT 1;");
        if (result && result->next()) {
            accountId = result->getNumber<uint32_t>("id");
        }

        if (accountId == -1) {
            std::cout << "[DebugAccount] Failed to retrieve account ID." << std::endl;
            return;
        }

        db.executeQuery(
            "INSERT IGNORE INTO `players` (`name`, `group_id`, `account_id`, `level`, `vocation`, `health`, `healthmax`, `experience`, `looktype`, `town_id`, `posx`, `posy`, `posz`, `cap`, `sex`) "
            "VALUES ('Admin', 6, " + std::to_string(accountId) + ", 100, 1, 150, 150, 420000, 128, 1, 100, 100, 7, 500, 1);");

        const std::array<std::string, 4> vocationNames = { "Sorcerer", "Druid", "Paladin", "Knight" };
        for (uint32_t voc = 1; voc <= 4; ++voc) {
            std::ostringstream query;
            query << "INSERT IGNORE INTO `players` (`name`, `group_id`, `account_id`, `level`, `vocation`, `health`, `healthmax`, `experience`, `looktype`, `town_id`, `posx`, `posy`, `posz`, `cap`, `sex`) "
                  << "VALUES ('Debug " << vocationNames[voc - 1] << "', 1, " << accountId
                  << ", 50, " << voc << ", 100, 100, 80000, 128, 1, 100, 100, 7, 400, 1);";
            db.executeQuery(query.str());
        }

        transaction.commit();
        std::cout << "[DebugAccount] Debug account and players created." << std::endl;
    } else {
        std::cout << "[DebugAccount] Removing debug account and players..." << std::endl;
        db.executeQuery("DELETE FROM `players` WHERE `account_id` = (SELECT `id` FROM `accounts` WHERE `name` = 'test');");
        db.executeQuery("DELETE FROM `accounts` WHERE `name` = 'test';");
        transaction.commit();
        std::cout << "[DebugAccount] Debug account and players removed." << std::endl;
    }
}

5. In game.h add:
C++:
void toggleDebugAccount();

6. At the end of Game::start(ServiceManager* manager) add:
C++:
toggleDebugAccount();

And you’re done.

Now every time you switch this to true, when logging in with test/test, you’ll have a ready-to-use account with 5 characters: Admin, Sorcerer, Druid, Paladin, and Knight.

When you switch it to false, it deletes all of them.
 
Back
Top