Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local T = {} -- A lua table (kind of like array) to store played data when he is logged in
- --[[
- -- Run to characters DB table
- CREATE TABLE `custom_xp_rates ` (
- `guid` INT UNSIGNED NOT NULL,
- `rate` FLOAT NOT NULL,
- PRIMARY KEY (`guid`)
- )
- COLLATE='utf8_general_ci'
- ENGINE=InnoDB
- ;
- ]]
- -- Loads player rate from DB when he logs in
- local function OnLogin(event, player)
- -- Use player's lowguid (DB guid) as the identifier in our data store and DB.
- local guid = player:GetGUIDLow()
- -- Make an SQL query to characters database to a custom table
- local Q = CharDBQuery("SELECT rate FROM custom_xp_rates WHERE guid = "..guid)
- if (not Q) then
- -- Query returned no results, set rate to nothing for the player and return
- T[guid] = nil
- return
- end
- -- If query returned something, set the rate to our data store
- T[guid] = Q:GetFloat(0)
- end
- local function OnLogout(event, player)
- -- Erase data on logout
- local guid = player:GetGUIDLow()
- T[guid] = nil
- end
- local function OnSave(event, player)
- -- Actually just loading would be fine, but this is needed if you want to change the rate on the fly ingame.
- -- Save data on player save. Should happen within a set interval and on logout and disconnect etc.
- local guid = player:GetGUIDLow()
- if (not T[guid]) then
- -- no data for player, delete already saved data
- CharDBExecute("DELETE FROM custom_xp_rates WHERE guid = "..guid)
- return
- end
- -- Save (overwrite) the rate the player has
- CharDBExecute("REPLACE INTO custom_xp_rates (rate, guid) VALUES ("..T[guid]..", "..guid..")")
- end
- local function OnGiveXp(event, player, amount, victim)
- -- Select custom rate or 1 for the player
- local guid = player:GetGUIDLow()
- local rate = T[guid] or 1
- -- Use the rate on the gained XP
- return amount * rate
- end
- -- Usage:
- -- player:SetXPRate(1.4)
- -- player:SetXPRate(nil)
- function Player.SetXPRate(player, rate)
- -- Select custom rate or 1 for the player
- local guid = player:GetGUIDLow()
- T[guid] = tonumber(rate)
- end
- RegisterPlayerEvent(3, OnLogin)
- RegisterPlayerEvent(4, OnLogout)
- RegisterPlayerEvent(25, OnSave)
- RegisterPlayerEvent(12, OnGiveXp)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement