Skip to content

Instantly share code, notes, and snippets.

@lyuz1n
Last active July 2, 2020 00:57
Show Gist options
  • Select an option

  • Save lyuz1n/3aa50dfcd31cc083dae163b931068d3d to your computer and use it in GitHub Desktop.

Select an option

Save lyuz1n/3aa50dfcd31cc083dae163b931068d3d to your computer and use it in GitHub Desktop.
Slot Machine (only works in forgottenserver 1.x)
<action uniqueid="48123" script="slot_machine.lua" />
--[[
Slot Machine created by Lyµ (used on superot.com.br)
Discord: Lyµ#8767
Necessary to use this helper: https://gist.github.com/Lyuzera/83e7fde34862140eff8739d2ca3e5d8f
]]
if not slotMachineData then
slotMachineData = {
needItem = {id = 6527, count = 5},
items = vector(7735, 8932, 8929, 8926, 5907),
positions = {
Position(1005, 990, 7),
Position(1006, 990, 7),
Position(1007, 990, 7)
},
-- don't touch here
rolled = vector()
}
end
local positions = slotMachineData.positions
local function drawEffects()
local n = 0
local function decrease()
if slotMachineData.owner then
return
end
local t = 20 - n
if t > 0 then
n = n + 1
for _, position in ipairs(positions) do
position:sendMagicEffect(math.random(CONST_ME_GIFT_WRAPS, CONST_ME_FIREWORK_BLUE))
end
addEvent(decrease, 850)
end
end
decrease()
end
local function checkEquals(itemVec)
local ret = true
for i = 1, itemVec:size() do
if itemVec:get(i) ~= itemVec:get(math.min(itemVec:size(), i + 1)) then
ret = false
break
end
end
return ret, itemVec:front()
end
function onUse(player, item, fromPosition, target, toPosition, isHotkey)
local name = player:getName()
if slotMachineData.owner then
player:sendCancelMessage('Aguarde a sua vez.')
return true
else
local item = slotMachineData.needItem
if not player:removeItem(item.id, item.count) then
player:sendCancelMessage(('Você não possui %dx %s.'):format(item.count, ItemType(item.id):getName()))
return true
end
slotMachineData.owner = name
end
item:transform(item.itemid + 1)
player:getPosition():sendMagicEffect(CONST_ME_MAGIC_GREEN)
fromPosition:sendMagicEffect(CONST_ME_MAGIC_BLUE)
-- clear items
for index, position in ipairs(positions) do
local tile = Tile(position)
if tile then
local item = tile:getTopDownItem()
if item then
item:remove()
end
end
-- delay effect :)
addEvent(function()
position:sendMagicEffect(CONST_ME_MAGIC_GREEN)
end, index * 100)
end
local rolledVec = slotMachineData.rolled
rolledVec:reset()
-- create random items
for index, position in ipairs(positions) do
addEvent(function()
local item = Game.createItem(slotMachineData.items:rand(), 1, position)
rolledVec:emplace_back(item:getId())
position:sendMagicEffect(CONST_ME_FIREATTACK)
end, index * 1000)
end
-- check the result
addEvent(function(name)
local win, rewardId = checkEquals(rolledVec)
local reward = Game.createItem(rewardId, 1)
local player = Player(name)
if not player then
if win then
-- [superot function]
-- Game.sendItem(name, reward, false)
end
else
if win then
local inbox = false
if player:addItemEx(reward) ~= RETURNVALUE_NOERROR then
-- [superot function]
-- Game.sendItem(name, reward, false)
inbox = true
end
-- [superot function]
-- reward:setPeriod(432000)
local rewardName = reward:getName()
player:sendTextMessage(MESSAGE_INFO_DESCR, ('Parabéns, você ganhou 1x %s com duração de 5 dias.%s'):format(rewardName, inbox and ' O item foi enviado para a sua caixa de entrada (inbox) porque você não tinha espaço ou capacidade.' or ''))
Game.broadcastMessage(('[Slot Machine]: %s encontrou 1x %s com duração de 5 dias, que sortudo(a).'):format(name, rewardName), MESSAGE_EVENT_ADVANCE)
end
end
item:transform(item.itemid - 1)
rolledVec:reset()
slotMachineData.owner = nil
local centerPosition = positions[math.ceil(#positions/2)]
if win then
Game.sendAnimatedText('WIN!', centerPosition, 30)
for _, position in ipairs(positions) do
position:sendMagicEffect(CONST_ME_ENERGYAREA)
end
drawEffects()
else
Game.sendAnimatedText('LOSE!', centerPosition, 180)
for _, position in ipairs(positions) do
position:sendMagicEffect(CONST_ME_POFF)
end
end
end, (#positions + 1) * 1000, name)
return true
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment