Raised This Month: $ Target: $400
 0% 

Infractions - The Early Stages


Post New Thread Reply   
 
Thread Tools Display Modes
Kryptanyte
Junior Member
Join Date: Dec 2015
Old 06-19-2017 , 21:06   Re: Infractions - The Early Stages
Reply With Quote #31

Quote:
Originally Posted by QuantumToast View Post
PHP Code:
  int bot 0;
  
GetEventInt(event"bot"0);
  
int index 0;
  
GetEventInt(event"index"1); 
I'm not sure what event you've hooked here for this function so I don't know what variables you can get from it but I can tell you that GetEventInt() returns it's value like so:

PHP Code:
int index GetEventInt(event"userid"0); 
So I'm not sure bot and index are actual valid keys for the event you're hooking and you shouldn't use 1 as the default value for a client because if you return said default value you might index a player that doesn't exist, it's better to return 0 and add a check to see if it's a valid client id. For more info check this wiki and the API documentation:
https://wiki.alliedmods.net/Events_(SourceMod_Scripting)
https://sm.alliedmods.net/new-api/events/GetEventInt

Also if you are hooking something to do with a player connecting there's a whole load of sourcemod forwards for different stages of a player connecting:

https://sm.alliedmods.net/new-api/clients
Kryptanyte is offline
QuantumToast
Member
Join Date: May 2017
Location: US
Old 06-20-2017 , 12:22   Re: Infractions - The Early Stages
Reply With Quote #32

Quote:
Originally Posted by Kryptanyte View Post
I'm not sure what event you've hooked here for this function so I don't know what variables you can get from it but I can tell you that GetEventInt() returns it's value like so:

PHP Code:
int index GetEventInt(event"userid"0); 
So I'm not sure bot and index are actual valid keys for the event you're hooking and you shouldn't use 1 as the default value for a client because if you return said default value you might index a player that doesn't exist, it's better to return 0 and add a check to see if it's a valid client id. For more info check this wiki and the API documentation:
https://wiki.alliedmods.net/Events_(SourceMod_Scripting)
https://sm.alliedmods.net/new-api/events/GetEventInt
I'm hooking player_connect, and index is the client index - right? Here's my mental steps for what I want the function to do:
  1. Hook into player_connect, and get the client to use for other functions
  2. If the player has no infractions or is a bot, display the usual connection message to all players
  3. If the player has infractions, display the usual connect message to all non-admins, and a special message with extra informations to admins
What should I change?
Also, it seems I am incorrect - the function IS working, but not properly - when another player connects, it displays MY information instead of theirs, and still displays the normal connection message.
QuantumToast is offline
Kryptanyte
Junior Member
Join Date: Dec 2015
Old 06-20-2017 , 22:44   Re: Infractions - The Early Stages
Reply With Quote #33

I stand corrected, I'm not well versed in most of the event but index is their entity index at least, not sure about client index though. It's safer to get their userid and use [BOLD]GetClientOfUserId()[/BOLD] as userid is unique on the server whereas index might not be.

I would suggest using one of the native sourcemod forwards instead of an event hook, I don't believe that there is any particular advantage for you. If someone knows better about that please correct me. For example you could use

PHP Code:
public void OnClientPostAdminCheck(int client)
{
    

This fires after the user is basically in game already, has a steamid and has admin flags if they're admin. If you don't need to check if the user is an admin then you can use:

PHP Code:
public void OnClientAuthorized(int client, const char[] auth)
{
    

The auth string passed is the clients Steam2 key if it's available in the game you're running this plugin in, if it's not supported then it is the engine auth id.

Some notes on how I would approach your steps:
I wouldn't have a case for a bot, if the client index is invalid/bot I automatically end the function.

PHP Code:
stock bool IsValidClient(int client)
{
    return (
client >= && client <= MaxClients && !IsFakeClient(client) && !IsClientSourceTV(client));

Check to see if user is an admin and have a global variable to track this for easier printing.

PHP Code:
bool gB_IsAdmin[MAXPLAYERS+1];

public 
void OnClientPostAdminCheck(int client)
{
    if(!
IsValidClient(client)
        return;

    if(
CheckCommandAccess(client""ADMFLAG_GENERIC))
    {
        
gB_IsAdmin[client] = true;
    }
    else
    {
        
gB_IsAdmin[client] = false;
    }


//This is just an example, you'd put this in whatever function handles the infractions like your SQL Callback. You could leave it as it's own function if you wanted to as well.
void InfractionsMessage(int client)
{
    for(
int i 1<= MaxClientsi++)
    {
        if(
gB_IsAdmin[i])
        {
             
//Print extra info
        
}
        else
        {
             
//Standard message
        
}
    }

As for it displaying your information when someone else joins that might have something to do with the index you're getting from the event not being unique to you or as I said previously using 1 as the default value for a client index just means whoever is client index 1 is going to be checked instead.
Kryptanyte is offline
QuantumToast
Member
Join Date: May 2017
Location: US
Old 06-22-2017 , 16:04   Re: Infractions - The Early Stages
Reply With Quote #34

Quote:
Originally Posted by Kryptanyte View Post
Spoiler
I appreciate all your help, as always A couple of questions, though:
Firstly, while I can agree with you that using a forward instead of hooking into an event would be easier, but can I block an event from firing using a forward? I want to completely block the player connection message when a player connects and "over-write" it with a custom one for admins if the player has infractions.
Secondly, I put forwards in the main body of the source code, on the same level as OnPluginStart(), correct?

Also, I seem to have figured out the first part of my previous issue - mainly that I was trying to fire an event that was not created by the plugin. Though, I'm re-writing the function so that much is pretty much irrelevant. I may just skip the whole "overwriting an event" step since that just adds complexity for the sake of looking nice.
QuantumToast is offline
headline
SourceMod Moderator
Join Date: Mar 2015
Old 06-22-2017 , 16:46   Re: Infractions - The Early Stages
Reply With Quote #35

Quote:
can I block an event from firing using a forward? I want to completely block the player connection message when a player connects and "over-write" it with a custom one for admins if the player has infractions.
Yes, you can hook connect messages. And just PrintToChatAll a copy of it with different info.

Last edited by headline; 06-22-2017 at 16:48. Reason: I miss understood
headline is offline
QuantumToast
Member
Join Date: May 2017
Location: US
Old 06-22-2017 , 17:24   Re: Infractions - The Early Stages
Reply With Quote #36

Alright, I re-wrote it all pretty much from scratch again, borrowing from Kryptanyte's example. Note: Everything in the body of PlayerHasInfractions is just a placeholder until I implement actual infraction-checking. Is this a little bit better/smoother? The plugin compiles but I haven't tested it yet.
PHP Code:
bool CanSeeInfractions[MAXPLAYERS+1];
*
snip*
  
HookEvent("player_connect"Event_BlockConnectionMessageEventHookMode_Pre);
}

stock bool IsValidClient(int client)
{
  return (
client >= && client <= MaxClients && !IsFakeClient(client) && !IsClientSourceTV(client));
}

public 
OnClientPostAdminCheck(int client)
{
  if(!
IsValidClient(client))
    return;
  if(
CheckCommandAccess(client""ADMFLAG_GENERIC))
  {
    
CanSeeInfractions[client] = true;
  }
  else
  {
    
CanSeeInfractions[client] = false;
  }
}

public 
void PostInfractionsMessage(int client)
{
  for(
int i 1<= MaxClientsi++)
  {
    if(
CanSeeInfractions[i] && PlayerHasInfractions(client))
    {
      
PrintToConsole("[SM] Connection handled by Infractions");
      
PrintToChat(i"%L has joined the game and has infractions"client);
    }
    else
    {
      
PrintToChat(i"%N has joined the game"client);
    }
  }
}

public 
Action Event_BlockConnectionMessage(Event event, const char[] namebool dontBroadcast)
{
  
PostInfractionsMessage(GetClientOfUserId(GetEventInt(event"userid")));
  return 
Plugin_Handled;
}

/**
 * Check the SQL database for the player's steamID,
 * and return whether or not their infractions count is greater than 0.
 */
public bool PlayerHasInfractions(int client)
{
  
int test GetRandomInt(01);
  if(
test == 1) return true;
  else return 
false;


Last edited by QuantumToast; 06-22-2017 at 17:25.
QuantumToast is offline
Kryptanyte
Junior Member
Join Date: Dec 2015
Old 06-22-2017 , 20:56   Re: Infractions - The Early Stages
Reply With Quote #37

Just looking at your code I realize you can shorten a function by doing this (that was my bad haha):

PHP Code:
public OnClientPostAdminCheck(int client)
{
  if(!
IsValidClient(client))
    return;
  
  
CanSeeInfractions[client] = CheckCommandAccess(client""ADMFLAG_GENERIC); 
Otherwise it looks a lot cleaner.

A note when you implement SQL, you want to run threaded queries so using a query instantly won't really work (For threaded queries you pass a callback function to the query and once the query is run it will send the results to said callback function where you can handle them. This prevents hanging in your plugin)

I would say check the player for infractions early so it's (probably) returned the results by the time you want to check the player for infractions. The earliest you can do this would be OnClientAuthorized()
Kryptanyte is offline
asherkin
SourceMod Developer
Join Date: Aug 2009
Location: OnGameFrame()
Old 06-23-2017 , 08:53   Re: Infractions - The Early Stages
Reply With Quote #38

Quote:
Originally Posted by Fyren View Post
SM itself only runs kickid to actually kick anyone.
This is incorrect, for non-bots SM will directly disconnect the client.
__________________
asherkin is offline
psychonic

BAFFLED
Join Date: May 2008
Old 06-23-2017 , 09:28   Re: Infractions - The Early Stages
Reply With Quote #39

Quote:
Originally Posted by asherkin View Post
Quote:
Originally Posted by Fyren View Post
SM itself only runs kickid to actually kick anyone.
This is incorrect, for non-bots SM will directly disconnect the client.
This is also incorrect for multiple games. Bots are also directly disconnected on some.
psychonic is offline
QuantumToast
Member
Join Date: May 2017
Location: US
Old 06-23-2017 , 12:14   Re: Infractions - The Early Stages
Reply With Quote #40

Quote:
Originally Posted by asherkin View Post
This is incorrect, for non-bots SM will directly disconnect the client.
Silly as it sounds, never expected to see you in my thread
Based on what you and psychonic said, there should be no problem listening for sm_kick?
QuantumToast is offline
Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 21:54.


Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Theme made by Freecode