Você está desconectado.

Conecte-se e veja nossos conteúdos exclusivos!

Compartilhe
Ir em baixo
avatar
Administrador
Administrador
King Coins King Coins : 74
Reputação : 59
Eu sou : Webmaster
Tópicos : 20
Data de Cadastro : 24/01/2018
Ver perfil do usuário

[System] Item Redemption Key

em 27/1/2018, 18:45
Como funciona:
Uma key única que pode ser usada somente uma vez adicionará um item ao player quando for utilizada.
 
- Criando uma key contendo 10 crystal coins: 
!createkey 2160,10 ou !createkey crystal coin,10 
 
O player receberá a mensagem: "You have created the key: rXo-980376. You can add other items using !addkey."
 
Adicionando novos itens à mesma key:
!addkey rXo-980376,2463,1
 
O player receberá a mensagem: "You have added other items to the key rXo-980376."
 
- Checando os itens que uma key possui:
!checkkey rXo-980376






- Utilizando a key o player receberá todos os itens associados a essa key. Depois disso, a key ficará inválida.
!key rXo-980376
 
Deletando a key da database (apenas se necessário):
!deletekey rXo-980376
 
Configuração:
 

Execute o seguinte comando no mysql do seu servidor:
Código:
CREATE TABLE `code_key` (
    `id`         INT NOT NULL AUTO_INCREMENT,
    `key`        VARCHAR(255)       NOT NULL,
    `item_id`    INT                NOT NULL,
    `count`      INT                NOT NULL,
    `invalid`    INT      NOT NULL DEFAULT 0,
    PRIMARY KEY (`id`)
);

Em talkactions/scripts, crie um arquivo chamado keysystem.lua e adicione o seguinte conteúdo:
Spoiler:

local config = {
["!createkey"] = {access = 3},
["!addkey"] =  {access = 3},
["!deletekey"] = {access = 3},
["!checkkey"] = {access = 3},
["!key"] = {access = 0}}

function onSay(cid, words, param)
   local p = getPlayerPosition(cid)

   if config[words].access > getPlayerAccess(cid) then
       doPlayerSendTextMessage(cid, MESSAGE_STATUS_CONSOLE_BLUE, "You don't have permission to do this.")
       doSendMagicEffect(p, CONST_ME_POFF)
       return true
   end
   
   if words == "!createkey" then
       if param == "" then
           doPlayerSendTextMessage(cid, MESSAGE_STATUS_CONSOLE_BLUE, "Command requires param. Say !createkey itemid,count")
           doSendMagicEffect(p, CONST_ME_POFF)
           return true
       end
       
       local t = string.explode(param, ",")
       
       local id = tonumber(t[1])
       
       if id then
           if not ExistItemById(id) then
               doPlayerSendTextMessage(cid, MESSAGE_STATUS_CONSOLE_BLUE, "Item ID "..t[1].." not found.")
               doSendMagicEffect(p, CONST_ME_POFF)
               return true
           end
       else
           id = getItemIdByName(t[1], false)
           if(not id) then
               doPlayerSendTextMessage(cid, MESSAGE_STATUS_CONSOLE_BLUE, "Item with such name is "..t[1].." not found.")
               doSendMagicEffect(p, CONST_ME_POFF)
               return true
           end
       end
       
       if(not t[2]) then
           doPlayerSendTextMessage(cid, MESSAGE_STATUS_CONSOLE_BLUE, "You must specifiy the quantity of items.")
           doSendMagicEffect(p, CONST_ME_POFF)
           return true
       end
       
       local key = generateKey()
       db.query("INSERT INTO `code_key`(`key`, `item_id`, `count`) VALUES ('"..key.."',"..id..","..tonumber(t[2])..");")
       doPlayerSendTextMessage(cid, MESSAGE_STATUS_CONSOLE_ORANGE, "You have created the key: "..key..". You can add other items using !addkey.")
       doSendMagicEffect(p, CONST_ME_MAGIC_GREEN)    

   elseif words == "!addkey" then
   
       if param == "" then
           doPlayerSendTextMessage(cid, MESSAGE_STATUS_CONSOLE_BLUE, "Command requires param. Say !addkey key,itemid,count")
           doSendMagicEffect(p, CONST_ME_POFF)
           return true
       end
   
       local t = string.explode(param, ",")
       
       local query = db.getResult("SELECT * FROM `code_key` WHERE `key` = '"..t[1].."';")
       if query:getID() == -1 or query:getDataInt("invalid") == 1 then
           doPlayerSendTextMessage(cid, MESSAGE_STATUS_CONSOLE_BLUE, "You can add new items to a key only if the key exists and is not invalid.")
           doSendMagicEffect(p, CONST_ME_POFF)
           return true
       end
       
       if(not t[2])then
           doPlayerSendTextMessage(cid, MESSAGE_STATUS_CONSOLE_BLUE, "You must specify an item to add.")
           doSendMagicEffect(p, CONST_ME_POFF)
           return true
       end
       
       local id = tonumber(t[2])
       
       if id then
           if not ExistItemById(id) then
               doPlayerSendTextMessage(cid, MESSAGE_STATUS_CONSOLE_BLUE, "Item ID "..t[2].." not found.")
               doSendMagicEffect(p, CONST_ME_POFF)
               return true
           end
       else
           id = getItemIdByName(t[2], false)
           if(not id) then
               doPlayerSendTextMessage(cid, MESSAGE_STATUS_CONSOLE_BLUE, "Item with such name is "..t[2].." not found.")
               doSendMagicEffect(p, CONST_ME_POFF)
               return true
           end
       end
       
       if(not t[3]) then
           doPlayerSendTextMessage(cid, MESSAGE_STATUS_CONSOLE_BLUE, "You must specifiy the quantity of items.")
           doSendMagicEffect(p, CONST_ME_POFF)
           return true
       end

       db.query("INSERT INTO `code_key`(`key`, `item_id`, `count`) VALUES ('"..t[1].."',"..id..","..tonumber(t[3])..");")
       doPlayerSendTextMessage(cid, MESSAGE_STATUS_CONSOLE_ORANGE, "You have added other items to the key " ..t[1]..".")
       doSendMagicEffect(p, CONST_ME_MAGIC_BLUE)            
   
   elseif words == "!deletekey" then
       if param == "" then
           doPlayerSendTextMessage(cid, MESSAGE_STATUS_CONSOLE_BLUE, "Command requires param. Say !deletekey key")
           doSendMagicEffect(p, CONST_ME_POFF)
           return true
       end
       
       if not doDeleteKey(param) then
           doPlayerSendTextMessage(cid, MESSAGE_STATUS_CONSOLE_BLUE, "Key "..param.." does not exist in your database.")
           doSendMagicEffect(p, CONST_ME_POFF)
           return true
       end
       
       doPlayerSendTextMessage(cid, MESSAGE_STATUS_CONSOLE_ORANGE, "You have deleted the key: "..param..".")
       doSendMagicEffect(p, CONST_ME_MAGIC_RED)  
       
   elseif words == "!checkkey" then
       if param == "" then
           doPlayerSendTextMessage(cid, MESSAGE_STATUS_CONSOLE_BLUE, "Command requires param. Say !checkkey KEY")
           doSendMagicEffect(p, CONST_ME_POFF)
           return true
       end
       
       if not checkKeyItems(cid, tostring(param)) then
           doPlayerSendTextMessage(cid, MESSAGE_STATUS_CONSOLE_BLUE, "Key "..param.." is invalid.")
           doSendMagicEffect(p, CONST_ME_POFF)
           return true
       end
   elseif words == "!key" then
       if param == "" then
           doPlayerSendTextMessage(cid, MESSAGE_STATUS_CONSOLE_BLUE, "Command requires param. Say !key KEY")
           doSendMagicEffect(p, CONST_ME_POFF)
           return true
       end
       doPlayerUseKey(cid, param)
   end      
return true
end

function generateKey()
   local strings = "AaBbCcDdEeFfGgHhIiJjKkLlMmOoPpQqRrSsTtUuVvWwXxYyZz"
   local newKeyStr, newKeyInt, newKey = "", 0, ""
   local query
   repeat
       for k = 1, math.random(1,10) do
           local l = math.random(1, string.len(strings))
           newKeyStr = newKeyStr .. string.sub(strings, l, l)
       end
       newKeyInt = math.random(999999)
       newKey = newKeyStr.."-"..newKeyInt
       query = db.getResult("SELECT * FROM `code_key` WHERE `key` = " .. db.escapeString(newKey))
   until query:getID() == -1
   if query:getID() ~= -1 then query:free() end
   return newKey
end

function doDeleteKey(key)
local query = db.getResult("SELECT `id` FROM `code_key` WHERE `key` = '" ..key.."';")
if query:getID() == -1 then
   return false
end
repeat
db.query("DELETE FROM `code_key` WHERE `id` = "..query:getDataInt("id")..";")
until not query:next()
query:free()
return true
end

function getKeyItems(key)
local items = {}
local query = db.getResult("SELECT * FROM `code_key` WHERE `key` = '"..key.."';")
if query:getID() == -1 or query:getDataInt("invalid") == 1 then
return nil
end
repeat
local itemid = query:getDataInt("item_id")
local count = query:getDataInt("count")
table.insert(items, {itemid = itemid, count = count})
until not query:next()
query:free()
return items
end

function checkKeyItems(cid, key)
local str = "  ~[Key Items]~  \n\n"
local keyitems = getKeyItems(key)
if not keyitems then return false end
for i = 1, #keyitems do
   str = str..i.. " - "..keyitems[i].count.." "..getItemNameById(keyitems[i].itemid).."\n"
end
doPlayerPopupFYI(cid, str)
return true
end

function doPlayerUseKey(cid, key)
local keyitems = getKeyItems(key)
local p = getPlayerPosition(cid)
if not keyitems then
   doPlayerSendCancel(cid, "This key is invalid.")
   doSendMagicEffect(p, CONST_ME_POFF)
   return true
end
for i = 1, #keyitems do
   if not isItemStackable(keyitems[i].itemid) then
       for k = 1, keyitems[i].count do
           doPlayerAddItem(cid, keyitems[i].itemid, 1)
       end
   else
       doPlayerAddItem(cid, keyitems[i].itemid, keyitems[i].count)
   end
end
db.query("UPDATE `code_key` SET `invalid` = 1 WHERE `key` = '"..key.."';")
doPlayerSendTextMessage(cid, MESSAGE_STATUS_CONSOLE_ORANGE, "You have received your items. The key "..key.." is invalid now.")
doSendMagicEffect(p, CONST_ME_STUN)
return true
end

function ExistItemById(id) --by dwarfer
local items = io.open("data/items/items.xml", "r"):read("*all")
local get = items:match('id="'..id..'"')
if get == nil or get == "" then
return false
end
return true
end

Em "access" edite o grupo que pode utilizar cada comando. No exemplo abaixo, apenas players com acesso maior que 3 podem utilizar o comando !createkey.

["!createkey"] = {access = 3},

Em talkactions.xml, adicione a tag:

E é isso, espero que seja útil Very Happy

Crédito:
Dwarfer.
Voltar ao Topo
Permissão deste fórum:
Você não pode responder aos tópicos neste fórum