Advertisement
Rochet2

Eluna Custom XP rates

Mar 6th, 2015
1,107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.25 KB | None | 0 0
  1. local T = {} -- A lua table (kind of like array) to store played data when he is logged in
  2.  
  3. --[[
  4. -- Run to characters DB table
  5. CREATE TABLE `custom_xp_rates ` (
  6.     `guid` INT UNSIGNED NOT NULL,
  7.     `rate` FLOAT NOT NULL,
  8.     PRIMARY KEY (`guid`)
  9. )
  10. COLLATE='utf8_general_ci'
  11. ENGINE=InnoDB
  12. ;
  13. ]]
  14.  
  15. -- Loads player rate from DB when he logs in
  16. local function OnLogin(event, player)
  17.     -- Use player's lowguid (DB guid) as the identifier in our data store and DB.
  18.     local guid = player:GetGUIDLow()
  19.    
  20.     -- Make an SQL query to characters database to a custom table
  21.     local Q = CharDBQuery("SELECT rate FROM custom_xp_rates WHERE guid = "..guid)
  22.     if (not Q) then
  23.         -- Query returned no results, set rate to nothing for the player and return
  24.         T[guid] = nil
  25.         return
  26.     end
  27.    
  28.     -- If query returned something, set the rate to our data store
  29.     T[guid] = Q:GetFloat(0)
  30. end
  31.  
  32. local function OnLogout(event, player)
  33.     -- Erase data on logout
  34.     local guid = player:GetGUIDLow()
  35.     T[guid] = nil
  36. end
  37.  
  38. local function OnSave(event, player)
  39.     -- Actually just loading would be fine, but this is needed if you want to change the rate on the fly ingame.
  40.  
  41.     -- Save data on player save. Should happen within a set interval and on logout and disconnect etc.
  42.     local guid = player:GetGUIDLow()
  43.     if (not T[guid]) then
  44.         -- no data for player, delete already saved data
  45.         CharDBExecute("DELETE FROM custom_xp_rates WHERE guid = "..guid)
  46.         return
  47.     end
  48.    
  49.     -- Save (overwrite) the rate the player has
  50.     CharDBExecute("REPLACE INTO custom_xp_rates (rate, guid) VALUES ("..T[guid]..", "..guid..")")
  51. end
  52.  
  53. local function OnGiveXp(event, player, amount, victim)
  54.     -- Select custom rate or 1 for the player
  55.     local guid = player:GetGUIDLow()
  56.     local rate = T[guid] or 1
  57.    
  58.     -- Use the rate on the gained XP
  59.     return amount * rate
  60. end
  61.  
  62. -- Usage:
  63. -- player:SetXPRate(1.4)
  64. -- player:SetXPRate(nil)
  65. function Player.SetXPRate(player, rate)
  66.     -- Select custom rate or 1 for the player
  67.     local guid = player:GetGUIDLow()
  68.     T[guid] = tonumber(rate)
  69. end
  70.  
  71. RegisterPlayerEvent(3, OnLogin)
  72. RegisterPlayerEvent(4, OnLogout)
  73. RegisterPlayerEvent(25, OnSave)
  74. RegisterPlayerEvent(12, OnGiveXp)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement