Here i'm releasing my lottery system, please report bugs here.
First, execute this on phpMyAdmin
Go to data/lib and create a file called 105-lottery.lua and paste:
Now go to data/globalevents/scripts create a file called lottery.lua and paste:
Paste this at globalevents.xml:
Now go to data/npc, create a file called lottery man.xml and paste:
Now got to data/npc/scripts, create a file called lottery.lua and paste:
Now go to data/creaturescripts/scripts/login.lua and paste this:
How it works?...
If you want to create a lottery, put the item on you left-hand and talk with the npc.
Now, globalevent will check for lotteries with all tickets sold, if the winner is online, it will get the reward, else, it'll get the reward when logs in.
Same think with the lottery owner, it'll receive the money used and the money from the tickets.
And that's all
First, execute this on phpMyAdmin
SQL:
CREATE TABLE IF NOT EXISTS `lottery` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(32) NOT NULL DEFAULT '',
`tickets` int(11) NOT NULL DEFAULT '0',
`buyed_tickets` int(11) NOT NULL DEFAULT '0',
`price` int(11) NOT NULL DEFAULT '0',
`owner` int(11) NOT NULL DEFAULT '0',
`reward` int(11) NOT NULL DEFAULT '0',
`reward_count` int(11) NOT NULL DEFAULT '0',
`winner` varchar(255) NOT NULL DEFAULT '0',
`enabled` int(11) NOT NULL DEFAULT '0',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=8 ;
CREATE TABLE IF NOT EXISTS `lottery_users` (
`lottery_id` int(11) NOT NULL DEFAULT '0',
`user_id` varchar(32) NOT NULL DEFAULT '',
`owner_id` int(11) NOT NULL DEFAULT '0'
) ENGINE=InnoDB;
Go to data/lib and create a file called 105-lottery.lua and paste:
Lua:
STORAGE_REWARD = 15265
STORAGE_REWARDCOUNT = 15266
STORAGE_MONEY = 15267
function getLotteryList()
local tmp = {}
local query = db.getResult("SELECT `id` FROM `lottery` WHERE `enabled` > 0;")
if(query:getID() ~= -1) then
while(true) do
table.insert(tmp, query:getDataInt("id"))
if not(query:next())then break end
end
query:free()
end
return tmp
end
function getLotteryInfo(id)
local query = db.getResult("select * from lottery where id = " .. id .. ";")
return query and {
id = id,
name = query:getDataString("name"),
tickets = query:getDataInt("tickets"),
price = query:getDataInt("price"),
owner = query:getDataInt("owner"),
buyedTickets = query:getDataInt("buyed_tickets"),
reward = query:getDataInt("reward"),
rewardCount = query:getDataInt("reward_count"),
winner = query:getDataInt("winner"),
enabled = query:getDataInt("enabled") == 1 and true or false
} or false
end
function getLotteryIdByName(name)
local t = 0
local query = db.getResult("SELECT `id` FROM `lottery` WHERE `name` = " .. db.escapeString(name) .. ";")
if query:getID() ~= -1 then
t = query:getDataInt("id")
end
return t
end
function getLotteryUsers(id)
local tmp = {}
local query = db.getResult("SELECT `user_id` FROM `lottery_users` WHERE `lottery_id` = " .. id .. ";")
if(query:getID() ~= -1) then
while(true) do
table.insert(tmp, query:getDataInt("user_id"))
if not(query:next())then break end
end
query:free()
end
return tmp
end
function doAddLottery(name, tickets, buyedtickets, price, playerid, reward, rewardCount, enabled)
local e = 0
if enabled then
e = 1
end
return db.executeQuery("INSERT INTO `lottery` (`name`, `tickets`, `price`, `owner`, `buyed_tickets`, `reward`, reward_count, `winner`, `enabled`) VALUES (" .. db.escapeString(name) .. ", " .. tickets .. ", " .. price .. ", " .. playerid .. ", " .. buyedtickets .. ", " .. reward .. ", " .. rewardCount .. ", `winner` = '', " .. e .. ");")
end
function doLotteryAddBuyedTickets(id, t)
return db.executeQuery("update lottery set buyed_tickets = buyed_tickets + " .. t .. " where id = " .. id .. ";")
end
function doLotteryAddUser(cid, lotteryid, ownerid)
return db.executeQuery("INSERT INTO `lottery_users` (`user_id`, `lottery_id`, `owner_id`) VALUES (" .. getPlayerGUID(cid) .. ", " .. lotteryid .. ", " .. ownerid .. ");")
end
function isOnline(id)
local query = db.getResult("select online from players where id = " .. id .. ";")
local online
if query:getID() ~= -1 then
online = query:getDataInt("online")
end
return online and online > 0 and true or false
end
Now go to data/globalevents/scripts create a file called lottery.lua and paste:
Lua:
function onThink(interval, lastExecution, thinkInterval)
local list, lot, users = getLotteryList()
local text = ""
if list and #list > 0 then
for _, lid in ipairs(list) do
lot = getLotteryInfo(lid)
if lot.tickets == lot.buyedTickets then
doBroadcastMessage("Lottery [" .. lot.name .. "] has ended, the winner have received the reward.", MESSAGE_EVENT_ADVANCE)
users = getLotteryUsers(lid)
local winner = users[math.random(#users)]
if isOnline(winner) then
doPlayerAddItem(getPlayerByGUID(winner), lot.reward, lot.rewardCount)
doPlayerSendTextMessage(getPlayerByGUID(winner), MESSAGE_INFO_DESCR, "You won " .. lot.rewardCount .. "x " .. getItemNameById(lot.reward) .. " from the lottery: " .. lot.name .. ".")
else
local query = db.getResult("SELECT `value` from `player_storage` where `key` = " .. STORAGE_REWARD .. " and `player_id` = " .. winner .. ";")
if query:getID() == -1 then
db.executeQuery("INSERT INTO `player_storage` (`player_id`, `key`, `value`) VALUES (" .. winner .. ", " .. STORAGE_REWARD .. ", " .. lot.reward .. ");")
db.executeQuery("INSERT INTO `player_storage` (`player_id`, `key`, `value`) VALUES (" .. winner .. ", " .. STORAGE_REWARDCOUNT .. ", " .. lot.rewardCount .. ");")
else
db.executeQuery("UPDATE `player_storage` SET `value` = " .. lot.reward .. " WHERE `key` = " .. STORAGE_REWARD .. " AND `player_id` = " .. winner .. ";")
db.executeQuery("UPDATE `player_storage` SET `value` = " .. lot.rewardCount .. " WHERE `key` = " .. STORAGE_REWARDCOUNT .. " AND `player_id` = " .. winner .. ";")
end
end
if isOnline(lot.owner) then
doPlayerAddMoney(getPlayerByGUID(lot.owner), (lot.tickets * lot.price) * 2)
doPlayerSendTextMessage(getPlayerByGUID(lot.owner), MESSAGE_INFO_DESCR, "Your lottery has ended, you get the money back.")
else
local query = db.getResult("SELECT `value` FROM `player_storage` WHERE `key` = " .. STORAGE_MONEY .. " AND `player_id` = " .. lot.owner .. ";")
if query:getID() == -1 then
db.executeQuery("INSERT INTO `player_storage` (`player_id`, `key`, `value`) VALUES (" .. lot.owner .. ", " .. STORAGE_MONEY .. ", " .. (lot.tickets * lot.price) * 2 .. ");")
else
db.executeQuery("UPDATE `player_storage` SET `value` = " .. (lot.tickets * lot.price) * 2 .. " WHERE `key` = " .. STORAGE_MONEY .. " AND `player_id` = " .. lot.owner .. ";")
end
end
db.executeQuery("DELETE FROM `lottery` WHERE `id` = " .. lid .. ";")
db.executeQuery("DELETE FROM `lottery_users` WHERE `lottery_id` = " .. lid .. ";")
end
end
end
return true
end
Paste this at globalevents.xml:
XML:
<globalevent name="lottery" interval="30" event="script" value="lottery.lua"/>
Now go to data/npc, create a file called lottery man.xml and paste:
XML:
<?xml version="1.0" encoding="UTF-8"?>
<npc name="Lottery Man" script="data/npc/scripts/lottery.lua" access="3" lookdir="2" walkinterval="2000">
<mana now="800" max="800"/>
<health now="200" max="200"/>
<look type="152" head="0" body="19" legs="114" feet="114" addons="3"/>
<parameters>
<parameter key="message_greet" value="Hello there |PLAYERNAME|, i can show you the {lottery list}, help you to {buy} a ticket in a lottery or create a {new} lottery." />
</parameters>
</npc>
Now got to data/npc/scripts, create a file called lottery.lua and paste:
Lua:
local keywordHandler = KeywordHandler:new()
local npcHandler = NpcHandler:new(keywordHandler)
NpcSystem.parseParameters(npcHandler)
function onCreatureAppear(cid) npcHandler:onCreatureAppear(cid) end
function onCreatureDisappear(cid) npcHandler:onCreatureDisappear(cid) end
function onCreatureSay(cid, type, msg) npcHandler:onCreatureSay(cid, type, msg) end
function onThink() npcHandler:onThink() end
local talkState = {}
local name = {}
local tickets = {}
local price = {}
local reward = {}
local lot = {}
local buyingPrice = {}
local buyingName = {}
local buyingTickets = {}
local maxTickets = 30
function creatureSayCallback(cid, type, msg)
if(not npcHandler:isFocused(cid)) then
return false
end
local talkUser = NPCHANDLER_CONVBEHAVIOR == CONVERSATION_PRIVATE and 0 or cid
if msgcontains(msg, 'lottery list') or msgcontains(msg, 'list') then
local list = getLotteryList()
local strings = {''}
local i = 1
local position = 1
for _, lid in ipairs(list) do
if(i > (position * 1)) then
strings[position] = strings[position] .. ''
position = position + 1
strings[position] = ''
else
strings[position] = i == 1 and '' or strings[position] .. ' . '
end
local l = getLotteryInfo(lid)
strings[position] = strings[position] .. '{' .. (l.name or '') .. '} [By: {' .. (getPlayerNameByGUID(l.owner) or '') .. '}, Able Tickets: {' .. (l.tickets - l.buyedTickets or 0) .. '}, Price: {' .. (l.price or 0) .. '}, Reward: {' .. (getItemNameById(l.reward) or 0) .. '}]'
i = i + 1
end
if (i - 1) > 0 then
npcHandler:say('Reading the lottery list... {' .. i - 1 .. '} lotteries found.', cid)
for i, str in ipairs(strings) do
if(str:sub(str:len()) ~= '-') then
str = str .. '.'
end
npcHandler:say(str, cid)
end
talkState[talkUser] = 0
else
npcHandler:say('There is not any lottery.', cid)
talkState[talkUser] = 0
end
elseif msgcontains(msg, 'new') then
npcHandler:say('Remember, the reward will be the item that you have in your left hand. Please tell me the name of the lottery.', cid)
talkState[talkUser] = 1
elseif talkState[talkUser] == 1 then
name[cid] = msg
local query = db.getResult("select name from lottery where name = " .. db.escapeString(name[cid]) .. ";")
if query:getID() == -1 then
npcHandler:say('Now tell me how many tickets will be available.', cid)
talkState[talkUser] = 2
else
npcHandler:say('There is already a lottery with that name, please tell me other name.', cid)
talkState[talkUser] = 1
query:free()
end
elseif talkState[talkUser] == 2 then
if not tonumber(msg) or tonumber(msg) < 1 then
npcHandler:say('Please, tell me how many tickets will be available.', cid)
talkState[talkUser] = 2
elseif tonumber(msg) > maxTickets then
npcHandler:say('Sorry, you only can active a lottery with a maximun of ' .. maxTickets .. ' tickets.', cid)
talkState[talkUser] = 2
else
tickets[cid] = math.abs(tonumber(msg))
npcHandler:say('Now tell me the price of each ticket.', cid)
talkState[talkUser] = 3
end
elseif talkState[talkUser] == 3 then
if not tonumber(msg) or tonumber(msg) < 1 then
npcHandler:say('Please, tell me the price of each ticket.', cid)
talkState[talkUser] = 3
else
if getPlayerSlotItem(cid, CONST_SLOT_LEFT).itemid < 1 then
npcHandler:say('Remember to put an item in your left-hand.', cid)
talkState[talkUser] = 0
else
price[cid] = math.abs(tonumber(msg))
reward[cid] = getPlayerSlotItem(cid, CONST_SLOT_LEFT)
npcHandler:say('So, your lottery will be named {' .. name[cid] .. '} with {' .. tickets[cid] .. '} available tickets with the price of {' .. price[cid] .. '} each ticket and the reward will be ' .. (reward[cid].type < 1 and 1 or reward[cid].type) .. 'x ' .. getItemNameById(reward[cid].itemid) .. '}, you need to pay {' .. tickets[cid] * price[cid] .. '} gold coins to active the lottery, of course this money will be reembolsed when the lottery ends. Do you want to active the lottery now?.', cid)
talkState[talkUser] = 4
end
end
elseif msgcontains(msg, 'yes') and talkState[talkUser] == 4 then
if getPlayerMoney(cid) >= tickets[cid] * price[cid] then
if reward[cid].itemid == getPlayerSlotItem(cid, CONST_SLOT_LEFT).itemid and reward[cid].type == getPlayerSlotItem(cid, CONST_SLOT_LEFT).type and doPlayerRemoveItem(cid, reward[cid].itemid, (reward[cid].type < 1 and 1 or reward[cid].type)) then
doPlayerRemoveMoney(cid, tickets[cid] * price[cid])
npcHandler:say('Lottery was added, thanks.', cid)
doAddLottery(name[cid], tickets[cid], 0, price[cid], getPlayerGUID(cid), reward[cid].itemid, (reward[cid].type < 1 and 1 or reward[cid].type), true)
talkState[talkUser] = 0
else
npcHandler:say('Remember to put the item in your left-hand.', cid)
talkState[talkUser] = 0
end
else
npcHandler:say('You do not have enough money.', cid)
talkState[talkUser] = 0
end
elseif msgcontains(msg, 'buy') then
npcHandler:say('Please tell me the name of the lottery that do you want to buy tickets.', cid)
talkState[talkUser] = 5
elseif talkState[talkUser] == 5 then
buyingName[cid] = msg
if getLotteryIdByName(buyingName[cid]) > 0 then
lot[cid] = getLotteryInfo(getLotteryIdByName(buyingName[cid]))
if(lot[cid].tickets - lot[cid].buyedTickets) > 0 then
npcHandler:say(buyingName[cid] .. ' eh?, do you want to buy a ticket for ' .. lot[cid].price .. ' gold coins?.', cid)
talkState[talkUser] = 6
else
npcHandler:say('Sorry, all tickets are selled.', cid)
talkState[talkUser] = 0
end
else
npcHandler:say('There is not any lottery with that name.', cid)
talkState[talkUser] = 5
return true
end
elseif msgcontains(msg, 'yes') and talkState[talkUser] == 6 then
local query = db.getResult("select user_id from lottery_users where lottery_id = " .. getLotteryIdByName(buyingName[cid]) .. ";")
if query:getID() ~= - 1 then
npcHandler:say("You already buyed a ticket in this lottery.", cid)
talkState[talkUser] = 0
else
if doPlayerRemoveMoney(cid, lot[cid].price) then
npcHandler:say('Now wait until the lottery finish to know the result.', cid)
doLotteryAddBuyedTickets(lot[cid].id, 1)
doLotteryAddUser(cid, lot[cid].id, lot[cid].owner)
talkState[talkUser] = 0
else
npcHandler:say('You do not have enough money.', cid)
talkState[talkUser] = 0
end
end
end
end
npcHandler:setCallback(CALLBACK_MESSAGE_DEFAULT, creatureSayCallback)
npcHandler:addModule(FocusModule:new())
Now go to data/creaturescripts/scripts/login.lua and paste this:
Lua:
if getCreatureStorage(cid, STORAGE_REWARD) > 0 then
doPlayerAddItem(cid, getCreatureStorage(cid, STORAGE_REWARD), getCreatureStorage(cid, STORAGE_REWARDCOUNT))
doPlayerSendTextMessage(cid, MESSAGE_INFO_DESCR, "You won " .. getCreatureStorage(cid, STORAGE_REWARDCOUNT) .. "x " .. getItemNameById(getCreatureStorage(cid, STORAGE_REWARD)) .. " from a lottery.")
doCreatureSetStorage(cid, STORAGE_REWARD, 0)
doCreatureSetStorage(cid, STORAGE_REWARDCOUNT, 0)
end
if getCreatureStorage(cid, STORAGE_MONEY) > 0 then
doPlayerAddMoney(cid, getCreatureStorage(cid, STORAGE_MONEY))
doPlayerSendTextMessage(cid, MESSAGE_INFO_DESCR, "You got " .. getCreatureStorage(cid, STORAGE_MONEY) .. " gold coins from your ended lottery.")
doCreatureSetStorage(cid, STORAGE_MONEY, 0)
end
How it works?...
If you want to create a lottery, put the item on you left-hand and talk with the npc.
Player: Hi
Lottery man: Hello!
Player: New
Lottery man: Remember, the reward will be the item that you have in your left hand. Please tell me the name of the lottery.
Player: TheBestLotteryInTheWorld
Lottery man: Now tell me how many tickets will be available.
Player: 5
Lottery man: Now tell me the price of each ticket.
Player: 5000
Lottery man: So, your lottery will be named TheBestLotteryInTheWorld with 5 available tickets with the price of 5000 each ticket and the reward will be 1x Boots of haste, you need to pay 25000 gold coins to active the lottery, of course this money will be reembolsed when the lottery ends. Do you want to active the lottery now?
Player: yes
Lottery man: Lottery was added, thanks.
--Buying a ticket
Player: buy
Lottery man: Please tell me the name of the lottery that do you want to buy tickets.
Player: TheBestLotteryInTheWorld
Lottery man: TheBestLotteryInTheWorld eh?, do you want to buy a ticket for 5000 gold coins?.'
Player: yes
Lottery man: Now wait until the lottery finish to know the result.
--Player can buy only one ticket.
Now, globalevent will check for lotteries with all tickets sold, if the winner is online, it will get the reward, else, it'll get the reward when logs in.
Same think with the lottery owner, it'll receive the money used and the money from the tickets.
And that's all
Last edited: