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

TFS 0.X Znote Referral System error

willdu

Active Member
Joined
Mar 11, 2017
Messages
91
Reaction score
25
I tried to edit the Znote Referral System to instead of add points, send some items for the player in a parcel as reward.
But my knowledge are limited, so i think i made some shit and i need some help XD

1- The server is crashing every time when the player invited get a level necessary to reward

Script now:
Lua:
local cfg = {
    stor = 250888,
    requiredLevel = 13,
    bonusPoints = 5
}
local town_id = 1
local refferPointItemID = 2159

function getPlayerNameByGUID(guid)
local name = 0
      local resultId = db.getResult("SELECT `name` FROM `players` WHERE `id` = " .. guid)
      if(resultId:getID() ~= -1) then
          name = resultId.getDataString(resultId, "name")
          resultId:free()
      end
      return name
end

function onAdvance(cid, skill, oldLevel, newLevel)
    --if (newLevel == cfg.requiredLevel and skill == SKILL__LEVEL) and getPlayerStorageValue(cid, cfg.stor) < 1 then
    if (newLevel >= cfg.requiredLevel and skill == SKILL__LEVEL) then
        local accountId = getPlayerAccountId(cid)

        --local resultId = db.storeQuery("SELECT `ref_key` FROM `__cornex_referral_actions` WHERE `registered_by` = " .. accountId .. " AND `blocked` = 0")
        local resultId = db.storeQuery("SELECT `ref_key` FROM `__cornex_referral_actions` WHERE `registered_by` = " .. accountId .. "")
        if resultId then
            print(resultId)
            local resultId2 = db.storeQuery("SELECT `belongs_to` FROM `__cornex_referral` WHERE `referral_key` = '" .. result.getDataString(resultId, "ref_key") .. "'")
            print(resultId2)
            if resultId2 then
                db.executeQuery("UPDATE `__cornex_referral_actions` SET `blocked` = 1 WHERE `registered_by` = " .. accountId)
                --db.executeQuery("UPDATE `znote_accounts` SET `points` = (points + " .. cfg.bonusPoints .. ") WHERE `account_id` = '" .. result.getDataInt(resultId2, "belongs_to") .. "'")
                local chest = doCreateItemEx(2595)
                doAddContainerItem(chest, refferPointItemID, cfg.bonusPoints)
                local targetaccountid = result.getDataInt(resultId2, "belongs_to")
                doPlayerSendMailByName(getPlayerNameByGUID(targetaccountid), chest, town_id)

                print('-> account id: ' .. result.getDataInt(resultId2, "belongs_to"))
                print('-> added ' .. cfg.bonusPoints .. ' points.')
                result.free(resultId2)
            else
                print("Missing ref_key in `__cornex_referral` where ref_key = " .. result.getDataString(resultId, "ref_key"))
            end
            result.free(resultId)
        end
    end
    return true
end

obs 1
i've coment this line and used that above to test in the same account, more easer
Lua:
    --if (newLevel == cfg.requiredLevel and skill == SKILL__LEVEL) and getPlayerStorageValue(cid, cfg.stor) < 1 then
    if (newLevel >= cfg.requiredLevel and skill == SKILL__LEVEL) then

obs 2
same thing as obs 1, to test in the same account
Lua:
        --local resultId = db.storeQuery("SELECT `ref_key` FROM `__cornex_referral_actions` WHERE `registered_by` = " .. accountId .. " AND `blocked` = 0")
        local resultId = db.storeQuery("SELECT `ref_key` FROM `__cornex_referral_actions` WHERE `registered_by` = " .. accountId .. "")


2- How to add 3 options of levels?
Lua:
requiredLevel = 13,
bonusPoints = 5

Lua:
requiredLevel = 50,
bonusPoints = 100

Lua:
requiredLevel = 100,
bonusPoints = 300

Maybe should i set the SET blocked = 1
To change for 1,2,3...
How to?







----


Original script (working with points):
Lua:
local cfg = {
    stor = 250888,
    requiredLevel = 13,
    bonusPoints = 5
}


function onAdvance(cid, skill, oldLevel, newLevel)
    if (newLevel == cfg.requiredLevel and skill == SKILL__LEVEL) and getPlayerStorageValue(cid, cfg.stor) < 1 then
        local accountId = getPlayerAccountId(cid)
  
        local resultId = db.storeQuery("SELECT `ref_key` FROM `__cornex_referral_actions` WHERE `registered_by` = " .. accountId .. " AND `blocked` = 0")
        if resultId then
            print(resultId)
            local resultId2 = db.storeQuery("SELECT `belongs_to` FROM `__cornex_referral` WHERE `referral_key` = '" .. result.getDataString(resultId, "ref_key") .. "'")
            print(resultId2)
            if resultId2 then
                db.executeQuery("UPDATE `__cornex_referral_actions` SET `blocked` = 1 WHERE `registered_by` = " .. accountId)
                db.executeQuery("UPDATE `znote_accounts` SET `points` = (points + " .. cfg.bonusPoints .. ") WHERE `account_id` = '" .. result.getDataInt(resultId2, "belongs_to") .. "'")
                print('-> account id: ' .. result.getDataInt(resultId2, "belongs_to"))
                print('-> added ' .. cfg.bonusPoints .. ' points.')
                result.free(resultId2)
            else
                print("Missing ref_key in `__cornex_referral` where ref_key = " .. result.getDataString(resultId, "ref_key"))
            end
            result.free(resultId)
        end
    end
    return true
end
 
Last edited by a moderator:
Any error messages when the server crashes? I don't have a test environment for old servers like this.

Have you tried to patch the original script step by step to isolate and identify the code line that crashes the server?
 
Any error messages when the server crashes? I don't have a test environment for old servers like this.

Have you tried to patch the original script step by step to isolate and identify the code line that crashes the server?

I've isolate everything and did the script on this way (commenting parts just to test):
Code:
local cfg = {
    stor = 250888,
    requiredLevel = 13,
    bonusPoints = 5
}
local town_id = 1
local refferPointItemID = 2159

function getPlayerNameByGUID(guid)
local name = 0
      local resultId = db.getResult("SELECT `name` FROM `players` WHERE `id` = " .. guid)
      if(resultId:getID() ~= -1) then
          name = resultId.getDataString(resultId, "name")
          resultId:free()
      end
      return name
end

function onAdvance(cid, skill, oldLevel, newLevel)
    print("--------------")
    --if (newLevel == cfg.requiredLevel and skill == SKILL__LEVEL) and getPlayerStorageValue(cid, cfg.stor) < 1 then
    if (newLevel >= cfg.requiredLevel and skill == SKILL__LEVEL) then
        local accountId = getPlayerAccountId(cid)

        --local resultId = db.storeQuery("SELECT `ref_key` FROM `__cornex_referral_actions` WHERE `registered_by` = " .. accountId .. " AND `blocked` = 0")
        local resultId = db.storeQuery("SELECT `ref_key` FROM `__cornex_referral_actions` WHERE `registered_by` = " .. accountId .. "")
        if resultId then
            print(resultId)
            local resultId2 = db.storeQuery("SELECT `belongs_to` FROM `__cornex_referral` WHERE `referral_key` = '" .. result.getDataString(resultId, "ref_key") .. "'")
            print(resultId2)
            if resultId2 then
                db.executeQuery("UPDATE `__cornex_referral_actions` SET `blocked` = 1 WHERE `registered_by` = " .. accountId)
                --db.executeQuery("UPDATE `znote_accounts` SET `points` = (points + " .. cfg.bonusPoints .. ") WHERE `account_id` = '" .. result.getDataInt(resultId2, "belongs_to") .. "'")
                
                local chest = doCreateItemEx(2595)
                doAddContainerItem(chest, refferPointItemID, cfg.bonusPoints)
                local targetaccountid = result.getDataInt(resultId2, "belongs_to")
                doPlayerSendMailByName(getPlayerNameByGUID(targetaccountid), chest, town_id)

                print('-> account id: ' .. result.getDataInt(resultId2, "belongs_to"))
                print('-> added ' .. cfg.bonusPoints .. ' points.')
                result.free(resultId2)
            else
                print("Missing ref_key in `__cornex_referral` where ref_key = " .. result.getDataString(resultId, "ref_key"))
            end
            result.free(resultId)
        end
    end
    print("--------------")
    return true
end


When players invited got a level, it prints:
Code:
--------------
0
1
-> account id: 5
-> added 5 points.
--------------
And server crash


If i coment this lines:
Code:
                -- local chest = doCreateItemEx(2595)
                -- doAddContainerItem(chest, refferPointItemID, cfg.bonusPoints)
                -- local targetaccountid = result.getDataInt(resultId2, "belongs_to")
                -- doPlayerSendMailByName(getPlayerNameByGUID(targetaccountid), chest, town_id)


it just print:
Code:
--------------
0
1
-> account id: 5
-> added 5 points.
--------------


And don't crash

What should i do?



I'm using doPlayerSendMailByName (https://github.com/Fir3element/3777/blob/master/src/luascript.cpp#L2153) wrong?
 
I've isolate everything and did the script on this way (commenting parts just to test):
Code:
local cfg = {
    stor = 250888,
    requiredLevel = 13,
    bonusPoints = 5
}
local town_id = 1
local refferPointItemID = 2159

function getPlayerNameByGUID(guid)
local name = 0
      local resultId = db.getResult("SELECT `name` FROM `players` WHERE `id` = " .. guid)
      if(resultId:getID() ~= -1) then
          name = resultId.getDataString(resultId, "name")
          resultId:free()
      end
      return name
end

function onAdvance(cid, skill, oldLevel, newLevel)
    print("--------------")
    --if (newLevel == cfg.requiredLevel and skill == SKILL__LEVEL) and getPlayerStorageValue(cid, cfg.stor) < 1 then
    if (newLevel >= cfg.requiredLevel and skill == SKILL__LEVEL) then
        local accountId = getPlayerAccountId(cid)

        --local resultId = db.storeQuery("SELECT `ref_key` FROM `__cornex_referral_actions` WHERE `registered_by` = " .. accountId .. " AND `blocked` = 0")
        local resultId = db.storeQuery("SELECT `ref_key` FROM `__cornex_referral_actions` WHERE `registered_by` = " .. accountId .. "")
        if resultId then
            print(resultId)
            local resultId2 = db.storeQuery("SELECT `belongs_to` FROM `__cornex_referral` WHERE `referral_key` = '" .. result.getDataString(resultId, "ref_key") .. "'")
            print(resultId2)
            if resultId2 then
                db.executeQuery("UPDATE `__cornex_referral_actions` SET `blocked` = 1 WHERE `registered_by` = " .. accountId)
                --db.executeQuery("UPDATE `znote_accounts` SET `points` = (points + " .. cfg.bonusPoints .. ") WHERE `account_id` = '" .. result.getDataInt(resultId2, "belongs_to") .. "'")
               
                local chest = doCreateItemEx(2595)
                doAddContainerItem(chest, refferPointItemID, cfg.bonusPoints)
                local targetaccountid = result.getDataInt(resultId2, "belongs_to")
                doPlayerSendMailByName(getPlayerNameByGUID(targetaccountid), chest, town_id)

                print('-> account id: ' .. result.getDataInt(resultId2, "belongs_to"))
                print('-> added ' .. cfg.bonusPoints .. ' points.')
                result.free(resultId2)
            else
                print("Missing ref_key in `__cornex_referral` where ref_key = " .. result.getDataString(resultId, "ref_key"))
            end
            result.free(resultId)
        end
    end
    print("--------------")
    return true
end


When players invited got a level, it prints:
Code:
--------------
0
1
-> account id: 5
-> added 5 points.
--------------
And server crash


If i coment this lines:
Code:
                -- local chest = doCreateItemEx(2595)
                -- doAddContainerItem(chest, refferPointItemID, cfg.bonusPoints)
                -- local targetaccountid = result.getDataInt(resultId2, "belongs_to")
                -- doPlayerSendMailByName(getPlayerNameByGUID(targetaccountid), chest, town_id)


it just print:
Code:
--------------
0
1
-> account id: 5
-> added 5 points.
--------------


And don't crash

What should i do?



I'm using doPlayerSendMailByName (https://github.com/Fir3element/3777/blob/master/src/luascript.cpp#L2153) wrong?

bump
 
I have this other script using the same function, works fine...
Code:
 function getPlayerNameByGUID(guid)
local name = 0
      local resultId = db.getResult("SELECT `name` FROM `players` WHERE `id` = " .. guid)
      if(resultId:getID() ~= -1) then
    name = resultId.getDataString(resultId, "name")
          resultId:free()
      end
      return name
end
 
function onStartup()
   local result = db.getResult("SELECT `id`, `player`, `item_id`, `item_name`, `count`, `cost`, `date` FROM `auction_system` ORDER by `date` ASC;")
   local days = 30*3600*24
   local nowtime = os.date('*t')
   if (result and result:getID() ~= -1) then
     while(true) do
       local id = result:getDataInt("id")
       local player = result:getDataString("player")
       local item_id = result:getDataInt("item_id")
       local item_name = result:getDataString("item_name")
       local count = result:getDataInt("count")
       local cost = result:getDataInt("cost")
       local date = result:getDataInt("date")
       local chest = doCreateItemEx(2595)
       local time= os.time(nowtime) - date
       local duedate = time - days
       local town_id = 1
       if duedate >= 0 then
           doAddContainerItem(chest, item_id, count)
           doPlayerSendMailByName(getPlayerNameByGUID(player), chest, town_id)
           db.executeQuery("DELETE FROM `auction_system` WHERE `id` = '".. id .."';")
       end
       if not(result:next()) then
           break
       end
   end
   end
end

What im doing wrong on referral script?
 
I have other script using this same function and it is working:

Code:
 function getPlayerNameByGUID(guid)
local name = 0
      local resultId = db.getResult("SELECT `name` FROM `players` WHERE `id` = " .. guid)
      if(resultId:getID() ~= -1) then
    name = resultId.getDataString(resultId, "name")
          resultId:free()
      end
      return name
end
 
function onStartup()
   local result = db.getResult("SELECT `id`, `player`, `item_id`, `item_name`, `count`, `cost`, `date` FROM `auction_system` ORDER by `date` ASC;")
   local days = 30*3600*24
   local nowtime = os.date('*t')
   if (result and result:getID() ~= -1) then
     while(true) do
       local id = result:getDataInt("id")
       local player = result:getDataString("player")
       local item_id = result:getDataInt("item_id")
       local item_name = result:getDataString("item_name")
       local count = result:getDataInt("count")
       local cost = result:getDataInt("cost")
       local date = result:getDataInt("date")
       local chest = doCreateItemEx(2595)
       local time= os.time(nowtime) - date
       local duedate = time - days
       local town_id = 1
       if duedate >= 0 then
           doAddContainerItem(chest, item_id, count)
           doPlayerSendMailByName(getPlayerNameByGUID(player), chest, town_id)
           db.executeQuery("DELETE FROM `auction_system` WHERE `id` = '".. id .."';")
       end
       if not(result:next()) then
           break
       end
   end
   end
end

Why?
 
Back
Top