Referral function?
#1

I've tried different stuff, and it ain't working. I've also searched for tutorials but I can't find one, if there's one then I may have not used the proper keyword. Anyways, I'm trying to make a referral function. Here:


A new player joins the server. I want to make a dialog input that will ask "Who referred you to join this server?" There will be a box that they can input the name of that user, example "DarkMythHunter" then in the database column "Referrals" it will add "1". But if the user don't put a registered name ( can be checked through database column "Username" it won't accept it and ask for a valid username". I hope someone helps me, thanks a lot.
Reply
#2

Okay, I'm assuming that you are using newest version of MySQL plugin and you have created connection to your database.

To start off, let's define our dialog id:
Код:
#define DIALOG_ID_REFERRAL_CHECK 2137
Then we need to put our dialog into OnPlayerConnect(or anywhere where you want it).
Код:
public OnPlayerConnect(playerid) {
	ShowPlayerDialog(playerid, DIALOG_ID_REFERRAL_CHECK, DIALOG_STYLE_INPUT,"Referral system", "If someone invited you to this server, input his name below", "OK", "Cancel");
//your code here
}
Next, let's handle the dialog response.
Код:
public OnDialogResponse(playerid, dialogid, response, listitem, inputtext[]) {
	switch(dialogid) 
	{
		case DIALOG_ID_REFERRAL_CHECK: 
		{
			if(!response) {
				//here you can put code that will execute when player clicks "Cancel" - the right button;
				return 1;
			}
			else {
				//you can add if's checking if user input is empty etc.
				new 	Query[128];
				mysql_format(YOUR_MYSQL_HANDLE, Query, sizeof Query,"SELECT * FROM `table_name` WHERE `Username` = '%e'", inputtext);
				mysql_tquery(YOUR_MYSQL_HANDLE, Query, "OnReferralNameCheck", playerid, inputtext);

			}
		}
	}
}
Don't forget to replace YOUR_MYSQL_HANDLE with handle that you're using to connect to your database and `table_name` with your table name!

And to end this simple script, create function that checks if that name is valid, and if it is add 1 to `Referrals` column.
Код:
forward OnReferralNameCheck(playerid, const name[]); 
public OnReferralNameCheck(playerid, const name[]) 
{
	if(cache_num_rows()) {
		//user has put right name
		new Query[128];
		mysql_format(YOUR_MYSQL_HANDLE, Query, sizeof Query,"UPDATE `table_name` SET `Referrals` = `Referrals` + 1 WHERE `Username` = '%e'", name);
		mysql_tquery(YOUR_MYSQL_HANDLE, Query);
	}
	else {
		//user has put wrong name
		SendClientMessage(playerid, -1, "This user doesn't exist!");
		ShowPlayerDialog(playerid, DIALOG_ID_REFERRAL_CHECK, DIALOG_STYLE_INPUT,"Referral system", "If someone invited you here, input his name below", "OK", "Cancel");
	}
	cache_unset_active();
}
Reply
#3

Quote:
Originally Posted by serqq
Посмотреть сообщение
Okay, I'm assuming that you are using newest version of MySQL plugin and you have created connection to your database.

To start off, let's define our dialog id:
Код:
#define DIALOG_ID_REFERRAL_CHECK 2137
Then we need to put our dialog into OnPlayerConnect(or anywhere where you want it).
Код:
public OnPlayerConnect(playerid) {
	ShowPlayerDialog(playerid, DIALOG_ID_REFERRAL_CHECK, DIALOG_STYLE_INPUT,"Referral system", "If someone invited you to this server, input his name below", "OK", "Cancel");
//your code here
}
Next, let's handle the dialog response.
Код:
public OnDialogResponse(playerid, dialogid, response, listitem, inputtext[]) {
	switch(dialogid) 
	{
		case DIALOG_ID_REFERRAL_CHECK: 
		{
			if(!response) {
				//here you can put code that will execute when player clicks "Cancel" - the right button;
				return 1;
			}
			else {
				//you can add if's checking if user input is empty etc.
				new 	Query[128];
				mysql_format(YOUR_MYSQL_HANDLE, Query, sizeof Query,"SELECT * FROM `table_name` WHERE `Username` = '%e'", inputtext);
				mysql_tquery(YOUR_MYSQL_HANDLE, Query, "OnReferralNameCheck", playerid, inputtext);

			}
		}
	}
}
Don't forget to replace YOUR_MYSQL_HANDLE with handle that you're using to connect to your database and `table_name` with your table name!

And to end this simple script, create function that checks if that name is valid, and if it is add 1 to `Referrals` column.
Код:
forward OnReferralNameCheck(playerid, const name[]); 
public OnReferralNameCheck(playerid, const name[]) 
{
	if(cache_num_rows()) {
		//user has put right name
		new Query[128];
		mysql_format(YOUR_MYSQL_HANDLE, Query, sizeof Query,"UPDATE `table_name` SET `Referrals` = `Referrals` + 1 WHERE `Username` = '%e'", name);
		mysql_tquery(YOUR_MYSQL_HANDLE, Query);
	}
	else {
		//user has put wrong name
		SendClientMessage(playerid, -1, "This user doesn't exist!");
		ShowPlayerDialog(playerid, DIALOG_ID_REFERRAL_CHECK, DIALOG_STYLE_INPUT,"Referral system", "If someone invited you here, input his name below", "OK", "Cancel");
	}
	cache_unset_active();
}

Edited some stuffs to match my gamemode, thanks a lot, it works fine!
Reply
#4

Just a follow-up question, I've added a column "Referred by" and it's supposed to show who (Username) invited him to the server. I want that name he put in "Input" to be entered in "Referred by" how do I do that?
Reply
#5

Quote:
Originally Posted by ******
Посмотреть сообщение
Don't use names, store it as a foreign key in to the user table.
I'm using names cause later on I want to check "who referred who".
Reply
#6

How tho?
Reply
#7

Bump.
Reply


Forum Jump:


Users browsing this thread: 2 Guest(s)