-- VoteScript for AzerothCore with mod-ale (formerly mod-eluna). Put in lua_scripts/. -- .claimvote or .claimvote CODE. TrinityCore's Eluna has no HttpRequest: use -- votescript-delivery.php there. -- -- Rewards in VoteScript: an item entry such as 49426, "gold" (amount in copper), or -- a type such as title/143/forever, xp/experience, teleport/NAME. What you allow is -- below; anything else stays waiting in VoteScript, and nothing from there is ever -- run as code. local URL = "https://votescript.voterank.net" local KEY = "" -- API key from Game server in your dashboard local MAX_XP = 1000000 -- teleport/NAME: map, x, y, z, orientation. Example: -- dalaran = { 571, 5804.1, 624.8, 647.8, 1.6 } local PLACES = {} -- title/ID/forever, spell/ID, mount/ID (the spell that teaches it), effect/ID (an aura -- spell): IDs that votes may give, e.g. { [143] = true } local TITLES, SPELLS, MOUNTS, EFFECTS = {}, {}, {}, {} -- run/NAME: your own actions, e.g. feast = function(player, amount) player:CastSpell(player, 57426, true) end local RUN = {} -- Your own types or words: HANDLERS["vip/gold"] = { can = function(player, args, amount) return true end, give = function(player, args, amount) end } local HANDLERS = {} local PLAYER_EVENT_ON_COMMAND = 42 local busy, refs = {}, {} local function encode(s) return (tostring(s):gsub("[^%w%-_%.~]", function(c) return ("%%%02X"):format(c:byte()) end)) end -- References of unfinished claims survive a restart, so nothing is lost or paid twice. local FILE = "votescript_refs.txt" -- in the folder the server runs in local f = io and io.open(FILE, "r") if f then for line in f:lines() do local slot, ref = line:match("^(%S+) (%S+)$") if slot then refs[slot] = ref end end f:close() end local function keep(slot, ref) refs[slot] = ref local out = io and io.open(FILE .. ".tmp", "w") if not out then return print("[VoteScript] cannot write " .. FILE) end for k, v in pairs(refs) do out:write(k, " ", v, "\n") end out:close() if not os.rename(FILE .. ".tmp", FILE) then -- Windows does not replace os.remove(FILE) os.rename(FILE .. ".tmp", FILE) end end ------------------------------------------------------------ reward types local function id(a, allowed) local n = a[1]:match("^%d+$") and tonumber(a[1]) return n and allowed[n] and n end local function add(name, handler) if HANDLERS[name] == nil then HANDLERS[name] = handler end end -- player is nil when this server has no such function; then the type is not offered. local function has(name) local class = Player or ElunaPlayer return type(class) ~= "table" or class[name] ~= nil end local function offer() local free = function(p) return not p:IsInCombat() and not p:IsDead() and not p:InBattleground() and not p:InArena() end if has("Teleport") then for name, at in pairs(PLACES) do add("teleport/" .. name, { can = free, give = function(p) return p:Teleport(at[1], at[2], at[3], at[4], at[5] or 0) end }) end end if has("GiveXP") then add("xp/experience", { give = function(p, a, n) p:GiveXP(math.min(n, MAX_XP)) end }) end if has("SetKnownTitle") then for t in pairs(TITLES) do add("title/" .. t .. "/forever", { give = function(p, a) p:SetKnownTitle(id(a, TITLES)) end }) end end if has("LearnSpell") then for t in pairs(SPELLS) do add("spell/" .. t, { give = function(p, a) p:LearnSpell(id(a, SPELLS)) end }) end for t in pairs(MOUNTS) do add("mount/" .. t, { give = function(p, a) p:LearnSpell(id(a, MOUNTS)) end }) end end if has("AddAura") then for t in pairs(EFFECTS) do add("effect/" .. t, { give = function(p, a) p:AddAura(id(a, EFFECTS), p) end }) end end for name, f in pairs(RUN) do add("run/" .. name, { give = function(p, a, n) return f(p, n) end }) end -- By mail: full bags cannot swallow a reward, and it arrives offline too. add("item/gold", { give = function(p, a, n) SendMail("Vote reward", "Thank you for voting!", p:GetGUIDLow(), 0, 61, 0, n) end }) add("item", { give = function(p, a, n) local entry = a[1]:match("^%d+$") and tonumber(a[1]) if not entry then return false end SendMail("Vote reward", "Thank you for voting!", p:GetGUIDLow(), 0, 61, 0, 0, 0, entry, n) end }) end offer() local KNOWN = {} for t in ("vip group permission title teleport storage until unlock xp boost effect outfit mount spell badge run custom"):gmatch("%a+") do KNOWN[t] = true end -- "vip/gold" -> its handler, { "gold" }; a key without a known type is an item. local function find(key) local t, rest = key:match("^(%l+)/([%w_%.:,/%-]+)$") if not t or not KNOWN[t] then return HANDLERS["item/" .. key] or HANDLERS.item, { key }, "item" end local args = {} for part in rest:gmatch("[^/]+") do args[#args + 1] = part end local k = key while k do if HANDLERS[k] then return HANDLERS[k], args, t end k = k:match("^(.+)/[^/]*$") end return nil, args, t end local function types() local out = {} for name in pairs(HANDLERS) do out[#out + 1] = name end table.sort(out) return table.concat(out, ",") end -- Checks everything, then gives everything, teleports last. local function ready(p, items) for _, i in ipairs(items) do local h, args = find(i.key) if h and h.can and not h.can(p, args, i.amount) then return false end end return true end local function give(p, items) for _, last in ipairs({ false, true }) do for _, i in ipairs(items) do local h, args, t = find(i.key) if (t == "teleport") == last then local ok, done = false, "nothing here gives it" if h then ok, done = pcall(h.give, p, args, i.amount) end if not ok or done == false then print("[VoteScript] " .. p:GetName() .. " did not get " .. i.key .. " x" .. i.amount .. (ok and "" or ": " .. tostring(done))) end end end end end ------------------------------------------------------------ claiming local function claim(player, code) local guid, low, name = player:GetGUID(), player:GetGUIDLow(), player:GetName() -- mod-ale never calls back on a network error, so a lock needs an end. if busy[low] and os.time() - busy[low] < 20 then player:SendBroadcastMessage("Your votes are already being checked.") return end busy[low] = os.time() local slot = low .. "|" .. (code or ""):sub(1, 40) if not refs[slot] then keep(slot, "wow-" .. os.time() .. "-" .. low .. "-" .. math.random(100000, 999999)) end local ref = refs[slot] local body = "format=text&player=" .. encode(name) .. "&ref=" .. ref .. "&types=" .. encode(types()) if code then body = body .. "&code=" .. encode(code) end HttpRequest("POST", URL .. "/index.php?r=api/v1/" .. (code and "claim-code" or "claim"), body, "application/x-www-form-urlencoded", { ["X-Api-Key"] = KEY }, function(status, text) busy[low] = nil local p = GetPlayerByGUID(guid) local function say(t) if p then p:SendBroadcastMessage(t) end end if status ~= 200 then print("[VoteScript] answer " .. tostring(status)) say("Votes cannot be checked right now. Try again in a minute.") return end if refs[slot] ~= ref then return end -- answered twice local kind, first = text:match("^(%a+)\t([^\n]*)") if kind ~= "ok" then keep(slot, nil) say(first or "This code cannot be used.") return end local items = {} for key, amount in text:gmatch("\n([^\t\n]+)\t(%d+)\t") do items[#items + 1] = { key = key, amount = tonumber(amount) } end local votes, held = first:match("^(%d+)\t?(%d*)") if tonumber(votes) == 0 and #items == 0 then keep(slot, nil) say((tonumber(held) or 0) > 0 and "Your vote reward cannot be given on this server yet. It stays waiting." or "You have no votes to claim.") return end if not p then return end -- left: the next claim repeats this one if not ready(p, items) then say("Your vote reward waits. Type .claimvote again when you are not fighting.") return end keep(slot, nil) give(p, items) say("Thank you for voting! Items are in the mail.") end) end RegisterPlayerEvent(PLAYER_EVENT_ON_COMMAND, function(event, player, command) if not player then return end local code = command:match("^claimvote%s*(%w*)%s*$") if not code then return end claim(player, code ~= "" and code or nil) return false end)