Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local DATA = {}
- local function LoadVotePoints() -- Use on startup or on reload
- local Q = WorldDBQuery("SELECT accID, vp FROM votepoints")
- if(Q) then
- repeat
- local accID = Q:GetColumn(0):GetULong()
- local vp = Q:GetColumn(1):GetULong()
- DATA[accID] = vp -- Save vp for the account ID to DATA table (on startup)
- until not Q:NextRow()
- end
- end
- local function GetVotePoints(player) -- Returns player's vote points or 0
- return DATA[player:GetAccountId()] or 0 -- No DB query
- end
- local function UpdateVotePoints(player, change) -- Updates player's vote points with the change (IE -5)
- if ((change or 0) == 0) then
- return -- change was nothing, dont do anything
- end
- local accID = player:GetAccountId()
- local newvp = GetVotePoints(player) + change
- if (newvp <= 0) then
- WorldDBQuery("DELETE FROM votepoints WHERE accID = "..accID) -- Remove from DB if vp is 0
- DATA[accID] = nil -- Remove from the lua table
- else
- WorldDBQuery("REPLACE INTO votepoints (accID, vp) VALUES ("..accID..", "..newvp..")") -- update / create new data to DB
- DATA[accID] = newvp -- update / create new data to lua table
- end
- end
- -- any code here that uses GetVotePoints(player) to get the vote points and/or UpdateVotePoints(player, change)
- LoadVotePoints() -- load data from DB on startup to DATA table
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement