• 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 Would be possible to add a storage and a addon to players offline?

sucibob

Member
Joined
Mar 28, 2017
Messages
128
Reaction score
13
Would be possible to add a storage and a addon to players offline?

That is my DB.sql
hastebin

I think storages are saved on: SELECT * FROM `player_storage`
So i did this, i think it gonna work:
db.executeQuery("INSERT INTO `player_storage` SET (`player_id`, `key`, `value`) VALUES ('" .. result.getDataInt(resultId2, "belongs_to") .. "', '" .. r.storage .. "', '" .. 1 .. "')")
It's right btw?

How the fuck it's save the players addon, i couldnt find it in no where
in players there is lookType, but i think that is the outfit player is using, not addon save stuff

How to add a addon for a player offline and a storage?

Full code:
Code:
function onAdvance(cid, skill, oldLevel, newLevel)


  local r = {
    [50] = {premmydays = 1, storage = 50010},
    [100] = {premmydays = 3, outfitid = 22, addon = 1},
    [150] = {premmydays = 5, outfitid = 22, addon = 2},
  }

  local requiredLevel = 15

   if skill == SKILL_LEVEL and newLevel >= requiredLevel then
     local accountId = getAccountId(cid)
     -- Fetch the ref_key where account_id = accountId and blocked = 0
     local resultId = db.storeQuery("SELECT `ref_key` FROM `__cornex_referral_actions` WHERE `registered_by` = " .. accountId .. " AND `blocked` = 0")
     -- If result is found
     if resultId ~= false then

       -- Fetch the referrer's account_id based on ref_key from previous query result
       local resultId2 = db.storeQuery("SELECT `belongs_to` FROM `__cornex_referral` WHERE `referral_key` = '" .. result.getDataString(resultId, "ref_key") .. "'")
         -- If result is found
       if resultId2 ~= false then
         -- Update accountId's row blocked value to 1
         db.executeQuery("UPDATE `__cornex_referral_actions` SET `blocked` = 1 WHERE `registered_by` = " .. accountId)
         -- Update points to referrer
         -- ADD REWARD TO REFFERER

         -- REWARD 1 storage
         db.executeQuery("INSERT INTO `player_storage` SET (`player_id`, `key`, `value`) VALUES ('" .. result.getDataInt(resultId2, "belongs_to") .. "', '" .. r.storage .. "', '" .. 1 .. "')")
         -- REWARD 2 addon 22,1
         -- REWARD 3 addon 22,2
         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

---
btw- 2 things too:
1) How to change:
local requiredLevel = 15
if skill == SKILL_LEVEL and newLevel >= requiredLevel then

To lvl in r
I mean [50], [100], [150]

2) How should i change to have 3 rewards, not only one, because it set the value to 1, no add more one, and after add 1 time, its over to the others
 
Last edited:
Here is a debugging version of the code.

Please use this code and do the following:
  1. Make 2 new characters
  2. Make 1 character referral the other.
  3. Level the character who has the referral GUID up to level 3.
  4. Log on the character who has was referred.
  5. Copy a screen shot of your console and let me see it.
Code:
referral_rewards = {
   --[Reward_Level] = {premmydays = #, storage = #},
   [50] = {premmydays = false, storage = 50010, outfit = false}, --Storage is only needed if you want them to only receive this reward on their first referal.
   [100] = {premmydays = 3, storage = false, outfit = {[1] = {o = 335, a = 1}, [2] = {o = 336, a = 1}}},
   [150] = {premmydays = 5, storage = false, outfit = {[1] = {o = 335, a = 2}, [2] = {o = 336, a = 2}}},
}

function doPlayerReferralRewards(referredPlayer, newLevel) -- Created New Function to save space and make things easier
   if referral_rewards[newLevel].storage then -- Check if they can only receive reward from 1 player
       if getPlayerStorageValue(referredPlayer, referral_rewards[newLevel].storage) == 1 then -- Player already received this reward
           print(""..(getCreatureName(referredPlayer)).." has already received this reward. Storage: "..(referral_rewards[newLevel].storage).."")
           return true
       else
           print(""..(getCreatureName(referredPlayer)).." setting storage: "..(referral_rewards[newLevel].storage).."")
           doPlayerSetStorageValue(referredPlayer, referral_rewards[newLevel].storage, 1) -- Player has not yet received reward (Marking it now)
       end
   end
   if referral_rewards[newLevel].premmydays then
       doPlayerAddPremiumDays(referredPlayer, referral_rewards[newLevel].premmydays) -- Adding Premium Days
       print(""..(getCreatureName(referredPlayer)).." adding Premium Days: "..(referral_rewards[newLevel].premmydays).."")
   end
   local outfits = referral_rewards[newLevel].outfit -- Getting Outfit Table
   if outfits then -- If there are Outfits to add
       for _, tid in ipairs(outfits) do -- Go through Table to add all outfits
           print(getCreatureName(referredPlayer).." adding Outfit: "..(tid.o).." Addon: "..(tid.a).."")
           doPlayerAddOutfit(referredPlayer, tid.o, tid.a) -- Adds each outfit and addon
       end
   end
   return true
end



function onAdvance(cid, skill, oldLevel, newLevel)
   print("Running onAdvance Referral System.")
   if skill == SKILL_LEVEL and referral_rewards[newLevel] ~= nil then -- This will check if there is an entry for this level
   ----Anti-Abuse Storage Value ------------------------------------------------------------------------------------------------------------------------------------------------
       local currentReferralLevel = getPlayerStorageValue(cid, "Referral") -- Checks the highest level this account has reached.
       if currentReferralLevel >= newLevel then -- This will make sure one player cannot keep giving rewards if they kill themselves then re-level over and over again
           return true
       end
       doPlayerSetStorageValue(cid, "Referral", newLevel) -- Sets the highest level reached to this newLevel
   -----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
       local accountId = getAccountId(cid)
       -- Fetch the ref_key where account_id = accountId and blocked = 0
       local resultId = db.storeQuery("SELECT `ref_key` FROM `__cornex_referral_actions` WHERE `registered_by` = " .. accountId .. " AND `blocked` = 0")
       if resultId ~= false then -- This means the player has a referral
           local resultId2 = db.storeQuery("SELECT `belongs_to` FROM `__cornex_referral` WHERE `referral_key` = '" .. result.getDataString(resultId, "ref_key") .. "'")
           if resultId2 ~= false then -- This means we have found the player's GUID
               local playerGUID = result.getDataInt(resultId2, "belongs_to")
               local referredPlayer = getPlayerByGUID(playerGUID) -- Attempts to get the player if he is online
               if isPlayer(referredPlayer) then -- Player is online
                   print("Giving online player rewards.")
                   doPlayerReferralRewards(referredPlayer, newLevel) -- Do Referral Function
               else -- Player is Offline
                   local str = "guid "..playerGUID -- Creates String to be used in global storage
                   local referralStorage = getStorage(str) -- Checks to see if Storage already Set
                   if referralStorage == -1 then -- Has never received a referral reward
                       referralStorage = {} -- Setting up table
                   end
                   if referralStorage[newLevel] ~= nil then -- Checking if you have a pending Referral Bonus Already
                       referralStorage[newLevel] = referralStorage[newLevel] + 1 -- If you do, add to current pending referrals
                   else
                       referralStorage[newLevel] = 1 -- If no, create new pending referral
                   end
                   print("Setting offline storage table for playerGUID "..playerGUID..".")
                   setStorage(str, table.serialize(referralStorage)) -- Sets the global storage for use upon login
               end
               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

function onLogin(cid)
   print("Running Login Referral System.")
   local playerGUID = getPlayerGUID(cid) -- Gets the Player's GUID
   local str = "guid "..playerGUID -- Creates the string to check storage
   local referralStorage = getStorage(str)
   if referralStorage ~= -1 then
       print("Found offline storage table for playerGUID "..playerGUID..".")
       referralStorage = table.unserialize(referralStorage) -- Unserialize stored table for use
       for i, tid in pairs(referralStorage) do -- Loop through table to give all pending referrals
           for a = 1, tid do
               doPlayerReferralRewards(cid, i) -- Gives player each referral
           end
       end
       setStorage(str, -1) -- Sets the storage to -1 to show you have received all pending referrals
   end
   return true
end
 
Last edited:
Here is a debugging version of the code.

Please use this code and do the following:
  1. Make 2 new characters
  2. Make 1 character referral the other.
  3. Level the character who has the referral GUID up to level 3.
  4. Log on the character who has was referred.
  5. Copy a screen shot of your console and let me see it.
Code:
referral_rewards = {
   --[Reward_Level] = {premmydays = #, storage = #},
   [50] = {premmydays = false, storage = 50010, outfit = false}, --Storage is only needed if you want them to only receive this reward on their first referal.
   [100] = {premmydays = 3, storage = false, outfit = {[1] = {o = 335, a = 1}, [2] = {o = 336, a = 1}}},
   [150] = {premmydays = 5, storage = false, outfit = {[1] = {o = 335, a = 2}, [2] = {o = 336, a = 2}}},
}

function doPlayerReferralRewards(referredPlayer, newLevel) -- Created New Function to save space and make things easier
   if referral_rewards[newLevel].storage then -- Check if they can only receive reward from 1 player
       if getPlayerStorageValue(referredPlayer, referral_rewards[newLevel].storage) == 1 then -- Player already received this reward
           print(""..(getCreatureName(referredPlayer)).." has already received this reward. Storage: "..(referral_rewards[newLevel].storage).."")
           return true
       else
           print(""..(getCreatureName(referredPlayer)).." setting storage: "..(referral_rewards[newLevel].storage).."")
           doPlayerSetStorageValue(referredPlayer, referral_rewards[newLevel].storage, 1) -- Player has not yet received reward (Marking it now)
       end
   end
   if referral_rewards[newLevel].premmydays then
       doPlayerAddPremiumDays(referredPlayer, referral_rewards[newLevel].premmydays) -- Adding Premium Days
       print(""..(getCreatureName(referredPlayer)).." adding Premium Days: "..(referral_rewards[newLevel].premmydays).."")
   end
   local outfits = referral_rewards[newLevel].outfit -- Getting Outfit Table
   if outfits then -- If there are Outfits to add
       for _, tid in ipairs(outfits) do -- Go through Table to add all outfits
           print(getCreatureName(referredPlayer).." adding Outfit: "..(tid.o).." Addon: "..(tid.a).."")
           doPlayerAddOutfit(referredPlayer, tid.o, tid.a) -- Adds each outfit and addon
       end
   end
   return true
end



function onAdvance(cid, skill, oldLevel, newLevel)
   print("Running onAdvance Referral System.")
   if skill == SKILL_LEVEL and referral_rewards[newLevel] ~= nil then -- This will check if there is an entry for this level
   ----Anti-Abuse Storage Value ------------------------------------------------------------------------------------------------------------------------------------------------
       local currentReferralLevel = getPlayerStorageValue(cid, "Referral") -- Checks the highest level this account has reached.
       if currentReferralLevel >= newLevel then -- This will make sure one player cannot keep giving rewards if they kill themselves then re-level over and over again
           return true
       end
       doPlayerSetStorageValue(cid, "Referral", newLevel) -- Sets the highest level reached to this newLevel
   -----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
       local accountId = getAccountId(cid)
       -- Fetch the ref_key where account_id = accountId and blocked = 0
       local resultId = db.storeQuery("SELECT `ref_key` FROM `__cornex_referral_actions` WHERE `registered_by` = " .. accountId .. " AND `blocked` = 0")
       if resultId ~= false then -- This means the player has a referral
           local resultId2 = db.storeQuery("SELECT `belongs_to` FROM `__cornex_referral` WHERE `referral_key` = '" .. result.getDataString(resultId, "ref_key") .. "'")
           if resultId2 ~= false then -- This means we have found the player's GUID
               local playerGUID = result.getDataInt(resultId2, "belongs_to")
               local referredPlayer = getPlayerByGUID(playerGUID) -- Attempts to get the player if he is online
               if isPlayer(referredPlayer) then -- Player is online
                   print("Giving online player rewards.")
                   doPlayerReferralRewards(referredPlayer, newLevel) -- Do Referral Function
               else -- Player is Offline
                   local str = "guid "..playerGUID -- Creates String to be used in global storage
                   local referralStorage = getStorage(str) -- Checks to see if Storage already Set
                   if referralStorage == -1 then -- Has never received a referral reward
                       referralStorage = {} -- Setting up table
                   end
                   if referralStorage[newLevel] ~= nil then -- Checking if you have a pending Referral Bonus Already
                       referralStorage[newLevel] = referralStorage[newLevel] + 1 -- If you do, add to current pending referrals
                   else
                       referralStorage[newLevel] = 1 -- If no, create new pending referral
                   end
                   print("Setting offline storage table for playerGUID "..playerGUID..".")
                   setStorage(str, table.serialize(referralStorage)) -- Sets the global storage for use upon login
               end
               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

function onLogin(cid)
   print("Running Login Referral System.")
   local playerGUID = getPlayerGUID(cid) -- Gets the Player's GUID
   local str = "guid "..playerGUID -- Creates the string to check storage
   local referralStorage = getStorage(str)
   if referralStorage ~= -1 then
       print("Found offline storage table for playerGUID "..playerGUID..".")
       referralStorage = table.unserialize(referralStorage) -- Unserialize stored table for use
       for i, tid in pairs(referralStorage) do -- Loop through table to give all pending referrals
           for a = 1, tid do
               doPlayerReferralRewards(cid, i) -- Gives player each referral
           end
       end
       setStorage(str, -1) -- Sets the storage to -1 to show you have received all pending referrals
   end
   return true
end



I've login, got lvl 4, logout, and log back in the inviter account, and only print this:
Code:
[14:0:37.597] Reffereal Test has logged in.
Running Login Referral System.
[14:1:35.574] Reffereal Test has logged out.
[14:1:42.183] Convidador has logged in.
Running Login Referral System.
[14:1:56.925] Convidador has logged out.

And you don't need to do it for me, just help me, giving a base so i can try do more

Code:
referral_rewards = {
   --[Reward_Level] = {premmydays = #, storage = #},
   [2] = {premmydays = false, storage = 50010, outfit = false}, --Storage is only needed if you want them to only receive this reward on their first referal.
   [3] = {premmydays = 3, storage = false, outfit = {[1] = {o = 335, a = 1}, [2] = {o = 336, a = 1}}},
   [4] = {premmydays = 5, storage = false, outfit = {[1] = {o = 335, a = 2}, [2] = {o = 336, a = 2}}},
}
function doPlayerReferralRewards(referredPlayer, newLevel) -- Created New Function to save space and make things easier
   if referral_rewards[newLevel].storage then -- Check if they can only receive reward from 1 player
       if getPlayerStorageValue(referredPlayer, referral_rewards[newLevel].storage) == 1 then -- Player already received this reward
           print(""..(getCreatureName(referredPlayer)).." has already received this reward. Storage: "..(referral_rewards[newLevel].storage).."")
           return true
       else
           print(""..(getCreatureName(referredPlayer)).." setting storage: "..(referral_rewards[newLevel].storage).."")
           doPlayerSetStorageValue(referredPlayer, referral_rewards[newLevel].storage, 1) -- Player has not yet received reward (Marking it now)
       end
   end
   if referral_rewards[newLevel].premmydays then
       doPlayerAddPremiumDays(referredPlayer, referral_rewards[newLevel].premmydays) -- Adding Premium Days
       print(""..(getCreatureName(referredPlayer)).." adding Premium Days: "..(referral_rewards[newLevel].premmydays).."")
   end
   local outfits = referral_rewards[newLevel].outfit -- Getting Outfit Table
   if outfits then -- If there are Outfits to add
       for _, tid in ipairs(outfits) do -- Go through Table to add all outfits
           print(getCreatureName(referredPlayer).." adding Outfit: "..(tid.o).." Addon: "..(tid.a).."")
           doPlayerAddOutfit(referredPlayer, tid.o, tid.a) -- Adds each outfit and addon
       end
   end
   return true
end
function onAdvance(cid, skill, oldLevel, newLevel)
   print("Running onAdvance Referral System.")
   if skill == SKILL_LEVEL and referral_rewards[newLevel] ~= nil then -- This will check if there is an entry for this level
   ----Anti-Abuse Storage Value ------------------------------------------------------------------------------------------------------------------------------------------------
       local currentReferralLevel = getPlayerStorageValue(cid, "Referral") -- Checks the highest level this account has reached.
       if currentReferralLevel >= newLevel then -- This will make sure one player cannot keep giving rewards if they kill themselves then re-level over and over again
           return true
       end
       doPlayerSetStorageValue(cid, "Referral", newLevel) -- Sets the highest level reached to this newLevel
   -----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
       local accountId = getAccountId(cid)
       -- Fetch the ref_key where account_id = accountId and blocked = 0
       local resultId = db.storeQuery("SELECT `ref_key` FROM `__cornex_referral_actions` WHERE `registered_by` = " .. accountId .. " AND `blocked` = 0")
       if resultId ~= false then -- This means the player has a referral
           local resultId2 = db.storeQuery("SELECT `belongs_to` FROM `__cornex_referral` WHERE `referral_key` = '" .. result.getDataString(resultId, "ref_key") .. "'")
           if resultId2 ~= false then -- This means we have found the player's GUID
               local playerGUID = result.getDataInt(resultId2, "belongs_to")
               local referredPlayer = getPlayerByGUID(playerGUID) -- Attempts to get the player if he is online
               if isPlayer(referredPlayer) then -- Player is online
                   print("Giving online player rewards.")
                   doPlayerReferralRewards(referredPlayer, newLevel) -- Do Referral Function
               else -- Player is Offline
                   local str = "guid "..playerGUID -- Creates String to be used in global storage
                   local referralStorage = getStorage(str) -- Checks to see if Storage already Set
                   if referralStorage == -1 then -- Has never received a referral reward
                       referralStorage = {} -- Setting up table
                   end
                   if referralStorage[newLevel] ~= nil then -- Checking if you have a pending Referral Bonus Already
                       referralStorage[newLevel] = referralStorage[newLevel] + 1 -- If you do, add to current pending referrals
                   else
                       referralStorage[newLevel] = 1 -- If no, create new pending referral
                   end
                   print("Setting offline storage table for playerGUID "..playerGUID..".")
                   setStorage(str, table.serialize(referralStorage)) -- Sets the global storage for use upon login
               end
               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
function onLogin(cid)
   print("Running Login Referral System.")
   local playerGUID = getPlayerGUID(cid) -- Gets the Player's GUID
   local str = "guid "..playerGUID -- Creates the string to check storage
   local referralStorage = getStorage(str)
   if referralStorage ~= -1 then
       print("Found offline storage table for playerGUID "..playerGUID..".")
       referralStorage = table.unserialize(referralStorage) -- Unserialize stored table for use
       for i, tid in pairs(referralStorage) do -- Loop through table to give all pending referrals
           for a = 1, tid do
               doPlayerReferralRewards(cid, i) -- Gives player each referral
           end
       end
       setStorage(str, -1) -- Sets the storage to -1 to show you have received all pending referrals
   end
   return true
end
 
Your onAdvance script is not running.

Every time a player levels up, it should say "Running onAdvance Referral System".

Make sure you have this set-up in Creaturescripts, and have it registered for players onLogin.
 
I'm so sorry, i didn't know onadvance scripts should be added on login.lua

login account who was invite and get lvl 5 (with inviter offline):
Code:
[7:54:11.612] Reffereal Test has logged in.
Running Login Referral System.
Running onAdvance Referral System.
Running onAdvance Referral System.
Running onAdvance Referral System.

login the inviter, and nothing was added:
Code:
[7:56:40.652] Convidador has logged in.
Running Login Referral System.
[7:56:54.919] Convidador has logged out.
[7:56:57.453] Convidador has logged in.
Running Login Referral System.
[7:56:59.495] Convidador has logged out.
[7:57:00.721] Convidador has logged in.

So with iniviter online i got lvl 6 on Reffereal Test
Code:
Running Login Referral System.
Running onAdvance Referral System.
Running onAdvance Referral System.

Relog the inviter to test, but nothing was added
Code:
[7:59:41.629] Convidador has logged out.
[7:59:43.821] Convidador has logged in.
Running Login Referral System.
 
Try This new onAdvance script and do the same tests.
It will give us more information so we know what is happening.

Code:
function onAdvance(cid, skill, oldLevel, newLevel)
   print("Running onAdvance Referral System.")
   if skill == SKILL_LEVEL and referral_rewards[newLevel] ~= nil then -- This will check if there is an entry for this level
   ----Anti-Abuse Storage Value ------------------------------------------------------------------------------------------------------------------------------------------------
       local currentReferralLevel = getPlayerStorageValue(cid, "Referral") -- Checks the highest level this account has reached.
       if currentReferralLevel >= newLevel then -- This will make sure one player cannot keep giving rewards if they kill themselves then re-level over and over again
           return true
       end
       doPlayerSetStorageValue(cid, "Referral", newLevel) -- Sets the highest level reached to this newLevel
   -----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
       local accountId = getAccountId(cid)
       -- Fetch the ref_key where account_id = accountId and blocked = 0
       local resultId = db.storeQuery("SELECT `ref_key` FROM `__cornex_referral_actions` WHERE `registered_by` = " .. accountId .. " AND `blocked` = 0")
       if resultId ~= false then -- This means the player has a referral
           print("onAdvance Player Has Referral.")
           local resultId2 = db.storeQuery("SELECT `belongs_to` FROM `__cornex_referral` WHERE `referral_key` = '" .. result.getDataString(resultId, "ref_key") .. "'")
           if resultId2 ~= false then -- This means we have found the player's GUID
               print("onAdvance found Referral PlayerGUID.")
               local playerGUID = result.getDataInt(resultId2, "belongs_to")
               local referredPlayer = getPlayerByGUID(playerGUID) -- Attempts to get the player if he is online
               if isPlayer(referredPlayer) then -- Player is online
                   print("Giving online player rewards.")
                   doPlayerReferralRewards(referredPlayer, newLevel) -- Do Referral Function
               else -- Player is Offline
                   local str = "guid "..playerGUID -- Creates String to be used in global storage
                   local referralStorage = getStorage(str) -- Checks to see if Storage already Set
                   if referralStorage == -1 then -- Has never received a referral reward
                       referralStorage = {} -- Setting up table
                   end
                   if referralStorage[newLevel] ~= nil then -- Checking if you have a pending Referral Bonus Already
                       referralStorage[newLevel] = referralStorage[newLevel] + 1 -- If you do, add to current pending referrals
                   else
                       referralStorage[newLevel] = 1 -- If no, create new pending referral
                   end
                   print("Setting offline storage table for playerGUID "..playerGUID..".")
                   setStorage(str, table.serialize(referralStorage)) -- Sets the global storage for use upon login
               end
               result.free(resultId2)
           else
               print("Missing ref_key in `__cornex_referral` where ref_key = " .. result.getDataString(resultId, "ref_key"))
           end
           result.free(resultId)
          return true
       end
      print("Player does not have a referral.")
   end
   print("No rewards for gaining level "..newLevel..".")
   return true
end
 
Try This new onAdvance script and do the same tests.
It will give us more information so we know what is happening.

Code:
function onAdvance(cid, skill, oldLevel, newLevel)
   print("Running onAdvance Referral System.")
   if skill == SKILL_LEVEL and referral_rewards[newLevel] ~= nil then -- This will check if there is an entry for this level
   ----Anti-Abuse Storage Value ------------------------------------------------------------------------------------------------------------------------------------------------
       local currentReferralLevel = getPlayerStorageValue(cid, "Referral") -- Checks the highest level this account has reached.
       if currentReferralLevel >= newLevel then -- This will make sure one player cannot keep giving rewards if they kill themselves then re-level over and over again
           return true
       end
       doPlayerSetStorageValue(cid, "Referral", newLevel) -- Sets the highest level reached to this newLevel
   -----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
       local accountId = getAccountId(cid)
       -- Fetch the ref_key where account_id = accountId and blocked = 0
       local resultId = db.storeQuery("SELECT `ref_key` FROM `__cornex_referral_actions` WHERE `registered_by` = " .. accountId .. " AND `blocked` = 0")
       if resultId ~= false then -- This means the player has a referral
           print("onAdvance Player Has Referral.")
           local resultId2 = db.storeQuery("SELECT `belongs_to` FROM `__cornex_referral` WHERE `referral_key` = '" .. result.getDataString(resultId, "ref_key") .. "'")
           if resultId2 ~= false then -- This means we have found the player's GUID
               print("onAdvance found Referral PlayerGUID.")
               local playerGUID = result.getDataInt(resultId2, "belongs_to")
               local referredPlayer = getPlayerByGUID(playerGUID) -- Attempts to get the player if he is online
               if isPlayer(referredPlayer) then -- Player is online
                   print("Giving online player rewards.")
                   doPlayerReferralRewards(referredPlayer, newLevel) -- Do Referral Function
               else -- Player is Offline
                   local str = "guid "..playerGUID -- Creates String to be used in global storage
                   local referralStorage = getStorage(str) -- Checks to see if Storage already Set
                   if referralStorage == -1 then -- Has never received a referral reward
                       referralStorage = {} -- Setting up table
                   end
                   if referralStorage[newLevel] ~= nil then -- Checking if you have a pending Referral Bonus Already
                       referralStorage[newLevel] = referralStorage[newLevel] + 1 -- If you do, add to current pending referrals
                   else
                       referralStorage[newLevel] = 1 -- If no, create new pending referral
                   end
                   print("Setting offline storage table for playerGUID "..playerGUID..".")
                   setStorage(str, table.serialize(referralStorage)) -- Sets the global storage for use upon login
               end
               result.free(resultId2)
           else
               print("Missing ref_key in `__cornex_referral` where ref_key = " .. result.getDataString(resultId, "ref_key"))
           end
           result.free(resultId)
          return true
       end
      print("Player does not have a referral.")
   end
   print("No rewards for gaining level "..newLevel..".")
   return true
end


Code:
[21:40:53.975] Inviter has logged in.
Running Login Referral System.
[21:41:34.218] King has logged in.
Running Login Referral System.
1
Running onAdvance Referral System.
No rewards for gaining level 2.
Running onAdvance Referral System.
No rewards for gaining level 3.

Code:
referral_rewards = {
   --[Reward_Level] = {premmydays = #, storage = #},
   [2] = {premmydays = false, storage = 50010, outfit = false}, --Storage is only needed if you want them to only receive this reward on their first referal.
   [3] = {premmydays = 3, storage = false, outfit = {[1] = {o = 335, a = 1}, [2] = {o = 336, a = 1}}},
   [4] = {premmydays = 5, storage = false, outfit = {[1] = {o = 335, a = 2}, [2] = {o = 336, a = 2}}},
}
function doPlayerReferralRewards(referredPlayer, newLevel) -- Created New Function to save space and make things easier
   if referral_rewards[newLevel].storage then -- Check if they can only receive reward from 1 player
       if getPlayerStorageValue(referredPlayer, referral_rewards[newLevel].storage) == 1 then -- Player already received this reward
           print(""..(getCreatureName(referredPlayer)).." has already received this reward. Storage: "..(referral_rewards[newLevel].storage).."")
           return true
       else
           print(""..(getCreatureName(referredPlayer)).." setting storage: "..(referral_rewards[newLevel].storage).."")
           doPlayerSetStorageValue(referredPlayer, referral_rewards[newLevel].storage, 1) -- Player has not yet received reward (Marking it now)
       end
   end
   if referral_rewards[newLevel].premmydays then
       doPlayerAddPremiumDays(referredPlayer, referral_rewards[newLevel].premmydays) -- Adding Premium Days
       print(""..(getCreatureName(referredPlayer)).." adding Premium Days: "..(referral_rewards[newLevel].premmydays).."")
   end
   local outfits = referral_rewards[newLevel].outfit -- Getting Outfit Table
   if outfits then -- If there are Outfits to add
       for _, tid in ipairs(outfits) do -- Go through Table to add all outfits
           print(getCreatureName(referredPlayer).." adding Outfit: "..(tid.o).." Addon: "..(tid.a).."")
           doPlayerAddOutfit(referredPlayer, tid.o, tid.a) -- Adds each outfit and addon
       end
   end
   return true
end
function onAdvance(cid, skill, oldLevel, newLevel)
   print("Running onAdvance Referral System.")
   if skill == SKILL_LEVEL and referral_rewards[newLevel] ~= nil then -- This will check if there is an entry for this level
   ----Anti-Abuse Storage Value ------------------------------------------------------------------------------------------------------------------------------------------------
       local currentReferralLevel = getPlayerStorageValue(cid, "Referral") -- Checks the highest level this account has reached.
       if currentReferralLevel >= newLevel then -- This will make sure one player cannot keep giving rewards if they kill themselves then re-level over and over again
           return true
       end
       doPlayerSetStorageValue(cid, "Referral", newLevel) -- Sets the highest level reached to this newLevel
   -----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
       local accountId = getAccountId(cid)
       -- Fetch the ref_key where account_id = accountId and blocked = 0
       local resultId = db.storeQuery("SELECT `ref_key` FROM `__cornex_referral_actions` WHERE `registered_by` = " .. accountId .. " AND `blocked` = 0")
       if resultId ~= false then -- This means the player has a referral
           print("onAdvance Player Has Referral.")
           local resultId2 = db.storeQuery("SELECT `belongs_to` FROM `__cornex_referral` WHERE `referral_key` = '" .. result.getDataString(resultId, "ref_key") .. "'")
           if resultId2 ~= false then -- This means we have found the player's GUID
               print("onAdvance found Referral PlayerGUID.")
               local playerGUID = result.getDataInt(resultId2, "belongs_to")
               local referredPlayer = getPlayerByGUID(playerGUID) -- Attempts to get the player if he is online
               if isPlayer(referredPlayer) then -- Player is online
                   print("Giving online player rewards.")
                   doPlayerReferralRewards(referredPlayer, newLevel) -- Do Referral Function
               else -- Player is Offline
                   local str = "guid "..playerGUID -- Creates String to be used in global storage
                   local referralStorage = getStorage(str) -- Checks to see if Storage already Set
                   if referralStorage == -1 then -- Has never received a referral reward
                       referralStorage = {} -- Setting up table
                   end
                   if referralStorage[newLevel] ~= nil then -- Checking if you have a pending Referral Bonus Already
                       referralStorage[newLevel] = referralStorage[newLevel] + 1 -- If you do, add to current pending referrals
                   else
                       referralStorage[newLevel] = 1 -- If no, create new pending referral
                   end
                   print("Setting offline storage table for playerGUID "..playerGUID..".")
                   setStorage(str, table.serialize(referralStorage)) -- Sets the global storage for use upon login
               end
               result.free(resultId2)
           else
               print("Missing ref_key in `__cornex_referral` where ref_key = " .. result.getDataString(resultId, "ref_key"))
           end
           result.free(resultId)
          return true
       end
      print("Player does not have a referral.")
   end
   print("No rewards for gaining level "..newLevel..".")
   return true
end
function onLogin(cid)
   print("Running Login Referral System.")
   local playerGUID = getPlayerGUID(cid) -- Gets the Player's GUID
   local str = "guid "..playerGUID -- Creates the string to check storage
   local referralStorage = getStorage(str)
   if referralStorage ~= -1 then
       print("Found offline storage table for playerGUID "..playerGUID..".")
       referralStorage = table.unserialize(referralStorage) -- Unserialize stored table for use
       for i, tid in pairs(referralStorage) do -- Loop through table to give all pending referrals
           for a = 1, tid do
               doPlayerReferralRewards(cid, i) -- Gives player each referral
           end
       end
       setStorage(str, -1) -- Sets the storage to -1 to show you have received all pending referrals
   end
   return true
end
 
Back
Top