|
BANNED
|

12-31-2006
, 04:36
Scripting Help with a port
|
#1
|
I need some help to port an plugin from adminmod to amxmodx
Here is adminmod codes:
Code:
#include <core>
#include <console>
#include <string>
#include <admin>
#include <adminlib>
new STRING_VERSION[] = "0.1";
// short names for lengths (so I'm lazy, sue me)
const TLEN = MAX_TEXT_LENGTH;
const DLEN = MAX_DATA_LENGTH;
const NLEN = MAX_NAME_LENGTH;
const CLEN = MAX_COMMAND_LENGTH;
// time constants
const SECINMIN = 60;
const SECINHOUR = SECINMIN * 60;
const SECINDAY = SECINHOUR * 24;
const SECINMONTH = SECINDAY * 30; // on average...
// application constants
const MAX_NAMES = 2000;
const MAX_MATCHES = 3;
new FILENAME[] = "seen.txt";
// global variables
new namecount=0;
new namelist[MAX_NAMES][NLEN];
new timelist[MAX_NAMES];
new matches[MAX_MATCHES];
new oldest = 0x7fffffff;
new roundstart = 0;
const ROUND_START_LIMIT = 10; /* seconds until first connect() is counted */
// functions
abs( x )
{
if ( x < 0 )
return -x;
return x;
}
timeago(when,str[],len)
{
new a,b;
new s[2][] = { "s", "" };
new diff = abs(systemtime() - when);
if (diff < SECINMIN) {
snprintf(str,len, "%d second%s",
diff, s[diff==1]);
}
else if (diff < SECINHOUR) {
a = diff/SECINMIN;
b = diff%SECINMIN;
snprintf(str,len, "%d minute%s and %d second%s",
a, s[a==1], b, s[b==1]);
}
else if (diff < SECINDAY) {
a = diff/SECINHOUR;
b = (diff%SECINHOUR)/SECINMIN;
snprintf(str,len, "%d hour%s and %d minute%s",
a, s[a==1], b, s[b==1]);
}
else if (diff < SECINMONTH) {
a = diff/SECINDAY;
b = (diff%SECINDAY)/SECINHOUR;
snprintf(str,len, "%d day%s and %d hour%s",
a, s[a==1], b, s[b==1]);
}
else {
a = diff/SECINMONTH;
b = (diff%SECINMONTH)/SECINDAY;
snprintf(str,len, "%d month%s and %d day%s",
a, s[a==1], b, s[b==1]);
}
}
findseen(name[]){
new i;
new count = 0;
/* find exact match */
for (i=0;i<namecount;i++) {
if(!strcasecmp(namelist[i],name)) {
matches[0] = i;
return 1;
}
}
/* find substring */
for (i=0;i<namecount;i++) {
if(-1!=strcasestr(namelist[i],name)) {
matches[count++] = i;
if ( count == MAX_MATCHES )
return MAX_MATCHES+1;
}
}
return count;
}
findonline(name[]){
new i;
new players = maxplayercount();
new count=0;
new player[NLEN];
new text[TLEN];
/* find exact match */
for (i=1;i<=players;i++) {
if(playerinfo(i,player,MAX_NAME_LENGTH)) {
if (!strcasecmp(player,name)) {
snprintf(text,TLEN,"%s is online now",player);
say(text);
return 1;
}
}
}
/* find substring */
for (i=1;i<=players;i++) {
if(playerinfo(i,player,MAX_NAME_LENGTH)) {
if (strcasestr(player,name)!=-1) {
snprintf(text,TLEN,"%s is online now",player);
say(text);
count++;
if ( count == MAX_MATCHES )
return MAX_MATCHES+1;
}
}
}
return count;
}
public say_seen(HLCommand,HLData,HLUserName,UserIndex) {
new Data[MAX_DATA_LENGTH];
new cmd[MAX_TEXT_LENGTH];
new name[MAX_TEXT_LENGTH];
convert_string(HLData,Data,MAX_DATA_LENGTH);
strsplit( Data, " ^"", cmd, MAX_TEXT_LENGTH, name, MAX_TEXT_LENGTH );
if (streq(cmd,"seen")) {
new text[TLEN];
new count;
if ( findonline(name) )
return PLUGIN_CONTINUE;
count += findseen(name);
switch ( count ) {
case 0: {
//snprintf(text,TLEN,"I have never seen '%s'",name);
//say(text);
}
case 1: {
new idx = matches[0];
new str[TLEN];
timeago(timelist[idx],str,TLEN);
snprintf(text,TLEN,"%s was here %s ago",
namelist[idx],str);
say(text);
}
case 2..MAX_MATCHES: {
new i;
for (i=0;i<count;i++) {
new idx = matches[i];
new str[TLEN];
timeago(timelist[idx],str,TLEN);
snprintf(text,TLEN,
"%s was here %s ago",
namelist[idx],str);
say(text);
}
}
default: {
snprintf(text,TLEN,
"Be more specific. '%s' matches %d names",
name,count);
say(text);
}
}
}
return PLUGIN_CONTINUE;
}
storetime( name[] ) {
new i;
new now = systemtime();
new firstavail=-1;
new str[TLEN];
for (i=0;i<namecount;i++) {
if(!strcmp(name,namelist[i])) {
timelist[i] = now;
snprintf(str,TLEN,"%s\%d",namelist[i],timelist[i]);
writefile(FILENAME,str,i+1);
break;
}
else if ( (timelist[i]==0) && (firstavail == -1) )
firstavail = i;
}
if ( i==namecount ) {
if ( namecount>=MAX_NAMES ) {
if ( firstavail != -1 )
i = firstavail;
else
return PLUGIN_CONTINUE;
}
else
namecount++;
strcpy(namelist[i],name,NLEN);
timelist[i]=now;
snprintf(str,TLEN,"%s\%d",name,now);
writefile(FILENAME,str,i+1);
}
return 1;
}
public plugin_disconnect(HLUserName,UserIndex) {
new name[NLEN];
convert_string(HLUserName,name,DLEN);
storetime(name);
return PLUGIN_CONTINUE;
}
public plugin_connect(HLUserName,HLIP,UserIndex) {
new name[NLEN];
new now = systemtime();
/* new HL code always connects everybody at round start */
if ( now > roundstart + ROUND_START_LIMIT ) {
convert_string(HLUserName,name,DLEN);
storetime(name);
}
return PLUGIN_CONTINUE;
}
loadnames() {
new i;
new str[TLEN];
new num[NLEN];
new t;
new empty=0;
roundstart = systemtime();
for (i=0;i<MAX_NAMES;i++) {
if ( !readfile(FILENAME,str,i+1,TLEN) )
break;
strsplit(str,"\",namelist[i],NLEN,num,NLEN);
t = strtonum(num);
timelist[i]=t;
if ( t==0 )
empty++;
else if ( t < oldest )
oldest = t;
}
namecount = i;
if ( i < MAX_NAMES )
empty += MAX_NAMES - i;
/* less than 10% room? if so, remove oldest 20% */
if ( empty < MAX_NAMES / 10 ) {
new cleared = 0;
new now = systemtime();
new limit = oldest + ((now-oldest) / 5);
for (i=0;i<namecount;i++) {
if ( timelist[i] < limit ) {
timelist[i] = 0;
cleared++;
}
}
snprintf(str,TLEN,"plugin_seen: %d seen slots cleared",cleared);
log(str);
}
}
public plugin_init() {
plugin_registerinfo("Seen plugin ","Reports when players were last online",STRING_VERSION);
plugin_registercmd("say","say_seen",ACCESS_ALL);
plugin_registerhelp("say",ACCESS_ALL,"say seen <player>: Will report when player was last online.");
loadnames();
return PLUGIN_CONTINUE;
}
So i tryed to port this code, i dunno only with compiler
Here is what i dunno
Code:
#include <amxmodx>
#include <amxmisc>
#include <regex>
new STRING_VERSION[] = "0.1";
// short names for lengths (so I'm lazy, sue me)
#define TLEN = MAX_TEXT_LENGTH;
#define DLEN = MAX_DATA_LENGTH;
#define NLEN = MAX_NAME_LENGTH;
#define CLEN = MAX_COMMAND_LENGTH;
// time constants
const SECINMIN = 60;
const SECINHOUR = SECINMIN * 60;
const SECINDAY = SECINHOUR * 24;
const SECINMONTH = SECINDAY * 30; // on average...
// application constants
const MAX_NAMES = 2000;
const MAX_MATCHES = 3;
new const FILENAME[] = "addons/amxmodx/configs/seen.txt";
// global variables
new namecount=0;
new namelist[32]
new timelist[32];
new matches[MAX_MATCHES];
new oldest = 0x7fffffff;
new roundstart = 0;
const ROUND_START_LIMIT = 10; /* seconds until first connect() is counted */
timeago(when,str[],len) {
new a,b;
new s[2][] = { "s", "" };
new gametime = floatround(get_gametime());
new x = gametime - when;
new diff = abs(x);
if (diff < SECINMIN) {
format(str,len, "%d second%s", diff, s[diff==1]);
}
else if (diff < SECINHOUR) {
a = diff/SECINMIN;
b = diff%SECINMIN;
format(str,len, "%d minute%s and %d second%s", a, s[a==1], b, s[b==1]);
}
else if (diff < SECINDAY) {
a = diff/SECINHOUR;
b = (diff%SECINHOUR)/SECINMIN;
format(str,len, "%d hour%s and %d minute%s", a, s[a==1], b, s[b==1]);
}
else if (diff < SECINMONTH) {
a = diff/SECINDAY;
b = (diff%SECINDAY)/SECINHOUR;
format(str,len, "%d day%s and %d hour%s", a, s[a==1], b, s[b==1]);
}
else {
a = diff/SECINMONTH;
b = (diff%SECINMONTH)/SECINDAY;
format(str,len, "%d month%s and %d day%s", a, s[a==1], b, s[b==1]);
}
}
findseen(name[]) {
new i;
new count = 0;
/* find exact match */
for (i=0;i<namecount;i++) {
if(!strcmp (namelist[i],name)) {
matches[0] = i;
return 1;
}
}
/* find substring */
for (i=0;i<namecount;i++) {
if(-1!=strfind (namelist[i],name)) {
matches[count++] = i;
if ( count == MAX_MATCHES )
return MAX_MATCHES+1;
}
}
return count;
}
findonline(name[]) {
new i;
new players = get_maxplayers();
new count=0;
new player[32];
new text[254];
/* find exact match */
for (i=1;i<=players;i++) {
if(get_user_name(i, player, 31)) {
if (!strcmp(player,name)) {
format(text,254,"%s is online now",player);
//say(text);
return 1;
}
}
}
/* find substring */
for (i=1;i<=players;i++) {
if(get_user_name(i,player, 31)) {
if (strfind(player,name)!=-1) {
format(text,254,"%s is online now",player);
// say(text);
count++;
if ( count == MAX_MATCHES )
return MAX_MATCHES+1;
}
}
}
return count;
}
public say_seen(id,level,cid) {
new Data[254];
new cmd[254];
new name[32];
get_user_name(id,name,31)
strsplit ( Data, cmd, 253, name, 253);
if (contain(cmd,"seen")) {
new text[254];
new count;
if ( findonline(name) )
return PLUGIN_CONTINUE;
count += findseen(name);
switch ( count ) {
case 0: {
client_print(id,print_chat,"I have never seen %s",name);
//say(text);
}
case 1: {
new idx = matches[0];
new str[254];
timeago(timelist[idx],str,254);
format(text,254,"%s was here %s ago",
namelist[idx],str);
//say(text);
}
case 2..MAX_MATCHES: {
new i;
for (i=0;i<count;i++) {
new idx = matches[i];
new str[254];
timeago(timelist[idx],str,254);
format(text,254,"%s was here %s ago", namelist[idx],str);
//say(text);
}
}
default: {
format(text,254, "Be more specific. '%s' matches %d names", name,count);
//say(text);
}
}
}
return PLUGIN_CONTINUE;
}
storetime( name[] ) {
new i;
new now = floatround(get_gametime());
new firstavail = -1;
new str[254];
for (i=0;i<namecount;i++) {
if(!strcmp(name,namelist[i])) {
timelist[i] = now;
format(str,254,"%s\%d",namelist[i],timelist[i]);
write_file(FILENAME,str,i+1);
break;
}
else if ( (timelist[i]==0) && (firstavail == -1) )
firstavail = i;
}
if ( i==namecount ) {
if ( namecount>=MAX_NAMES ) {
if ( firstavail != -1 )
i = firstavail;
else
return PLUGIN_CONTINUE;
}
else
namecount++;
name[31] =namelist[i];
timelist[i]=now;
format(str,254,"%s\%d",name,now);
write_file(FILENAME,str,i+1);
}
return 1;
}
public client_disconnect(id) {
new name[32];
get_user_name(id,name,31)
storetime(name);
return PLUGIN_CONTINUE;
}
public client_connect(id) {
new name[32];
get_user_name(id,name,31)
new Float:now = get_gametime();
/* new HL code always connects everybody at round start */
if ( now > roundstart + ROUND_START_LIMIT ) {
storetime(name);
}
return PLUGIN_CONTINUE;
}
loadnames() {
new i;
new str[254];
new num[254];
new t;
new empty=0;
roundstart = floatround(get_gametime());
for (i=0;i<254;i++) {
if ( !read_file(FILENAME, i+1, str, 253, t) )
break;
strsplit(str,"\",namelist[i],253,num,253);
t = str_to_num(num);
timelist[i]=t;
if ( t==0 )
empty++;
else if ( t < oldest )
oldest = t;
}
namecount = i;
if ( i < MAX_NAMES )
empty += MAX_NAMES - i;
/* less than 10% room? if so, remove oldest 20% */
if ( empty < MAX_NAMES / 10 ) {
new cleared = 0;
new now = floatround(get_gametime());
new limit = oldest + ((now-oldest) / 5);
for (i=0;i<namecount;i++) {
if ( timelist[i] < limit ) {
timelist[i] = 0;
cleared++;
}
}
format(str,253,"plugin_seen: %d seen slots cleared",cleared);
log_amx(str);
}
}
stock strsplit(const sSource[], const sDelimiters[], ...)
{
new num=numargs();
if ((num % 2)!=0 || num<=2)
return -1;
num--; // decrease since curParam will be zero-based
new len=strlen(sSource);
new slen=strlen(sDelimiters);
new maxlen;
new i=0;
new j=0;
new ret=0;
new start=i;
new e;
new curParam=2; // arg is zero based in [s/g]etarg
while (i<=len)
{
j=0;
while (j<slen)
{
if (sSource[i]==sDelimiters[j] || sSource[i]=='^0') // match, return from start to here
{
if (start != i)
{
e=0; // location in setarg()
maxlen=getarg(curParam+1)-1; // adminmod auto decreases len
while (start<i&&e<maxlen)
setarg(curParam,e++,sSource[start++]);
setarg(curParam,e,'^0');
curParam+=2; // iterate by 2 (string,len);
ret++;
if (curParam>=num)
return ret;
}
start=i+1;
}
j++;
}
i++;
}
return ret;
}
public plugin_init() {
register_plugin("Seen plugin ","0.1",STRING_VERSION);
register_concmd("say","say_seen");
loadnames();
return PLUGIN_CONTINUE;
}
/* AMXX-Studio Notes - DO NOT MODIFY BELOW HERE
*{\\ rtf1\\ ansi\\ deff0{\\ fonttbl{\\ f0\\ fnil Tahoma;}}\n\\ viewkind4\\ uc1\\ pard\\ lang2067\\ f0\\ fs16 \n\\ par }
*/
If somebody can fix my plugin because on server doesen't works (appear nothing).. Thanks
Last edited by SAMURAI16; 12-31-2006 at 05:11.
|
|