Advertisement
Guest User

Untitled

a guest
Jun 13th, 2020
164
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 16.76 KB | None | 0 0
  1. script_name('NEWS-HELPER')
  2. script_author("Serhiy_Rubin")
  3.  
  4. local inicfg, sampev, milisec, vkeys = require 'inicfg', require 'lib.samp.events', require 'socket', require 'lib.vkeys'
  5. require 'lib.sampfuncs'
  6. require 'lib.moonloader'
  7. local antiflood = os.clock() * 1000 ; timer = os.clock() ; adid = 21 ; arrayAD = {} ; ad_check = 0
  8.  
  9. function main()
  10.     if not isSampLoaded() or not isSampfuncsLoaded() then return end
  11.     while not isSampAvailable() do wait(100) end
  12.     repeat wait(0) until sampGetCurrentServerName() ~= 'SA-MP'
  13.     repeat
  14.         wait(0)
  15.         for id = 0, 2303 do
  16.             if sampTextdrawIsExists(id) and sampTextdrawGetString(id):find('Samp%-Rp.Ru') then
  17.                 samp_rp = true
  18.             end
  19.         end
  20.     until samp_rp ~= nil
  21.     _, my_id = sampGetPlayerIdByCharHandle(PLAYER_PED)
  22.     my_nick = sampGetPlayerNickname(my_id)
  23.     ini = inicfg.load({
  24.         Settings = {
  25.             send_number = 0,
  26.             mili_time = 100,
  27.             ad_cena = 100
  28.         },
  29.         Activation = {
  30.             [my_nick] = false,
  31.         },
  32.         Auto = {
  33.             [my_nick] = false,
  34.         },
  35.         Render = {
  36.             FontName='Segoe UI',
  37.             FontSize=10,
  38.             FontFlag=15,
  39.             Color='FFFFFF'
  40.         },
  41.         Pos = {
  42.             X = 0,
  43.             Y = 0
  44.         },
  45.         Key = {
  46.             read = 'VK_1',
  47.             send = 'VK_3',
  48.             auto = 'VK_4',
  49.             ad_list = 'VK_N',
  50.             check_ad = 'VK_O'
  51.         }
  52.     })
  53.     inicfg.save(ini)
  54.     font = renderCreateFont(ini.Render.FontName, ini.Render.FontSize, ini.Render.FontFlag)
  55.     -- Auto-Send settings
  56.     last_send = tonumber(os.date("%S")) ; next_second = math.fmod(last_send + 16, 60) ; status = false
  57.     if math.fmod(next_second, 2) ~= ini.Settings.send_number then
  58.         next_second = next_second - 1
  59.     end
  60.     while true do
  61.         wait(0)
  62.         if ini.Activation[my_nick] then
  63.             doAutoSend()
  64.             doRender()
  65.             doKeyCheck()
  66.             doPos()
  67.             doSetKey()
  68.         end
  69.     end
  70. end
  71.  
  72. -- main
  73. function doAutoSend()
  74.     local this_second = tonumber(os.date("%S"))
  75.     local this_milisecond = tonumber(tostring(milisec.gettime()):sub(12, 14))
  76.     if this_milisecond ~= nil and this_milisecond ~= 999 and this_second  == next_second and this_milisecond > ini.Settings.mili_time then
  77.         status = true
  78.     end
  79.     if ini.Auto[my_nick] and status and (os.clock() * 1000 - antiflood) > 1000 then
  80.         sampSendChat("/adsend "..adid)
  81.     end
  82. end
  83.  
  84. function doRender()
  85.     if sampIsScoreboardOpen() or not sampIsChatVisible() or isKeyDown(116) or isKeyDown(121) then return end
  86.     local S1 = os.clock() * 1000
  87.     local S2 = timer * 1000
  88.     local S3 = (S1 - S2) / 1000
  89.     local autot = (ini.Auto[my_nick] and '!' or '')
  90.     local rttext = string.format("{%s}%s%0.1f", ini.Render.Color, autot, S3)
  91.     renderFontDrawText(font, rttext, ini.Pos.X, ini.Pos.Y, 0xFFFFFFFF)
  92. end
  93.  
  94. function doKeyCheck()
  95.     if (not sampIsDialogActive() or (sampGetCurrentDialogType() ~= 1 and sampGetCurrentDialogType() ~= 3)) and not sampIsChatInputActive() then
  96.         if wasKeyPressed(vkeys[ini.Key.read]) then
  97.             sampSendChat("/adedit "..adid)
  98.         end
  99.         if wasKeyPressed(vkeys[ini.Key.send]) then
  100.             sampSendChat("/adsend "..adid)
  101.         end
  102.         if wasKeyPressed(vkeys[ini.Key.auto]) then
  103.             ini.Auto[my_nick] = not ini.Auto[my_nick]
  104.             inicfg.save(ini)
  105.         end
  106.         if wasKeyPressed(vkeys[ini.Key.ad_list]) then
  107.             list_ad = 1
  108.             lua_thread.create(function()
  109.                 repeat
  110.                     wait(0)
  111.                     if list_ad == 1 and (os.clock() * 1000) - antiflood > 1100 then
  112.                         list_ad = -1
  113.                         sampSendChat('/n')
  114.                     end
  115.                 until list_ad == 0
  116.             end)
  117.         end
  118.         if wasKeyPressed(vkeys[ini.Key.check_ad]) then
  119.             ad_check = 1
  120.             lua_thread.create(function()
  121.                 repeat
  122.                     wait(0)
  123.                     if ad_check == 1 and (os.clock() * 1000) - antiflood > 1100 then
  124.                         ad_check = -1
  125.                         sampSendChat('/n')
  126.                     end
  127.                 until ad_check == 0
  128.             end)
  129.         end
  130.     end
  131. end
  132.  
  133. function doPos()
  134.     if setpos ~= nil then
  135.         sampSetCursorMode(3)
  136.         curX, curY = getCursorPos()
  137.         ini.Pos.X = curX
  138.         ini.Pos.Y = curY
  139.         if isKeyJustPressed(1) then
  140.             sampSetCursorMode(0)
  141.             setpos = nil
  142.             inicfg.save(ini)
  143.         end
  144.     end
  145. end
  146.  
  147. function doSetKey()
  148.     if setkey ~= nil then
  149.         local key = ""
  150.         for k, v in pairs(vkeys) do
  151.             if wasKeyPressed(v) and k ~= "VK_ESCAPE" and k ~= "VK_RETURN" then
  152.                 key = k
  153.             end
  154.         end
  155.         if key ~= '' then
  156.             ini.Key[setkey] = key
  157.             inicfg.save(ini)
  158.             setkey = nil
  159.             Dialog()
  160.             start_dialog(dialog[1])
  161.         end
  162.     end
  163. end
  164.  
  165. -- HOOK
  166.  
  167. function sampev.onSendChat(message) antiflood = os.clock() * 1000 end
  168. function sampev.onSendCommand(cmd) antiflood = os.clock() * 1000
  169.     if cmd:lower() == "/nn" then
  170.         lua_thread.create(function()
  171.             Dialog()
  172.             start_dialog(dialog[1])
  173.         end)
  174.         return false
  175.     end
  176. end
  177.  
  178. function sampev.onServerMessage(color, message)
  179.     if message:find("сотрудник News (.*): (.*)") and my_nick ~= nil then
  180.         if message:find(my_nick) and ini.Auto[my_nick] then
  181.             ini.Auto[my_nick] = false
  182.             inicfg.save(ini)
  183.         end
  184.         last_send = tonumber(os.date("%S"))
  185.         next_second = math.fmod(last_send + 16, 60)
  186.         if math.fmod(next_second, 2) ~= ini.Settings.send_number then
  187.             next_second = next_second - 1
  188.         end
  189.         timer = os.clock()
  190.         status = false
  191.     end
  192.     if message:find('Добавлено новое объявление от .+. Номер объявления (%d+)') then
  193.         local id = tonumber(message:match('Добавлено новое объявление от .+. Номер объявления (%d+)'))
  194.         arrayAD[id] = nil
  195.     end
  196.     if status and message == " Объявления с таким ID не существует" or message == ' Объявление больше не существует' then
  197.         status = false
  198.     end
  199.     if message == ' Не флуди!' then
  200.         if ad_check == -1 then
  201.             ad_check = 1
  202.         end
  203.         if list_ad == -1 then
  204.             list_ad = 1
  205.         end
  206.     end
  207.     if message == ' Вы не репортер' then
  208.         ad_check = 0
  209.         list_ad = 0
  210.         ini.Auto[my_nick] = false
  211.     end
  212. end
  213.  
  214. function sampev.onShowDialog(dialogId, style, title, button1, button2, text)
  215.     if title == 'Репортеры' and text:find('%[3%] Объявления') then -- Меню /n
  216.         if ad_check == -1 then
  217.             ad_check = 2
  218.             ThreadSendDialog(90, dialogId, 1, 3, '[3] Объявления')
  219.             lua_thread.create(function()
  220.                 checking, checkall = 0, 0
  221.                 repeat wait(0)
  222.                     local text = 'Checking '..checking..'/'..checkall
  223.                     local x, y = getScreenResolution()
  224.                     x = x - renderGetFontDrawTextLength(font, text)
  225.                     y = y - renderGetFontDrawHeight(font)
  226.                     renderFontDrawText(font, text, x, y, -1)
  227.                 until ad_check == 0
  228.             end)
  229.             return false
  230.         end
  231.         if list_ad == -1 then
  232.             list_ad = 0
  233.             ThreadSendDialog(90, dialogId, 1, 3, '[3] Объявления')
  234.             return false
  235.         end
  236.     end
  237.     if title == 'Объявления' and text:find('%[id: ') then -- Объявления
  238.         local array = split(text, '\n')
  239.         local dialogText = ''
  240.         if ad_check == 2 then
  241.             dialogArray = {}
  242.         end
  243.         for k,v in pairs(array) do
  244.             local id = tonumber(v:match('%[id: (%d+)%]'))
  245.             if id ~= nil then
  246.                 if ad_check == 2 and not v:find('Проверил') and arrayAD[id] == nil then
  247.                     checkall = checkall + 1
  248.                     dialogArray[#dialogArray + 1] = { text = v, list = k-1, id = id}
  249.                 end
  250.                 if arrayAD[id] ~= nil then
  251.                     v = v:gsub('%[id', string.format('{59de00}[%s$]{FFFFFF} [id', arrayAD[id]))
  252.                 end
  253.                 dialogText = string.format('%s%s\n', dialogText, v)
  254.             end
  255.         end
  256.         if ad_check == 2 then ad_check = -2 end
  257.         if ad_check == -2 or ad_check == 3 then
  258.             if dialogArray[#dialogArray] ~= nil then
  259.                 adstring = dialogArray[#dialogArray].text
  260.                 adid = dialogArray[#dialogArray].id
  261.                 ad_check = 3
  262.                 ThreadSendDialog(90, 22, 1, dialogArray[#dialogArray].list, adstring)
  263.                 dialogArray[#dialogArray] = nil
  264.                 return false
  265.             else
  266.                 ad_check = 0
  267.             end
  268.         end
  269.         return {dialogId, style, title, button1, button2, dialogText}
  270.     end
  271.     if title == 'Меню' and text:find('%[0%] Читать') then -- Меню редактировать, отправить
  272.         ShowNewsMenu = 1
  273.         if ad_check == 3 then
  274.             ad_check = -3
  275.             ThreadSendDialog(90, dialogId, 1, 0, '[0] Читать')
  276.             return false
  277.         end
  278.         if ad_check == 4 then
  279.             ad_check = -2
  280.             ThreadSendDialog(90, dialogId, 0, 0, '[0] Читать')
  281.             return false
  282.         end
  283.     end
  284.     if title == 'Сообщение' or title == 'Ввод параметра' and ShowNewsMenu ~= nil then -- Диалог с текстом объявления
  285.         ShowNewsMenu = nil
  286.         if adstring ~= nil and not adstring:find('Проверил') then
  287.             arrayAD[adid] = ((#text * ini.Settings.ad_cena) / 100) * 60
  288.         end
  289.         if ad_check == -3 then
  290.             ad_check = 4
  291.             checking = checking + 1
  292.             ThreadSendDialog(90, dialogId, 0, 0, '')
  293.             return false
  294.         end
  295.     end
  296. end
  297.  
  298. function ThreadSendDialog(sleep, id, button, listitem, input)
  299.     lua_thread.create(function(sleep, id, button, listitem, input)
  300.         wait(sleep)
  301.         sampSendDialogResponse(id, button, listitem, input)
  302.     end, sleep, id, button, listitem, input)
  303. end
  304.  
  305. function sampev.onSendDialogResponse(dialogId, button, listboxId, input)
  306.     -- Получаем ID последнего открытого объявления.
  307.     if string.find(sampGetDialogCaption(), "Объявления") and button == 1 then
  308.         local array = split(sampGetDialogText(), '\n')
  309.         for k,v in pairs(array) do
  310.             if listboxId == (k - 1) then
  311.                 adid = tonumber(string.match(v, "%[id: (%d+)%]"))
  312.                 adstring = input
  313.             end
  314.         end
  315.     end
  316. end
  317.  
  318.  
  319. -- Dialog
  320. function Dialog()
  321. dialog = {
  322.     {
  323.         settings = {title = "NEWS-HELPER" ,style = 4 ,btn1 = "Далее" ,btn2 = "Закрыть" ,forward =  "{ffffff}" ,backwards = "\n" ,score = true},
  324.         {
  325.             {
  326.                 title = "Таймер объявлений\t"..ON_OFF(ini.Activation[my_nick]),
  327.                 click = function(button, list, input , outs)
  328.                     if button == 1 then
  329.                         ini.Activation[my_nick] = not ini.Activation[my_nick]
  330.                         inicfg.save(ini)
  331.                         Dialog()
  332.                         return dialog[1]   
  333.                     end
  334.                 end
  335.             },
  336.             {
  337.                 title = "Сменить позицию таймера\t",
  338.                 click = function(button, list, input , outs)
  339.                     if button == 1 then
  340.                         setpos = 1
  341.                         Dialog()
  342.                         return dialog[1]   
  343.                     end
  344.                 end
  345.             },
  346.             {
  347.                 title = "[AUTO] Условие 1\t"..(ini.Settings.send_number == 0 and 'Чётные' or 'Нечётные'),
  348.                 click = function(button, list, input , outs)
  349.                     if button == 1 then
  350.                         ini.Settings.send_number = (ini.Settings.send_number == 0 and 1 or 0)
  351.                         inicfg.save(ini)
  352.                         Dialog()
  353.                         return dialog[1]   
  354.                     end
  355.                 end
  356.             },
  357.             {
  358.                 title = "[AUTO] Условие 2\t"..ini.Settings.mili_time..' мс.',
  359.                 click = function(button, list, input , outs)
  360.                     if button == 1 then
  361.                         return dialog[2]   
  362.                     end
  363.                 end
  364.             },
  365.             {
  366.                 title = "Кнопка для проверки\t"..ini.Key.read:gsub("VK_", ''),
  367.                 click = function(button, list, input , outs)
  368.                     if button == 1 then
  369.                         setkey = 'read'
  370.                         sampShowDialog(584, "SET POS", "{FFFFFF}Нажмите на нужную клавишу", "Сохранить", "Назад", 0)
  371.                     end
  372.                 end
  373.             },
  374.             {
  375.                 title = "Кнопка для отправки\t"..ini.Key.send:gsub("VK_", ''),
  376.                 click = function(button, list, input , outs)
  377.                     if button == 1 then
  378.                         setkey = 'send'
  379.                         sampShowDialog(584, "SET POS", "{FFFFFF}Нажмите на нужную клавишу", "Сохранить", "Назад", 0)
  380.                     end
  381.                 end
  382.             },
  383.             {
  384.                 title = "Кнопка для авто-отправки\t"..ini.Key.auto:gsub("VK_", ''),
  385.                 click = function(button, list, input , outs)
  386.                     if button == 1 then
  387.                         setkey = 'auto'
  388.                         sampShowDialog(584, "SET POS", "{FFFFFF}Нажмите на нужную клавишу", "Сохранить", "Назад", 0)
  389.                     end
  390.                 end
  391.             },
  392.             {
  393.                 title = "Кнопка для открытия списка объявлений\t"..ini.Key.ad_list:gsub("VK_", ''),
  394.                 click = function(button, list, input , outs)
  395.                     if button == 1 then
  396.                         setkey = 'ad_list'
  397.                         sampShowDialog(584, "SET POS", "{FFFFFF}Нажмите на нужную клавишу", "Сохранить", "Назад", 0)
  398.                     end
  399.                 end
  400.             },
  401.             {
  402.                 title = "Кнопка для проверки стоимости объявлений\t"..ini.Key.check_ad:gsub("VK_", ''),
  403.                 click = function(button, list, input , outs)
  404.                     if button == 1 then
  405.                         setkey = 'check_ad'
  406.                         sampShowDialog(584, "SET POS", "{FFFFFF}Нажмите на нужную клавишу", "Сохранить", "Назад", 0)
  407.                     end
  408.                 end
  409.             },
  410.         },
  411.     },
  412.     {
  413.         settings = {title = "NEWS-TIMER" ,style = 1 ,btn1 = "Сохранить",btn2 = "Назад",forward =  "{ff0000}" ,backwards = "\n" ,score = false},
  414.         {
  415.             text = "{ffffff}Введите миллисекунды от 0 до 1000.",
  416.             {
  417.                 click = function(button, list, input , outs)
  418.                     if button == 0 then
  419.                         return dialog[1]
  420.                     else
  421.                         sampAddChatMessage("Вы написали в диалог вот это: "..input, 0xffcecece)
  422.                         if input:find('%d+') then
  423.                             ini.Settings.mili_time = tonumber(input:match('%d+'))
  424.                             inicfg.save(ini)
  425.                             Dialog()
  426.                             return dialog[1]
  427.                         else
  428.                             sampAddChatMessage("Ошибка. Введите число от 0 до 1000.", 0xffcecece)
  429.                             return dialog[2]
  430.                         end
  431.                     end
  432.                 end
  433.             }
  434.         }
  435.     },
  436. }
  437. end
  438.  
  439. function ON_OFF(bool)
  440.     return (bool and '{45d900}ON' or '{ff0000}OFF')
  441. end
  442.  
  443. -- Function Dialog
  444. function start_dialog(menu)
  445.     function _dialog(menu, id,  outs)
  446.         sampShowDialog(id, menu.settings.title, tbl_split(menu.settings.style, menu, menu.settings.forward ,menu.settings.backwards ,menu.settings.score), menu.settings.btn1, (menu.settings.btn2 ~= nil and menu.settings.btn2 or _), menu.settings.style)
  447.             repeat
  448.                 wait(0)
  449.                 local result, button, list, input = sampHasDialogRespond(id)
  450.                 if result then
  451.                     local out, outs = menu[((menu.settings.style == 0 or menu.settings.style == 1 or menu.settings.style == 3) and 1 or ((list + 1) > #menu[1] and 2 or 1))][((menu.settings.style == 0 or menu.settings.style == 1 or menu.settings.style == 3) and 1 or ((list + 1) > #menu[1] and (list - #menu[1]) + 1  or list + 1))].click(button, list, input, outs)
  452.                     if type(out) == "table" then
  453.                         return _dialog(out, id - 1, outs)
  454.                     elseif type(out) == "boolean" then
  455.                         if not out then
  456.                             return out
  457.                         end
  458.                             return _dialog(menu, id, outs)
  459.                     end
  460.                 end
  461.             until result
  462.     end
  463.  
  464.     function tbl_split(style, tbl, forward ,backwards ,score)
  465.         if style == 2 or style == 4 or style == 5 then
  466.             text = (style == 5 and tbl[1].text.."\n" or "")
  467.             for i, val in ipairs(tbl[1]) do
  468.                 text = text..""..forward..""..(score and "["..i.."]{ffffff} " or "")..""..val.title..""..backwards
  469.             end
  470.             if tbl[2] ~= nil then
  471.                 for _, val in ipairs(tbl[2]) do
  472.                     text = text..""..forward..""..val.title..""..backwards
  473.                 end
  474.             end
  475.             return text
  476.         end
  477.         return tbl[1].text
  478.     end
  479.  
  480.     return _dialog(menu, 1337, outs)
  481. end
  482.  
  483.  
  484. --- Function split
  485. function split(str, delim, plain)
  486.     local tokens, pos, plain = {}, 1, not (plain == false) --[[ delimiter is plain text by default ]]
  487.     repeat
  488.         local npos, epos = string.find(str, delim, pos, plain)
  489.         table.insert(tokens, string.sub(str, pos, npos and npos - 1))
  490.         pos = epos and epos + 1
  491.     until not pos
  492.     return tokens
  493. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement