Advertisement
Stawlie0

Untitled

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