Stawlie0

Untitled

Oct 14th, 2023
517
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.58 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:SearchMoney()
  34.     if not self.addressMoneyChest then
  35.         chatbox.say("Address is nil.")
  36.         os.sleep(0.5)
  37.         return nil
  38.     end
  39.     local trans = component.proxy(self.addressMoneyChest)
  40.     if not trans or not trans.getStackInSlot then     -- Проверка успешности получения прокси
  41.         chatbox.say("Не удалось получить прокси компонента для адреса.: " .. tostring(address))
  42.         os.sleep(0.5)
  43.         return nil
  44.     end
  45.     local stack = trans.getStackInSlot(0, 1)
  46.  
  47.     if stack and stack.name == "contenttweaker:money" and stack.size then     -- Проверка наличия стека денег и обновление общего баланса
  48.         return stack.size
  49.     end
  50.     return nil
  51. end
  52.  
  53. function Casino:displayItems()
  54.     for _, item in ipairs(self.items or {}) do
  55.         if item.address == nil then
  56.             chatbox.say("Name: " .. item.name)
  57.             chatbox.say("Title: " .. item.title)
  58.             chatbox.say("count: " .. item.count)
  59.             chatbox.say("-----")
  60.         end
  61.     end
  62. end
  63.  
  64. function Casino:search_transposers()
  65.     local itemMap = {}
  66.  
  67.     for _, item in ipairs(self.items or {}) do
  68.         itemMap[item.name] = item
  69.         if item.title then
  70.             itemMap[item.title] = item
  71.         end
  72.     end
  73.  
  74.     for address in component.list("transposer") do
  75.         local trans = component.proxy(address)
  76.         if not trans then return end
  77.  
  78.         local invName = trans.getInventoryName(0)
  79.         if invName == "appliedenergistics2:interface" then
  80.             local size = trans.getInventorySize(0) or 0
  81.             for i = 1, size do
  82.                 local stack = trans.getStackInSlot(0, i)
  83.                 if stack then
  84.                     local item = itemMap[stack.name] or itemMap[stack.label]
  85.                     if item then
  86.                         item.address = address
  87.                         item.slot = i
  88.                     end
  89.                 end
  90.             end
  91.         else -- Интерфейс хранения валюты
  92.             self.addressMoneyChest = address
  93.             if chatbox then chatbox.say("§fСохранил адрес сундука с деньгами: " .. address) end
  94.         end
  95.     end
  96. end
  97.  
  98. function Casino:get_list()
  99.     local result = {}
  100.     for _, item in ipairs(self.items or {}) do
  101.         local displayName = string.gsub(item.name, "&", "§", 1)
  102.         if item.count == 1 then
  103.             table.insert(result, "§l" .. displayName .. " Шанс: "  .. item.chance * 100 .. "%")
  104.         else
  105.             table.insert(result, "§l" .. displayName .. " x" .. item.count.. " Шанс: "  .. item.chance * 100 .. "%")
  106.         end
  107.     end
  108.     local resultString = table.concat(result, "\n")
  109.     chatbox.say(resultString)
  110. end
  111.  
  112. function Casino:sendMoney(count)
  113.     component.proxy(self.addressMoneyChest).transferItem(0, 1, count, 1, 1)
  114. end
  115.  
  116. function Casino:generateWeightedArray()
  117.     local weightedArray = {}
  118.     for i, item in ipairs(self.items) do
  119.         local repetitions = math.floor(item.chance * 100)         -- Умножаем вероятность на 100, чтобы получить целое число для повторений
  120.         for j = 1, repetitions do
  121.             table.insert(weightedArray, i)
  122.         end
  123.     end
  124.     return weightedArray
  125. end
  126.  
  127. function Casino:weightedRoll()
  128.     local weightedArray = self:generateWeightedArray()
  129.     local randomIndex = math.random(1, #weightedArray)
  130.     local itemIndex = weightedArray[randomIndex]
  131.     return self.items[itemIndex]
  132. end
  133.  
  134. return Casino
Advertisement
Add Comment
Please, Sign In to add comment