оптимизация кода

Lance_Sterling

Известный
Автор темы
803
284
Версия MoonLoader
.026-beta
есть ли способ оптимизировать данный код?
если есть, пж дайте обьяснения и чо как где
если кто не понял это чекер
Lua:
local checkerFrame = imgui.OnFrame(
    function() return not isGamePaused() and not isPauseMenuActive() end,
    function(player)
        player.HideCursor = true
        local dl = imgui.GetBackgroundDrawList()
        if mainIni.settings.show_checker and sampGetGamestate() == 3 then
            dl:AddTextFontPtr(Font[20], 20, imgui.ImVec2(checkerPos[1].x - 1, checkerPos[1].y - 20 + 1), 0xFF000000, u8'Игроки онлайн:')
            dl:AddTextFontPtr(Font[20], 20, imgui.ImVec2(checkerPos[1].x + 1, checkerPos[1].y - 20 - 1), 0xFF000000, u8'Игроки онлайн:')
            dl:AddTextFontPtr(Font[20], 20, imgui.ImVec2(checkerPos[1].x - 1, checkerPos[1].y - 20 - 1), 0xFF000000, u8'Игроки онлайн:')
            dl:AddTextFontPtr(Font[20], 20, imgui.ImVec2(checkerPos[1].x + 1, checkerPos[1].y - 20 + 1), 0xFF000000, u8'Игроки онлайн:')
            dl:AddTextFontPtr(Font[20], 20, imgui.ImVec2(checkerPos[1].x, checkerPos[1].y - 20), 0xFFFF8C00, u8'Игроки онлайн:')
            for i = 0, sampGetMaxPlayerId(false) do
                if sampIsPlayerConnected(i) then
                    for k, _ in ipairs(checker) do
                        if checker[k] then
                            if checker[k].NICK == sampGetPlayerNickname(i) then
                                lua_thread.create(function()
                                    wait(1000)
                                    local result = true
                                    for _, v in ipairs(checkerFriends) do
                                        wait(0)
                                        if v and checker[k] then
                                            if v:gsub('%[%d+%]', '') == checker[k].NICK then
                                                result = false
                                            end
                                        end
                                    end
                                    if result and checker[k] then
                                        table.insert(checkerPos, imgui.ImVec2(checkerPos[#checkerPos].x, checkerPos[#checkerPos].y + 20))
                                        table.insert(checkerFriends, checker[k].NICK..'['..i..']')
                                    end
                                end)
                            end
                        end
                    end
                end
            end
            if not MOVE_CHECKER then
                for k, v in ipairs(checkerFriends) do
                    dl:AddTextFontPtr(Font[20], 20, imgui.ImVec2(checkerPos[k].x - 1, checkerPos[k].y + 1), 0xFF000000, v)
                    dl:AddTextFontPtr(Font[20], 20, imgui.ImVec2(checkerPos[k].x + 1, checkerPos[k].y - 1), 0xFF000000, v)
                    dl:AddTextFontPtr(Font[20], 20, imgui.ImVec2(checkerPos[k].x - 1, checkerPos[k].y - 1), 0xFF000000, v)
                    dl:AddTextFontPtr(Font[20], 20, imgui.ImVec2(checkerPos[k].x + 1, checkerPos[k].y + 1), 0xFF000000, v)
                    dl:AddTextFontPtr(Font[20], 20, imgui.ImVec2(checkerPos[k].x, checkerPos[k].y), 0xFFFFFFFF, v)
                    if sampGetPlayerNickname(v:match('.+%[(%d+)%]')) ~= v:gsub('%[%d+%]', '') then
                        table.remove(checkerFriends, k)
                    end
                    if sampGetPlayerIdByNickname(v:gsub('%[%d+%]', '')) == -1 then
                        table.remove(checkerFriends, k)
                    end
                end
            end
        end
    end
)

@chapo
 
Последнее редактирование:
  • Злость
Реакции: qdIbp

хуега)

РП игрок
Модератор
2,576
2,279
есть ли способ оптимизировать данный код?
если есть, пж дайте обьяснения и чо как где
если кто не понял это чекер
Lua:
local checkerFrame = imgui.OnFrame(
    function() return not isGamePaused() and not isPauseMenuActive() end,
    function(player)
        player.HideCursor = true
        local dl = imgui.GetBackgroundDrawList()
        if mainIni.settings.show_checker and sampGetGamestate() == 3 then
            dl:AddTextFontPtr(Font[20], 20, imgui.ImVec2(checkerPos[1].x - 1, checkerPos[1].y - 20 + 1), 0xFF000000, u8'Игроки онлайн:')
            dl:AddTextFontPtr(Font[20], 20, imgui.ImVec2(checkerPos[1].x + 1, checkerPos[1].y - 20 - 1), 0xFF000000, u8'Игроки онлайн:')
            dl:AddTextFontPtr(Font[20], 20, imgui.ImVec2(checkerPos[1].x - 1, checkerPos[1].y - 20 - 1), 0xFF000000, u8'Игроки онлайн:')
            dl:AddTextFontPtr(Font[20], 20, imgui.ImVec2(checkerPos[1].x + 1, checkerPos[1].y - 20 + 1), 0xFF000000, u8'Игроки онлайн:')
            dl:AddTextFontPtr(Font[20], 20, imgui.ImVec2(checkerPos[1].x, checkerPos[1].y - 20), 0xFFFF8C00, u8'Игроки онлайн:')
            for i = 0, sampGetMaxPlayerId(false) do
                if sampIsPlayerConnected(i) then
                    for k, _ in ipairs(checker) do
                        if checker[k] then
                            if checker[k].NICK == sampGetPlayerNickname(i) then
                                lua_thread.create(function()
                                    wait(1000)
                                    local result = true
                                    for _, v in ipairs(checkerFriends) do
                                        wait(0)
                                        if v and checker[k] then
                                            if v:gsub('%[%d+%]', '') == checker[k].NICK then
                                                result = false
                                            end
                                        end
                                    end
                                    if result and checker[k] then
                                        table.insert(checkerPos, imgui.ImVec2(checkerPos[#checkerPos].x, checkerPos[#checkerPos].y + 20))
                                        table.insert(checkerFriends, checker[k].NICK..'['..i..']')
                                    end
                                end)
                            end
                        end
                    end
                end
            end
            if not MOVE_CHECKER then
                for k, v in ipairs(checkerFriends) do
                    dl:AddTextFontPtr(Font[20], 20, imgui.ImVec2(checkerPos[k].x - 1, checkerPos[k].y + 1), 0xFF000000, v)
                    dl:AddTextFontPtr(Font[20], 20, imgui.ImVec2(checkerPos[k].x + 1, checkerPos[k].y - 1), 0xFF000000, v)
                    dl:AddTextFontPtr(Font[20], 20, imgui.ImVec2(checkerPos[k].x - 1, checkerPos[k].y - 1), 0xFF000000, v)
                    dl:AddTextFontPtr(Font[20], 20, imgui.ImVec2(checkerPos[k].x + 1, checkerPos[k].y + 1), 0xFF000000, v)
                    dl:AddTextFontPtr(Font[20], 20, imgui.ImVec2(checkerPos[k].x, checkerPos[k].y), 0xFFFFFFFF, v)
                    if sampGetPlayerNickname(v:match('.+%[(%d+)%]')) ~= v:gsub('%[%d+%]', '') then
                        table.remove(checkerFriends, k)
                    end
                    if sampGetPlayerIdByNickname(v:gsub('%[%d+%]', '')) == -1 then
                        table.remove(checkerFriends, k)
                    end
                end
            end
        end
    end
)

@chapo
А зачем ты значению при парсинге присваиваешь название _, и потом обращаешься к массиву через индекс, когда можно просто брать значение, которое ты сделал неактивным
 

Lance_Sterling

Известный
Автор темы
803
284
А зачем ты значению при парсинге присваиваешь название _, и потом обращаешься к массиву через индекс, когда можно просто брать значение, которое ты сделал неактивным
просто в прошлом коде с добавлением так было легче, поэтому запутался :>

чо дальше?

А зачем ты значению при парсинге присваиваешь название _, и потом обращаешься к массиву через индекс, когда можно просто брать значение, которое ты сделал неактивным
попробовал, в пизду, он некорректно работает
 
Последнее редактирование:

qdIbp

Автор темы
Проверенный
1,392
1,151
Lua:
                    if sampGetPlayerNickname(v:match('.+%[(%d+)%]')) ~= v:gsub('%[%d+%]', '') or sampGetPlayerIdByNickname(v:gsub('%[%d+%]', '')) == -1 then
                        table.remove(checkerFriends, k)
                    end

15 строку тоже можно удалить

та и то, вместо sampGetMaxPlayerId пиши 1000, т.к на серв может быть 52 игрока, а ид последнего может быть 99
 

хуега)

РП игрок
Модератор
2,576
2,279
Lua:
                    if sampGetPlayerNickname(v:match('.+%[(%d+)%]')) ~= v:gsub('%[%d+%]', '') or sampGetPlayerIdByNickname(v:gsub('%[%d+%]', '')) == -1 then
                        table.remove(checkerFriends, k)
                    end

15 строку тоже можно удалить

та и то, вместо sampGetMaxPlayerId пиши 1000, т.к на серв может быть 52 игрока, а ид последнего может быть 99
можно регулярку вынести в отдельную локалку local pattern = "%[%d+%]"
 
  • Нравится
Реакции: qdIbp

Lance_Sterling

Известный
Автор темы
803
284
Lua:
                    if sampGetPlayerNickname(v:match('.+%[(%d+)%]')) ~= v:gsub('%[%d+%]', '') or sampGetPlayerIdByNickname(v:gsub('%[%d+%]', '')) == -1 then
                        table.remove(checkerFriends, k)
                    end

15 строку тоже можно удалить

та и то, вместо sampGetMaxPlayerId пиши 1000, т.к на серв может быть 52 игрока, а ид последнего может быть 99
можно регулярку вынести в отдельную локалку local pattern = "%[%d+%]"
ну изменений не так много значит у меня новая стадия скриптинга
05d63851ce1a5731f3b93b0ae21e9f70.jpg
 
  • Эм
  • Ха-ха
Реакции: хуега) и qdIbp

chapo

🫡 В армии с 17.10.2023. В ЛС НЕ ОТВЕЧАЮ
Друг
8,776
11,236
та и то, вместо sampGetMaxPlayerId пиши 1000, т.к на серв может быть 52 игрока, а ид последнего может быть 99
функция вернет 99, соответственно перебор пойдет от 0 до 99, а это лучше чем перебирать циферки от 0 до 1000
 

wojciech?

Известный
Проверенный
229
138
функция вернет 99, соответственно перебор пойдет от 0 до 99, а это лучше чем перебирать циферки от 0 до 1000

рекомендую избегать эту функцию, потому что она тоже использует цикл внутри себя
пишите просто 999
 
  • Вау
Реакции: Z3roKwq и Lance_Sterling