Stawlie0

Untitled

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