-- ? precache: gamesense
local ui_new_checkbox, ui_new_slider, ui_get, ui_set_callback, ui_set_visible = ui.new_checkbox, ui.new_slider, ui.get, ui.set_callback, ui.set_visible;
local entity_get_local_player, entity_is_alive = entity.get_local_player, entity.is_alive;
local client_userid_to_entindex, client_set_event_callback, client_unset_event_callback = client.userid_to_entindex, client.set_event_callback, client.unset_event_callback;
-- ? defines
local menu = {};
local death = {};
-- ? defines: menu
menu.tab = 'Lua';
menu.container = 'A';
menu.master = ui_new_checkbox(menu.tab, menu.container, 'Ragequit on death-streak');
menu.slider = ui_new_slider(menu.tab, menu.container, 'Maximum streak', 0, 20, 0, true, '', 1, {[ 0 ] = 'Off'});
-- ? defines: death
death.count = 0;
death.cvar = cvar.exit;
death.reset = function()
death.count = 0;
end
death.is_valid = function()
return ui_get(menu.slider) ~= 0
end
death.check = function(count)
if count <= ui_get(menu.slider) then
return
end
death.cvar:invoke_callback();
end
death.on_player_death = function(e)
if not death.is_valid() then
return
end
local lp = entity_get_local_player();
local userid = client_userid_to_entindex(e.userid);
local attacker = client_userid_to_entindex(e.attacker);
if userid ~= lp or attacker == lp then
return
end
death.count = death.count + 1;
death.check(death.count);
end
death.round_end = function(e)
if not death.is_valid() then
return
end
local lp = entity_get_local_player();
if lp == nil then
return
end
if not entity_is_alive(lp) then
return
end
death.reset();
end
-- ? menu: callbacks
ui_set_callback(menu.master, function(self)
local value = ui_get(self);
local fn = client_set_event_callback;
if not value then
death.reset();
fn = client_unset_event_callback;
end
ui_set_visible(menu.slider, value);
fn('round_end', death.round_end);
fn('player_death', death.on_player_death);
end);