Stawlie0

Untitled

Oct 12th, 2023
1,087
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.10 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:search_transposers()
  74.     -- Предварительное создание маппинга элементов для быстрого поиска
  75.     local itemMap = {}
  76.  
  77.     for _, item in ipairs(self.items or {}) do
  78.         itemMap[item.name] = item
  79.     end
  80.  
  81.     for address in component.list("transposer") do
  82.         local trans = component.proxy(address)
  83.         if not trans then return end
  84.  
  85.         local invName = trans.getInventoryName(0)
  86.         if invName == "appliedenergistics2:interface" then
  87.             local size = trans.getInventorySize(0) or 0
  88.             for i = 1, size do
  89.                 local stack = trans.getStackInSlot(0, i)
  90.                 if stack then
  91.                     local item = itemMap[stack.name] or itemMap[stack.label]
  92.                     if item then
  93.                         item.address = address
  94.                         item.slot = i
  95.                     end
  96.                 end
  97.             end
  98.         else -- Интерфейс хранения валюты
  99.             self.addressMoneyChest = address
  100.             if chatbox then chatbox.say("§fСохранил адрес сундука с деньгами: " .. address) end
  101.         end
  102.     end
  103. end
  104.  
  105. function Casino:get_list()
  106.     local result = {}
  107.     table.insert(result, "\n")
  108.     for _, item in ipairs(self.items or {}) do
  109.         local displayName = string.gsub(item.name, "&", "§", 1)
  110.         if item.count == 1 then
  111.             table.insert(result, "§l" .. displayName .. " Шанс: "  .. item.chance * 100 * 10 .. "%")
  112.         else
  113.             table.insert(result, "§l" .. displayName .. " x" .. item.count.. " Шанс: "  .. item.chance * 100 * 10 .. "%")
  114.         end
  115.     end
  116.     local resultString = table.concat(result, "\n")
  117. end
  118.  
  119. function Casino:sendMoney(count)
  120.     component.proxy(self.addressMoneyChest).transferItem(0, 1, count, 1, 1)
  121. end
  122.  
  123. function Casino:generateWeightedArray()
  124.     local weightedArray = {}
  125.     for i, item in ipairs(self.items) do
  126.         -- Умножаем вероятность на 100, чтобы получить целое число для повторений
  127.         local repetitions = math.floor(item.chance * 100)
  128.         for j = 1, repetitions do
  129.             print(self.items.title .. " " .. tonumber(repetitions))
  130.             table.insert(weightedArray, i)  -- сохраняем индекс предмета в массив
  131.         end
  132.     end
  133.     return weightedArray
  134. end
  135.  
  136. function Casino:weightedRoll()
  137.     local weightedArray = self:generateWeightedArray()
  138.     local randomIndex = math.random(1, #weightedArray)
  139.     print("Число индекса: " .. tonumber(randomIndex))
  140.     local itemIndex = weightedArray[randomIndex]
  141.     print(self.items[itemIndex])
  142.     return self.items[itemIndex]
  143. end
  144.  
  145. return Casino
  146.  
  147.  
Advertisement
Comments
  • Kerniol
    172 days (edited)
    # text 0.11 KB | 0 0
    1. <a href="https://google.com">google</a>
    2. [url=https://google.com/]google[/url]
    3. [google](https://google.com/)
Add Comment
Please, Sign In to add comment