• 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!

C++ UpdateStadistics from Market History Average Price

edwinaaa

Member
Joined
Jan 31, 2014
Messages
72
Reaction score
11
Hello, how are you? I'm having a problem with the updatestadistic system. What it does is set the lowest and highest price and the average price of said product. The problem is that it shows everything in min 0, max 0, etc., in advance say that the market_history table already has the records but in the C++ of the code there is a problem that it does not read the amounts can someone help me?

C++:
(iomarket.cpp)

void IOMarket::updateStatistics()
{
    Database& db = Database::getInstance();
    std::ostringstream query;
    query << "SELECT `sale`, `itemtype`, COUNT(`price`) AS `num`, MIN(`price`) AS `min`, MAX(`price`) AS `max`, SUM(`price`) AS `sum` FROM `market_history` WHERE `state` = "
        << OFFERSTATE_ACCEPTED << " AND `world_id` = " << g_config.getNumber(ConfigManager::WORLD_ID) << " GROUP BY `itemtype`, `sale`;";
    DBResult_ptr result = db.storeQuery(query.str());
    if (!result) {
        return;
    }

    do {
        MarketStatistics* statistics;
        if (result->getNumber<uint16_t>("sale") == MARKETACTION_BUY) {
            statistics = &purchaseStatistics[result->getNumber<uint16_t>("itemtype")];
        }
        else {
            statistics = &saleStatistics[result->getNumber<uint16_t>("itemtype")];
        }

        statistics->numTransactions = result->getNumber<uint32_t>("num");
        statistics->lowestPrice = result->getNumber<uint32_t>("min");
        statistics->totalPrice = result->getNumber<uint64_t>("sum");
        statistics->highestPrice = result->getNumber<uint32_t>("max");
    } while (result->next());
}


C++:
(protocolgame.cpp)


    MarketStatistics* statistics = IOMarket::getInstance().getPurchaseStatistics(itemId);
    if (statistics)
    {
        msg.addByte(0x01);
        msg.add<uint32_t>(statistics->numTransactions);
        msg.add<uint64_t>(statistics->totalPrice);
        msg.add<uint64_t>(statistics->highestPrice);
        msg.add<uint64_t>(statistics->lowestPrice);
    }
    else
    {
        msg.addByte(0x00);
    }

    statistics = IOMarket::getInstance().getSaleStatistics(itemId);
    if (statistics)
    {
        msg.addByte(0x01);
        msg.add<uint32_t>(statistics->numTransactions);
        msg.add<uint64_t>(std::min<uint64_t>(std::numeric_limits<uint32_t>::max(), statistics->totalPrice));
        msg.add<uint64_t>(statistics->highestPrice);
        msg.add<uint64_t>(statistics->lowestPrice);
    }
    else
    {
        msg.addByte(0x00);
    }
 
Hello, how are you? I'm having a problem with the updatestadistic system. What it does is set the lowest and highest price and the average price of said product. The problem is that it shows everything in min 0, max 0, etc., in advance say that the market_history table already has the records but in the C++ of the code there is a problem that it does not read the amounts can someone help me?

C++:
(iomarket.cpp)

void IOMarket::updateStatistics()
{
    Database& db = Database::getInstance();
    std::ostringstream query;
    query << "SELECT `sale`, `itemtype`, COUNT(`price`) AS `num`, MIN(`price`) AS `min`, MAX(`price`) AS `max`, SUM(`price`) AS `sum` FROM `market_history` WHERE `state` = "
        << OFFERSTATE_ACCEPTED << " AND `world_id` = " << g_config.getNumber(ConfigManager::WORLD_ID) << " GROUP BY `itemtype`, `sale`;";
    DBResult_ptr result = db.storeQuery(query.str());
    if (!result) {
        return;
    }

    do {
        MarketStatistics* statistics;
        if (result->getNumber<uint16_t>("sale") == MARKETACTION_BUY) {
            statistics = &purchaseStatistics[result->getNumber<uint16_t>("itemtype")];
        }
        else {
            statistics = &saleStatistics[result->getNumber<uint16_t>("itemtype")];
        }

        statistics->numTransactions = result->getNumber<uint32_t>("num");
        statistics->lowestPrice = result->getNumber<uint32_t>("min");
        statistics->totalPrice = result->getNumber<uint64_t>("sum");
        statistics->highestPrice = result->getNumber<uint32_t>("max");
    } while (result->next());
}


C++:
(protocolgame.cpp)


    MarketStatistics* statistics = IOMarket::getInstance().getPurchaseStatistics(itemId);
    if (statistics)
    {
        msg.addByte(0x01);
        msg.add<uint32_t>(statistics->numTransactions);
        msg.add<uint64_t>(statistics->totalPrice);
        msg.add<uint64_t>(statistics->highestPrice);
        msg.add<uint64_t>(statistics->lowestPrice);
    }
    else
    {
        msg.addByte(0x00);
    }

    statistics = IOMarket::getInstance().getSaleStatistics(itemId);
    if (statistics)
    {
        msg.addByte(0x01);
        msg.add<uint32_t>(statistics->numTransactions);
        msg.add<uint64_t>(std::min<uint64_t>(std::numeric_limits<uint32_t>::max(), statistics->totalPrice));
        msg.add<uint64_t>(statistics->highestPrice);
        msg.add<uint64_t>(statistics->lowestPrice);
    }
    else
    {
        msg.addByte(0x00);
    }
It looks like the updateStatistics function is responsible for querying the market_history table and calculating some statistics about past market transactions for a given item type. These statistics include the number of transactions, the lowest and highest prices, and the total price of all transactions.
The updateStatistics function first constructs a SQL query that selects the sale, itemtype, count of price, minimum price, maximum price, and sum of price for all rows in the market_history table where the state is OFFERSTATE_ACCEPTED and the world_id is a specific value. Then it executes this query and stores the result in a DBResult_ptr object.
If the query returned any rows, the updateStatistics function loops through the rows and stores the statistics in a MarketStatistics object. The MarketStatistics object is either the purchaseStatistics object for the current itemtype if the sale value is MARKETACTION_BUY, or the saleStatistics object for the current itemtype if the sale value is MARKETACTION_SELL.
It's possible that the issue you're experiencing is related to the DBResult_ptr object being null when the query returns no rows. This would indicate that there are no rows in the market_history table that meet the criteria specified in the query (i.e. state is OFFERSTATE_ACCEPTED and world_id is a specific value). In this case, the updateStatistics function would return without doing anything, resulting in the purchaseStatistics and saleStatistics objects being left uninitialized.
If you are expecting there to be rows in the market_history table that meet the criteria specified in the query, it's possible that there is a problem with the database connection or the query itself. You may want to check the logs to see if there are any error messages related to the database connection or the query execution.
Alternatively, if you are not expecting there to be any rows in the market_history table that meet the criteria specified in the query, it's possible that the state and/or world_id values in the table are not what you expect them to be. You may want to verify that these values are correct and that the updateStatistics function is using the correct values in its query.
 
It looks like the updateStatistics function is responsible for querying the market_history table and calculating some statistics about past market transactions for a given item type. These statistics include the number of transactions, the lowest and highest prices, and the total price of all transactions.
The updateStatistics function first constructs a SQL query that selects the sale, itemtype, count of price, minimum price, maximum price, and sum of price for all rows in the market_history table where the state is OFFERSTATE_ACCEPTED and the world_id is a specific value. Then it executes this query and stores the result in a DBResult_ptr object.
If the query returned any rows, the updateStatistics function loops through the rows and stores the statistics in a MarketStatistics object. The MarketStatistics object is either the purchaseStatistics object for the current itemtype if the sale value is MARKETACTION_BUY, or the saleStatistics object for the current itemtype if the sale value is MARKETACTION_SELL.
It's possible that the issue you're experiencing is related to the DBResult_ptr object being null when the query returns no rows. This would indicate that there are no rows in the market_history table that meet the criteria specified in the query (i.e. state is OFFERSTATE_ACCEPTED and world_id is a specific value). In this case, the updateStatistics function would return without doing anything, resulting in the purchaseStatistics and saleStatistics objects being left uninitialized.
If you are expecting there to be rows in the market_history table that meet the criteria specified in the query, it's possible that there is a problem with the database connection or the query itself. You may want to check the logs to see if there are any error messages related to the database connection or the query execution.
Alternatively, if you are not expecting there to be any rows in the market_history table that meet the criteria specified in the query, it's possible that the state and/or world_id values in the table are not what you expect them to be. You may want to verify that these values are correct and that the updateStatistics function is using the correct values in its query.
Hi this query SQL response correct

Code:
SELECT `sale`, `itemtype`, COUNT(`price`) AS `num`, MIN(`price`) AS `min`, MAX(`price`) AS `max`, SUM(`price`) AS `sum` FROM `market_history` WHERE `state` = 3 GROUP BY `itemtype`, `sale`;

1671671156517.png

I removed the word_id but now when I open the client and go to the market and select the item that was transacted, the client crashes and closes, I don't know if it is due to the structure to display the results of said query, I don't know if it should be something with the result bits in each box
Post automatically merged:

It looks like the updateStatistics function is responsible for querying the market_history table and calculating some statistics about past market transactions for a given item type. These statistics include the number of transactions, the lowest and highest prices, and the total price of all transactions.
The updateStatistics function first constructs a SQL query that selects the sale, itemtype, count of price, minimum price, maximum price, and sum of price for all rows in the market_history table where the state is OFFERSTATE_ACCEPTED and the world_id is a specific value. Then it executes this query and stores the result in a DBResult_ptr object.
If the query returned any rows, the updateStatistics function loops through the rows and stores the statistics in a MarketStatistics object. The MarketStatistics object is either the purchaseStatistics object for the current itemtype if the sale value is MARKETACTION_BUY, or the saleStatistics object for the current itemtype if the sale value is MARKETACTION_SELL.
It's possible that the issue you're experiencing is related to the DBResult_ptr object being null when the query returns no rows. This would indicate that there are no rows in the market_history table that meet the criteria specified in the query (i.e. state is OFFERSTATE_ACCEPTED and world_id is a specific value). In this case, the updateStatistics function would return without doing anything, resulting in the purchaseStatistics and saleStatistics objects being left uninitialized.
If you are expecting there to be rows in the market_history table that meet the criteria specified in the query, it's possible that there is a problem with the database connection or the query itself. You may want to check the logs to see if there are any error messages related to the database connection or the query execution.
Alternatively, if you are not expecting there to be any rows in the market_history table that meet the criteria specified in the query, it's possible that the state and/or world_id values in the table are not what you expect them to be. You may want to verify that these values are correct and that the updateStatistics function is using the correct values in its query.
I have been moving the parameters of the bits where the results are displayed a little and I found that now it works but I get many numbers, I tried the query and it is fine since it returns what it should but at the time of displaying it many numbers come out , I leave the bits that I use and the result of the numbers that it throws me


C++:
(protocolgame.cpp)

    MarketStatistics* statistics = IOMarket::getInstance().getPurchaseStatistics(itemId);
    if (statistics)
    {
        msg.addByte(0x01);
        msg.add<uint16_t>(statistics->numTransactions);
        msg.add<uint64_t>(statistics->totalPrice);
        msg.add<uint32_t>(statistics->highestPrice);
        msg.add<uint32_t>(statistics->lowestPrice);
    }
    else
    {
        msg.addByte(0x00);
    }

    statistics = IOMarket::getInstance().getSaleStatistics(itemId);
    if (statistics)
    {
        msg.addByte(0x01);
        msg.add<uint16_t>(statistics->numTransactions);
        msg.add<uint64_t>(std::min<uint64_t>(std::numeric_limits<uint16_t>::max(), statistics->totalPrice));
        msg.add<uint32_t>(statistics->highestPrice);
        msg.add<uint32_t>(statistics->lowestPrice);
    }
    else
    {
        msg.addByte(0x00);
    }

    writeToOutputBuffer(msg);
}

C++:
(iomarket.cpp)


void IOMarket::updateStatistics()
{
    std::ostringstream query;
    query << "SELECT `sale` AS `sale`, `itemtype` AS `itemtype`, COUNT(`price`) AS `num`, MIN(`price`) AS `min`, MAX(`price`) AS `max`, SUM(`price`) AS `sum` FROM `market_history` WHERE `state` = " << OFFERSTATE_ACCEPTED << " GROUP BY `itemtype`, `sale`";
    DBResult_ptr result = Database::getInstance().storeQuery(query.str());
    if (!result) {
        return;
    }

    do {
        MarketStatistics* statistics;
        if (result->getNumber<uint16_t>("sale") == MARKETACTION_BUY) {
            statistics = &purchaseStatistics[result->getNumber<uint16_t>("itemtype")];
        }
        else {
            statistics = &saleStatistics[result->getNumber<uint16_t>("itemtype")];
        }

        statistics->numTransactions = result->getNumber<uint16_t>("num");
        statistics->lowestPrice = result->getNumber<uint32_t>("min");
        statistics->totalPrice = result->getNumber<uint64_t>("sum");
        statistics->highestPrice = result->getNumber<uint32_t>("max");
    } while (result->next());
    return;

}

C++:
(enums.h)


struct MarketStatistics
{
    uint16_t numTransactions = 0;
    uint32_t highestPrice = 0;
    uint64_t totalPrice = 0;
    uint32_t lowestPrice = 0;
};

if you could help me in which bits should i put them thanks :)


1671675286067.png
 
Last edited:
Back
Top