Codigo de sobres de Jaro

mail@pastecode.io avatar
unknown
lua
6 days ago
2.7 kB
2
Indexable
Never
drop_slots = {
-- 1 Legendary
{
    total = 0, -- Total de Legendarias en JaroTCG
    target = 1,
    shuffle = {}
}, 
-- 2 Epic
{
    total = 0, -- Total de Epicas en JaroTCG
    target = 1,
    shuffle = {}
}, 
-- 3 Rare
{
    total = 0, -- Total de Raras en JaroTCG
    target = 3,
    shuffle = {}
}, 
-- 4 Common
{
    total = 0, -- Total de Comunes en JaroTCG
    target = 6,
    shuffle = {}
}, 
-- 5 Singular
{
    total = 0, -- Total de Singulares en JaroTCG
    target = 1,
    shuffle = {}
}}

function onObjectLeaveContainer(container, leave_object)
    if container ~= self then return end

    local slot1legendary = true;
    local slot2epic = true;

    -- Calculate Chances
    if (math.random(1, 4) == 1) then
        print ("Slot 1 is an Epic")
        -- First Slot will have an Epic
        slot1legendary = false;
    else
        print ("Slot 1 is a LEGENDARY!!!")
    end

    if (math.random(1, 4) == 1) then
        -- Second Slot will have a Rare
        print ("Slot 2 is a rare")
        slot2epic = false;
    else
        print ("Slot 2 is an Epic!")
    end

    drop_slots[1].target = 0 + bool_to_number(slot1legendary)
    drop_slots[2].target = 1 + bool_to_number(slot2epic) - bool_to_number(slot1legendary)
    drop_slots[3].target = 4 - bool_to_number(slot2epic)

    self.destruct() -- Destroy Card Pack

    -- Shuffle Cards within their Rarities
    total = 0
    for k, v in pairs(drop_slots) do
        -- offset indices
        t = 0
        for j = 1, v.total do
            v.shuffle[j] = j - 1 + total
            t = t + 1
        end
        total = total + t
        -- do shuffle
        v.shuffle = shuffleTable(v.shuffle)
    end

    -- Destroy remaining cards outside the target, within each rarity
    for k, v in pairs(drop_slots) do
        for j = 1, v.total - v.target do
            destroyCardAtIndex(leave_object, v.shuffle[j])
        end
    end
end

function bool_to_number(value)
    return value and 1 or 0
end

function destroyCardAtIndex(leave_object, index)
    leave_object.takeObject({index = index}).destruct()
  
    for k,v in pairs(drop_slots) do
      decrementValuesGreaterThan(v.shuffle, index)
    end
  end

function decrementValuesGreaterThan(tbl, num)
    local size = #tbl
    for i = 1, size do
        if tbl[i] > num then
            tbl[i] = tbl[i] - 1
        end
    end
    return tbl
end

function shuffleTable(tbl)
    local size = #tbl
    for i = size, 1, -1 do
        local rand = math.random(i)
        tbl[i], tbl[rand] = tbl[rand], tbl[i]
    end
    return tbl
end
Leave a Comment