ULX

Author Topic: USwep Beta  (Read 17729 times)

0 Members and 1 Guest are viewing this topic.

Offline spbogie

  • Ulysses Team Member
  • Sr. Member
  • *****
  • Posts: 456
  • Karma: 41
USwep Beta
« on: November 19, 2006, 12:20:03 PM »
USwep
By: spbogie

Current Version: Beta 1.01

Description:
USwep is an enhanced SWEP menu designed to use ULib's UCL to allow multiple levels of access.

Features:
  • Multiple Access groups
  • SubFolder support
  • Wildcard folder inclusion filters
  • Spawn sweps, or give directly to self or others

Requirements:
ULib v1.2

Code:
Code: [Select]
--USwep beta 1.01
--by Spbogie

--Requires ULib v1.2 or higher.
assert( _file.Exists( "lua/ULib/init.lua" ), "USwep needs ULib to run!" )
_OpenScript( "ULib/init.lua" )
assert( ULib.ULIB_VERSION >= 1.2, "USwep requires ULib version 1.2 or higher to run!" )

USwep = {}
------------
-- CONFIG --
------------
-- Groups list. Groups take priority by order in list.
USwep.Groups =
{
{
name = "Admin", --Name of the group.
folders = {"*"}, --Folders to give access to. Use * for all folders/sub-folders.
exfolders = {"build"}, --Optional. Exclude folders in this list (NO wildcards allowed)
spawn = true, --Allow this group to spawn SWEPs on the ground.
give = true, --Allow this group to give SWEPs to any player.
giveself = true, --Allow this group to give SWEPs to themselves.
access = ACCESS_BAN --Access required to be in this group
},
{
name = "SubAdmin",
folders = {"Counter-Strike", "gm_laserdance"},
spawn = false,
give = true,
giveself = true,
access = ACCESS_KICK
},
{
name = "Public",
folders = {"Construction"},
spawn = false,
give = false,
giveself = true,
access = ACCESS_ALL
}
}
USwep.recurseLevel = 2 --Number of sub-folder levels to explore
USwep.ucl = ULib.mainUcl --UCL to use (Only change if you use a custom UCL)

----------
-- MAIN --
----------

function USwep.getGroup(userid)
for i=1, table.getn(USwep.Groups) do
if USwep.ucl:query(userid, USwep.Groups[i].access) then
return i
end
end
end

function USwep.getFolders(dir, level)
dir = dir or "lua/weapons"
level = level or -1
if level == USwep.recurseLevel then return {} end
local list = {}
local files = _file.Find(dir .. "/*") or {}
table.remove(files, ULib.findInTable(files, "."))
table.remove(files, ULib.findInTable(files, ".."))
for _,v in ipairs(files) do
if _file.IsDir(dir .. "/" .. v) then
table.insert(list, dir .. "/" .. v)
list = ULib.mergeTable(list, USwep.getFolders(dir .. "/" .. v, level + 1))
end
end
return list
end

function USwep.getFiles(dir)
local list = _file.Find(dir .. "/*.lua")
local ret = {}
for _,v in ipairs(list) do
if not _file.IsDir(dir .. "/" .. v) then
table.insert(ret, string.sub(dir .. "/" .. v, 5))
end
end
return ret
end

function USwep.checkFolder(group, dir)
for i=1, table.getn(USwep.Groups[group].folders) do
if string.find(dir, "lua/weapons/" .. string.gsub(ULib.makePatternSafe(USwep.Groups[group].folders[i]),"%%%*","[^/]+")) then
if USwep.Groups[group].exfolders ~= nil then
for j=1, table.getn(USwep.Groups[group].exfolders) do
if string.find(dir, "lua/weapons/" .. ULib.makePatternSafe(USwep.Groups[group].exfolders[1])) then
return false
end
end
end
return true
end
end
return false
end

function USwep.getList(group)
local sList = {}
for _,v in USwep.getFolders() do
if USwep.checkFolder(group, v) then
local files = USwep.getFiles(v)
if files ~= {} then
table.insert(sList, {string.gsub(string.sub(v, 13), "[^/]+/", "  "), files})
end
end
end
return sList
end

--------------
-- COMMANDS --
--------------
function USwep.cc_giveSWEP(userid, args, argv, argc)
if argc < 1 then
ULib.tsay( userid, ulx.LOW_ARGS )
return
end
if not _file.Exists("lua/"..argv[1]) then
ULib.tsay(userid, "Invalid SWEP specified")
return
end
local group = USwep.getGroup(userid)
if group == nil or not USwep.Groups[group].give then
ULib.tsay(userid, "You do not have access to this command, " .. _PlayerInfo( userid, "name" ) .. "." )
return
end
if not USwep.checkFolder(group, "lua/" .. argv[1]) then
ULib.tsay(userid, "You do not have access to this SWEP, " .. _PlayerInfo( userid, "name" ) .. "." )
return
end

if argc == 1 then
local f = function (userid, target)
if target == ULib.ID_EXIT then return
elseif target == ULib.ID_ALLPLAYERS then
for i=1,_MaxPlayers() do
if _PlayerInfo(i,"alive") then
_PlayerGiveSWEP(i, argv[1])
end
end
else
_PlayerGiveSWEP(target, argv[1])
end
end

local menu = ULib.Menu:new("Choose a Player", f, USwep.menu_skin)
menu:addPlayerOptions(true)
menu:showMenu(userid)
else
targets, err = ULib.getUsers( argv[ 2 ], true, true, USwep.ucl, userid ) -- Enable keywords, ignore immunity
if not targets then
ULib.tsay( userid, err )
return
end

for _,v in ipairs(targets) do
_PlayerGiveSWEP(v, argv[1])
end
end
end
ULib.CONCOMMAND("giveSWEP", USwep.cc_giveSWEP, "Give a player a SWEP.")

function USwep.cc_giveSWEPself(userid, args, argv, argc)
if argc < 1 then
ULib.tsay( userid, ulx.LOW_ARGS )
return
end
if not _file.Exists("lua/"..argv[1]) then
ULib.tsay(userid, "Invalid SWEP specified")
return
end
local group = USwep.getGroup(userid)
if group == nil or not USwep.Groups[group].giveself then
ULib.tsay(userid, "You do not have access to this command, " .. _PlayerInfo( userid, "name" ) .. "." )
return
end
if not USwep.checkFolder(group, "lua/" .. argv[1]) then
ULib.tsay(userid, "You do not have access to this SWEP, " .. _PlayerInfo( userid, "name" ) .. "." )
return
end

_PlayerGiveSWEP(userid, argv[1])
end
ULib.CONCOMMAND("giveSWEPself", USwep.cc_giveSWEPself, "Give a SWEP to yourself.")

function USwep.cc_spawnSWEP(userid, args, argv, argc)
if argc < 1 then
ULib.tsay( userid, ulx.LOW_ARGS )
return
end
if not _file.Exists("lua/"..argv[1]) then
ULib.tsay(userid, "Invalid SWEP specified")
return
end
local group = USwep.getGroup(userid)
if group == nil or not USwep.Groups[group].spawn then
ULib.tsay(userid, "You do not have access to this command, " .. _PlayerInfo( userid, "name" ) .. "." )
return
end
if not USwep.checkFolder(group, "lua/" .. argv[1]) then
ULib.tsay(userid, "You do not have access to this SWEP, " .. _PlayerInfo( userid, "name" ) .. "." )
return
end

PlayerLookTrace(userid, 200)
if not _TraceHit() then return end
local spawnPos = _TraceEndPos();
local normal = _TraceGetSurfaceNormal();
spawnPos = vecAdd( spawnPos, vecMul( normal, 50 ) )
local ent = _EntCreate("weapon_swep")
_EntSetPos(ent, spawnPos)
_EntSetKeyValue(ent, "Script", argv[1])
_EntSpawn(ent)
_util.DropToFloor(ent)
end
ULib.CONCOMMAND("spawnSWEP", USwep.cc_spawnSWEP, "Spawn a SWEP.")

----------
-- MENU --
----------
function USwep.sendMenu(userid)
local group = USwep.getGroup(userid)
local category = "USwep (" .. USwep.Groups[group].name .. ")"
for _,v in ipairs(USwep.getList(group)) do
_spawnmenu.AddItem(userid, category, "@" .. v[1])
for _,f in ipairs(v[2]) do
if USwep.Groups[group].giveself then
_spawnmenu.AddItem(userid, category, "+" .. string.sub(string.gsub(f, "[^/]+/", ""), 1, -5), "giveSWEPself \"" .. f .. "\"")
end
if USwep.Groups[group].spawn then
_spawnmenu.AddItem(userid, category, "+S " .. string.sub(string.gsub(f, "[^/]+/", ""), 1, -5), "spawnSWEP \"" .. f .. "\"")
end
if USwep.Groups[group].give then
_spawnmenu.AddItem(userid, category, "+G " .. string.sub(string.gsub(f, "[^/]+/", ""),1 ,-5), "giveSWEP \"" .. f .. "\"")
end
end
end
end
USwep.ucl:addAccessCallback(USwep.sendMenu)

This is currently in beta, and as such I am looking for comments/suggestions. Please don't hesitate to post your ideas.
« Last Edit: November 25, 2006, 02:49:36 PM by spbogie »
I have not failed. I've just found 10,000 ways that won't work. - Thomas A. Edison
I reject your reality and substitute my own. - Adam Savage

Offline JamminR

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 8096
  • Karma: 390
  • Sertafide Ulysses Jenius
    • Team Ulysses [ULib/ULX, other fine releases]
Re: USwep Beta
« Reply #1 on: November 19, 2006, 02:56:27 PM »
I understand its beta now, but having the config in a separate file in the future would be great. This way, the general folks of the world don't have to update the configs everytime a code base is released and overwrites the .lua file. :)
"Though a program be but three lines long, someday it will have to be maintained." -- The Tao of Programming

Offline Golden-Death

  • Hero Member
  • *****
  • Posts: 751
  • Karma: 0
  • Honored Lua Scripter
    • BlueFire
Re: USwep Beta
« Reply #2 on: November 20, 2006, 03:47:46 PM »
Just a tip about a section you missed: What is it?



(I'm too lazy to check it myself :))


Offline JamminR

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 8096
  • Karma: 390
  • Sertafide Ulysses Jenius
    • Team Ulysses [ULib/ULX, other fine releases]
Re: USwep Beta
« Reply #3 on: November 20, 2006, 03:56:30 PM »
HAHA.
True, I knew what it was from previous conversations.

GD, others, USwep allows Ulib to control access to who has what SWEPS on your server.
Think Eswep, but written totally for ULib. :)
"Though a program be but three lines long, someday it will have to be maintained." -- The Tao of Programming

Offline Golden-Death

  • Hero Member
  • *****
  • Posts: 751
  • Karma: 0
  • Honored Lua Scripter
    • BlueFire
Re: USwep Beta
« Reply #4 on: November 20, 2006, 04:38:31 PM »
Ah, i see.


Offline spbogie

  • Ulysses Team Member
  • Sr. Member
  • *****
  • Posts: 456
  • Karma: 41
Re: USwep Beta
« Reply #5 on: November 20, 2006, 05:13:56 PM »
Ah yes, thank you, I forgot to include a discription.
I have not failed. I've just found 10,000 ways that won't work. - Thomas A. Edison
I reject your reality and substitute my own. - Adam Savage

Offline Golden-Death

  • Hero Member
  • *****
  • Posts: 751
  • Karma: 0
  • Honored Lua Scripter
    • BlueFire
Re: USwep Beta
« Reply #6 on: November 20, 2006, 11:10:52 PM »
Ah yes, thank you, I forgot to include a discription.

We always forget the most important part don't we?

When I released HaloGM, I didn't include the Sweps...
I forgot the download link to one of my plugins here.

 :D


Offline Lore T

  • Newbie
  • *
  • Posts: 4
  • Karma: 0
Re: USwep Beta
« Reply #7 on: November 23, 2006, 10:24:46 AM »
Can this use access flags too?  Or if not, can you make it use them so making new groups is simplified a bit more.

Offline JamminR

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 8096
  • Karma: 390
  • Sertafide Ulysses Jenius
    • Team Ulysses [ULib/ULX, other fine releases]
Re: USwep Beta
« Reply #8 on: November 23, 2006, 04:55:40 PM »
Lore T,
It already uses access flags. The code as it stands now has 3 groups, which are reasonably shown in the code, and more could be added.
Admin, SubAdmin, and Public
Admins, can spawn on ground, give to players, can give to self, all weapons, and require "ACCESS_BAN" to do this.
SubAdmins, can't spawn on ground, can give to players, can give to self, weapons in Counter-Strike and gm_laserdance, and require "ACCESS_KICK" to do this.
Public, can't spawn on ground, can't give to players, can give to self, only SWEPS found in "Construction" (You would have to create this folder under weapons, and put the SWEPS you want included for public there I think)
If you need help with any of that, let us know.


SPBogie,
I haven't yet loaded this script but I have a question. Before loading ULib, you set the variable
USwep.ucl = ULib.mainUcl
 Would that be nil at first, since ULib isn't loaded, or, would it become a string on its own, until checked later in the script when it becomes the variable access list from Ulib.
My concern is that since ULib.mainUcl might be nil, it would break something somewhere.
"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: 6213
  • Karma: 394
  • Project Lead
Re: USwep Beta
« Reply #9 on: November 23, 2006, 05:48:00 PM »
I bet he's testing it out of the module folder so ULib was loaded first anyways... good catch JamminR.
« Last Edit: November 24, 2006, 11:37:00 AM by Megiddo »
Experiencing God's grace one day at a time.

Offline JamminR

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 8096
  • Karma: 390
  • Sertafide Ulysses Jenius
    • Team Ulysses [ULib/ULX, other fine releases]
Re: USwep Beta
« Reply #10 on: November 24, 2006, 11:32:59 AM »
I wasn't sure it would be a bug or not. I know Lua is pretty lenient on variables. I thought it might take it as a string, even without quotes.
"Though a program be but three lines long, someday it will have to be maintained." -- The Tao of Programming

Offline spbogie

  • Ulysses Team Member
  • Sr. Member
  • *****
  • Posts: 456
  • Karma: 41
Re: USwep Beta
« Reply #11 on: November 25, 2006, 02:47:48 PM »
Yeah, good catch.

Megiddo is right it was in the modules folder so ULib was already loaded. I've fixed it now.
I have not failed. I've just found 10,000 ways that won't work. - Thomas A. Edison
I reject your reality and substitute my own. - Adam Savage

Lunchbox

  • Guest
Re: USwep Beta
« Reply #12 on: November 27, 2006, 06:32:23 PM »
I have this running on my server and thus far it works well.  One thing I noticed was that it doesn't have any GUI hooks for spawning weapons like other menus (ESwep for instance)

Is this by design, not finished, or am I doing something wrong?


Offline Megiddo

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 6213
  • Karma: 394
  • Project Lead
Re: USwep Beta
« Reply #13 on: November 27, 2006, 06:35:18 PM »
What do you mean by "gui hooks"?
Experiencing God's grace one day at a time.

Lunchbox

  • Guest
Re: USwep Beta
« Reply #14 on: November 27, 2006, 06:56:33 PM »
Ah sorry, I should have been more clear.

I mean a graphical user interface in which you can click on a weapon name and spawn it; similar to ESwep menu, but with the ULib group interactions.