AdminMod.de
https://www.adminmod.de/

script help
https://www.adminmod.de/viewtopic.php?t=9942
Seite 1 von 1

Autor:  caramel [ 08.03.2007, 20:28 ]
Betreff des Beitrags:  script help

eh, first take a look here:
Code:
#include <core>
#include <console>
#include <string>
#include <admin>
#include <adminlib>

new gVersion[] = "0.1";


const TLEN = MAX_TEXT_LENGTH;
const DLEN = MAX_DATA_LENGTH;
const NLEN = MAX_NAME_LENGTH;
const CLEN = MAX_COMMAND_LENGTH;


const SECINMIN   = 60;
const SECINHOUR  = SECINMIN * 60;
const SECINDAY   = SECINHOUR * 24;
const SECINMONTH = SECINDAY * 30; 


const MAX_NAMES = 2000;
const MAX_MATCHES = 3;
new FILENAME[] = "seen_plugin.txt"; // here for save stuff


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; 



public hook_say(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 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: 
			{
				for (new 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;
}

stock abs( x )
{
	if ( x < 0 )
		return -x;
	return x;
}

stock 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]);
	}
}

stock findseen(name[]){
	new i;
	new count = 0;

	
	for (i=0;i<namecount;i++) {
		if(!strcasecmp(namelist[i],name)) {
			matches[0] = i;
			return 1;
		}
	}


	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;
}

stock findonline(name[]){
	new i;
	new players = maxplayercount();
	new count=0;
	new player[NLEN];
	new text[TLEN];

	
	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;
			}
		}
	}
	

	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;
}


stock 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();

	if ( now > roundstart + ROUND_START_LIMIT ) {
		convert_string(HLUserName,name,DLEN);
		storetime(name);
	}
	return PLUGIN_CONTINUE;
}

stock 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;


	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,"%d seen slots cleared",cleared);
		log(str);
	}
}

public plugin_init() {
	plugin_registerinfo("eh","who else",gVersion);
	
	plugin_registercmd("say","hook_say",0);

	loadnames();
	return PLUGIN_CONTINUE;
}
/* AMXX-Studio Notes - DO NOT MODIFY BELOW HERE
*{\\ rtf1\\ ansi\\ deff0{\\ fonttbl{\\ f0\\ fnil Tahoma;}}\n\\ viewkind4\\ uc1\\ pard\\ lang1033\\ f0\\ fs16 \n\\ par }
*/
If have anybody a suggestion to optimize this, i'm not sure if is the best coding to save on file etc
On server it's works, buy anyway

Thanks

Autor:  Sir Drink a lot [ 08.03.2007, 23:35 ]
Betreff des Beitrags: 

wow :)

okay... and does it work for adminmod? That code looks much more complicated, that even i can't say, it is correct or not :)

But the function of this plugin is another than your first plugin, you wrote here. Isn't it?

Okay.. i am a little bit confused *gg*

1. only one issue at first.. maybe it is better to check file_access_read 1 in plugin_init for right settings in adminmod.cfg

2. please take atttention to storing data in RAM...
new namelist[MAX_NAMES][NLEN];
WOW....

3. findonline(name[])... two for next loops with same function (if else)?

4. please use more { } :) i am old and it is easier for me looking through the code with {} (if(a==1){ ... }else{ ... })

5. hm... MAX_NAMES + 1 does work, if it is const? *lol* never get the idea to use const... hm... always used #define for this... omg.

6. *lol* the function timeago, i have to learn too... I don't know how it works :)

okay :) i see, that you have a lot of experience. Would be nice, if you keep on writing plugins for AdminMod. Just welcome ;)

Autor:  caramel [ 09.03.2007, 13:33 ]
Betreff des Beitrags: 

the plugin works perfect. I tested on my server .
I just asked if see something who aren't required, for optimeze or something
Anyway, that for that suggestions

Autor:  [WING] Black Knight [ 09.03.2007, 16:05 ]
Betreff des Beitrags: 

Well, from programming style it looks really good.

You may run into problems (lags) in case you get a long list. I don't know whether 2000 as max is low enough. You should try.

I would also suggest, you use the standard constants provided in the admin.inc.

Autor:  Sir Drink a lot [ 16.03.2007, 13:32 ]
Betreff des Beitrags: 

some issues:

1.
Code:
 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;
i would use filesize and strsep, no extra "t" and namecount.. hm.. could be optimized too :) (starting readfile at line 1, but array at 0.)
Code:
iFileSize=filesize(FILENAME);
	
	if(iFileSize>MAX_NAMES){
		iFileSize = MAX_NAMES; //avoiding ARRAY OVERFLOW
	}
	
	for(i=1;i<=iFileSize;i++){
		readfile(FILENAME,Data,i,MAX_DATA_LENGTH);
strsep(Data,"\",namelist[namecount],MAX_NAME_LENGTH,sTime,MAX_NUMBER_LENGTH);
		timestamp[namecount] = strtonum(sTime);
		namecount++;
	}
	namecount--;
2. stock findonline(name[]){

only one loop over maxplayers? why not using strcasestrx?

3. why using array matches? Just for the info, that there are more entries for given name? I would print out the first three names found and then give a hint, that more names are available...

Okay... i tried some code, but is not omptimized too :)
Code:
public hook_say(HLCommand,HLData,HLUserName,UserIndex){
	new Data[MAX_DATA_LENGTH];
	new UserName[MAX_NAME_LENGTH];
	new cmd[MAX_TEXT_LENGTH];
	new name[MAX_TEXT_LENGTH];
	convert_string(HLData,Data,MAX_DATA_LENGTH);
	convert_string(HLUserName,UserName,MAX_NAME_LENGTH);
	strsplit( Data, " ^"", cmd, MAX_TEXT_LENGTH, name, MAX_TEXT_LENGTH );
	if (streq(cmd,"/seen")) {
		//only check, if name is given
		if (strlen(name)!=0){
			check_name(name);
		}else{
			messageex(UserName,"[ONLINE] Please give me a name to search for!",print_chat);
		}
	}
	return PLUGIN_CONTINUE;
}

check_name(name[]){
	new Text[MAX_TEXT_LENGTH];
	new str[MAX_TEXT_LENGTH];
	new i;
	new count;
	new foundonline;
	new maxplayers=maxplayercount();
	
	//check, if player is online
	for(i=1;i<=maxplayers;i++){
		if (playerinfo(i,str,MAX_NAME_LENGTH) && strstrx(str,name)!=-1){
			foundonline=1;
			snprintf(Text,MAX_TEXT_LENGTH,"say %s is ONLINE",str);
			exec(Text);
			count++;
			if(count == MAX_MATCHES){
				break;
			}
		}
	}
	
	//if no player online find, check list
	if(!foundonline){
		for(i=0;i<=namecount;i++){
			if(strstrx(namelist[i],name)!=-1){
				timeago(timestamp[i],str,MAX_TEXT_LENGTH);
				snprintf(Text,MAX_TEXT_LENGTH,"say %s was here %s ago",namelist[i],str);
				exec(Text);
				count++;
				if(count == MAX_MATCHES){
					break;
				}
			}
		}
	}
}

Autor:  caramel [ 17.03.2007, 19:18 ]
Betreff des Beitrags: 

thanks dude ;)

Seite 1 von 1 Alle Zeiten sind UTC+01:00
Powered by phpBB® Forum Software © phpBB Limited
https://www.phpbb.com/