Por
Vodkart
Descrição:
Execute no banco de dados:
CREATE TABLE resgate_codes (
id INTEGER NOT NULL,
code VARCHAR( 255 ) NOT NULL,
items VARCHAR( 500 ) NOT NULL,
premium_points INT NOT NULL
DEFAULT 0,
premium_days INT NOT NULL
DEFAULT 0,
PRIMARY KEY ( id )
);
Adicione na sua lib:
function getPremiumPoints(cid)
local query = db.getResult("SELECT `premium_points` FROM `accounts` WHERE `id` = "..getPlayerAccountId(cid))
return query:getDataInt("premium_points") <= 0 and 0 or query:getDataInt("premium_points")
end
function setPremiumPoints(cid, amount)
return db.executeQuery("UPDATE `accounts` SET `premium_points` = "..amount.." WHERE `id` = "..getPlayerAccountId(cid))
end
function getCodesFromServe(p)
local ret = db.getResult("SELECT `id` FROM `resgate_codes` WHERE `code` = "..db.escapeString(p))
return ret:getID() ~= -1 and ret:getDataInt("id") or 0
end
Data/Talkactions
createcode.lua
function onSay(cid, words, param, channel)
local param = param:lower()
if param == "" or not param then doPlayerSendCancel(cid, "insira um codigo novo.") return true end
local code = getCodesFromServe(param)
if code ~= 0 then
doPlayerSendCancel(cid, "Your code ["..param.."] already exist.") return true
end
doPlayerSendTextMessage(cid, MESSAGE_STATUS_CONSOLE_ORANGE, "Your code ["..param.."] create sucefull, now you can add items, premium days or premium points.")
return db.executeQuery("INSERT INTO `resgate_codes` (`code`, `items`, `premium_points`, `premium_days`) VALUES ('".. param .."', '{}', '0', '0');")
end
addcode.lua
function onSay(cid, words, param, channel)
local t = string.explode(param:lower(), ",")
if (param == "") then
doPlayerPopupFYI(cid, "[Redeem Code System]\n\nHow Use Command?\n\nTo add rewards from code enter: /addcode,type\n\n[Types:]\n\nItems\nPremium\nPoints\n\n\Exemples:\n\n/addcode items,crystal coin, 100 --To add item from code, use item name, amount.\n\n/addcode premium,10 -- To set 10 premium days in code\n\n/addcode points,20 -- To set 20 premium points in code") return true
end
local code = getCodesFromServe(t[1])
if code == 0 then
doPlayerSendCancel(cid, "Code ["..t[1].."] does exist.") return true
end
if isInArray({"items","item","itens","iten"}, t[2]) then
if tonumber(t[4]) ~= nil and tostring(t[3]) ~= nil then
local itemid, amount = getItemIdByName(t[3], false), tonumber(t[4])
if not itemid then
doPlayerSendCancel(cid, "Sorry, this item does not exist.") return true
end
local d = db.getResult("SELECT `items` FROM `resgate_codes` WHERE `id` = "..code):getDataString("items")
local y,n = {},0
for a,b in d:gmatch("(%d+),(%d+)") do
n = n + 1
y[n] = {a,b}
end
table.insert(y, {itemid, amount})
local str = "{"
for i, x in pairs(y) do
str = str.."{".. x[1] .. "," .. x[2] .. "}"
if i ~= table.maxn(y) then str = str .. ',' else str = str .. '}' end
end
db.executeQuery("UPDATE `resgate_codes` SET `items` = "..db.escapeString(str).." WHERE `id` = "..code)
doPlayerSendTextMessage(cid, MESSAGE_STATUS_CONSOLE_RED, "You Added "..amount.." "..t[3].." from code ["..t[1].."].")
return true
else
doPlayerSendTextMessage(cid, MESSAGE_STATUS_CONSOLE_BLUE, "You need to enter the item name, amount.")return true
end
elseif isInArray({"pa","premium","premmy","premiun"}, t[2]) then
local min, max = 1, 999
if not tonumber(t[3]) then
doPlayerSendTextMessage(cid, MESSAGE_STATUS_CONSOLE_BLUE, "Sorry, only number.") return true
elseif tonumber(t[3]) < min or tonumber(t[3]) > max then
doPlayerSendTextMessage(cid, MESSAGE_STATUS_CONSOLE_BLUE, "Sorry, Min "..min.." and Max "..max.." points.") return true
end
doPlayerSendTextMessage(cid, MESSAGE_STATUS_CONSOLE_RED, "Now the code receives total "..t[3].." premium days.")
db.executeQuery("UPDATE `resgate_codes` SET `premium_days` = "..t[3].." WHERE `id` = "..code) return true
elseif isInArray({"points","pp","point","pontos"}, t[2]) then
local min, max = 1, 999
if not tonumber(t[3]) then
doPlayerSendTextMessage(cid, MESSAGE_STATUS_CONSOLE_BLUE, "Sorry, only number.") return true
elseif tonumber(t[3]) < min or tonumber(t[3]) > max then
doPlayerSendTextMessage(cid, MESSAGE_STATUS_CONSOLE_BLUE, "Sorry, Min "..min.." and Max "..max.." points.") return true
end
doPlayerSendTextMessage(cid, MESSAGE_STATUS_CONSOLE_RED, "Now the code receives total "..t[3].." premium days.")
db.executeQuery("UPDATE `resgate_codes` SET `premium_points` = "..t[3].." WHERE `id` = "..code) return true
end
return true
end
codes.lua
function onSay(cid, words, param, channel)
local param,str = param:lower(),''
if not param then return true end
local code = getCodesFromServe(param)
if code == 0 then
doPlayerSendCancel(cid, "Your code ["..param.."] does exist or Already used.") return true
end
local info = db.getResult("SELECT * FROM `resgate_codes` WHERE `id` = "..code)
local d,k,p = info:getDataString("items"),info:getDataInt("premium_points"),info:getDataInt("premium_days")
local t,n = {},0
for a,b in d:gmatch("(%d+),(%d+)") do
n = n + 1
t[n] = {a,b}
end
if #t > 0 then
local backpack = doPlayerAddItem(cid, 1999, 1)
for _, i_i in ipairs(t) do
local item, amount = i_i[1],i_i[2]
if isItemStackable(item) or amount == 1 then
doAddContainerItem(backpack, item, amount)
else
for i = 1, amount do
doAddContainerItem(backpack, item, 1)
end
end
end
str = str.."".. (str == "" and "" or ", ") ..""..getItemsFromList(t)
end
if p > 0 then
doPlayerAddPremiumDays(cid, p)
str = str.."".. (str == "" and "" or ", ") .." "..p.." premium days"
end
if k > 0 then
local total = (getPremiumPoints(cid)+k)
setPremiumPoints(cid, total)
str = str.."".. (str == "" and "" or ", ") .." "..k.." premium points"
end
doPlayerSendTextMessage(cid, MESSAGE_INFO_DESCR, "You successfully redeemed the code ["..param.."]! Reward(s):\n"..str)
return db.executeQuery("DELETE FROM `resgate_codes` WHERE `id` = "..code)
end
talkactions.xml
<talkaction words="/resgatecode;!resgatecode" event="script" value="codes.lua"/>
<talkaction words="!createcode;/createcode" access="5" event="script" value="createcode.lua"/>
<talkaction words="!addcode;/addcode" access="5" event="script" value="addcode.lua"/>
Observações:
* Testado somente em servidores com SQLITE
* Futuramente estarei acrescentando e modificando o código para melhorias
* Em breve um comando especial "/generate codes" para gerar códigos com itens randômicos(com chances)
* Quem tiver alguma ideia ou querer passar para servidores com site estarei dando auxilia no tópico.
Posts Recomendados
Participe da conversa
Você pode postar agora e se cadastrar mais tarde. Se você tem uma conta, faça o login para postar com sua conta.