Advertisement
Guest User

Search

a guest
Aug 13th, 2021
195
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 8.04 KB | None | 0 0
  1. script_name("Search helper")
  2. script_version("1.0")
  3.  
  4. --------------------------------------------------------------------------------
  5.  
  6. require "lib.moonloader"
  7. local json = require 'lib.jsoncfg'
  8. local mad = require 'MoonAdditions'
  9. local defaults = {enable = true, showId = true, textPosX = 180, textPosY = 375}
  10. local players = {{id = 666, nickname = ''}}
  11. local chatText = ''
  12. local textMod = {'%/%a+ '}
  13. local selector = 0
  14. local nick_textdraw
  15. local firstDraw = true
  16. local dispNick, dispId = '', ''
  17. local oldTimer, timer
  18. --------------------------------------------------------------------------------
  19.  
  20. function main()
  21.     repeat wait(0) until isSampAvailable()
  22.     -- if not doesFileExist(getGameDirectory().."//moonloader//config//proInv//settings.ini") then
  23.     --  local iniBool = inicfg.save(mainCfg, "proInv/settings")
  24.     -- end
  25.     -- iniData = inicfg.load(mainCfg, "proInv/settings")
  26.  
  27.     if not doesDirectoryExist(getGameDirectory() ..
  28.                                   '//moonloader//config//search') then
  29.         local resultas = createDirectory(
  30.                              getGameDirectory() ..
  31.                                  '//moonloader//config//search')
  32.     end
  33.     if not doesFileExist(getGameDirectory() ..
  34.                              '//moonloader//config//search//settings.json') then
  35.         json.write(getGameDirectory() ..
  36.                        '//moonloader//config//search//settings.json', defaults)
  37.     end
  38.     data = json.read(getGameDirectory() ..
  39.                          '//moonloader//config//search//settings.json')
  40.  
  41.     getAllPlayers()
  42.     oldTimer = getGameTimer()
  43.     while true do
  44.         wait(0)
  45.         local cmdResult = sampIsChatCommandDefined("search")
  46.         if not cmdResult then
  47.             local result = sampRegisterChatCommand("search", toggleScript)
  48.         end
  49.         if data.enable then
  50.             if sampIsChatInputActive() then
  51.                 chatText = sampGetChatInputText()
  52.                 -- printString(chatText, 300)
  53.                 toggleSearch(chatText)
  54.                 showText(true)
  55.             else
  56.                 hideText()
  57.                 dispNick, dispId = '', ''
  58.             end
  59.             timer = getGameTimer()
  60.             if timer > oldTimer + 20000 then
  61.                 getAllPlayers()
  62.                 oldTimer = getGameTimer()            
  63.             end
  64.         end
  65.     end
  66. end
  67.  
  68. --------------------------------------------------------------------------------
  69. -- Komandos registravimas
  70.  
  71. function toggleScript()
  72.     data.enable = not data.enable
  73.     if data.enable then
  74.         sampAddChatMessage(ltu("[Search] Greita žaidėjų paieška įjungta."), 0x1cd031)
  75.     else
  76.         sampAddChatMessage(ltu("[Search] Greita žaidėjų paieška {ff0000}išjungta."), 0x1cd031)
  77.     end
  78.     getAllPlayers()
  79.     writeSetting(false)
  80. end
  81.  
  82. function toggleSearch(text)
  83.     for i = 1, #textMod do
  84.         if string.find(text, textMod[i]) then
  85.  
  86.             local text = string.gsub(text, textMod[i], '')
  87.             -- printString(text, 300)
  88.             -- text = string.gsub(text, ' ', '')
  89.             if text ~= '' then
  90.                 -- printString(text, 150)
  91.                 searchPlayer(text)
  92.                 break
  93.             end
  94.         else
  95.             dispNick, dispId = '', ''
  96.         end
  97.     end
  98. end
  99.  
  100. function searchPlayer(text)
  101.     local text = string.lower(text)
  102.     for i = 1, #players do
  103.         if string.find(string.lower(players[i].nickname), text) then
  104.             -- printString('Radau: ' .. players[i].nickname .. ' id: ' ..
  105.             -- players[i].id, 300)
  106.  
  107.             dispNick = players[i].nickname
  108.             dispId = players[i].id
  109.             break
  110.         elseif i == #players then
  111.             dispNick, dispId = '', ''
  112.         end
  113.        
  114.     end
  115. end
  116.  
  117. function showText(showBool)
  118.     if firstDraw and showBool then
  119.         if dispNick ~= '' and dispId ~= '' then
  120.             firstDraw = false
  121.             nick_textdraw = mad.textdraw.new(dispNick .. ' ID: ' .. dispId,
  122.                                              data.textPosX, data.textPosY)
  123.             nick_textdraw.width = 0.6
  124.             nick_textdraw.height = 0.85
  125.             nick_textdraw:set_text_color(255, 255, 255, 255)
  126.             nick_textdraw.outline = 1
  127.             nick_textdraw.background = false
  128.             nick_textdraw.wrap = 500
  129.         end
  130.     else
  131.         if dispNick ~= '' and dispId ~= '' and nick_textdraw ~= nil then
  132.             nick_textdraw.text = '~r~' .. dispNick .. ' ~w~ID: ~r~' .. dispId
  133.         elseif dispNick == '' and dispId == '' and nick_textdraw ~= nil then
  134.             nick_textdraw.text = ''
  135.         end
  136.     end
  137. end
  138.  
  139. function hideText()
  140.     if not firstDraw and nick_textdraw ~= nil then nick_textdraw.text = '' end
  141. end
  142.  
  143. function getAllPlayers()
  144.     players = {{id = 666, nickname = ''}} -- wipes
  145.     local maxId = sampGetMaxPlayerId(false)
  146.     for i = 0, maxId do
  147.         if sampIsPlayerConnected(i) then
  148.             local nick = sampGetPlayerNickname(i)
  149.             local newEntry = {id = i, nickname = nick}
  150.             table.insert(players, newEntry)
  151.         end
  152.     end
  153. end
  154.  
  155. function getScreenAspect()
  156.     local function gcd(a, b) return b == 0 and a or gcd(b, a % b) end
  157.     local w, h = getScreenResolution()
  158.     local r = gcd(w, h)
  159.     return w / r, h / r
  160. end
  161.  
  162. function sampGetPlayerIdByNickname(nick)
  163.     local result, id = sampGetPlayerIdByCharHandle(PLAYER_PED)
  164.     if nick == sampGetPlayerNickname(id) then return id end
  165.     for i = 0, sampGetMaxPlayerId(false) do
  166.         if sampIsPlayerConnected(i) and sampGetPlayerNickname(i) == nick then
  167.             return i
  168.         end
  169.     end
  170. end
  171.  
  172. --------------------------------------------------------------------------------
  173.  
  174. -- writeSetting(false)
  175. function writeSetting(msgBool)
  176.     json.write(getGameDirectory() ..
  177.                    '//moonloader//config//search//settings.json', data)
  178.     if msgBool then
  179.         sampAddChatMessage(ltu("[Search] Nustatymai įrašyti."), 0x1cd031)
  180.     end
  181. end
  182.  
  183. --[[ Colors:
  184. {ff0000} - raudona
  185. {1cd031} - default zalia kaip serve
  186. {16f534} - zalia, ryskesne
  187. {b88d0f} - oranzine, komandu paaiskinimas
  188. ]]
  189.  
  190. function ltu(text)
  191.     local encoding = require 'encoding'
  192.     encoding.default = 'cp1257'
  193.     local u8 = encoding.UTF8
  194.     local ltu = {
  195.         "\xc4\x85", -- ą
  196.         "\xc4\x8d", "\xc4\x99", "\xc4\x97", "\xc4\xaf", "\xc5\xa1", "\xc5\xb3",
  197.         "\xc5\xab", "\xc5\xbe", "\xc4\x84", "\xc4\x8c", "\xc4\x98", "\xc4\x96",
  198.         "\xc4\xae", "\xc5\xa0", "\xc5\xb2", "\xc5\xaa", "\xc5\xbd"
  199.     }
  200.     -- ąčęėįšųūž ĄČĘĖĮŠŲŪŽ
  201.     if string.find(text, "ą") then text = string.gsub(text, "ą", ltu[1]) end
  202.     if string.find(text, "č") then text = string.gsub(text, "č", ltu[2]) end
  203.     if string.find(text, "ę") then text = string.gsub(text, "ę", ltu[3]) end
  204.     if string.find(text, "ė") then text = string.gsub(text, "ė", ltu[4]) end
  205.     if string.find(text, "į") then text = string.gsub(text, "į", ltu[5]) end
  206.     if string.find(text, "š") then text = string.gsub(text, "š", ltu[6]) end
  207.     if string.find(text, "ų") then text = string.gsub(text, "ų", ltu[7]) end
  208.     if string.find(text, "ū") then text = string.gsub(text, "ū", ltu[8]) end
  209.     if string.find(text, "ž") then text = string.gsub(text, "ž", ltu[9]) end
  210.     if string.find(text, "Ą") then text = string.gsub(text, "Ą", ltu[10]) end
  211.     if string.find(text, "Č") then text = string.gsub(text, "Č", ltu[11]) end
  212.     if string.find(text, "Ę") then text = string.gsub(text, "Ę", ltu[12]) end
  213.     if string.find(text, "Ė") then text = string.gsub(text, "Ė", ltu[13]) end
  214.     if string.find(text, "Į") then text = string.gsub(text, "Į", ltu[14]) end
  215.     if string.find(text, "Š") then text = string.gsub(text, "Š", ltu[15]) end
  216.     if string.find(text, "Ų") then text = string.gsub(text, "Ų", ltu[16]) end
  217.     if string.find(text, "Ū") then text = string.gsub(text, "Ū", ltu[17]) end
  218.     if string.find(text, "Ž") then text = string.gsub(text, "Ž", ltu[18]) end
  219.     text = u8:decode(text)
  220.     return text
  221. end
  222.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement