I'm sure there's a very easy way, but I was bored. So you can try this.
I made two versions, one for dynamic natives, so you can use it in any plugin.
And, just the normal 'local' functions.
Dynamic Native:
Code:
#include <amxmodx>
#define PLUGIN "Spectator Time"
#define VERSION "1.0"
#define AUTHOR "Drak"
#define TASK_ID 8699
new counter[33]
public plugin_init() {
register_plugin(PLUGIN, VERSION, AUTHOR)
register_event("TeamInfo","teamInfo", "a");
}
public plugin_natives() {
register_library("SpecTime");
register_native("get_user_spectime","_get_user_spectime");
}
public teamInfo()
{
new id = read_data(1);
new team[12];
read_data(2,team,sizeof team - 1);
switch(team[0])
{
case 'S': {
// They re-joined spectators, let's reset the timer.
if(counter[id] >= 1) {
counter[id] = 0;
}
set_task(1.0,"counterTask",id+TASK_ID,"",0,"b"); // Begin counting.
}
default: {
// We aren't on spectators anymore.. Stop the timer.
if(counter[id] >=1) {
remove_task(id+TASK_ID);
counter[id] = 0; // Reset it.
}
}
}
}
public counterTask(id) {
counter[id]++
}
// This returns how many seconds they have been in spectator.
public _get_user_spectime(Plugin,Params)
{
if(Params != 1)
{
server_print("[%s] Parameters do not match. Expected: 1, Found: %d",PLUGIN,Params)
return 0;
}
new id = get_param(1);
if(is_user_connected(id))
return counter[id];
return PLUGIN_HANDLED
}
public client_disconnect(id) {
if(task_exists(id+TASK_ID)) {
remove_task(id+TASK_ID);
}
counter[id] = 0
}
public client_putinserver(id) counter[id] = 0
The include file, if you use the dynamic version:
Code:
#pragma library "SpecTime"
// Returns how many seconds the user has been in spectator
native get_user_spectime(id)
Normal Version:
Code:
#include <amxmodx>
#define PLUGIN "Spectator Time"
#define VERSION "1.0"
#define AUTHOR "Drak"
#define TASK_ID 8699
new counter[33]
public plugin_init() {
register_plugin(PLUGIN, VERSION, AUTHOR)
register_event("TeamInfo","teamInfo", "a");
}
public teamInfo()
{
new id = read_data(1);
new team[12];
read_data(2,team,sizeof team - 1);
switch(team[0])
{
case 'S': {
// They re-joined spectators, let's reset the timer.
if(counter[id] >= 1) {
counter[id] = 0;
}
set_task(1.0,"counterTask",id+TASK_ID,"",0,"b"); // Begin counting.
}
default: {
// We aren't on spectators anymore.. Stop the timer.
if(counter[id] >=1) {
remove_task(id+TASK_ID);
counter[id] = 0; // Reset it.
}
}
}
}
public counterTask(id) {
counter[id]++
}
public get_user_spectime(id)
{
if(is_user_connected(id))
return counter[id];
}
public client_disconnect(id) {
if(task_exists(id+TASK_ID)) {
remove_task(id+TASK_ID);
}
counter[id] = 0
}
public client_putinserver(id) counter[id] = 0
Like i said, this is probably way over-done, but i don't see why it wont work.
The function "get_user_spectime" will return the amount of seconds the user has been in the spectator team. If the user switches out of spectator, the counter is reset. (You can obviously change that)
__________________