CCCPaste
local caller = getScriptCaller()

local fart_types = {
  "тихий, но смертоносный 💨",
  "мощный, с эхом по всей комнате 💥",
  "протяжный, как труба судного дня 📯",
  "осторожный, но подозрительно вонючий 🐍",
  "короткий, но звонкий как выстрел 🎯",
  "неожиданный, даже для тебя самого 😳",
  "с душой, прямо от сердца ❤️💨",
  "легендарный, оставивший след в истории 🏆",
  "пропитанный гордостью и фасолью 🫘",
  "едва слышный, но смертельно токсичный ☠️"
}

local fart_smells = {
  {name = "свежий, как утренняя роса 🌸", toxicity = 0},
  {name = "немного подозрительный 😶‍🌫️", toxicity = 1},
  {name = "резкий, с привкусом фасоли 🫘", toxicity = 2},
  {name = "зловонный, заставил всех отвернуться 🤢", toxicity = 3},
  {name = "токсичный, зафиксирован на радарах НАТО ☣️", toxicity = 4},
  {name = "радиоактивный, оставил ожоги воздуха ☢️", toxicity = 5}
}

function signRoll(num)
  local n = math.random(1,10)
  if n <= 7 then
    return num
  end
  return -num
end

function getDelta()
  local t = {}
  t.current = getLocalData(tostring(caller.id))
  if not t.current then t.current = 0 end
  t.delta = math.random(0, 10)
  t.delta = signRoll(t.delta)
  if t.current + t.delta < 0 then
    t.delta = -t.current
    t.current = 0
  else
    t.current = t.current + t.delta
  end
  setLocalData(tostring(caller.id), t.current)
  return t
end

function updateTime()
  setLocalData(tostring(caller.id) .. 'time', os.time())
end

function checkTime()
  local time = getLocalData(tostring(caller.id) .. 'time')
  if not time then
    setLocalData(tostring(caller.id), 0)
    updateTime()
    return true
  end
  if time and os.time() - tonumber(time) > 24*60*60 then
    updateTime()
    return true
  end
  return false 
end

function fart()
  if isChat() then
    local tag = ''
    if caller.username then
      tag = '@' .. caller.username .. ' '
    elseif caller.first_name then
      tag = caller.first_name .. ' '
    end
    if checkTime() then
      local t = getDelta()
      local fart_type = fart_types[math.random(1, #fart_types)]
      local fart_smell = fart_smells[math.random(1, #fart_smells)]
      
      local str = tag
      if t.delta < 0 then
        str = str .. 'сегодня пукнул слабее на ' .. -t.delta .. ' очков 💨\n'
      elseif t.delta == 0 then
        str = str .. 'сегодня не пукал. 😶\n'
      else
        str = str .. 'пукнул сильнее на ' .. t.delta .. ' очков! 💪\n'
      end
      
      str = str .. 'Тип пука: ' .. fart_type .. '\n'
      str = str .. 'Запах: ' .. fart_smell.name .. '\n'
      str = str .. 'Токсичность: ' .. fart_smell.toxicity .. '/5 ☠️\n'
      str = str .. 'Общий рейтинг пуков: ' .. t.current .. ' 💨'
      return str
    else
      return os.date(tag .. 'ты уже пукал сегодня! Возвращайся через %H часов %M минут 💨', 45*60*60 - os.time() + tonumber(getLocalData(tostring(caller.id .. 'time'))))
    end
  else
    return 'Юзай в чате, чтобы все услышали твой пук! 💨'
  end
end

function printTop()
  local t = getAllUserData()
  local s = '💨 ТОП ПУКАНОВ ЧАТА 💨\n\n'
  if not getUserCount() then
    return 'Используй в конфе!'
  end
  for i = 1, getUserCount() do
    if t[i] and t[i].id and getLocalData(tostring(t[i].id)) then
      t[i].score = tonumber(getLocalData(tostring(t[i].id)))
    else
      t[i] = nil
    end
  end
  local c = {}
  for i = 1, getUserCount() do
    if t[i] then
      table.insert(c, t[i])
    end
  end
  table.sort(c, function(a,b) return a.score > b.score end)
  for i = 1, #c do
    if c[i].first_name then
      s = s .. i .. '. ' .. c[i].first_name .. ' — ' .. c[i].score .. ' очков пука 💨\n'
    end
  end
  return s
end

if _ARGS == 'top' or _ARGS == 'топ' then
  return printTop()
end

return fart()