Ulysses

General => Developers Corner => Topic started by: easer7 on January 03, 2013, 08:44:35 PM

Title: Tags
Post by: easer7 on January 03, 2013, 08:44:35 PM
How would I give tags to moderators and admins?
This is the code I have so far.

if (SERVER) then
        AddCSLuaFile("autorun/tags.lua")
end
 
if (CLIENT) then
function Insert( pl, msg )
                local tab = {}
      if pl:IsUserGroup("headmanagement") then
                table.insert( tab, Color( 255, 0, 0, 255 ) )
        table.insert( tab, "[Head Management] " )
                table.insert( tab, Color( 255, 255, 255, 255 ) )
                        table.insert( tab, pl )
                        table.insert( tab, ": " )
                table.insert( tab, msg )
        elseif pl:IsUserGroup("superadmin") then
                table.insert( tab, Color( 0, 255, 0, 255 ) )
                        table.insert( tab, "[Head Management] " )
                table.insert( tab, Color( 255, 255, 255, 255 ) )
                        table.insert( tab, pl )
                        table.insert( tab, ": " )
                table.insert( tab, msg )
        elseif pl:IsUserGroup("admin") then
                table.insert( tab, Color( 0, 255, 0, 255 ) )
                        table.insert( tab, "[Administrator] " )
                table.insert( tab, Color( 255, 255, 255, 255 ) )
                        table.insert( tab, pl )
                        table.insert( tab, ": " )
                table.insert( tab, msg )
        elseif pl:IsUserGroup("leadmanagement") then
                table.insert( tab, Color( 0, 255, 0, 255 ) )
                        table.insert( tab, "[Lead Management] " )
                table.insert( tab, Color( 255, 255, 255, 255 ) )
                        table.insert( tab, pl )
                        table.insert( tab, ": " )
                table.insert( tab, msg )
        elseif pl:IsUserGroup("management") then
                table.insert( tab, Color( 0, 35, 175, 255 ) )
                        table.insert( tab, "[Management] " )
                table.insert( tab, Color( 255, 255, 255, 255 ) )
                        table.insert( tab, ply:Nick( ply ) )
                        table.insert( tab, ": " )
                table.insert( tab, msg )
        elseif pl:IsUserGroup("leadmoderator") then
                table.insert( tab, Color( 0, 74, 175, 255 ) )
                        table.insert( tab, "[Lead Moderator] " )
                table.insert( tab, Color( 255, 255, 255, 255 ) )
                        table.insert( tab, pl )
                        table.insert( tab, ": " )
                table.insert( tab, msg )
        elseif pl:IsUserGroup("moderator") then
                table.insert( tab, Color( 0, 185, 35, 255 ) )
                  table.insert( tab, "[Moderator] " )
                table.insert( tab, Color( 255, 255, 255, 255 ) )
                        table.insert( tab, pl )
                        table.insert( tab, ": " )                     
                table.insert( tab, msg )
        elseif pl:IsUserGroup("premium") then
                table.insert( tab, Color( 255, 0, 175, 255 ) )
                  table.insert( tab, "[Premium] " )
                table.insert( tab, Color( 255, 255, 255, 255 ) )
                        table.insert( tab, pl )
                        table.insert( tab, ": " )                     
                table.insert( tab, msg )
        elseif pl:IsUserGroup("trusted") then
                table.insert( tab, Color( 255, 0, 175, 255 ) )
                  table.insert( tab, "[Trusted] " )
                table.insert( tab, Color( 255, 255, 255, 255 ) )
                        table.insert( tab, pl )
                        table.insert( tab, ": " )                     
                table.insert( tab, msg )
        elseif pl:IsUserGroup("supporter") then
                table.insert( tab, Color( 255, 0, 175, 255 ) )
                  table.insert( tab, "[Supporter] " )
                table.insert( tab, Color( 255, 255, 255, 255 ) )
                        table.insert( tab, pl )
                        table.insert( tab, ": " )                     
                table.insert( tab, msg )
        else
                table.insert( tab, Color( 86, 185, 80, 255 ) )
                  table.insert( tab, "[User] " )
                table.insert( tab, Color( 255, 255, 255, 255 ) )
                        table.insert( tab, pl )
                        table.insert( tab, ": " )                     
                table.insert( tab, msg )
        end
chat.AddText( unpack(tab) )
return true
        end
end
hook.Add("OnPlayerChat", "InsertTags", Insert)

The result is that it is saying "admin" and in the correct color but it completely gets rid of the name, how would I fix this?
Title: Re: Tags
Post by: Megiddo on January 04, 2013, 03:01:57 AM
ply:Nick() for the player's name
Title: Re: Tags
Post by: easer7 on January 04, 2013, 12:30:46 PM
Yes but where and how would I add it? And thank you very much for responding =D
Title: Re: Tags
Post by: easer7 on January 04, 2013, 03:14:03 PM
I changes all the if/elses to look like this:

 elseif pl:IsUserGroup("admin") then
                table.insert( tab, Color( 0, 255, 0, 255 ) )
                        table.insert( tab, "[Administrator] " )
         table.insert( tab, ply:Nick() )
                table.insert( tab, Color( 255, 255, 255, 255 ) )
                        table.insert( tab, pl )
                        table.insert( tab, ": " )
                table.insert( tab, msg )
But that didnt work either, what is wrong?
Title: Re: Tags
Post by: JamminR on January 04, 2013, 04:13:42 PM
When speaking outright about code, we often use 'ply' as the 'player object'.
So when Megiddo stated ply:Nick(), you would actually need to use the variable of YOUR player object in your code, which is 'pl', not 'ply'.
So in your case, you'd need 'pl:Nick()'
Remove the original edit you did where you added ply:Nick(), and
I'm pretty sure you would not need your  "table.insert( tab, pl )" parts, and instead replace those with table.insert( tab, pl:Nick() )
I think that would get you on your way, may have to adjust for insertion order after that.
Title: Re: Tags
Post by: easer7 on January 05, 2013, 11:43:00 AM
Thank you! You deserve a medal =3
Title: Re: Tags
Post by: easer7 on January 05, 2013, 01:44:23 PM
How do I make it so that the names are not the color of the tag though?
Title: Re: Tags
Post by: JamminR on January 05, 2013, 04:57:39 PM
Research/figure out if there's a way to turn color off/default, and set that command after the tag.
Title: Re: Tags
Post by: Megiddo on January 05, 2013, 08:38:03 PM
Insert the color you want before the player's name.
Title: Re: Tags
Post by: easer7 on January 07, 2013, 04:50:41 PM
Well I don't want it to be a certain color, I just want it to be the default.  So how would I add these tags in their specific colors, without affecting the colors of the names?
Title: Re: Tags
Post by: Megiddo on January 07, 2013, 05:48:50 PM
Default color is approximately Color(150,200,255)
Title: Re: Tags
Post by: easer7 on January 07, 2013, 06:12:23 PM
This is for a Trouble in Terrorist Town server so the color of the name changes if you are innocent or detective in the chat though. So I don't know if there is any way to set it to be like that.
Title: Re: Tags
Post by: Megiddo on January 07, 2013, 06:31:17 PM
It's could be their team color (http://wiki.garrysmod.com/page/Libraries/team/GetColor).
Title: Re: Tags
Post by: easer7 on January 07, 2013, 07:18:44 PM
But they change teams, like so if trusted is purple, and if they are detective, the code clashes.  This is trying to make it purple and the other is trying to make him blue (detective).  But in another round he could be traitor, so his name would be green, but the code would again clash b/c it would try to make his name purple.
Title: Re: Tags
Post by: JamminR on January 07, 2013, 08:57:58 PM
Ok, just to confuse you, here's how I would do it.
ULib has a command that I'd hoped Garry would just create, but apparently never did.
We wrote a 'GetUserGroup' (http://ulyssesmod.net/docs/files/lua/ulib/shared/sh_ucl-lua.html#Player:GetUserGroup) command IN ULib. Simply enough, it returns the group name the user is a part of. (not including inherits, just the top group)
Using that, I got rid of all your if/else IsUserGroup statements.

Now, the code below probably won't work as is.
See if you can figure it out though.
First I set a table of 'tags' by group/color/title.
Then, guessing that your gamemode uses teams for each job, I set a group of 'job' team colors. That is where you'd have to look in the original game mode to see what colors for each team they used.
ProTip, learn to concatenate in Lua. Not having so many table inserts will save you lua code time. Even if you end up going back to your if/then/elseif tree (because you want your code to run on NON ULib servers too), you can still save some table.inserts by using
Code: [Select]
table.insert( tab, jobcolor .. pl:Name() .. ": " .. msg )
rather than
Code: [Select]
table.insert( tab, msg )
table.insert( tab, ": ")
table.insert( tab, pl:Nick() )
table.insert( tab, jobcolor )

Again, below is example, untested. 98% guaranteed not to work out of the box.
Code: [Select]
if (SERVER) then
        AddCSLuaFile("autorun/tags.lua")
end

-- Table of TAG colors and name per group
taggroup_color =  { }
taggroup_color.headmanagement =  { Color( 255, 0, 0, 255 ) .. "[Head Management] " }
taggroup_color.superadmin = { Color( 0, 255, 0, 255 ) .. "[Head Management] " }
taggroup_color.admin = { Color( 0, 255, 0, 255 ) ..  "[Administrator] " }
taggroup_color.leadmanagement = { Color( 0, 255, 0, 255 ) ..  "[Lead Management] " }
-- and so on .

-- Table of JOB (team??) colors, as specified somewhere in your gamemode already.
tagjob_color = { }
tagjob_color.1 = { Color( #whatever, #color, #here ) }
tagjob_color.2 = { Color( #whatever, #color, #here ) }
tagjob_color.someotherjobteam# = { Color( #whatever, #color, #here ) }
 -- and so on for each job(team?) color as used in your gamemode.
 
if (CLIENT) then
function TagInsert( pl, msg )
local tab = {}
local group = pl:GetUserGroup() -- THIS IS A ULib ONLY command. If you don't want the code ULib required, you must then use your 'if/then' for every group test tree as your originally had
local job = pl:GetTeam()
local grouptag = taggroup_color[group]
local jobcolor = tagjob_color[job]
--error checks
if grouptag == nil then grouptag = Color( 255, 0, 0, 255 ) .. "[Unknown] " end
if jobcolor =- nil then jobcolor = Color( #some, #generic, #color ) end
        table.insert( tab, grouptag )
table.insert( tab, jobcolor .. pl:Nick() .. ": " .. msg )
        chat.AddText( unpack(tab) )
return true
end
end
hook.Add("OnPlayerChat", "InsertTags", TagInsert)
Title: Re: Tags
Post by: easer7 on January 08, 2013, 04:14:21 PM
Quick question about the else/if part though.


if (SERVER) then
        AddCSLuaFile("autorun/tags.lua")
end
 
if (CLIENT) then
function Insert( pl, msg )
                local tab = {}
        if pl:IsUserGroup("headmanagement") then
                table.insert( tab, Color( 255, 0, 0, 255 ) )
                        table.insert( tab, "[Head Management] " )
                table.insert( tab, Color( 255, 255, 255, 255 ) )
                        table.insert( tab, pl:Nick() )
                        table.insert( tab, ": " )
                table.insert( tab, msg )
        elseif pl:IsUserGroup("leadmanagement") then
                table.insert( tab, Color( 255, 0, 0, 255 ) )
                        table.insert( tab, "[Lead Management] " )
                table.insert( tab, Color( 255, 255, 255, 255 ) )
                        table.insert( tab, pl:Nick() )
                        table.insert( tab, ": " )
                table.insert( tab, msg )
        elseif pl:IsUserGroup("management") then
                table.insert( tab, Color( 255, 0, 0, 255 ) )
                        table.insert( tab, "[Management] " )
                table.insert( tab, Color( 255, 255, 255, 255 ) )
                        table.insert( tab, pl:Nick() )
                        table.insert( tab, ": " )
                table.insert( tab, msg )
        elseif pl:IsUserGroup("leadmoderator") then
                table.insert( tab, Color( 33, 110, 252, 255 ) )
                        table.insert( tab, "[Lead Moderator] " )
                table.insert( tab, Color( 255, 255, 255, 255 ) )
                        table.insert( tab, pl:Nick() )
                        table.insert( tab, ": " )
                table.insert( tab, msg )
        elseif pl:IsUserGroup("moderator") then
                table.insert( tab, Color( 33, 110, 252, 255 ) )
                        table.insert( tab, "[Moderator] " )
                table.insert( tab, Color( 255, 255, 255, 255 ) )
                        table.insert( tab, pl:Nick() )
                        table.insert( tab, ": " )
                table.insert( tab, msg )
        elseif pl:IsUserGroup("premium") then
                table.insert( tab, Color( 245, 226, 20, 255 ) )
                        table.insert( tab, "[Premium] " )
                table.insert( tab, Color( 255, 255, 255, 255 ) )
                        table.insert( tab, pl:Nick() )
                        table.insert( tab, ": " )
                table.insert( tab, msg )
        elseif pl:IsUserGroup("trusted") then
                table.insert( tab, Color( 194, 19, 153, 255 ) )
                        table.insert( tab, "[Trusted] " )
                table.insert( tab, Color( 255, 255, 255, 255 ) )
                        table.insert( tab, pl:Nick() )
                        table.insert( tab, ": " )
                table.insert( tab, msg )
        elseif pl:IsUserGroup("supporter") then
                table.insert( tab, Color( 86, 185, 80, 255 ) )
                        table.insert( tab, "[Supporter] " )
                table.insert( tab, Color( 255, 255, 255, 255 ) )
                        table.insert( tab, pl:Nick() )
                        table.insert( tab, ": " )
                table.insert( tab, msg )
        else
                table.insert( tab, Color( 86, 185, 80, 255 ) )
                        table.insert( tab, "[User] " )
                table.insert( tab, Color( 255, 255, 255, 255 ) )
                        table.insert( tab, pl:Nick() )
                        table.insert( tab, ": " )                     
                table.insert( tab, msg )
        end
chat.AddText( unpack(tab) )
return true
        end
end
hook.Add("OnPlayerChat", "InsertTags", Insert)


I'm getting the error: /tags.lua.lua:0
[ERROR] lua/autorun/tags.lua.lua:8: unfinished string near '"headmanagement) then'  1. unknown - lua/autorun/tags.
What is this?
Title: Re: Tags
Post by: MrPresident on January 08, 2013, 04:40:05 PM
Try this...

btw.. do you use something like Notepad++ to edit your lua? Your tabulation is atrocious. lol

Code: [Select]
if (SERVER) then
AddCSLuaFile("autorun/tags.lua")
end

if (CLIENT) then
function Insert( pl, msg )
local tab = {}
if pl:IsUserGroup("headmanagement") then
table.insert( tab, Color( 255, 0, 0, 255 ) )
table.insert( tab, "[Head Management] " )
table.insert( tab, Color( 255, 255, 255, 255 ) )
table.insert( tab, pl:Nick() )
table.insert( tab, ": " )
table.insert( tab, msg )
elseif pl:IsUserGroup("leadmanagement") then
table.insert( tab, Color( 255, 0, 0, 255 ) )
table.insert( tab, "[Lead Management] " )
table.insert( tab, Color( 255, 255, 255, 255 ) )
table.insert( tab, pl:Nick() )
table.insert( tab, ": " )
table.insert( tab, msg )
elseif pl:IsUserGroup("management") then
table.insert( tab, Color( 255, 0, 0, 255 ) )
table.insert( tab, "[Management] " )
table.insert( tab, Color( 255, 255, 255, 255 ) )
table.insert( tab, pl:Nick() )
table.insert( tab, ": " )
table.insert( tab, msg )
elseif pl:IsUserGroup("leadmoderator") then
table.insert( tab, Color( 33, 110, 252, 255 ) )
table.insert( tab, "[Lead Moderator] " )
table.insert( tab, Color( 255, 255, 255, 255 ) )
table.insert( tab, pl:Nick() )
table.insert( tab, ": " )
table.insert( tab, msg )
elseif pl:IsUserGroup("moderator") then
table.insert( tab, Color( 33, 110, 252, 255 ) )
table.insert( tab, "[Moderator] " )
table.insert( tab, Color( 255, 255, 255, 255 ) )
table.insert( tab, pl:Nick() )
table.insert( tab, ": " )
table.insert( tab, msg )
elseif pl:IsUserGroup("premium") then
table.insert( tab, Color( 245, 226, 20, 255 ) )
table.insert( tab, "[Premium] " )
table.insert( tab, Color( 255, 255, 255, 255 ) )
table.insert( tab, pl:Nick() )
table.insert( tab, ": " )
table.insert( tab, msg )
elseif pl:IsUserGroup("trusted") then
table.insert( tab, Color( 194, 19, 153, 255 ) )
table.insert( tab, "[Trusted] " )
table.insert( tab, Color( 255, 255, 255, 255 ) )
table.insert( tab, pl:Nick() )
table.insert( tab, ": " )
table.insert( tab, msg )
elseif pl:IsUserGroup("supporter") then
table.insert( tab, Color( 86, 185, 80, 255 ) )
table.insert( tab, "[Supporter] " )
table.insert( tab, Color( 255, 255, 255, 255 ) )
table.insert( tab, pl:Nick() )
table.insert( tab, ": " )
table.insert( tab, msg )
else
table.insert( tab, Color( 86, 185, 80, 255 ) )
table.insert( tab, "[User] " )
table.insert( tab, Color( 255, 255, 255, 255 ) )
table.insert( tab, pl:Nick() )
table.insert( tab, ": " )                     
table.insert( tab, msg )
end
chat.AddText( unpack(tab) )
return true
end
hook.Add("OnPlayerChat", "InsertTags", Insert)
end
Title: Re: Tags
Post by: easer7 on January 08, 2013, 05:04:37 PM
I do use notepad++ =( I thought notepad++ was of the highest quality, what should I use?
Title: Re: Tags
Post by: JamminR on January 08, 2013, 08:12:07 PM
I do use notepad++ =( I thought notepad++ was of the highest quality, what should I use?
Notepad++ is fine. Just keep your lines together.
Also, in case you missed what MrPresident did, from your error, your missing the closing quotation mark.
'"headmanagement) then' should have been  '"headmanagement") then'
Also, why use so many table.inserts?
Table manipulation is time consuming (for any programming language, even Lua).
It would be better, optimization wise, to use this ...
Code: [Select]
elseif pl:IsUserGroup("management") then
table.insert( tab, Color( 255, 0, 0, 255 ) .. "[Management] " .. Color( 255, 255, 255, 255 ) .. pl:Nick() .. ": " .. msg )
elseif ...
than what you're doing currently.
Look up "lua concatenation".
Those ".." bring together many string items.
Title: Re: Tags
Post by: Megiddo on January 09, 2013, 09:15:10 AM
Also, why use so many table.inserts?
Table manipulation is time consuming (for any programming language, even Lua).
It would be better, optimization wise, to use this ...
Code: [Select]
elseif pl:IsUserGroup("management") then
table.insert( tab, Color( 255, 0, 0, 255 ) .. "[Management] " .. Color( 255, 255, 255, 255 ) .. pl:Nick() .. ": " .. msg )
elseif ...
than what you're doing currently.
Look up "lua concatenation".
Those ".." bring together many string items.

It's necessary to do it the way he currently is. The color objects are not strings, and chat.AddText wants a list of string or color objects.
Title: Re: Tags
Post by: easer7 on January 09, 2013, 04:58:30 PM
Sorry I'm very new to this.  I've been going through the tutorials but im not done yet! :) Also, how do I get it to say |TEAM| when they talk in team chat and |DEAD| When dead players are talking? Would this work?


                if ( pl:Alive == false ) then
      table.insert( tab, Color( 255, 30, 40 ) )
      table.insert( tab, "|DEAD| " )
      end
      
      if ( bTeamOnly ) then
      table.insert( tab, Color( 30, 160, 40 ) )
      table.insert( tab, "|TEAM|" )
      end