Skip to content

Commit

Permalink
Use better variable names; guard feature with config setting.
Browse files Browse the repository at this point in the history
  • Loading branch information
bsb002-flash-tester committed Sep 3, 2021
1 parent 5bfb657 commit 911fc7b
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 32 deletions.
16 changes: 8 additions & 8 deletions addons/shortcuts/helper_functions.lua
Original file line number Diff line number Diff line change
Expand Up @@ -80,30 +80,30 @@ function bracket_closer(str,opener,closer)
end

-----------------------------------------------------------------------------------
--Name: strip()
--Name: strip_non_alphanumeric_convert_digits_to_roman()
--Args:
---- name (string): Name to be slugged
---- name (string): Name to be stripped
-----------------------------------------------------------------------------------
--Returns:
---- string with a gsubbed version of name that removes non-alphanumeric characters,
-------- forces the string to lower-case, and converts numbers to Roman numerals,
-------- which are upper case.
-----------------------------------------------------------------------------------
function strip(name)
function strip_non_alphanumeric_convert_digits_to_roman(name)
return name:gsub('[^%w]',''):lower():gsub('(%d+)',to_roman)
end

-----------------------------------------------------------------------------------
--Name: strip2()
--Name: strip_non_alphanumeric_keep_plus()
--Args:
---- name (string): Name to be slugged
---- name (string): Name to be stripped
-----------------------------------------------------------------------------------
--Returns:
---- string with a gsubbed version of name that removes non-alphanumeric characters,
-------- but allows the character '+' (unlike strip), forces the string to
-------- lower-case, and does *not* convert numbers to roman numerals (unlike strip).
-------- but allows the character '+', and forces the string to lower-case. Does not
-------- convert numbers to roman numerals.
-----------------------------------------------------------------------------------
function strip2(name)
function strip_non_alphanumeric_keep_plus(name)
return name:gsub('[^%w+]',''):lower()
end

Expand Down
63 changes: 44 additions & 19 deletions addons/shortcuts/shortcuts.lua
Original file line number Diff line number Diff line change
Expand Up @@ -117,10 +117,16 @@ default_aliases = {
cw5="Curing Waltz V",
hw="Healing Waltz"
}
default_settings = {
include_items = false,
}

aliases = config.load('data\\aliases.xml',default_aliases)
aliases = config.load('data/aliases.xml', default_aliases)
settings = config.load('data/settings.xml', default_settings)
config.save(aliases)
config.save(settings)
setmetatable(aliases,nil)
setmetatable(settings,nil)


require 'statics'
Expand Down Expand Up @@ -339,30 +345,49 @@ end
---- Sends a command if the command needs to be changed.
-----------------------------------------------------------------------------------
function interp_text(splitline,offset,modified)
local temptarg,abil
local no_targ_abil = table.concat(splitline,' ',1+offset,splitline.n)
local no_targ_abil_1 = strip(no_targ_abil)
local no_targ_abil_2 = strip2(no_targ_abil)

if validabils[no_targ_abil_2] then
abil = no_targ_abil_2
elseif validabils[no_targ_abil_1] then
abil = no_targ_abil_1
-- Assume there was not a target suffix on the command.
local preliminary_action_name = table.concat(splitline,' ',1+offset,splitline.n)
local preliminary_action_name_normalized_as_item = strip_non_alphanumeric_keep_plus(preliminary_action_name)
local preliminary_action_name_normalized_as_nonitem = strip_non_alphanumeric_convert_digits_to_roman(preliminary_action_name)

-- Note: The normalized 'item' name is almost strictly more specific than
-- the normalized 'nonitem' name, and thus the former must be searched
-- before the latter to avoid falsely matching the wrong entry.
local temporary_target_name, normalized_preliminary_action_name
if validabils[preliminary_action_name_normalized_as_item] then
normalized_preliminary_action_name = preliminary_action_name_normalized_as_item
elseif validabils[preliminary_action_name_normalized_as_nonitem] then
normalized_preliminary_action_name = preliminary_action_name_normalized_as_nonitem
elseif splitline.n > 1 then
temptarg = valid_target(targ_reps[splitline[splitline.n]] or splitline[splitline.n])
temporary_target_name = valid_target(targ_reps[splitline[splitline.n]] or splitline[splitline.n])
end

-- Compute a better name to look up based on the result of the above.
local finalized_action_name = normalized_preliminary_action_name
if temporary_target_name then
finalized_action_name = _raw.table.concat(splitline,' ',1+offset,splitline.n-1)
elseif not normalized_preliminary_action_name then
finalized_action_name = _raw.table.concat(splitline,' ',1+offset,splitline.n)
end

if temptarg then abil = _raw.table.concat(splitline,' ',1+offset,splitline.n-1)
elseif not abil then abil = _raw.table.concat(splitline,' ',1+offset,splitline.n) end
-- Re-normalize the action name, but using the finalized name
local finalized_action_name_normalized_as_item = strip_non_alphanumeric_keep_plus(finalized_action_name)
local finalized_action_name_normalized_as_nonitem = strip_non_alphanumeric_convert_digits_to_roman(finalized_action_name)

local strippedabil_1 = strip(abil) -- Slug the ability
local strippedabil_2 = strip2(abil) -- Slug the ability
local validstrippedabils = validabils[strippedabil_2] or validabils[strippedabil_1]
-- Note: The normalized 'item' name is almost strictly more specific than
-- the normalized 'nonitem' name, and thus the former must be searched
-- before the latter to avoid falsely matching the wrong entry.
local actions_by_normalized_name
if validabils[finalized_action_name_normalized_as_item] then
actions_by_normalized_name = validabils[finalized_action_name_normalized_as_item]
else
actions_by_normalized_name = validabils[finalized_action_name_normalized_as_nonitem]
end

if validstrippedabils then
if actions_by_normalized_name then
local options,nonoptions,num_opts = {},{},0
local player = windower.ffxi.get_player()
for v in validstrippedabils:it() do
for v in actions_by_normalized_name:it() do
if check_usability(player,v.res,v.id) then
options[v.res] = v.id
num_opts = num_opts + 1
Expand Down Expand Up @@ -417,7 +442,7 @@ function interp_text(splitline,offset,modified)
abil_name = abil_name:sub(1,-2)
end

local out_tab = {prefix = in_game_res_commands[r_line.prefix:gsub("/","")], name = abil_name, target = temptarg or target_make(targets)}
local out_tab = {prefix = in_game_res_commands[r_line.prefix:gsub("/","")], name = abil_name, target = temporary_target_name or target_make(targets)}
if not out_tab.prefix then print('Could not find prefix',r_line.prefix) end
lastsent = out_tab.prefix..' "'..out_tab.name..'" '..out_tab.target
if logging then
Expand Down
8 changes: 5 additions & 3 deletions addons/shortcuts/statics.lua
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ function validabils_it(resource)
for id,v in pairs(res[resource]) do
if (not v.monster_level and v.prefix) or (v.monster_level and v.monster_level ~= -1 and v.ja:sub(1,1) ~= '#' ) or (v.category == 'Usable') then
-- Monster Abilities contains a large number of player-usable moves (but not monstrosity-usable). This excludes them.
local name = resource ~= 'items' and strip(v.english) or strip2(v.english)
local name = resource ~= 'items' and strip_non_alphanumeric_convert_digits_to_roman(v.english) or strip_non_alphanumeric_keep_plus(v.english)
make_abil(name,resource,id)
end
end
Expand All @@ -210,5 +210,7 @@ validabils_it('spells')
validabils_it('job_abilities')
validabils_it('weapon_skills')
validabils_it('monster_abilities')
validabils_it('items')
validabils_it('mounts')
validabils_it('mounts')
if settings.include_items then
validabils_it('items')
end
4 changes: 2 additions & 2 deletions addons/shortcuts/targets.lua
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
-----------------------------------------------------------------------------------
function valid_target(targ,flag)
local spell_targ
local san_targ = find_san(strip(targ))
local san_targ = find_san(strip_non_alphanumeric_convert_digits_to_roman(targ))
-- If the target is whitelisted, pass it through.
if pass_through_targs:contains(targ:lower()) or st_targs:contains(targ:lower()) or (tonumber(targ:lower()) and windower.ffxi.get_mob_by_id(tonumber(targ:lower()))) then
return targ:lower()
Expand All @@ -48,7 +48,7 @@ function valid_target(targ,flag)
local current_target = windower.ffxi.get_mob_by_target('t')
local targar = {}
for i,v in pairs(windower.ffxi.get_mob_array()) do
if string.find(strip(v.name),san_targ) and (v.valid_target or v.id == windower.ffxi.get_player().id) then -- Malformed pattern somehow
if string.find(strip_non_alphanumeric_convert_digits_to_roman(v.name),san_targ) and (v.valid_target or v.id == windower.ffxi.get_player().id) then -- Malformed pattern somehow
-- Handling for whether it's a monster or not
if v.is_npc and v.spawn_type ~= 14 and current_target then
if v.id == current_target.id then
Expand Down

0 comments on commit 911fc7b

Please sign in to comment.