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[] name, bool dontBroadcast)
{
PlayerInfo info;
event.GetString("networkid", info.steam, sizeof(PlayerInfo::steam));
if (StrEqual(info.steam, "BOT"))
{
return;
}
event.GetString("name", info.name, sizeof(PlayerInfo::name));
info.time = GetTime();
g_Players.PushArray(info);
if (g_Players.Length > 10)
{
g_Players.Erase(0);
}
}
public Action Command_Last(int client, int args)
{
for (int i = 0; i < g_Players.Length; i++)
{
PlayerInfo info;
g_Players.GetArray(i, info);
char time[65];
FormatTimeDuration(time, sizeof(time), GetTime() - info.time);
PrintToConsole(client, "[%02d] %s : %s : %s ago", i + 1, info.steam, info.name, time);
}
return Plugin_Handled;
}
int FormatTimeDuration(char[] buffer, int maxlen, int time)
{
int hours = time / 3600;
int minutes = (time / 60) % 60;
if (hours > 0)
{
return Format(buffer, maxlen, "%dh %dm", hours, minutes);
}
if (minutes > 0)
{
return Format(buffer, maxlen, "%dm", minutes);
}
return Format(buffer, maxlen, "%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.
__________________