Stawlie0

Untitled

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