что изменить в этом коде чтобы график шел медленее(умещал больше информации на экране)? и что чтобы график стал больше.
script_name('Ping Chart')
script_author('FYP')
local ini = require 'inicfg'
local imgui = require 'imgui'
imgui.GetStyle().WindowRounding = 5.0
imgui.GetStyle().Colors[imgui.Col.WindowBg] = imgui.ImVec4(0.0, 0.0, 0.0, 0.7)
imgui.GetStyle().Colors[imgui.Col.PlotLines] = imgui.ImVec4(0.93, 0.85, 0.45, 1.0)
-- Config
local active = true
local ini_settings = ini.load({settings = {
show_local_player_ping = true,
max_remote_players = 4,
ping_update_rate = 0.3, -- in seconds
ping_show_duration = 15, -- in seconds
ping_history_entries = 150,
}})
local settings = ini_settings.settings
local ping_list = {}
local players_to_draw = {}
function update_player_ping(id, ping)
if ping_list[id] then
local data = ping_list[id]
if os.clock() > data.update_tick + settings.ping_update_rate then
if ping > data.max then
data.max = ping
end
--[[data.chart[data.pos + 1] = ping
data.pos = data.pos + 1
if data.pos > settings.ping_history_entries then
data.pos = 1
end]]
table.insert(data.chart, ping)
if #data.chart > settings.ping_history_entries then
table.remove(data.chart, 1)
end
data.update_tick = os.clock()
end
return data
else
local data = {
chart = {ping},
--pos = 1,
max = ping,
update_tick = os.clock()
}
ping_list[id] = data
return data
end
end
local ping_history_cleanup_tick = 0
local settings_menu = imgui.ImBool(false)
function main()
if isSampLoaded() and isSampfuncsLoaded() then
while not isSampAvailable() or not isPlayerPlaying(PLAYER_HANDLE) do
wait(100)
end
sampRegisterChatCommand('pingchart', function()
settings_menu.v = true
imgui.ShowCursor = true
imgui.DisableInput = false
end)
while true do
wait(0)
-- disable input when it's not needed
if not settings_menu.v and not imgui.DisableInput then
imgui.ShowCursor = false
imgui.DisableInput = true
end
code
Assistant
Чтобы график шел медленнее (то есть уместить больше информации на экране), необходимо увеличить количество записей истории пинга (ping_history_entries), и уменьшить частоту обновления графика (ping_update_rate). Для того чтобы увеличить размеры графика, нужно изменить размер окна ImGui, где отображается график.
Измените количество записей истории пинга следующим образом:
lua
ready_to_process = active and isPlayerPlaying(PLAYER_HANDLE) and not isCharDead(PLAYER_PED) -- and isCharOnFoot(PLAYER_PED)
imgui.Process = settings_menu.v or (ready_to_process and (#players_to_draw > 0 or settings.show_local_player_ping))
if settings.max_remote_players > 0 then
if ready_to_process then
local target_id = get_target_player_id()
if target_id then
if players_to_draw[1] and players_to_draw[1].id == target_id then
-- just update time
players_to_draw[1].update_tick = os.clock()
else
for i, v in ipairs(players_to_draw) do
if v.id == target_id then
table.remove(players_to_draw, i)
break
end
end
table.insert(players_to_draw, 1, {id = target_id, update_tick = os.clock()})
if #players_to_draw > settings.max_remote_players then
table.remove(players_to_draw, #players_to_draw)
end
end
end
end
local player_idx_to_remove = {}
for i, v in ipairs(players_to_draw) do
if sampIsPlayerConnected(v.id)
and sampGetCharHandleBySampPlayerId(v.id)
and v.update_tick + settings.ping_show_duration > os.clock()
then
local ping = sampGetPlayerPing(v.id)
update_player_ping(v.id, ping)
else
ping_list[v.id] = nil
table.insert(player_idx_to_remove, i)
end
end
for i, idx in ipairs(player_idx_to_remove) do
table.remove(players_to_draw, idx)
end
-- ping history cleanup
if os.clock() > ping_history_cleanup_tick + settings.ping_show_duration then
for k, v in pairs(ping_list) do
if os.clock() > v.update_tick + settings.ping_show_duration then
ping_list[k] = nil
end
end
ping_history_cleanup_tick = os.clock()
end
end
end
else
return script.this:unload()
end
end
function get_target_player_id()
local result, ped = getCharPlayerIsTargeting(player)
if result then
local res_target, target_id = sampGetPlayerIdByCharHandle(ped)
if res_target then return target_id end
end
return nil
end
function get_local_player_id()
local res, local_id = sampGetPlayerIdByCharHandle(PLAYER_PED)
if res then return local_id end
return nil
end
-- ImGui
function im_colored_text_with_shadow(color, text)
local pos = imgui.GetCursorPos()
imgui.SetCursorPos(imgui.ImVec2(pos.x + 1, pos.y + 1))
imgui.TextColored(imgui.ImVec4(0, 0, 0, 1), text) -- shadow
imgui.SetCursorPos(pos)
imgui.TextColored(color, text)
end
function draw_ping_chart(player_id)
if player_id ~= get_local_player_id() then
print('drawing', player_id)
end
local ping = sampGetPlayerPing(player_id)
local player_nick = sampGetPlayerNickname(player_id)
local b, g, r, a = imgui.ImColor(sampGetPlayerColor(player_id)):GetRGBA()
local wf = imgui.WindowFlags
imgui.SetMouseCursor(imgui.MouseCursor.None)
imgui.Begin('Ping Chart', nil, wf.NoTitleBar + wf.NoResize + wf.AlwaysAutoResize + wf.NoMove + wf.NoSavedSettings + wf.NoInputs)
im_colored_text_with_shadow(imgui.ImColor(r, g, b).v, player_nick)
imgui.SameLine(0, 4)
imgui.TextColored(imgui.ImVec4(1, 1, 1, 0.7), '[' .. player_id .. ']')
imgui.Separator()
local ping_data = update_player_ping(player_id, ping)
imgui.PlotLines('##Ping', ping_data.chart, 0, 'ping: ' .. ping .. ' max: ' .. ping_data.max, 0, ping_data.max * 2, imgui.ImVec2(200, 40))
imgui.End()
end
local im_active = imgui.ImBool(active)
local im_show_local_player_ping = imgui.ImBool(settings.show_local_player_ping)
local im_max_remote_players = imgui.ImInt(settings.max_remote_players)
local im_ping_update_rate = imgui.ImFloat(settings.ping_update_rate)
local im_ping_show_duration = imgui.ImFloat(settings.ping_show_duration)
local im_ping_history_entries = imgui.ImInt(settings.ping_history_entries)
function draw_settings_menu()
imgui.Begin('Ping Chart Settings', settings_menu, imgui.WindowFlags.AlwaysAutoResize + imgui.WindowFlags.NoCollapse)
imgui.Text('Status')
if imgui.Checkbox('Enable', im_active) then
active = im_active.v
end
imgui.Separator()
imgui.Spacing()
imgui.Text('Settings')
imgui.Checkbox('Show local player ping chart', im_show_local_player_ping)
imgui.PushItemWidth(100)
imgui.SliderInt('Maximum players to be shown at the same time', im_max_remote_players, 0, 10)
imgui.SliderInt('Maximum ping entries on chart', im_ping_history_entries, 10, 1000)
imgui.DragFloat('Chart showing duration (in seconds)', im_ping_show_duration, 0.2, 0.5, math.huge)
imgui.DragFloat('Ping update rate (in seconds)', im_ping_update_rate, 0.01, 0.01, 5)
imgui.PopItemWidth()
imgui.Dummy(imgui.ImVec2(0, 10))
if imgui.Button('Save settings') then
settings.show_local_player_ping = im_show_local_player_ping.v
settings.max_remote_players = im_max_remote_players.v
settings.ping_update_rate = im_ping_update_rate.v
settings.ping_show_duration = im_ping_show_duration.v
settings.ping_history_entries = im_ping_history_entries.v
ini.save(ini_settings)
ping_list = {}
players_to_draw = {}
settings_menu.v = false
end
imgui.SameLine()
if imgui.Button('Cancel') then
settings_menu.v = false
end
imgui.End()
end
function imgui.OnDrawFrame()
if ready_to_process then
local sw, sh = getScreenResolution()
if settings.show_local_player_ping or #players_to_draw > 0 then
imgui.SetNextWindowPos(imgui.ImVec2(sw - 10, sh - 10), imgui.Cond.Always, imgui.ImVec2(1, 1))
end
if settings.max_remote_players > 0 then
for i, v in ipairs(players_to_draw) do
if sampIsPlayerConnected(v.id) then
draw_ping_chart(v.id)
end
end
end
if settings.show_local_player_ping then
local local_id = get_local_player_id()
if local_id then
draw_ping_chart(local_id)
end
end
end
if settings_menu.v then
draw_settings_menu()
end
end
local ini_settings = ini.load({settings = {
show_local_player_ping = true,
max_remote_players = 4,
ping_update_rate = 1.0, -- уменьшить частоту обновления (в секундах)
ping_show_duration = 15, -- в секундах
ping_history_entries = 300, -- увеличить количество записей истории
}})
В параметрах ImGui увеличьте размеры графика:
lua
function draw_ping_chart(player_id)
if player_id ~= get_local_player_id() then
print('drawing', player_id)
end
local ping = sampGetPlayerPing(player_id)
local player_nick = sampGetPlayerNickname(player_id)
local b, g, r, a = imgui.ImColor(sampGetPlayerColor(player_id)):GetRGBA()
local wf = imgui.WindowFlags
imgui.SetMouseCursor(imgui.MouseCursor.None)
imgui.Begin('Ping Chart', nil, wf.NoTitleBar + wf.NoResize + wf.AlwaysAutoResize + wf.NoMove + wf.NoSavedSettings + wf.NoInputs)
im_colored_text_with_shadow(imgui.ImColor(r, g, b).v, player_nick)
imgui.SameLine(0, 4)
imgui.TextColored(imgui.ImVec4(1, 1, 1, 0.7), '[' .. player_id .. ']')
imgui.Separator()
local ping_data = update_player_ping(player_id, ping)
imgui.PlotLines('##Ping', ping_data.chart, 0, 'ping: ' .. ping .. ' max: ' .. ping_data.max, 0, ping_data.max * 2, imgui.ImVec2(400, 80)) -- увеличить размер окна графика
imgui.End()
end
Таким образом, при изменении ping_update_rate с 0.3 на 1.0 и увеличение записей истории (ping_history_entries) с 150 до 300, график будет идти медленнее, уместив больше информации. Также было увеличено окно графика с размеров 200x40 до 400x80, что сделает график больше.