• Print

Author Topic: ply:isUserGroup advice  (Read 11840 times)

0 Members and 1 Guest are viewing this topic.

Offline Tomzen

  • Full Member
  • ***
  • Posts: 115
  • Karma: -1
  • A new lua adventurer
    • Thirdage Gaming
ply:isUserGroup advice
« on: May 13, 2015, 07:58:05 pm »
Hello, instead of using a password for my "ServerMode" (thanks to JamminR for the reminder), I would be using ply:isUserGroup, but I was wondering wether you could use a code like this for example:

Code: [Select]
RANKS = {"admin","owner"}
if not ply:isUserGroup( RANKS ) then
    ply:kick( "bleh" )
end

or would this not work:
Code: [Select]
ply:isUserGroup( RANKS )
and I would have to use:
Code: [Select]
ply:isUserGroup("admin") or ply:isUserGroup("owner") then ...
Help here would help a lot :P
Thanks.
Finished:
Impersonate
<==> FakePromote/Demote <==> RandomMap <==> ForceMic <==> Search <==> PlayMenu <==
WIP:
ServerMode <==

Offline Zmaster

  • Full Member
  • ***
  • Posts: 235
  • Karma: 25
Re: ply:isUserGroup advice
« Reply #1 on: May 13, 2015, 08:18:10 pm »
I did something like this for an addon I made for a friend a while ago

Although I never tried using an if statement like that, I'm 99% sure that won't work
Here's how I would do it while still using tables like you are
table.HasValue

Player:GetUserGroup

Offline MrPresident

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 2727
  • Karma: 430
    • |G4P| Gman4President
Re: ply:isUserGroup advice
« Reply #2 on: May 13, 2015, 08:21:06 pm »
It would return an error.
"string expected ... returned table"

The proper way to do it, would be as Zmaster states. Use those 2 functions to create your check.

Offline Tomzen

  • Full Member
  • ***
  • Posts: 115
  • Karma: -1
  • A new lua adventurer
    • Thirdage Gaming
Re: ply:isUserGroup advice
« Reply #3 on: May 13, 2015, 08:21:46 pm »
Thanks Zmaster, another question:

How would I get it to ONLY activate when "maintenance mode" is activated, the only way I could think of is to create a ulx command that changes a variable to true or something... like "active = true", but when a map is changed, or server restarted this would be reset back to false wouldn't it?

example:
Code: [Select]
function playerJoin( ply )
if active == true then
...

but as I said, if the map has changed or server restarted then the "active" would be reset back to false
Finished:
Impersonate
<==> FakePromote/Demote <==> RandomMap <==> ForceMic <==> Search <==> PlayMenu <==
WIP:
ServerMode <==

Offline Aaron113

  • Hero Member
  • *****
  • Posts: 803
  • Karma: 102
Re: ply:isUserGroup advice
« Reply #4 on: May 13, 2015, 08:23:24 pm »
http://wiki.garrysmod.com/page/gameevent/Listen
https://github.com/Nayruden/Ulysses/blob/052983fb132af59859784d3d6320ccdba01fe802/ulib/lua/ulib/server/ucl.lua#L562-L573

That is literally everything you need to do this.  It won't even let them download files.

I got ninja'd, but for the mode being saved, you could just create a cvar or save to a data file.

Offline Tomzen

  • Full Member
  • ***
  • Posts: 115
  • Karma: -1
  • A new lua adventurer
    • Thirdage Gaming
Re: ply:isUserGroup advice
« Reply #5 on: May 13, 2015, 08:25:23 pm »
http://wiki.garrysmod.com/page/gameevent/Listen
https://github.com/Nayruden/Ulysses/blob/052983fb132af59859784d3d6320ccdba01fe802/ulib/lua/ulib/server/ucl.lua#L562-L573

That is literally everything you need to do this.  It won't even let them download files.

I got ninja'd, but for the mode being saved, you could just create a cvar or save to a data file.

New to lua, could you post an example? or perhaps link to another add-on that's used that before.
Finished:
Impersonate
<==> FakePromote/Demote <==> RandomMap <==> ForceMic <==> Search <==> PlayMenu <==
WIP:
ServerMode <==

Offline Tomzen

  • Full Member
  • ***
  • Posts: 115
  • Karma: -1
  • A new lua adventurer
    • Thirdage Gaming
Re: ply:isUserGroup advice
« Reply #6 on: May 13, 2015, 08:33:38 pm »
Would the following work:

Code: [Select]
CreateConVar( "server_mode", "1" )
(server_mode 1 to enable) and to turn it off just add another but with 0?

Code: [Select]
CreateConVar( "server_mode", "0" )
and to read it:
Code: [Select]
if GetConVarNumber( "server_mode" ) == 1 then
...

Is this right?
Finished:
Impersonate
<==> FakePromote/Demote <==> RandomMap <==> ForceMic <==> Search <==> PlayMenu <==
WIP:
ServerMode <==

Offline Aaron113

  • Hero Member
  • *****
  • Posts: 803
  • Karma: 102
Re: ply:isUserGroup advice
« Reply #7 on: May 13, 2015, 08:38:17 pm »
Untested, but it theoretically should work.  If I'm not mistaken, you can return false on that to deny them in.  I will double check.
Code: Lua
  1. local convar = CreateConVar("server_mode", "0", FCVAR_ARCHIVE)
  2.  
  3. local whitelisted_groups = {}
  4.  
  5. gameevent.Listen( "player_connect" )
  6. hook.Add( "player_connect", "player_whitelist", function( data )
  7.         if convar:GetInt() == 1 then
  8.                 local steamid = data.networkid
  9.                 local group = ULib.ucl.getUserInfoFromID(steamid)
  10.                 if not table.HasValue(whitelisted_groups, group) then
  11.                         return false
  12.                 end
  13.         end
  14. end )
« Last Edit: May 13, 2015, 08:52:03 pm by Aaron113 »

Offline Tomzen

  • Full Member
  • ***
  • Posts: 115
  • Karma: -1
  • A new lua adventurer
    • Thirdage Gaming
Re: ply:isUserGroup advice
« Reply #8 on: May 13, 2015, 08:46:30 pm »
Wow lol, I think I should learn a lot more lua... looks complicated lol, don't even know where to start.
Finished:
Impersonate
<==> FakePromote/Demote <==> RandomMap <==> ForceMic <==> Search <==> PlayMenu <==
WIP:
ServerMode <==

Offline Tomzen

  • Full Member
  • ***
  • Posts: 115
  • Karma: -1
  • A new lua adventurer
    • Thirdage Gaming
Re: ply:isUserGroup advice
« Reply #9 on: May 13, 2015, 08:49:59 pm »
Would this work? or would I have to stick with your idea?
Except for line 7

Code: Lua
  1. SERVER_NAME = " _ SERVER NAME HERE _"
  2. RANKS = {"admin", "headadmin", "manager", "coowner", "owner"}
  3. WEBSITE = "http://www. _ website here _ .com/"
  4.  
  5. if GetConVarNumber( "server_mode" ) == 1 then
  6. function playerJoin( ply )
  7. if ply:IsUserGroup( RANKS ) then
  8.    ply:PrintMessage( "The server is currently on maintenance mode, welcome." )
  9. else
  10.    ply:kick( "The server is currently on maintenance mode!\n If you think this is an error, please contact an admin at " .. WEBSITE .. "\n If not, then join again some other time, sorry." )
  11. end
  12. end
  13. end
Finished:
Impersonate
<==> FakePromote/Demote <==> RandomMap <==> ForceMic <==> Search <==> PlayMenu <==
WIP:
ServerMode <==

Offline Aaron113

  • Hero Member
  • *****
  • Posts: 803
  • Karma: 102
Re: ply:isUserGroup advice
« Reply #10 on: May 13, 2015, 08:53:35 pm »
You never call the join function, so it would not work.  That would also only work on GM:PlayerInitialSpawn().

I did update my code to work.  The only issue is, if you're hosting a listen server, it will not work for you because ULib hasn't had time to load fully by the time you join.

Offline Tomzen

  • Full Member
  • ***
  • Posts: 115
  • Karma: -1
  • A new lua adventurer
    • Thirdage Gaming
Re: ply:isUserGroup advice
« Reply #11 on: May 13, 2015, 08:56:35 pm »
Ah, well could you do a brief walkthrough of your code?
Finished:
Impersonate
<==> FakePromote/Demote <==> RandomMap <==> ForceMic <==> Search <==> PlayMenu <==
WIP:
ServerMode <==

Offline Aaron113

  • Hero Member
  • *****
  • Posts: 803
  • Karma: 102
Re: ply:isUserGroup advice
« Reply #12 on: May 13, 2015, 09:02:50 pm »
The best I can do without you asking specific questions.

Code: Lua
  1. local convar = CreateConVar("server_mode", "0", FCVAR_ARCHIVE)  -- Basically creates a console command that saves between maps
  2.  
  3. local whitelisted_groups = {"superadmin", "admin"} -- table for whitelisted groups
  4.  
  5. gameevent.Listen( "player_connect" ) -- Hooks into the engine's code if I'm not mistaken  (I don't fully understand how it works)
  6.  
  7.  
  8. hook.Add( "player_connect", "player_whitelist", function( data )  -- Lets us hook into the event we're listening too up there ^^
  9.  
  10.         if convar:GetInt() == 1 then -- if server_mode is equal to 1
  11.  
  12.                 local steamid = data.networkid -- this is the steamid from the data we got from the hook
  13.  
  14.                 local group = ULib.ucl.getUserInfoFromID(steamid) -- new ULib function to check user info by steamid
  15.  
  16.                 if not table.HasValue(whitelisted_groups, group) then -- checks if the group is not in your server whitelist
  17.  
  18.                         return false -- Not in whitelist, kick them out... I think you can even have a string here.
  19.  
  20.                 end
  21.         end
  22. end )
« Last Edit: May 13, 2015, 09:04:28 pm by Aaron113 »

Offline Tomzen

  • Full Member
  • ***
  • Posts: 115
  • Karma: -1
  • A new lua adventurer
    • Thirdage Gaming
Re: ply:isUserGroup advice
« Reply #13 on: May 13, 2015, 09:07:17 pm »
so to kick someone, would I add ply:kick at the "return false" part, or would I add an "if __ = false then.." or what? basically, were would I add messages, and kicks, etc.
Finished:
Impersonate
<==> FakePromote/Demote <==> RandomMap <==> ForceMic <==> Search <==> PlayMenu <==
WIP:
ServerMode <==

Offline Aaron113

  • Hero Member
  • *****
  • Posts: 803
  • Karma: 102
Re: ply:isUserGroup advice
« Reply #14 on: May 13, 2015, 09:17:52 pm »
That is my bad, I didn't fully test it.

Code: [Select]
local convar = CreateConVar("server_mode", "0", FCVAR_ARCHIVE)  -- Basically creates a console command that saves between maps

local whitelisted_groups = {} -- table for whitelisted groups

gameevent.Listen( "player_connect" ) -- Hooks into the engine's code if I'm not mistaken  (I don't fully understand how it works)


hook.Add( "player_connect", "player_whitelist", function( data )  -- Lets us hook into the even we're listening too up there ^^
if convar:GetInt() == 1 then -- if server_mode is equal to 1
local steamid = data.networkid -- this is the steamid from the data we got from the hook
local userid = data.userid
local group = ULib.ucl.getUserInfoFromID(steamid) -- new ULib function to check user info by steamid
if not table.HasValue(whitelisted_groups, group.group) then -- checks if the group is not in your server whitelist
RunConsoleCommand("kickid", userid, "ENTER YOUR MESSAGE HERE")   
end
end
end )

EDIT:  Ignore me, now I'm doing something else wrong now.... I need to pay closer attention.

EDIT2:  Should be fixed again.
« Last Edit: May 13, 2015, 09:21:17 pm by Aaron113 »

  • Print