Codigo de Sobres (Terminado ?)

mail@pastecode.io avatar
unknown
lua
14 days ago
3.0 kB
2
Indexable
Never
drop_slots = {
    -- 1 Singular
    {
        total = 17,
        target = 1,
        shuffle = {}
    },
    -- 2 Legendary
    {
        total = 57,
        target = 1,
        shuffle = {}
    }, 
    -- 3 Epic
    {
        total = 59,
        target = 1,
        shuffle = {}
    }, 
    -- 4 Rare
    {
        total = 60,
        target = 3,
        shuffle = {}
    }, 
    -- 5 Common
    {
        total = 114,
        target = 6,
        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[2].target = 0 + bool_to_number(slot1legendary)
        drop_slots[3].target = 1 + bool_to_number(slot2epic) - bool_to_number(slot1legendary)
        drop_slots[4].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