local Casino = {} local component = require("component") local chatbox = component.chat_box chatbox.say("Casino: загруженно") function Casino:new() local newObj = { items = {}, -- список предметов, их шансы и дополнительные свойства totalChance = 0, -- общий шанс (сумма всех шансов) addressMoneyChest = nil, -- Адрес сундука с деньгами goodWin = 20, -- Последний хороший выигрыш } self.__index = self return setmetatable(newObj, self) end function Casino:addItem(name, title, count, chance) table.insert(self.items, { name = name, title = title, count = count, chance = chance, address = nil, slot = nil, }) self.totalChance = self.totalChance + chance end math.randomseed(os.time()) function Casino:roll(chance) local randomValue = math.random() * self.totalChance local accumulatedChance = 0 if self.goodWin <= 0 then self.goodWin = self.goodWin - 1 self.totalChance = self.totalChance - chance end for _, item in ipairs(self.items) do accumulatedChance = accumulatedChance + item.chance if randomValue <= accumulatedChance then if item.chance == 0.001 then self.goodWin = 100 end return item end end end function Casino:SearchMoney() if not self.addressMoneyChest then chatbox.say("Address is nil.") os.sleep(0.5) return nil end local trans = component.proxy(self.addressMoneyChest) if not trans or not trans.getStackInSlot then -- Проверка успешности получения прокси chatbox.say("Не удалось получить прокси компонента для адреса.: " .. tostring(address)) os.sleep(0.5) return nil end local stack = trans.getStackInSlot(0, 1) if stack and stack.name == "contenttweaker:money" and stack.size then -- Проверка наличия стека денег и обновление общего баланса return stack.size end return nil end function Casino:search_transposers() -- Предварительное создание маппинга элементов для быстрого поиска local itemMap = {} for _, item in ipairs(self.items or {}) do itemMap[item.name] = item end for address in component.list("transposer") do local trans = component.proxy(address) if not trans then return end local invName = trans.getInventoryName(0) if invName == "appliedenergistics2:interface" then local size = trans.getInventorySize(0) or 0 for i = 1, size do local stack = trans.getStackInSlot(0, i) if stack then local item = itemMap[stack.name] or itemMap[stack.label] if item then item.address = address item.slot = i end end end else -- Интерфейс хранения валюты self.addressMoneyChest = address if chatbox then chatbox.say("§fСохранил адрес сундука с деньгами: " .. address) end end end end function Casino:get_list() local result = {} table.insert(result, "\n") for _, item in ipairs(self.items or {}) do local displayName = string.gsub(item.name, "&", "§", 1) if item.count == 1 then table.insert(result, "§l" .. displayName .. " Шанс: " .. item.chance * 100 * 10 .. "%") else table.insert(result, "§l" .. displayName .. " x" .. item.count.. " Шанс: " .. item.chance * 100 * 10 .. "%") end end local resultString = table.concat(result, "\n") end function Casino:sendMoney(count) component.proxy(self.addressMoneyChest).transferItem(0, 1, count, 1, 1) end function Casino:generateWeightedArray() local weightedArray = {} for i, item in ipairs(self.items) do -- Умножаем вероятность на 100, чтобы получить целое число для повторений local repetitions = math.floor(item.chance * 100) for j = 1, repetitions do print(self.items.title .. " " .. tonumber(repetitions)) table.insert(weightedArray, i) -- сохраняем индекс предмета в массив end end return weightedArray end function Casino:weightedRoll() local weightedArray = self:generateWeightedArray() local randomIndex = math.random(1, #weightedArray) print("Число индекса: " .. tonumber(randomIndex)) local itemIndex = weightedArray[randomIndex] print(self.items[itemIndex]) return self.items[itemIndex] end return Casino