Here i'm releasing my own lottery system.
Update Log:
Features:
First, execute this with MySQL
Add these functions to data/globalevents/lib/globalevents.lua and data/npc/lib/npc.lua, DO NOT ADD THESE FUNCTIONS IN data/lib/function.lua
Now add these functions to data/lib/function.lua
Go to data/globalevents/scripts and create a file called lottery.lua and paste this:
Now paste this in data/globalevents/globalevents.xml
Now.. go to data/npc and create a file called Lottery Man.xml and paste this:
Go to data/npc/scripts and create a file called lottery.lua and paste this:
Now paste this in your login.lua
Remember to change the "offStor" in globalevents/scripts/lottery.lua and login.lua, maxTickets for a lottery can be changed in npc/scripts/lottery.lua
Please report bugs here..
Update Log:
Code:
None.
Features:
Code:
- Posibility to create your lottery talking with the Lottery Man.
- Player can choose all the tickets they will be available.
- Player can choose the price of each ticket.
- The reward will be the item that you have in your Left-Hand. (Only one item)
- If winner is online, he receive the prize immediately, if isn't online, receive his prize when he connects.
- The lottery owner will pay the price of all tickets to active the lottery, when the lottery ends the money will be reimbursed.
- If the owner is online, he recieve the money inmediately, if heisn't online, the money will be added to her bank balance.
- There will no be two lotteries with the same name.
- Npc will show the lottery list.
- All the lotteries will have a maximum of 100 tickets available.
- Lotteries will be checked each 3 hours (You can change "interval" in globalevents)
First, execute this with MySQL
PHP:
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',
`price` int(11) NOT NULL DEFAULT '0',
`player_id` int(11) NOT NULL DEFAULT '0',
`buyed_tickets` int(11) NOT NULL DEFAULT '0',
`reward` 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` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`user_id` varchar(32) NOT NULL DEFAULT '',
`lottery_id` int(11) NOT NULL DEFAULT '0',
`owner_id` int(11) NOT NULL DEFAULT '0',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
ALTER TABLE `players` ADD `lottery_balance` INT(11) NOT NULL DEFAULT 0;
Add these functions to data/globalevents/lib/globalevents.lua and data/npc/lib/npc.lua, DO NOT ADD THESE FUNCTIONS IN data/lib/function.lua
Lua:
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 getLotteryName(id)
local query = db.getResult("SELECT `name` FROM `lottery` WHERE `id` = " .. id .. " LIMIT 1")
if(query:getID() ~= -1) then
local t = query:getDataString("name")
return t
end
query:free()
return LUA_ERROR
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 getLotteryOwnerId(id)
local query = db.getResult("SELECT `player_id` FROM `lottery` WHERE `id` = " .. id .. " LIMIT 1")
if(query:getID() ~= -1) then
local t = query:getDataInt("player_id")
return t
end
query:free()
return LUA_ERROR
end
function getLotteryPrice(id)
local query = db.getResult("SELECT `price` FROM `lottery` WHERE `id` = " .. id .. " LIMIT 1")
if(query:getID() ~= -1) then
local t = query:getDataInt("price")
return t
end
query:free()
return LUA_ERROR
end
function getLotteryTickets(id)
local query = db.getResult("SELECT `tickets` FROM `lottery` WHERE `id` = " .. id .. " LIMIT 1")
if(query:getID() ~= -1) then
local t = query:getDataInt("tickets")
return t
end
query:free()
return LUA_ERROR
end
function getLotteryBuyedTickets(id)
local query = db.getResult("SELECT `buyed_tickets` FROM `lottery` WHERE `id` = " .. id .. " LIMIT 1")
if(query:getID() ~= -1) then
local t = query:getDataInt("buyed_tickets")
return t
end
query:free()
return LUA_ERROR
end
function getLotteryReward(id)
local query = db.getResult("SELECT `reward` FROM `lottery` WHERE `id` = " .. id .. " LIMIT 1")
if(query:getID() ~= -1) then
local t = query:getDataInt("reward")
return t
end
query:free()
return LUA_ERROR
end
function getLotteryWinner(id)
local query = db.getResult("SELECT `winner` FROM `lottery` WHERE `id` = " .. id .. " LIMIT 1")
if(query:getID() ~= -1) then
local t = query:getDataInt("winner")
return t
end
query:free()
return LUA_ERROR
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, enabled)
if enabled == TRUE then
enabled = 1
elseif enabled == FALSE then
enabled = 0
else
return LUA_ERROR
end
return db.executeQuery("INSERT INTO `lottery` (`name`, `tickets`, `price`, `player_id`, `buyed_tickets`, `reward`, `winner`, `enabled`) VALUES (" .. db.escapeString(name) .. ", " .. tickets .. ", " .. price .. ", " .. playerid .. ", " .. buyedtickets .. ", " .. reward .. ", `winner` = '', " .. enabled .. ");")
end
function doUpdateLottery(lotteryid, name, tickets, buyedtickets, price, playerid, reward, winnerid, enabled)
if enabled == TRUE then
enabled = 1
elseif enabled == FALSE then
enabled = 0
else
return LUA_ERROR
end
return db.executeQuery("UPDATE `lottery` SET `name` = " .. db.escapeString(name) .. ", `tickets` = " .. tickets .. ", `price` = " .. price .. ", `player_id` = " .. playerid .. ", `buyed_tickets` = " .. buyedtickets .. ", `reward` = " .. reward .. ", `winner` = " .. winnerid .. ", `enabled` = " .. enabled .. " WHERE `id` = " .. lotteryid .. ";")
end
function doAddLotteryUser(cid, lotteryid, ownerid)
return db.executeQuery("INSERT INTO `lottery_users` (`user_id`, `lottery_id`, `owner_id`) VALUES (" .. getPlayerGUID(cid) .. ", " .. lotteryid .. ", " .. ownerid .. ");")
end
function doResetLotteryUser(lotteryid)
return db.executeQuery("DELETE FROM `lottery_users` WHERE `lottery_id` = " .. lotteryid .. ";")
end
function getPlayerLotteryBalance(playerid)
local query = db.getResult("SELECT `lottery_balance` FROM `players` WHERE `id` = " .. playerid .. ";")
if query:getID() ~= -1 then
local b = query:getDataInt("lottery_balance")
return b
end
query:free()
return LUA_ERROR
end
function doPlayerSetLotteryBalance(playerid, balance)
return db.executeQuery("UPDATE `players` SET `lottery_balance` = " .. balance .. " WHERE `id` = " .. playerid .. ";")
end
Now add these functions to data/lib/function.lua
Lua:
function db.doPlayerSetStorageValue(playerid, key, value)
return db.executeQuery("INSERT INTO `player_storage` (`player_id`, `key`, `value`) VALUES (" .. playerid .. ", " .. key .. ", " .. value .. ");")
end
function isOnline(name)
if playerExists(name) then
local Info = db.getResult("SELECT `online` FROM `players` WHERE `id` = " .. getPlayerGUIDByName(name) .. " LIMIT 1")
if Info:getID() ~= LUA_ERROR then
local online = Info:getDataInt("online")
if online == 1 then
Info:free()
return TRUE
end
end
return LUA_ERROR
end
end
Go to data/globalevents/scripts and create a file called lottery.lua and paste this:
Lua:
function onThink(interval, lastExecution, thinkInterval)
local tickets = 0
local strings = {""}
local i = 1
local position = 1
local name = ""
local tickets = 0
local buyedTickets = 0
local price = 0
local owner = 0
local reward = 0
local id = 0
local offStor = 65535
doReloadInfo(RELOAD_GLOBALEVENTS)
for _, lid in ipairs(getLotteryList()) do
name = getLotteryName(lid)
tickets = getLotteryTickets(lid)
buyedTickets = getLotteryBuyedTickets(lid)
price = getLotteryPrice(lid)
owner = getLotteryOwnerId(lid)
reward = getLotteryReward(lid)
id = lid
if(i > (position * 7)) then
strings[position] = strings[position] .. ","
position = position + 1
strings[position] = ""
else
strings[position] = i == 1 and "" or strings[position] .. ", "
end
strings[position] = strings[position] .. "Lottery [" .. name .. "] has ended, the winner have received the reward"
i = i + 1
end
if (i - 1) > 0 then
if tickets == buyedTickets then
local users = getLotteryUsers(id)
local winner = users[math.random(1, table.maxn(users))]
doUpdateLottery(id, name, tickets, buyedTickets, price, owner, reward, winner, FALSE)
doResetLotteryUser(id)
if isOnline(getPlayerNameByGUID(winner)) then
doPlayerAddItem(getPlayerByName(getPlayerNameByGUID(getLotteryWinner(id))), reward, 1)
doPlayerPopupFYI(getPlayerByName(getPlayerNameByGUID(getLotteryWinner(id))), "Congratulations!, you win in the lottery [" .. name .. "]\n\nReward: " .. getItemNameById(reward) .. ".")
else
db.doPlayerSetStorageValue(winner, offStor, reward)
end
if isOnline(getPlayerNameByGUID(getLotteryOwnerId(id))) then
doPlayerAddMoney(getPlayerByName(getPlayerNameByGUID(getLotteryOwnerId(id))), getPlayerLotteryBalance(getLotteryOwnerId(id)))
doPlayerSetLotteryBalance(getLotteryOwnerId(id), 0)
else
doPlayerSetBalance(getPlayerByName(getPlayerNameByGUID(getLotteryOwnerId(id))), getPlayerLotteryBalance(getLotteryOwnerId(id)))
doPlayerSetLotteryBalance(getLotteryOwnerId(id), 0)
end
for i, str in ipairs(strings) do
if(str:sub(str:len()) ~= ",") then
str = str .. "."
end
doBroadcastMessage(str)
end
end
end
return TRUE
end
Now paste this in data/globalevents/globalevents.xml
Lua:
<globalevent name="lottery" interval="10800" event="script" value="lottery.lua"/>
Now.. go to data/npc and create a file called Lottery Man.xml and paste this:
Lua:
<?xml version="1.0"?>
<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>
Go to data/npc/scripts and create a file called lottery.lua and paste this:
Lua:
local keywordHandler = KeywordHandler:new()
local npcHandler = NpcHandler:new(keywordHandler)
NpcSystem.parseParameters(npcHandler)
local talkState = {}
local voc = {}
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 name = ''
local tickets = 0
local price = 0
local reward = 0
local buyingPrice = 0
local buyingName = ''
local buyingTickets = 0
local list = getLotteryList()
local maxTickets = 100
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
doReloadInfo(RELOAD_NPCS)
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
strings[position] = strings[position] .. '{' .. getLotteryName(lid) .. '} [By: {' .. getPlayerNameByGUID(getLotteryOwnerId(lid)) .. '}, Able Tickets: {' .. getLotteryTickets(lid) - getLotteryBuyedTickets(lid) .. '}, Price: {' .. getLotteryPrice(lid) .. '}, Reward: {' .. getItemNameById(getLotteryReward(lid)) .. '}]'
i = i + 1
end
if (i - 1) > 0 then
selfSay('Reading the lottery list... {' .. i - 1 .. '} lotteries founded.', cid)
for i, str in ipairs(strings) do
if(str:sub(str:len()) ~= '-') then
str = str .. '.'
end
selfSay(str, cid)
end
talkState[talkUser] = 0
else
selfSay('There is not any lottery.', cid)
talkState[talkUser] = 0
end
elseif msgcontains(msg, 'new') then
selfSay('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 = msg
local query = db.getResult("SELECT `name` FROM `lottery` WHERE `name` = " .. db.escapeString(name) .. ";")
if query:getID() == -1 then
selfSay('Now tell me how many tickets will be available.', cid)
talkState[talkUser] = 2
else
selfSay('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
selfSay('Please, tell me how many tickets will be available.', cid)
talkState[talkUser] = 2
elseif tonumber(msg) > maxTickets then
selfSay('Sorry, you only can active a lottery with a maximun of ' .. maxTickets .. ' tickets.', cid)
talkState[talkUser] = 2
else
tickets = tonumber(msg)
tickets = math.abs(tickets)
selfSay('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
selfSay('Please, tell me the price of each ticket.', cid)
talkState[talkUser] = 3
else
if getPlayerSlotItem(cid, CONST_SLOT_LEFT).itemid < 1 then
selfSay('Remember to put an item in your left-hand.', cid)
talkState[talkUser] = 0
else
price = math.abs(tonumber(msg))
price = math.abs(price)
reward = getPlayerSlotItem(cid, CONST_SLOT_LEFT).itemid
selfSay('So, your lottery will be named {' .. name .. '} with {' .. tickets .. '} available tickets with the price of {' .. price .. '} each ticket and the reward will be {' .. getItemArticleById(getPlayerSlotItem(cid, CONST_SLOT_LEFT).itemid) .. ' ' .. getItemName(getPlayerSlotItem(cid, CONST_SLOT_LEFT).uid) .. '}, you need to pay {' .. tickets * price .. '} 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 doPlayerRemoveMoney(cid, tickets * price) == TRUE then
if doPlayerRemoveItem(cid, getPlayerSlotItem(cid, CONST_SLOT_LEFT).itemid, 1) == TRUE then
selfSay('Lottery was added, thanks.', cid)
doAddLottery(name, tickets, 0, price, getPlayerGUID(cid), reward, TRUE)
doPlayerSetLotteryBalance(getPlayerGUID(cid), tickets * price)
doReloadInfo(RELOAD_NPCS)
talkState[talkUser] = 0
else
selfSay('Remember to put the item in your left-hand.', cid)
talkState[talkUser] = 0
end
else
selfSay('You do not have enough money.', cid)
talkState[talkUser] = 0
end
end
if msgcontains(msg, 'buy') then
selfSay('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 = msg
if getLotteryIdByName(buyingName) > 0 then
if (getLotteryTickets(getLotteryIdByName(buyingName)) - getLotteryBuyedTickets(getLotteryIdByName(buyingName))) > 0 then
selfSay('' .. buyingName .. ' eh?, how many tickets do you want to buy?.', cid)
talkState[talkUser] = 6
else
selfSay('Sorry, all tickets was selled.', cid)
talkState[talkUser] = 0
end
else
selfSay('There is not any lottery with that name.', cid)
talkState[talkUser] = 5
return true
end
elseif talkState[talkUser] == 6 then
buyingTickets = tonumber(msg)
buyingPrice = getLotteryPrice(getLotteryIdByName(buyingName))
if buyingTickets and buyingTickets > 0 then
buyingTickets = math.abs(buyingTickets)
if (getLotteryBuyedTickets(getLotteryIdByName(buyingName)) + buyingTickets) <= getLotteryTickets(getLotteryIdByName(buyingName)) then
selfSay('Do you want to buy ' .. buyingTickets .. ' tickets for ' .. buyingTickets * buyingPrice .. ' gold pieces?.', cid)
talkState[talkUser] = 7
else
selfSay('There are not too many tickets available.', cid)
talkState[talkUser] = 0
end
else
selfSay('How many tickets do you want to buy?.', cid)
talkState[talkUser] = 6
end
elseif msgcontains(msg, 'yes') and talkState[talkUser] == 7 then
if doPlayerRemoveMoney(cid, buyingTickets * buyingPrice) == TRUE then
selfSay('Now wait until the lottery finish to know the result.', cid)
doUpdateLottery(getLotteryIdByName(buyingName), getLotteryName(getLotteryIdByName(buyingName)), getLotteryTickets(getLotteryIdByName(buyingName)), getLotteryBuyedTickets(getLotteryIdByName(buyingName)) + buyingTickets, getLotteryPrice(getLotteryIdByName(buyingName)), getLotteryOwnerId(getLotteryIdByName(buyingName)), getLotteryReward(getLotteryIdByName(buyingName)), getLotteryWinner(getLotteryIdByName(buyingName)), TRUE)
for i = 1, buyingTickets do
doAddLotteryUser(cid, getLotteryIdByName(buyingName), getLotteryOwnerId(getLotteryIdByName(buyingName)))
end
talkState[talkUser] = 0
else
selfSay('You do not have enough money.', cid)
talkState[talkUser] = 0
end
end
end
npcHandler:setCallback(CALLBACK_MESSAGE_DEFAULT, creatureSayCallback)
npcHandler:addModule(FocusModule:new())
Now paste this in your login.lua
Lua:
local offStor = 65535
if getPlayerStorageValue(cid, offStor) > 0 then
doPlayerAddItem(cid, getPlayerStorageValue(cid, offStor), 1)
doPlayerPopupFYI(cid, "You win a lottery!\n\nReward: " .. getItemNameById(getPlayerStorageValue(cid, offStor)) .. ".")
doPlayerSetStorageValue(cid, offStor, 0)
end
Remember to change the "offStor" in globalevents/scripts/lottery.lua and login.lua, maxTickets for a lottery can be changed in npc/scripts/lottery.lua
Please report bugs here..