Ulysses

Ulysses Stuff => Ulysses Release Archives => Releases => Releases for ULib v1.* => Topic started by: spbogie on November 19, 2006, 12:20:03 PM

Title: USwep Beta
Post by: spbogie 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:

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.
Title: Re: USwep Beta
Post by: JamminR 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. :)
Title: Re: USwep Beta
Post by: Golden-Death 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 :))
Title: Re: USwep Beta
Post by: JamminR 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. :)
Title: Re: USwep Beta
Post by: Golden-Death on November 20, 2006, 04:38:31 PM
Ah, i see.
Title: Re: USwep Beta
Post by: spbogie on November 20, 2006, 05:13:56 PM
Ah yes, thank you, I forgot to include a discription.
Title: Re: USwep Beta
Post by: Golden-Death 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
Title: Re: USwep Beta
Post by: Lore T 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.
Title: Re: USwep Beta
Post by: JamminR 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.
Title: Re: USwep Beta
Post by: Megiddo 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.
Title: Re: USwep Beta
Post by: JamminR 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.
Title: Re: USwep Beta
Post by: spbogie 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.
Title: Re: USwep Beta
Post by: Lunchbox 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?

Title: Re: USwep Beta
Post by: Megiddo on November 27, 2006, 06:35:18 PM
What do you mean by "gui hooks"?
Title: Re: USwep Beta
Post by: Lunchbox 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.
Title: Re: USwep Beta
Post by: JamminR on November 27, 2006, 08:00:20 PM
Lunchbox, you're doing nothing wrong.
This uses the Ulib menu 100%, not a Dropdown selection in the Spawn menu.

SPBogie, I believe what Lunchbox is looking for would be a great addition for a future version.
Have a spawn menu.
That's where most people look for SWEPS.

Title: Re: USwep Beta
Post by: spbogie on November 28, 2006, 04:57:44 AM
There is already a spawn menu It only uses the ULib menu for selecting a player to give to if you don't specify one.

Lunchbox, please recheck your spawnmenu for a category "USwep (<access lecel name here>)". If it is not there then please check your console for any errors while the script is loading and post them here.
Title: Re: USwep Beta
Post by: Lunchbox on November 28, 2006, 02:21:55 PM
Interesting.  I'm watching the server output on my Linux Dedicated server through SSH and it says it loaded fine then proceeds to the next file.  Where in the Spawn Menu should I be looking?

P.S.
ULib is running and I have full admin permissions.
Title: Re: USwep Beta
Post by: spbogie on November 29, 2006, 09:15:36 PM
You said the console commands work though? That is strange. The menu should be after all your regular spawn menu categories, down by things like prop protector or VMF.

Can you please post your configuration.
Title: Re: USwep Beta
Post by: Lunchbox on December 02, 2006, 01:20:10 PM
Hey spbogie,

That's correct, they worked through the console, but I didn't get a spawn category.  VMF Loader/CopyGun and my eSwep menu showed up, but not uSwep.  I thought that ESwep might be the problem (don't ask why, there really wasn't any logic behind it) so I removed it and was left with no ESwep or USwep.  Unfortunately this is the least of my problems.

I jumped the gun and upgraded my server to GMOD10 when it was released.  Unfortunately during this process I was not aware that GMOD10 linux binaries haven't been released!  So basically I deleted my GMOD9 directory and didn't have anything to replace with...

Now I am left without any server at all.  I will try this again as soon as I'm done reinstalling GMOD9.

I really appreciate your interest in my problem.

P.S. (offtopic)
I wish there was an ETA from Garry on when linux binaries will be out for GMOD10...