View Single Post
Ilusion9
Veteran Member
Join Date: Jun 2018
Location: Romania
Old 02-11-2019 , 07:15   Re: Enum Structs Available This Holiday Season
Reply With Quote #22

PHP Code:

#pragma semicolon 1
#pragma newdecls required

#include <sourcemod>

enum struct PlayerInfo
{
    
char steam[65];
    
char name[65];
    
int time;
};

ArrayList g_Players;

public 
void OnPluginStart()
{
    
g_Players = new ArrayList(sizeof(PlayerInfo));
    
HookEvent("player_disconnect"Event_PlayerDisconnect);
    
    
RegConsoleCmd("sm_lastpl"Command_Last);
}

public 
void Event_PlayerDisconnect(Event event, const char[] namebool dontBroadcast
{
    
PlayerInfo info;
    
event.GetString("networkid"info.steamsizeof(PlayerInfo::steam));
    
    if (
StrEqual(info.steam"BOT"))
    {
        return;
    }
    
    
event.GetString("name"info.namesizeof(PlayerInfo::name));
    
info.time GetTime();
    
    
g_Players.PushArray(info);
    if (
g_Players.Length 10)
    {
        
g_Players.Erase(0);
    }
}

public 
Action Command_Last(int clientint args)
{    
    for (
int i 0g_Players.Lengthi++)
    {
        
PlayerInfo info;
        
g_Players.GetArray(iinfo);

        
char time[65];
        
FormatTimeDuration(timesizeof(time), GetTime() - info.time);
        
PrintToConsole(client"[%02d] %s : %s : %s ago"1info.steaminfo.nametime);
    }
    
    return 
Plugin_Handled;
}


int FormatTimeDuration(char[] bufferint maxlenint time)
{
    
int hours time 3600;
    
int minutes = (time 60) % 60;

    if (
hours 0)
    {
        return 
Format(buffermaxlen"%dh %dm"hoursminutes);        
    }
    
    if (
minutes 0)
    {
        return 
Format(buffermaxlen"%dm"minutes);        
    }
    
    return 
Format(buffermaxlen"%ds"time 60);        

Here's an example with structs and arraylists.
I don't know how sizeof of structs works, but if i have 3 members, calling g_Players = new ArrayList(3) it's wrong (only the first paramater - "steam" works fine), so g_Players = new ArrayList(sizeof(PlayerInfo)) it's the correct one.
__________________
Ilusion9 is offline