Author Topic: Ulx completes  (Read 11119 times)

0 Members and 1 Guest are viewing this topic.

Offline skillz

  • Newbie
  • *
  • Posts: 29
  • Karma: 1
Re: Ulx completes
« Reply #30 on: June 07, 2013, 03:57:25 PM »
I've been experiencing a lot of problems when interpreting the name of the item to get the id, this is going to become impossible if i use the LANG.TryTranslation(name) to convert the string into the actual name for the item, so for the users i want them to see the name but for the command i need the id. Any ideas how to accomplish this?

Current code for help.
Code: [Select]
--[Ulx Completes]------------------------------------------------------------------------------
ulx.get_equipment = {}
function GetEquipment()
table.Empty( ulx.get_equipment )

for role, _ in pairs(EquipmentItems) do
for _, item in pairs(EquipmentItems[role]) do
if not table.HasValue(ulx.get_equipment, tostring(item.name)) then
table.insert(ulx.get_equipment, tostring(item.name))
end
end
end
end
hook.Add( "Initialize", "ULXGetEquipment", GetEquipment )
hook.Add( ULib.HOOK_UCLCHANGED, "ULXGetEquipment", GetEquipment )
--[End]----------------------------------------------------------------------------------------



--[Toggle spectator]---------------------------------------------------------------------------
--[[ulx.spec][Forces <target(s)> to and from spectator.]
@param  {[PlayerObject]} calling_ply   [The player who used the command.]
@param  {[PlayerObject]} target_plys   [The player(s) who will have the effects of the command applied to them.]
--]]
function ulx.equipment( calling_ply, target_plys, equipment )
if GamemodeCheck(calling_ply) then return end

local affected_plys = {}
for i=1, #target_plys do
local v = target_plys[ i ]
local match = FindEquipmentMatch(name, equipment, id )

if ulx.getExclusive( v, calling_ply ) then
ULib.tsayError( calling_ply, ulx.getExclusive( v, calling_ply ), true )
elseif GetRoundState() == 1 or GetRoundState() == 2 then
    ULib.tsayError( calling_ply, "The round has not begun!", true )
elseif not v:Alive() then
ULib.tsayError( calling_ply, v:Nick() .. " is dead!", true )
elseif not match and v:HasEquipmentItem(FindEquipmentMatch(name, equipment, id )) then
ULib.tsayError( calling_ply, v:Nick() .. " already has that equipment!", true )
else
local match = FindEquipmentMatch(name, equipment, id )
if match then
v:GiveEquipmentItem(match)
GiveEquipmentWeapon(v:UniqueID(), match)
end
table.insert( affected_plys, v )
end
end
ulx.fancyLogAdmin( calling_ply, "#A gave #T " .. equipment, affected_plys)
end
local equipment = ulx.command( CATEGORY_NAME, "ulx give equipment", ulx.equipment )
equipment:addParam{ type=ULib.cmds.PlayersArg }
equipment:addParam{ type=ULib.cmds.StringArg, completes=ulx.get_equipment, hint="Equipment", error="Invalid equpiment:\"%s\" specified", ULib.cmds.restrictToCompletes }
equipment:defaultAccess( ULib.ACCESS_SUPERADMIN )
equipment:help( "Give <target(s)> specified equpiment." )
--[Helper Functions]---------------------------------------------------------------------------
function FindEquipmentMatch(type, equipment, return_type )
for role, _ in pairs(EquipmentItems) do
for _, item in pairs(EquipmentItems[role]) do
if equipment == item[type] then
return item[return_type]
else
return false
end
end
end
end
--[End]----------------------------------------------------------------------------------------

Would it be possible to define ulx.get_equipment as
ulx.get_equipment = {["name"] = {}, ["id"] ={} }
then set the autocomplete table to ulx.get_equipment.name?
I can't think of any other way to pass the id aswell.

Offline JamminR

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 8096
  • Karma: 390
  • Sertafide Ulysses Jenius
    • Team Ulysses [ULib/ULX, other fine releases]
Re: Ulx completes
« Reply #31 on: June 07, 2013, 08:43:12 PM »
Skillz, my lua knowledge is so rusty, I can't directly help you.
However, looking into URS code during another thread, loadout restrictions, what seems to be what you are asking but for other items, is a function in ( http://u-r-s.googlecode.com/svn/trunk/lua/ulx/xgui/settings/cl_urs_gui.lua ) that SEEMS to, upon quick review, do the id lookup/table assignment you're looking for.
See function URSLoadoutsProcess at the bottom of the code.
Maybe it can be inspiration.
"Though a program be but three lines long, someday it will have to be maintained." -- The Tao of Programming

Offline skillz

  • Newbie
  • *
  • Posts: 29
  • Karma: 1
Re: Ulx completes
« Reply #32 on: June 08, 2013, 02:51:44 AM »
I don't think my lua understanding is great enough to gather the gist of that function without following every function back to its actual application. I guess i'll just try what i suggested and if it doesn't work i'll continue reading programming in lua until a new strategy reveals itself.

Offline JamminR

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 8096
  • Karma: 390
  • Sertafide Ulysses Jenius
    • Team Ulysses [ULib/ULX, other fine releases]
Re: Ulx completes
« Reply #33 on: June 08, 2013, 07:27:40 AM »
Without me digging into your actual code, can't you just assign key value pairs to ulx_get_equipment?
insert ulx_get_equipment = { realid = name }
Then you are storing the info you need, just work with it in another table, or same, if needed.
"Though a program be but three lines long, someday it will have to be maintained." -- The Tao of Programming

Offline skillz

  • Newbie
  • *
  • Posts: 29
  • Karma: 1
Re: Ulx completes
« Reply #34 on: June 08, 2013, 07:56:30 AM »
I did consider that but when i took that approach things went buggy, i've not got working code and i've even managed to use the ttt translator to return the actual names for items.

Code: [Select]
local CATEGORY_NAME  = "TTT Fun"
local gamemode_error = "The current gamemode is not trouble in terrorest town"

function GamemodeCheck(calling_ply)
if not GetConVarString("gamemode") == "terrortown" then
ULib.tsayError( calling_ply, gamemode_error, true )
return true
else
return false
end
end

--[Helper Functions]---------------------------------------------------------------------------
--[End]----------------------------------------------------------------------------------------



--[Ulx Completes]------------------------------------------------------------------------------
ulx.get_equipment_names = {}
ulx.get_equipment_ids   = {}
function GetEquipment()
table.Empty( ulx.get_equipment_names )
table.Empty( ulx.get_equipment_ids )
if CLIENT then LANG.Init() end

for role, _ in pairs(EquipmentItems) do
for _, item in pairs(EquipmentItems[role]) do
local name_final = string.lower(LANG.TryTranslation(item.name))
if not table.HasValue(ulx.get_equipment_names, name_final) then
table.insert(ulx.get_equipment_names, name_final)
end
if not table.HasValue(ulx.get_equipment_ids, item.id) then
ulx.get_equipment_ids[name_final] = item.id
end
end
end
end
hook.Add( "InitPostEntity", "ULXGetEquipment", GetEquipment )
--[End]----------------------------------------------------------------------------------------



--[Toggle spectator]---------------------------------------------------------------------------
--[[ulx.spec][Forces <target(s)> to and from spectator.]
@param  {[PlayerObject]} calling_ply   [The player who used the command.]
@param  {[PlayerObject]} target_plys   [The player(s) who will have the effects of the command applied to them.]
--]]
function ulx.equipment( calling_ply, target_plys, equipment )
if GamemodeCheck(calling_ply) then return end

local affected_plys = {}
for i=1, #target_plys do
local v = target_plys[ i ]

if ulx.getExclusive( v, calling_ply ) then
ULib.tsayError( calling_ply, ulx.getExclusive( v, calling_ply ), true )
elseif GetRoundState() == 1 or GetRoundState() == 2 then
    ULib.tsayError( calling_ply, "The round has not begun!", true )
elseif not v:Alive() then
ULib.tsayError( calling_ply, v:Nick() .. " is dead!", true )
--elseif not table.HasValue(ulx.get_equipment_names, equipment) then
-- ULib.tsayError( calling_ply, "Invalid equipment:\"".. equipment .."\" specified.", true )
elseif v:HasEquipmentItem(FindEquipmentMatch(equipment)) then
ULib.tsayError( calling_ply, v:Nick() .. " already has that equipment!", true )
else
local match = DetectEquipmentMatch(equipment)
if match then
v:GiveEquipmentItem(match)
--GiveEquipmentWeapon(v:UniqueID(), match)
end
table.insert( affected_plys, v )
end
end
ulx.fancyLogAdmin( calling_ply, "#A gave #T " .. equipment, affected_plys)
end
local equipment = ulx.command( CATEGORY_NAME, "ulx give equipment", ulx.equipment )
equipment:addParam{ type=ULib.cmds.PlayersArg }
equipment:addParam{ type=ULib.cmds.StringArg, completes=ulx.get_equipment_names, hint="Equipment", error="Invalid equpiment:\"%s\" specified", ULib.cmds.restrictToCompletes}
equipment:defaultAccess( ULib.ACCESS_SUPERADMIN )
equipment:help( "Give <target(s)> specified equpiment." )
--[Helper Functions]---------------------------------------------------------------------------
function DetectEquipmentMatch( equipment )
for name, id in pairs(ulx.get_equipment_ids) do
if equipment == name then
return id
end
end
end
--[End]----------------------------------------------------------------------------------------

That's my code now.

My problem is now that i always get Command "ulx give equipment", argument #2: Invalid equpiment:"body armor" specified.
No matter the value it gives me that error, i removed restrict to completes and even in my own tests i got errors, can you see any problem with that?

Another error i'm getting is when i initialise the ttt language, if CLIENT then LANG.Init() end, obviously this is done when the function is called, by then all ttt files have loaded, but when the ulx files are loaded the function presents 1 error
[ERROR] addons/ulx/lua/ulx/modules/sh/ttt_fun.lua:41: attempt to call field 'TryTranslation' (a nil value)
1. fn - addons/ulx/lua/ulx/modules/sh/ttt_fun.lua:41
2. unknown - addons/ulib/lua/ulib/shared/hook.lua:183

How can i stop this from being called before the ttt files are loaded?
This is just to keep things neat and clean so people don't think theres a serious problem the first problem is more important.

Offline skillz

  • Newbie
  • *
  • Posts: 29
  • Karma: 1
Re: Ulx completes
« Reply #35 on: June 08, 2013, 01:28:08 PM »
After much testing, it appears that ulx hates autocomplete strings that are uppercase. However after converting the string to lowercase before storing them, I still get the same error where they aren't correct. I'm really quite baffled why this is happening i just hope one of you can figure it out :/.
« Last Edit: June 08, 2013, 01:32:57 PM by skillz »

Offline JamminR

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 8096
  • Karma: 390
  • Sertafide Ulysses Jenius
    • Team Ulysses [ULib/ULX, other fine releases]
Re: Ulx completes
« Reply #36 on: June 08, 2013, 05:19:24 PM »
And here is where I bow out to my team mates, or other community members that are still active with Lua.
I'm sure what you're trying can be done overall... It would just require I dig deeper than I have a ladder to get out with. (time :) )
"Though a program be but three lines long, someday it will have to be maintained." -- The Tao of Programming

Offline skillz

  • Newbie
  • *
  • Posts: 29
  • Karma: 1
Re: Ulx completes
« Reply #37 on: June 09, 2013, 02:54:36 AM »
No problem thank your for your assistance anyway, i'll just wait for Megiddo or someone else to answer :), in the meantime i've got some qualifications to finish of for school.

Offline JamminR

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 8096
  • Karma: 390
  • Sertafide Ulysses Jenius
    • Team Ulysses [ULib/ULX, other fine releases]
Re: Ulx completes
« Reply #38 on: June 09, 2013, 07:38:22 AM »
Won't solve all your problems, but using 2 words with a space for a command "give equipment" may be causing some glitches in the parameter parsing ulx uses.
Additionally, there's another release that uses "give" as it's command. Using "give equipment" might conflict with that release if someone had both.
I'd recommend something that's clearly a TTT related. "ttt_equip" maybe? As in equip player with this TTT item?

"Though a program be but three lines long, someday it will have to be maintained." -- The Tao of Programming

Offline Megiddo

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 6214
  • Karma: 394
  • Project Lead
Re: Ulx completes
« Reply #39 on: June 10, 2013, 10:09:45 AM »
"give equipment" should work fine, as ULX checks for cases like this. Additionally, I tested autocompletes and it deals with strings of all cases appropriately. Try paring your code down by working without the TTT translator. Get something that works first, then add additional functionality so you can better understand what breaks where and why.
Experiencing God's grace one day at a time.