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

Monster Kill to Database 1.2

trustjah

Newbie
Joined
Jan 22, 2009
Messages
124
Solutions
6
Reaction score
14
Location
Belgium
Hey, could anyone convert this to TFS 1.2?
Code:
function onKill(cid, target)
    if(isMonster(target) == TRUE) then
        local name = getCreatureName(target)
    
        local monsterkills = db.query("SELECT mid, kills FROM vorgpl_monsterskills WHERE name = '" .. name .. "' LIMIT 1")
        if(monsterkills:getID() ~= -1) then
            db.query("UPDATE vorgpl_monsterskills SET kills = " .. (monsterkills:getDataInt("kills") + 1) .. " WHERE mid = " .. monsterkills:getDataInt("mid") .. " LIMIT 1")
        else
            db.query("INSERT INTO vorgpl_monsterskills (name, kills) VALUES ('" .. name .. "', 1)")
        end
    end
    return TRUE
end

it keeps track of the amount of monsters a player has killed so it can be viewed on the webpage.
Thanks in advance.
 
Solution
Lua:
local config = {
    blacklist = {"rat", "rotworm"}
}

function onKill(cid, target)  
       local targetMonster = target:getMonster()
        if not targetMonster then
            return true
        end

    local name = target:getName()
    if isInArray(config.blacklist, name:lower()) then
        return true
    end

    local resultId = db.storeQuery("SELECT mid, kills FROM vorgpl_monsterskills WHERE name = " ..  db.escapeString(name) .. " LIMIT 1")

    if(resultId ~= false) then
        local m_kills = result.getDataInt(resultId, "kills")
        local m_Id = result.getDataInt(resultId, "mid")
        db.asyncQuery("UPDATE vorgpl_monsterskills SET kills = " .. (m_kills + 1) .. " WHERE mid = " .. m_Id .. " LIMIT 1")
    else...
Lua:
local config = {
    blacklist = {"rat", "rotworm"}
}

function onKill(cid, target)  
       local targetMonster = target:getMonster()
        if not targetMonster then
            return true
        end

    local name = target:getName()
    if isInArray(config.blacklist, name:lower()) then
        return true
    end

    local resultId = db.storeQuery("SELECT mid, kills FROM vorgpl_monsterskills WHERE name = " ..  db.escapeString(name) .. " LIMIT 1")

    if(resultId ~= false) then
        local m_kills = result.getDataInt(resultId, "kills")
        local m_Id = result.getDataInt(resultId, "mid")
        db.asyncQuery("UPDATE vorgpl_monsterskills SET kills = " .. (m_kills + 1) .. " WHERE mid = " .. m_Id .. " LIMIT 1")
    else
        db.asyncQuery("INSERT INTO vorgpl_monsterskills (name, kills) VALUES (" .. db.escapeString(name) .. ", 1)")
    end

    result.free(resultId)
end

Wouldn't really recommend you do it this way though. writing to db on every kill.. ouch for the server.
 
Last edited:
Solution
Have you added the code to the other files

creaturescript.xml
Lua:
<event type="kill" name="dbkill" script="dbkill.lua" />

creaturescript - login.lua
Lua:
player:registerEvent("dbkill")
 
Have you added the code to the other files

creaturescript.xml
Lua:
<event type="kill" name="dbkill" script="dbkill.lua" />

creaturescript - login.lua
Lua:
player:registerEvent("dbkill")

Seems like i forgot to register it in login.lua
Works as requested, thanks!
 
If I was you, I would store it in a table first then every 5 minutes or so, write to the database, if you UE a screen full of mobs it writes every time.
If 200+ people are aoeing thats alot of DB writes.

Good luck.
 
would be a larger operation to handle if you saved everything to write at once rather than small ones spread out
+ it's an async query, it won't freeze the process while it writes to db
the script is fine as-is

most optimal would be to save it but for when you shut the server down and write it all to db at once using onShutdown() globalevent
 
Hey Vulcan, cheers for the reply.

If you kill 10 trolls, 5 troll champs, 2 demons and 5 rats at once with ue, that does 22 db writes at the same time..
or just save +1 for each kill in memory then write that the +10 to trolls, + 5 to champs, +2 to demons, + 5 to rats. to the db would be alot less resource hogging especially with like 200+ people all killing at the exact same time, thats 200 people killing 1-10 mobs at the same time etc.

Not tested it with that many people etc, just a thought.

Also that last part to save till server shut down sounds good actually, maybe i will look into that at some point when I get a bit more time.

Cheers
 
Back
Top