Advertisement
Rochet2

Query DB once ("Good")

Dec 11th, 2013
275
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.40 KB | None | 0 0
  1. local DATA = {}
  2.  
  3. local function LoadVotePoints() -- Use on startup or on reload
  4.     local Q = WorldDBQuery("SELECT accID, vp FROM votepoints")
  5.     if(Q) then
  6.         repeat
  7.             local accID = Q:GetColumn(0):GetULong()
  8.             local vp = Q:GetColumn(1):GetULong()
  9.             DATA[accID] = vp -- Save vp for the account ID to DATA table (on startup)
  10.         until not Q:NextRow()
  11.     end
  12. end
  13.  
  14. local function GetVotePoints(player) -- Returns player's vote points or 0
  15.     return DATA[player:GetAccountId()] or 0 -- No DB query
  16. end
  17.  
  18. local function UpdateVotePoints(player, change) -- Updates player's vote points with the change (IE -5)
  19.     if ((change or 0) == 0) then
  20.         return -- change was nothing, dont do anything
  21.     end
  22.     local accID = player:GetAccountId()
  23.     local newvp = GetVotePoints(player) + change
  24.     if (newvp <= 0) then
  25.         WorldDBQuery("DELETE FROM votepoints WHERE accID = "..accID) -- Remove from DB if vp is 0
  26.         DATA[accID] = nil -- Remove from the lua table
  27.     else
  28.         WorldDBQuery("REPLACE INTO votepoints (accID, vp) VALUES ("..accID..", "..newvp..")") -- update / create new data to DB
  29.         DATA[accID] = newvp -- update / create new data to lua table
  30.     end
  31. end
  32.  
  33. -- any code here that uses GetVotePoints(player) to get the vote points and/or UpdateVotePoints(player, change)
  34.  
  35. LoadVotePoints() -- load data from DB on startup to DATA table
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement