Stawlie0

Untitled

Oct 12th, 2023
814
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.89 KB | None | 0 0
  1. local Casino = {}
  2.  
  3. local component = require("component")
  4. local chatbox = component.chat_box
  5.  
  6. function Casino:new()
  7.     local newObj = {
  8.         items = {},       -- список предметов, их шансы и дополнительные свойства
  9.         totalChance = 0,   -- общий шанс (сумма всех шансов)
  10.         addressMoneyChest = nil, -- Адрес сундука с деньгами
  11.         goodWin = 20, -- Последний хороший выигрыш
  12.     }
  13.     self.__index = self
  14.     return setmetatable(newObj, self)
  15. end
  16.  
  17. function Casino:addItem(name, title, count, chance)
  18.     table.insert(self.items, {
  19.         name = name,
  20.         title = title,
  21.         count = count,
  22.         chance = chance,
  23.         address = nil,
  24.         slot = nil,
  25.     })
  26.  
  27.     self.totalChance = self.totalChance + chance
  28. end
  29. math.randomseed(os.time())
  30.  
  31. function Casino:roll(chance)
  32.     local randomValue = math.random() * self.totalChance
  33.     local accumulatedChance = 0
  34.  
  35.     if self.goodWin <= 0 then
  36.         self.goodWin = self.goodWin - 1
  37.         self.totalChance = self.totalChance - chance
  38.     end
  39.  
  40.     for _, item in ipairs(self.items) do
  41.         accumulatedChance = accumulatedChance + item.chance
  42.         if randomValue <= accumulatedChance then
  43.             if item.chance == 0.001 then
  44.                 self.goodWin = 100
  45.             end
  46.             return item
  47.         end
  48.     end
  49. end
  50.  
  51. function Casino:SearchMoney()
  52.     if not self.addressMoneyChest then
  53.         chatbox.say("Address is nil.")
  54.         os.sleep(0.5)
  55.         return nil
  56.     end
  57.     local trans = component.proxy(self.addressMoneyChest)
  58.     if not trans or not trans.getStackInSlot then     -- Проверка успешности получения прокси
  59.         chatbox.say("Не удалось получить прокси компонента для адреса.: " .. tostring(address))
  60.         os.sleep(0.5)
  61.         return nil
  62.     end
  63.     local stack = trans.getStackInSlot(0, 1)
  64.  
  65.     if stack and stack.name == "contenttweaker:money" and stack.size then     -- Проверка наличия стека денег и обновление общего баланса
  66.         return stack.size
  67.     end
  68.     return nil
  69. end
  70.  
  71. function Casino:search_transposers()
  72.     -- Предварительное создание маппинга элементов для быстрого поиска
  73.     local itemMap = {}
  74.  
  75.     for _, item in ipairs(self.items or {}) do
  76.         itemMap[item.name] = item
  77.     end
  78.  
  79.     for address in component.list("transposer") do
  80.         local trans = component.proxy(address)
  81.         if not trans then return end
  82.  
  83.         local invName = trans.getInventoryName(0)
  84.         if invName == "appliedenergistics2:interface" then
  85.             local size = trans.getInventorySize(0) or 0
  86.             for i = 1, size do
  87.                 local stack = trans.getStackInSlot(0, i)
  88.                 if stack then
  89.                     local item = itemMap[stack.name] or itemMap[stack.label]
  90.                     if item then
  91.                         item.address = address
  92.                         item.slot = i
  93.                     end
  94.                 end
  95.             end
  96.         else -- Интерфейс хранения валюты
  97.             self.addressMoneyChest = address
  98.             if chatbox then chatbox.say("§fСохранил адрес сундука с деньгами: " .. address) end
  99.         end
  100.     end
  101. end
  102.  
  103. function Casino:get_list()
  104.     local result = {}
  105.     table.insert(result, "\n")
  106.     for _, item in ipairs(self.items or {}) do
  107.         local displayName = string.gsub(item.name, "&", "§", 1)
  108.         if item.count == 1 then
  109.             table.insert(result, "§l" .. displayName .. " Шанс: "  .. item.chance * 100 * 10 .. "%")
  110.         else
  111.             table.insert(result, "§l" .. displayName .. " x" .. item.count.. " Шанс: "  .. item.chance * 100 * 10 .. "%")
  112.         end
  113.     end
  114.     local resultString = table.concat(result, "\n")
  115. end
  116.  
  117. function Casino:sendMoney(count)
  118.     component.proxy(self.addressMoneyChest).transferItem(0, 1, count, 1, 1)
  119. end
  120.  
  121. function Casino:generateWeightedArray()
  122.     local weightedArray = {}
  123.     for i, item in ipairs(self.items) do
  124.         -- Умножаем вероятность на 100, чтобы получить целое число для повторений
  125.         local repetitions = math.floor(item.chance * 100)
  126.         for j = 1, repetitions do
  127.             table.insert(weightedArray, i)  -- сохраняем индекс предмета в массив
  128.         end
  129.     end
  130.     return weightedArray
  131. end
  132.  
  133. function Casino:weightedRoll()
  134.     local weightedArray = self:generateWeightedArray()
  135.     local randomIndex = math.random(1, #weightedArray)
  136.     local itemIndex = weightedArray[randomIndex]
  137.     return self.items[itemIndex]
  138. end
  139.  
  140. return Casino
  141.  
  142.  
Advertisement
Add Comment
Please, Sign In to add comment