Untitled

mail@pastecode.io avatar
unknown
lua
16 days ago
3.1 kB
2
Indexable
Never
turnCounter = 0

function gameSetup()
    if #getSeatedPlayers() == 1 then
        print('Solo Mode Baybeee') -- this hasn't been started
    elseif #getSeatedPlayers() < 4 then
        Wait.time(function () restock() end, 0.7)
        randomizeTurnOrderAndEnableTurns()
    else
        Wait.time(function () restock() end, 0.7)
        fourPlayerChanges()
        randomizeTurnOrderAndEnableTurns()
    end
    removeSetupItems() -- Destorys unused assets
end

function onPlayerTurn(player, previous_player)
    Wait.time(function () restock() end, 0.1) -- Restocks any missing cards from the middle

    if previous_player == nil then
        print("this should only print once")
        turnCounter = 0
    end

    turnCounter = turnCounter + 1
    -- Check if it's the end of a round
    if turnCounter > 10 then 
        print("turn counter has matured to " .. turnCounter .. " time for a new round" )
        nextRound()
    end

end

function nextRound()
    -- Reset turn counter at the start of each round
    turnCounter = 1
    
    -- Move object from first player to next player in turn order
    local objectGuid = "03f05d"
    local firstPlayer = Turns.order[1]
    local nextPlayerIndex = 2  -- Index of the next player in turn order

    -- If there is only one player, just return
    if #Turns.order < 2 then
        return
    end

    -- Move the object from the first player to the next player
    local firstPlayerHand = Player[firstPlayer].getHandObjects()
    for _, obj in ipairs(firstPlayerHand) do
        if obj.getGUID() == objectGuid then
            obj.deal(1, Turns.order[nextPlayerIndex])
            break
        end
    end

    -- Adjust the turn order for the new round
    local newTurnOrder = {}
    for i = 2, #Turns.order do
        table.insert(newTurnOrder, Turns.order[i])
    end
    table.insert(newTurnOrder, firstPlayer)

    -- Set the new turn order
    Turns.order = newTurnOrder

    -- Enable turn for the new first player
    Turns.enable = true
    Turns.turn_color = Turns.order[1]

    print("Round Complete")
end

function randomizeTurnOrderAndEnableTurns()
    local playerColors = {}  -- Table to store player colors

    -- Get all player colors
    for _, player in ipairs(Player.getPlayers()) do
        if player.seated then
            table.insert(playerColors, player.color)
        end
    end

    -- Randomize player order
    for i = #playerColors, 2, -1 do
        local j = math.random(1, i)
        playerColors[i], playerColors[j] = playerColors[j], playerColors[i]
    end

    -- Set the turn order using Turns.order
    Turns.order = playerColors

    -- Get the first player color after shuffling
    local firstPlayerColor = playerColors[1]

    -- Enable turn for the first player
    Turns.enable = true
    broadcastToAll(playerColors[1] .. " Is the first player", Pink)

    local objectGuid = "03f05d"
    local playerHand = Player[firstPlayerColor].getHandObjects()
    if playerHand == nil or #playerHand == 0 then
        getObjectFromGUID(objectGuid).deal(1, firstPlayerColor)
    end
end

Leave a Comment