Stawlie0

Untitled

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