<commit_before>//  illarionserver - server for the game Illarion
//  Copyright 2011 Illarion e.V.
//
//  This file is part of illarionserver.
//
//  illarionserver 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 3 of the License, or
//  (at your option) any later version.
//
//  illarionserver 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 illarionserver.  If not, see <http://www.gnu.org/licenses/>.


#include "db/ConnectionManager.hpp"
#include "luabind/luabind.hpp"
#include "TableStructs.hpp"
#include "World.hpp"
#include "TilesTable.hpp"
#include "script/LuaTileScript.hpp"
#include "constants.hpp"
#include <iostream>
#include "Logger.hpp"

TilesTable::TilesTable() : m_dataOK(false) {
    reload();
}


void TilesTable::reload() {
#ifdef DataConnect_DEBUG
    std::cout << "TilesTable: reload" << std::endl;
#endif

    try {
        ConnectionManager::TransactionHolder transaction = dbmgr->getTransaction();

        std::vector<TYPE_OF_ITEM_ID> ids;
        std::vector<uint8_t> notpassable;
        std::vector<uint8_t> nottransparent;
        std::vector<uint8_t> notpenetrateable;
        std::vector<uint8_t> specialtile;
        std::vector<unsigned char> groundlevel;
        std::vector<std::string> name[3];
        std::vector<TYPE_OF_WALKINGCOST> walkingcost;
        std::vector<std::string> scriptname;
        di::isnull_vector<std::vector<std::string> > n_scriptname(scriptname);

        size_t rows = di::select_all<
                      di::Integer, di::Integer, di::Integer, di::Integer, di::Integer, di::Integer, di::Varchar, di::Varchar, di::Varchar, di::Integer, di::Varchar
                      >(transaction, ids, notpassable, nottransparent, notpenetrateable, specialtile, groundlevel, name[0], name[1], name[2], walkingcost, n_scriptname,
                        "SELECT til_id, til_isnotpassable, til_isnottransparent, til_isnotpenetrateable,"
                        "til_specialtile, til_groundlevel, til_german, til_english, til_french,"
                        "til_walkingcost, til_script FROM tiles");

        if (rows > 0) {
            clearOldTable();

            for (size_t i = 0; i < rows; ++i) {
                TilesStruct temprecord;
                temprecord.flags = 0;
                temprecord.flags = groundlevel[i];
                temprecord.flags |= notpassable[i] ? FLAG_PASSABLE : 0;
                temprecord.flags |= nottransparent[i] ? FLAG_TRANSPARENT : 0;
                temprecord.flags |= notpenetrateable[i] ? FLAG_PENETRATEABLE : 0;
                temprecord.flags |= specialtile[i]? FLAG_SPECIALTILE : 0;
                temprecord.German = name[0][i];
                temprecord.English = name[1][i];
                temprecord.French = name[2][i];
                temprecord.walkingCost = walkingcost[i];

                if (!n_scriptname.var[i] && scriptname[i] != "") {
                    try {
                        boost::shared_ptr<LuaTileScript> script(new LuaTileScript(scriptname[i], temprecord));
                        temprecord.script = script;
                    } catch (ScriptException &e) {
                        Logger::writeError("scripts", "Error while loading script: " + scriptname[i] + ":\n" + e.what() + "\n");
                    }
                }

                m_table[ ids[i] ] = temprecord;
            }

            m_dataOK = true;
        } else {
            m_dataOK = false;
        }


#ifdef DataConnect_DEBUG
        std::cout << "loaded " << rows << " rows into TilesTable" << std::endl;
#endif

    } catch (std::exception &e) {<commit_msg>Implemented new database interface to the tiles table<commit_after>/*
 * Illarionserver - server for the game Illarion
 * Copyright 2011 Illarion e.V.
 *
 * This file is part of Illarionserver.
 *
 * Illarionserver  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 3 of the License, or (at your option) any
 * later version.
 *
 * Illarionserver 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
 * Illarionserver. If not, see <http://www.gnu.org/licenses/>.
 */

#include "data/TilesTable.hpp"

#include <iostream>
#include <string>

#include "db/SelectQuery.hpp"
#include "db/Result.hpp"

#include "script/LuaTileScript.hpp"

#include "constants.hpp"
#include "TableStructs.hpp"
#include "Logger.hpp"

TilesTable::TilesTable() : m_dataOK(false) {
    reload();
}


void TilesTable::reload() {
#ifdef DataConnect_DEBUG
    std::cout << "TilesTable: reload" << std::endl;
#endif

    try {
        Database::SelectQuery query;
        query.addColumn("tiles", "til_id");
        query.addColumn("tiles", "til_isnotpassable");
        query.addColumn("tiles", "til_isnottransparent");
        query.addColumn("tiles", "til_isnotpenetrateable");
        query.addColumn("tiles", "til_specialtile");
        query.addColumn("tiles", "til_groundlevel");
        query.addColumn("tiles", "til_german");
        query.addColumn("tiles", "til_english");
        query.addColumn("tiles", "til_french");
        query.addColumn("tiles", "til_walkingcost");
        query.addColumn("tiles", "til_script");
        query.addServerTable("tiles");

        Database::Result results = query.execute();

        if (!results.empty()) {
            clearOldTable();
            TilesStruct temprecord;
            std::string scriptname;
            TYPE_OF_ITEM_ID tileId;

            for (Database::Result::ConstIterator itr = results.begin();
                 itr != results.end(); ++itr) {
                tileId = (*itr)["til_id"].as<TYPE_OF_ITEM_ID>();
                temprecord.flags = (uint8_t)((*itr)["til_groundlevel"].as<uint16_t>());
                temprecord.flags |= (*itr)["til_isnotpassable"].as<bool>() ? FLAG_PASSABLE : 0;
                temprecord.flags |= (*itr)["til_isnottransparent"].as<bool>() ? FLAG_TRANSPARENT : 0;
                temprecord.flags |= (*itr)["til_isnotpenetrateable"].as<bool>() ? FLAG_TRANSPARENT : 0;
                temprecord.flags |= (*itr)["til_specialtile"].as<bool>() ? FLAG_SPECIALITEM : 0;
                temprecord.German = (*itr)["til_german"].as<std::string>();
                temprecord.English = (*itr)["til_english"].as<std::string>();
                temprecord.French = (*itr)["til_french"].as<std::string>();
                temprecord.walkingCost = (TYPE_OF_WALKINGCOST)((*itr)["til_walkingcost"].as<int16_t>());

                if (!(*itr)["til_script"].is_null()) {
                    scriptname = (*itr)["til_script"].as<std::string>();

                    if (!scriptname.empty()) {
                        try {
                            boost::shared_ptr<LuaTileScript> script(new LuaTileScript(scriptname, temprecord));
                            temprecord.script = script;
                        } catch (ScriptException &e) {
                            Logger::writeError("scripts", "Error while loading script: " + scriptname + ":\n" + e.what() + "\n");
                        }
                    }
                }

                m_table[tileId] = temprecord;
            }

            m_dataOK = true;
        } else {
            m_dataOK = false;
        }


#ifdef DataConnect_DEBUG
        std::cout << "loaded " << rows << " rows into TilesTable" << std::endl;
#endif

    } catch (std::exception &e) {
{'fraction_non_alphanumeric': 0.12832899789421529, 'fraction_numerical': 0.004583178496221974, 'mean_word_length': 2.475678002582867, 'pattern_counts': {'":': 2, '<': 58, '<?xml version=': 0, '>': 35, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4469156', 'n_tokens_mistral': 2478, 'n_tokens_neox': 2355, 'n_words': 560}