/********************************************************
*  AWP Limit script by DLB                              *
*                                                       *
*  Description: Lets you limit how many AWPs each       *
*    player can buy per map. Meant to limit people      *
*    from overusing a very powerful weapon and ruining  *
*    the game for others possibly.                      *
*                                                       *
*  Requires: StatsMe (http://www.olo.counter-strike.pl) *
*                                                       *
*  Website: Http://www.BobDev.com                       *
*  E-mail: Bob@BobDev.com                               *
********************************************************/
 
/*
  Admin commands:
    admin_awplimit: Set the limit, -1 disables the script
    admin_awpexploit: (1) allow known exploit to occur (0) block known weapon restriction exploit
      (the reason this is an option is so you can catch exploiters red-handed for fun)
    admin_awpcheck: Prints list of users' purchase amounts
      (again, use this to check if they have purchased over the command because it keeps counting)
 
  Public stuff:
    checkawp: prints out purchased AWPs
    say checkawp: same thing
*/
 
#include <core>
#include <string>
#include <admin>
#include <adminlib>
 
#define MENU_EQUIPMENT 8
#define MENU_INVALID -1
#define MENU_TOP 0
#define ACCESS_RESTRICT 8192
#define ACCESS_CONSOLE 131071
 
new UserMenu[MAX_PLAYERS];
new AwpCount[MAX_PLAYERS];
new Money[MAX_PLAYERS];
 
 
new STRING_VERSION[] = "2.51.00";
new sb_awplimit = -1;
new sb_awpallowexploit = 0;
 
public admin_awplimit(HLCommand,HLData,HLUserName,UserIndex) {
	new Data[MAX_DATA_LENGTH];
	new User[MAX_NAME_LENGTH];
	new Text[MAX_TEXT_LENGTH];
	convert_string(HLData,Data,MAX_DATA_LENGTH);
	convert_string(HLUserName,User,MAX_NAME_LENGTH);
	//Data[0] means Data isn't empty (usually)
	if (Data[0]) {
		//Set and vault
		sb_awplimit = strtonum(Data);
		set_vaultdata("admin_awplimit",Data);
	}
	//Tell the admin what the limit is (does it even if it was empty so it just reports)
	snprintf(Text, MAX_TEXT_LENGTH, "AWP limit is ^"%i^"", sb_awplimit);
	messageex(User, Text, print_console);
	return PLUGIN_HANDLED;
}
 
public admin_awpexploit(HLCommand,HLData,HLUserName,UserIndex) {
	new Data[MAX_DATA_LENGTH];
	new User[MAX_NAME_LENGTH];
	new Text[MAX_TEXT_LENGTH];
	convert_string(HLData,Data,MAX_DATA_LENGTH);
	convert_string(HLUserName,User,MAX_NAME_LENGTH);
	//See admin_awplimit...same process
	if (Data[0]) {
		sb_awpallowexploit = strtonum(Data);
		set_vaultdata("admin_awpexploit",Data);
	}	
	snprintf(Text, MAX_TEXT_LENGTH, "AWP allow exploit is ^"%i^"", sb_awpallowexploit);
	messageex(User, Text, print_console);
	return PLUGIN_HANDLED;
}
public admin_awpcheck(HLCommand,HLData,HLUserName,UserIndex) {
	new i;
	new Text[MAX_TEXT_LENGTH];
	new AdminUser[MAX_NAME_LENGTH];
	new User[MAX_NAME_LENGTH];
	convert_string(HLUserName,AdminUser,MAX_NAME_LENGTH);
	Text = "AWP Count Listing:^n-------------------------------";
	messageex(AdminUser, Text, print_console);
	//Cycle players
	for(i=1;i<=MAX_PLAYERS;i+=1) {
		if (AwpCount[i] > 0) {
			//Load player name and continue if player exists as well, then print it
			if (playerinfo(i, User, MAX_NAME_LENGTH)) {
				snprintf(Text, MAX_TEXT_LENGTH, "^"%s^" - %i", User, AwpCount[i]);
				messageex(AdminUser, Text, print_console);
			}
		}
	}
	return PLUGIN_HANDLED;
}
public ClearMenuData(HLCommand,HLData,HLUserName,UserIndex) {
	if (sb_awplimit > -1) {
		UserMenu[UserIndex] = MENU_INVALID;
	}
	return PLUGIN_CONTINUE;
}
public HandleSay(HLCommand,HLData,HLUserName,UserIndex) {
	new Data[MAX_DATA_LENGTH];
	new User[MAX_NAME_LENGTH];
	//Handles if the person says "checkawp"
	if (sb_awplimit > -1) {
		convert_string(HLData,Data,MAX_DATA_LENGTH);
		convert_string(HLUserName,User,MAX_DATA_LENGTH);
		strstripquotes(Data);
		if (streq(Data,"checkawp")) {
			PrintAwpCount(User,UserIndex);
		}
	}
}
public PrintAwpCount(User[MAX_NAME_LENGTH],UserIndex) {
	new Text[MAX_TEXT_LENGTH];
	//Gets called from someone saying "checkawp" or using /checkawp command, both come here
	snprintf(Text,MAX_TEXT_LENGTH,"You have purchased (%i/%i) allowed AWPs.",AwpCount[UserIndex],sb_awplimit);
	messageex(User,Text,print_chat);
}
public CheckAWP(HLCommand,HLData,HLUserName,UserIndex) {
	new User[MAX_NAME_LENGTH];
	//Direct command handle
	if (sb_awplimit > -1) {
		convert_string(HLUserName,User,MAX_DATA_LENGTH);
		PrintAwpCount(User,UserIndex);
	}
	return PLUGIN_HANDLED;
}
public SetMenuData(HLCommand,HLData,HLUserName,UserIndex) {
	new Command[MAX_COMMAND_LENGTH];
 
	if (sb_awplimit > -1) {
		convert_string(HLCommand,Command,MAX_COMMAND_LENGTH);
		if (streq(Command, "buyequip")==1) {
			UserMenu[UserIndex] = MENU_EQUIPMENT;
		} else if (streq(Command, "buy")==1) {
			UserMenu[UserIndex] = MENU_TOP;		
		} else if (streq(Command, "showbriefing")==1) {
			new User[MAX_NAME_LENGTH];
			convert_string(HLUserName,User,MAX_NAME_LENGTH);
			messageex(User,"Briefing unavailable while weapon restrictions are enabled",print_chat);
			return PLUGIN_HANDLED;
		}
	}
	return PLUGIN_CONTINUE;
}
public ChooseTeam(HLCommand,HLData,HLUserName,UserIndex) {
	//Prevent the exploit block from keeping people from going into spectator while full on AWPs
	UserMenu[UserIndex] = -2;
}
public handle_money(HLCommand,HLData,HLUserName,UserIndex) {
	new Data[MAX_DATA_LENGTH];
	new sUserIndex[MAX_TEXT_LENGTH];
	new User[MAX_NAME_LENGTH];
	new sMoney[MAX_TEXT_LENGTH];
	new Text[MAX_TEXT_LENGTH];
 
	new iUserIndex, iMoney;
	if (sb_awplimit > -1) {
		convert_string(HLData,Data,MAX_DATA_LENGTH);
 
		strsplit(Data, " ", sUserIndex, 4, sMoney, 7);
		iUserIndex = strtonum(sUserIndex);
		iMoney = strtonum(sMoney);
		if (iUserIndex == 0) return;
		if (iMoney == (Money[iUserIndex] - 4750)) {
			if (playerinfo(iUserIndex, User, MAX_NAME_LENGTH)) {
				AwpCount[iUserIndex] += 1;
				snprintf(Text, MAX_TEXT_LENGTH, "You have purchased (%i/%i) allowed AWPs.^nYou have %i purchases remaining.",AwpCount[iUserIndex],sb_awplimit,(sb_awplimit - AwpCount[iUserIndex]));
				messageex(User, Text, print_tty);
			}
		}
		Money[iUserIndex] = iMoney;
	}
}
public SetSelectData(HLCommand,HLData,HLUserName,UserIndex) {
	new Data[MAX_DATA_LENGTH];
	new User[MAX_NAME_LENGTH];
 
	new iMenu;
 
	if (sb_awplimit > -1) {
		convert_string(HLData,Data,MAX_DATA_LENGTH);
		convert_string(HLUserName,User,MAX_NAME_LENGTH);
		iMenu = UserMenu[UserIndex];
 
		if (iMenu == MENU_TOP) {
			new iNewMenu = strtonum(Data);
			if (iNewMenu == 6 || iNewMenu == 7 || iNewMenu == 10) {
				UserMenu[UserIndex] = MENU_INVALID;
			} else {
				UserMenu[UserIndex] = iNewMenu;
			}
		//If they're in rifle menu or no menu (usually meaning exploit)
		} else if (iMenu == 4 || (iMenu == MENU_INVALID && sb_awpallowexploit == 0)) {
			new iNewItem = strtonum(Data);
 
			if (iNewItem==10) {
				UserMenu[UserIndex] = MENU_INVALID;
			} else if (iNewItem == 6 && AwpCount[UserIndex] >= sb_awplimit) {
				messageex(User, "You have used all your AWP purchases.^nYou cannot buy any more.", print_tty);
				UserMenu[UserIndex] = MENU_INVALID;			
				return PLUGIN_HANDLED;
			} else {
				UserMenu[UserIndex] = MENU_INVALID;
			}
		} else {
			UserMenu[UserIndex] = MENU_INVALID;
		}
	}
	return PLUGIN_CONTINUE;
}
 
public plugin_connect(HLUserName, HLIP, UserIndex) {
	if (UserIndex >= 1 && UserIndex <= MAX_PLAYERS) {
		Money[UserIndex] = 0;
		AwpCount[UserIndex] = 0;
		UserMenu[UserIndex] = MENU_INVALID;
	}
	return PLUGIN_CONTINUE;
}
 
public plugin_disconnect(HLUserName, UserIndex) {
	if (UserIndex >= 1 && UserIndex <= MAX_PLAYERS) {
		Money[UserIndex] = 0;
		AwpCount[UserIndex] = 0;
		UserMenu[UserIndex] = MENU_INVALID;
	}
	return PLUGIN_CONTINUE;
}
 
public plugin_init() {
	new Data[MAX_DATA_LENGTH];
	new i;
	//Clear out the "Bought" flags to Zero (just in case)
	for(i=1;i<=MAX_PLAYERS;i+=1) Money[i] = 0;
	plugin_registerinfo("AWP limit script","Lets you limit how many times one person can buy the AWP.",STRING_VERSION);
 
	//Engine event handlers:
	plugin_registercmd("handle_money","handle_money",ACCESS_CONSOLE);
 
	//Admin command:
	plugin_registercmd("admin_awplimit","admin_awplimit",ACCESS_RESTRICT,"admin_awplimit <limit>: Changes the limit for amount of AWP purchases, -1 for no limit.");
	plugin_registercmd("admin_awpcheck","admin_awpcheck",ACCESS_RESTRICT,"admin_awpcheck : Lists the players who have above a 0 purchase record.");
	plugin_registercmd("admin_awpexploit","admin_awpexploit",ACCESS_RESTRICT,"admin_awpexploit : Lets you allow players to get around the restrict with a known exploit, for fun. 1 to allow exploit, 0 to not.");
 
	//User command events:
	plugin_registercmd("buyequip","SetMenuData",ACCESS_ALL);
	plugin_registercmd("buy","SetMenuData",ACCESS_ALL);	
	plugin_registercmd("menuselect","SetSelectData",ACCESS_ALL);
	plugin_registercmd("showbriefing","SetMenuData",ACCESS_ALL);
	plugin_registercmd("chooseteam","ChooseTeam", ACCESS_ALL);
	plugin_registercmd("say","HandleSay",ACCESS_ALL);
	plugin_registercmd("checkawp","CheckAWP",ACCESS_ALL);
 
	//Get vault value to save across map changes and stuff
	get_vaultdata("admin_awplimit",Data,MAX_DATA_LENGTH);
	if (Data[0]) sb_awplimit = strtonum(Data);
	Data = "";
	get_vaultdata("admin_awpexploit",Data,MAX_DATA_LENGTH);
	if (Data[0]) sb_awpallowexploit = strtonum(Data);
 
	//Hook events to be handled using StatsMe
	exec("sm_reg Money ^"admin_command handle_money^" bd");
 
	return PLUGIN_CONTINUE;
}