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

Request NPC log.

Kayan

Active Member
Joined
Sep 19, 2007
Messages
1,561
Reaction score
38
Location
Santa Catarina
I need um function to log all who players talk with NPC.

Example: Player say "Hi" it'll be logged at a file.. I need something like this.

Code:
function onCreatureSay(cid, type, msg)    
doWriteLogFile("data/logs/npc.txt", getCreatureName(cid) + onCreatureSay")    
npcHandler:onCreatureSay(cid, type, msg)        end

Repp++
 
I used this formula to get a complete log of the NPC... I only wanted it for when it uses buy/sell, but I couldn't manage to do it... If anyone knows how to do it, I would be grateful.

in npc script

LUA:
local function logMessage(message)   
local logFile = io.open("data/logs/npc.log", "a")   
logFile:write(os.date("%Y-%m-%d %H:%M:%S") .. " - " .. message .. "\n")   
logFile:close()end
Code:
function onCreatureSay(cid, type, msg)
    npcHandler:onCreatureSay(cid, type, msg)
    logMessage("onCreatureSay: " .. cid .. " - " .. msg)
end


______________________________________________________________________________________________________

I also tried to include it in the log at the moment of the buy/sell... but I still haven't succeeded.

npc.cpp

Code:
#include <fstream>
#include <ctime>

// Função para registrar transações em um arquivo de log
void logTransaction(const std::string& playerName, const std::string& npcName, const std::string& action, int itemId, int amount, int price) {
    std::ofstream logFile;
    logFile.open("data/logs/npc_transactions.log", std::ios_base::app); // Abre o arquivo no modo append

    // Obtém a hora atual
    std::time_t t = std::time(nullptr);
    char timeStr[100];
    std::strftime(timeStr, sizeof(timeStr), "%Y-%m-%d %H:%M:%S", std::localtime(&t));

    // Escreve a transação no log
    logFile << "[" << timeStr << "] Player: " << playerName << ", NPC: " << npcName << ", Action: " << action
            << ", Item ID: " << itemId << ", Amount: " << amount << ", Price: " << price << std::endl;

    logFile.close();
}

in script from npc
Code:
local function logTransaction(action, itemName, itemId, amount, cost)
    local message = string.format("%s: %s x%d (ItemID: %d) for %d gold coins", action, itemName, amount, itemId, cost)
    logMessage(message)
end

function ShopModule.callbackOnSell(cid, item, subType, amount, ignoreEquipped)
    local itemName = getItemNameById(item)
    local cost = self:getSellItemCost(item, subType, amount)
    logTransaction("Sell", itemName, item, amount, cost)
    return true
end

function ShopModule.callbackOnBuy(cid, item, subType, amount, ignoreEquipped)
    local itemName = getItemNameById(item)
    local cost = self:getBuyItemCost(item, subType, amount)
    logTransaction("Buy", itemName, item, amount, cost)
    return true
end
 
Back
Top